ExcelWriterTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Ddeboer\DataImport\Tests\Writer;
  3. use Ddeboer\DataImport\Writer\ExcelWriter;
  4. class ExcelWriterTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function setUp()
  7. {
  8. if (!extension_loaded('zip')) {
  9. $this->markTestSkipped();
  10. }
  11. }
  12. public function testWriteItemAppendWithSheetTitle()
  13. {
  14. $file = tempnam(sys_get_temp_dir(), null);
  15. $writer = new ExcelWriter(new \SplFileObject($file, 'w'), 'Sheet 1');
  16. $writer->prepare();
  17. $writer->writeItem(array('first', 'last'));
  18. $writer->writeItem(array(
  19. 'first' => 'James',
  20. 'last' => 'Bond'
  21. ));
  22. $writer->writeItem(array(
  23. 'first' => '',
  24. 'last' => 'Dr. No'
  25. ));
  26. $writer->finish();
  27. // Open file with append mode ('a') to add a sheet
  28. $writer = new ExcelWriter(new \SplFileObject($file, 'a'), 'Sheet 2');
  29. $writer->prepare();
  30. $writer->writeItem(array('first', 'last'));
  31. $writer->writeItem(array(
  32. 'first' => 'Miss',
  33. 'last' => 'Moneypenny'
  34. ));
  35. $writer->finish();
  36. $excel = \PHPExcel_IOFactory::load($file);
  37. $this->assertTrue($excel->sheetNameExists('Sheet 1'));
  38. $this->assertEquals(3, $excel->getSheetByName('Sheet 1')->getHighestRow());
  39. $this->assertTrue($excel->sheetNameExists('Sheet 2'));
  40. $this->assertEquals(2, $excel->getSheetByName('Sheet 2')->getHighestRow());
  41. }
  42. public function testWriteItemWithoutSheetTitle()
  43. {
  44. $outputFile = new \SplFileObject(tempnam(sys_get_temp_dir(), null));
  45. $writer = new ExcelWriter($outputFile);
  46. $writer->prepare();
  47. $writer->writeItem(array('first', 'last'));
  48. $writer->finish();
  49. }
  50. /**
  51. * Test that column names not prepended to first row if ExcelWriter's 4-th
  52. * parameter not given
  53. *
  54. * @author Igor Mukhin <igor.mukhin@gmail.com>
  55. */
  56. public function testHeaderNotPrependedByDefault()
  57. {
  58. $file = tempnam(sys_get_temp_dir(), null);
  59. $writer = new ExcelWriter(new \SplFileObject($file, 'w'), null, 'Excel2007');
  60. $writer->prepare();
  61. $writer->writeItem(array(
  62. 'col 1 name'=>'col 1 value',
  63. 'col 2 name'=>'col 2 value',
  64. 'col 3 name'=>'col 3 value'
  65. ));
  66. $writer->finish();
  67. $excel = \PHPExcel_IOFactory::load($file);
  68. $sheet = $excel->getActiveSheet()->toArray();
  69. # Values should be at first line
  70. $this->assertEquals(array('col 1 value', 'col 2 value', 'col 3 value'), $sheet[0]);
  71. }
  72. /**
  73. * Test that column names prepended at first row
  74. * and values have been written at second line
  75. * if ExcelWriter's 4-th parameter set to true
  76. *
  77. * @author Igor Mukhin <igor.mukhin@gmail.com>
  78. */
  79. public function testHeaderPrependedWhenOptionSetToTrue()
  80. {
  81. $file = tempnam(sys_get_temp_dir(), null);
  82. $writer = new ExcelWriter(new \SplFileObject($file, 'w'), null, 'Excel2007', true);
  83. $writer->prepare();
  84. $writer->writeItem(array(
  85. 'col 1 name'=>'col 1 value',
  86. 'col 2 name'=>'col 2 value',
  87. 'col 3 name'=>'col 3 value'
  88. ));
  89. $writer->finish();
  90. $excel = \PHPExcel_IOFactory::load($file);
  91. $sheet = $excel->getActiveSheet()->toArray();
  92. # Check column names at first line
  93. $this->assertEquals(array('col 1 name', 'col 2 name', 'col 3 name'), $sheet[0]);
  94. # Check values at second line
  95. $this->assertEquals(array('col 1 value', 'col 2 value', 'col 3 value'), $sheet[1]);
  96. }
  97. }