You are here

public function PHPExcelTest::testSingleWorksheetExport in PHPExcel 7.3

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.2 tests/phpexcel.test \PHPExcelTest::testSingleWorksheetExport()

Test a simple, single worksheet Excel export.

File

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

Class

PHPExcelTest
@file Defines the test case for phpexcel

Code

public function testSingleWorksheetExport() {

  // 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($this
    ->getFilename(), $this->directory);

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

  // Create a wrong path. Should not be able to export.
  $wrong_path = 'path/to/nowhere/file.xls';
  $this
    ->assertEqual(PHPEXCEL_ERROR_PATH_NOT_WRITABLE, phpexcel_export($headers, $data, $wrong_path), 'Passed an incorrect path');
  $this
    ->assertEqual(PHPEXCEL_SUCCESS, phpexcel_export($headers, $data, $correct_path), t('Exported data to !path', array(
    '!path' => $actual_path,
  )));
  $this
    ->assertTrue(filesize($actual_path) > 0, 'Filesize should be bigger than 0');

  // Import, not keyed by headers.
  $data = phpexcel_import($actual_path, FALSE);
  $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, expect 4.', 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, expect 2.', 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"');
}