CustomValueBinderTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. class CustomValuBinderTest extends TestCase {
  3. /**
  4. * Setup
  5. */
  6. public function setUp()
  7. {
  8. parent::setUp();
  9. // Set excel class
  10. $this->excel = App::make('phpexcel');
  11. // Set writer class
  12. $this->reader = App::make('excel.reader');
  13. $this->reader->injectExcel($this->excel);
  14. $this->reader->noHeading(true);
  15. // Set value binder
  16. $binder = new StubValueBinder();
  17. $this->reader->setValueBinder($binder);
  18. // Load csv file
  19. $this->loadFile();
  20. }
  21. /**
  22. * Tear down
  23. */
  24. public function tearDown()
  25. {
  26. // Necessary to reset the value binder back to default so that future test classes are unaffected.
  27. $this->reader->resetValueBinder();
  28. }
  29. public function testDefaultGet()
  30. {
  31. $got = $this->loadedFile->get();
  32. $this->assertInstanceOf('Maatwebsite\Excel\Collections\RowCollection', $got);
  33. $this->assertCount(5, $got);
  34. }
  35. public function testNumeric()
  36. {
  37. $got = $this->loadedFile->toArray();
  38. $this->assertTrue(is_string($got[0][0]));
  39. $this->assertEquals('00123', $got[0][0]);
  40. }
  41. public function testRealNull()
  42. {
  43. $got = $this->loadedFile->toArray();
  44. $this->assertTrue(is_null($got[1][0]));
  45. $this->assertEquals('', $got[1][0]);
  46. }
  47. public function testStringNull()
  48. {
  49. $got = $this->loadedFile->toArray();
  50. $this->assertTrue(is_string($got[2][0]));
  51. $this->assertEquals('null', $got[2][0]);
  52. }
  53. public function testEquation()
  54. {
  55. $got = $this->loadedFile->toArray();
  56. $this->assertTrue(is_string($got[3][0]));
  57. $this->assertEquals('=1+2', $got[3][0]);
  58. }
  59. public function testBoolean()
  60. {
  61. $got = $this->loadedFile->toArray();
  62. $this->assertTrue(is_string($got[4][0]));
  63. $this->assertEquals('true', $got[4][0]);
  64. }
  65. /**
  66. * Load a csv file
  67. * @return [type] [description]
  68. */
  69. protected function loadFile()
  70. {
  71. // Set test csv file
  72. $file = __DIR__ . '/files/' . 'customBinder.csv';
  73. // Loaded csv
  74. $this->loadedFile = $this->reader->load($file);
  75. }
  76. }
  77. class StubValueBinder extends PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder
  78. {
  79. public function bindValue(PHPExcel_Cell $cell, $value = null)
  80. {
  81. $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
  82. return true;
  83. }
  84. }