You are here

public function PHPExcelTest::testDBResultExport in PHPExcel 7.3

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

Test db_result export.

File

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

Class

PHPExcelTest
@file Defines the test case for phpexcel

Code

public function testDBResultExport() {

  // Create 10 nodes.
  for ($i = 10; $i > 0; $i--) {
    $this
      ->createNode();
  }

  // Get the db query result.
  $result = db_select('node', 'n')
    ->fields('n', array(
    'nid',
    'title',
  ))
    ->execute();

  // Create a path.
  $correct_path = file_create_filename($this
    ->getFilename('xlsx'), $this->directory);

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

  // Try exporting to Excel 2007.
  $options = array(
    'format' => 'xlsx',
    'creator' => 'SimpleTest',
    'title' => 'DBResult',
    'subject' => 'test',
    'description' => 'my description',
  );
  $this
    ->assertEqual(PHPEXCEL_SUCCESS, phpexcel_export_db_result($result, $correct_path), t('Exported data to !path', array(
    '!path' => $actual_path,
  )));
  $this
    ->assertTrue(filesize($actual_path) > 0, 'Filesize should be bigger than 0');

  // Import, cells keyed by headers.
  $data = phpexcel_import($actual_path);
  $this
    ->assertTrue(!!$data, 'Import succeeded');

  // Should have 10 rows.
  $count = count($data[0]);
  $this
    ->assertTrue($count === 10, t('!count rows, expect 10', array(
    '!count' => $count,
  )));

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

  // Should be keyed by headers (nid & title).
  $this
    ->assertTrue(isset($data[0][0]['nid']), 'Keyed by header (nid)');
  $this
    ->assertTrue(isset($data[0][1]['title']), 'Keyed by header (title)');
}