You are here

public function PHPExcelTest::testMultipleWorksheetExport in PHPExcel 6.2

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

Test multiple worksheet Excel export.

File

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

Class

PHPExcelTest
@file Defines the test case for phpexcel

Code

public function testMultipleWorksheetExport() {

  /**
   * Export
   */

  // Prepare data
  $headers = array(
    'Sheet 1' => array(
      'Header 1.1',
      'Header 1.2',
    ),
    'Sheet 2' => array(
      'Header 2.1',
      'Header 2.2',
    ),
  );
  $data = array(
    'Sheet 1' => array(
      array(
        'Data 1.1.1',
        'Data 1.1.2',
      ),
      array(
        'Data 1.2.1',
        'Data 1.2.2',
      ),
      array(
        'Data 1.3.1',
        'Data 1.3.2',
      ),
    ),
    'Sheet 2' => array(
      array(
        'Data 2.1.1',
        'Data 2.1.2',
      ),
      array(
        'Data 2.2.1',
        'Data 2.2.2',
      ),
      array(
        'Data 2.3.1',
        'Data 2.3.2',
      ),
    ),
  );

  // Create a file path
  $correct_path = file_create_filename('phpexcel.test2.xls', file_directory_path());

  // The filename will be munged by the export function, so:
  $this->multiple_worksheet_file = file_munge_filename($correct_path, 'xls xlsx');

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

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

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

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

  // Should have 3 rows
  $count = !empty($data[0]) ? count($data[0]) : 0;
  $this
    ->assertTrue($count === 3, 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,
  )));

  // Should be keyed by headers
  $this
    ->assertTrue(isset($data[0][0]['Header 1.1']), 'Keyed by header ("Header 1.1")');
  $this
    ->assertTrue(isset($data[1][0]['Header 2.2']), 'Keyed by header ("Header 2.2")');
  $header = !empty($data[0][0]['Header 1.1']) ? $data[0][0]['Header 1.1'] : 'no';
  $this
    ->assertTrue($header === 'Data 1.1.1', 'Should be "Data 1.1.1"');
  $header = !empty($data[1][1]['Header 2.2']) ? $data[1][1]['Header 2.2'] : 'no';
  $this
    ->assertTrue($header === 'Data 2.2.2', 'Should be "Data 2.2.2"');
}