You are here

public function PHPExcelTest::testDBResultExport in PHPExcel 6.2

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

Test db_result export.

File

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

Class

PHPExcelTest
@file Defines the test case for phpexcel

Code

public function testDBResultExport() {

  /**
   * Export
   */

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

  // Get the db query result
  $result = db_query("SELECT nid, title FROM {node}");

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

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

  // Try exporting to Excel 2007
  $options = array(
    'format' => 'xlsx',
    'creator' => 'SimpleTest',
    'title' => 'DBResult',
    'subject' => 'test',
    'description' => 'my description',
  );

  // Should pass
  $this
    ->assertTrue(phpexcel_export_db_result($result, $correct_path), t('Exported data to !path', array(
    '!path' => $this->db_result_file,
  )));

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

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

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

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

  // Should only have 2 cells
  $count = count($data[0][0]);
  $this
    ->assertTrue($count === 2, t('!count cells', 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)');
}