You are here

public function PHPExcelTest::testSingleWorksheetExport in PHPExcel 7.2

Same name and namespace in other branches
  1. 8.3 tests/phpexcel.test \PHPExcelTest::testSingleWorksheetExport()
  2. 6.2 tests/phpexcel.test \PHPExcelTest::testSingleWorksheetExport()
  3. 6 tests/phpexcel.test \PHPExcelTest::testSingleWorksheetExport()
  4. 7.3 tests/phpexcel.test \PHPExcelTest::testSingleWorksheetExport()

Test a simple, single worksheet Excel export.

File

tests/phpexcel.test, line 49
Defines the test case for phpexcel

Class

PHPExcelTest
@file Defines the test case for phpexcel

Code

public function testSingleWorksheetExport() {

  /**
   * Export
   */

  // Prepare the data
  $headers = array(
    'Header 1',
    'Header 2',
  );
  $data = array(
    array(
      'Data 1.1',
      'Data 1.2',
    ),
    array(
      'Data 2.1',
      'Data 2.2',
    ),
    array(
      'Data 3.1',
      'Data 3.2',
    ),
  );

  // Create a file path
  $correct_path = file_create_filename('phpexcel_test1.xls', $this->directory);

  // The filename will be munged by the export function, so:
  $this->single_worksheet_file = phpexcel_munge_filename($correct_path);

  // Create a wrong path. Should not be able to export.
  $wrong_path = 'path/to/nowhere/file.xls';

  // Should fail
  $this
    ->assertFalse(phpexcel_export($headers, $data, $wrong_path), 'Passed an incorrect path');

  // Should pass
  $this
    ->assertTrue(phpexcel_export($headers, $data, $correct_path), t('Exported data to !path', array(
    '!path' => $this->single_worksheet_file,
  )));

  // Should pass
  $this
    ->assertTrue(filesize($this->single_worksheet_file) > 0, 'Filesize should be bigger than 0');

  /**
   * Import and check.
   * Import, not keyed by headers
   */
  $data = phpexcel_import($this->single_worksheet_file, FALSE);

  // Should pass
  $this
    ->assertTrue(!!$data, 'Import succeeded');

  // Should have 4 rows (3 rows + headers)
  $count = !empty($data[0]) ? count($data[0]) : 0;
  $this
    ->assertTrue($count === 4, t('!count rows', array(
    '!count' => $count,
  )));

  // Should only have 2 cells
  $count = !empty($data[0][0]) ? count($data[0][0]) : 0;
  $this
    ->assertTrue($count === 2, t('!count cells', array(
    '!count' => $count,
  )));

  // First row, 1st cell should be 'Header 1'
  $header = !empty($data[0][0][0]) ? $data[0][0][0] : 'no';
  $this
    ->assertTrue($header === 'Header 1', 'First row, 1st cell data should be "Header 1"');

  // Second row, 1st cell should be 'Data 1.1'
  $header = !empty($data[0][1][0]) ? $data[0][1][0] : 'no';
  $this
    ->assertTrue($header === 'Data 1.1', 'Second row, 1st cell data should be "Data 1.1"');
}