ExcelReaderTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Ddeboer\DataImport\Tests\Reader;
  3. use Ddeboer\DataImport\Reader\ExcelReader;
  4. class ExcelReaderTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function setUp()
  7. {
  8. if (!extension_loaded('zip')) {
  9. $this->markTestSkipped();
  10. }
  11. }
  12. public function testGetFields()
  13. {
  14. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_column_headers.xlsx');
  15. $reader = new ExcelReader($file, 0);
  16. $this->assertEquals(array('id', 'number', 'description'), $reader->getFields());
  17. $this->assertEquals(array('id', 'number', 'description'), $reader->getColumnHeaders());
  18. }
  19. public function testCountWithoutHeaders()
  20. {
  21. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_no_column_headers.xls');
  22. $reader = new ExcelReader($file);
  23. $this->assertEquals(3, $reader->count());
  24. }
  25. public function testCountWithHeaders()
  26. {
  27. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_column_headers.xlsx');
  28. $reader = new ExcelReader($file, 0);
  29. $this->assertEquals(3, $reader->count());
  30. }
  31. public function testIterate()
  32. {
  33. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_column_headers.xlsx');
  34. $reader = new ExcelReader($file, 0);
  35. foreach ($reader as $row) {
  36. $this->assertInternalType('array', $row);
  37. $this->assertEquals(array('id', 'number', 'description'), array_keys($row));
  38. }
  39. }
  40. public function testMultiSheet()
  41. {
  42. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_multi_sheet.xls');
  43. $sheet1reader = new ExcelReader($file, null, 0);
  44. $this->assertEquals(3, $sheet1reader->count());
  45. $sheet2reader = new ExcelReader($file, null, 1);
  46. $this->assertEquals(2, $sheet2reader->count());
  47. }
  48. }