You are here

public function PHPExcelTest::testTemplateExport in PHPExcel 6

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

Test a simple, single worksheet Excel export.

File

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

Class

PHPExcelTest
@file Defines the test case for phpexcel

Code

public function testTemplateExport() {

  /**
   * Export
   */

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

  // Options
  $options = array(
    'format' => 'xlsx',
    'template' => drupal_get_path('module', 'phpexcel') . '/tests/data/phpexcel.test.template.xlsx',
    'ignore_headers' => TRUE,
  );

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

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

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

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

  /**
   * Import and check.
   */
  $data = phpexcel_import($this->template_file);

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

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

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

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