ExcelReaderLibTest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  3. App::uses('ExcelReaderLib', 'Tools.Lib');
  4. /**
  5. * Testing basic functions
  6. */
  7. class ExcelReaderLibTest extends MyCakeTestCase {
  8. public $ExcelReader;
  9. public function setUp() {
  10. parent::setUp();
  11. $this->ExcelReader = new ExcelReaderLib();
  12. }
  13. /**
  14. * Read binary data directly
  15. */
  16. public function readFromBlob() {
  17. $path = CakePlugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'xls' . DS;
  18. $file = $path . 'excel_2000.xls';
  19. $content = file_get_contents($file);
  20. $this->ExcelReader->readFromBlob($content);
  21. $sheets = $this->ExcelReader->sheets();
  22. $this->assertSame(3, $sheets);
  23. $array = $this->ExcelReader->dumpToArray();
  24. $expected = [
  25. ['A', 'B', 'C'],
  26. ['titleA', 'titleB', 'titleC'],
  27. ];
  28. $this->assertSame($expected, $array);
  29. }
  30. /**
  31. * Currently the ExcelReader only works with Excel97/2000/XP etc
  32. * Not with any versions prio to that
  33. */
  34. public function testRead() {
  35. $path = CakePlugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'xls' . DS;
  36. $file = $path . 'excel_2000.xls';
  37. $this->ExcelReader->read($file);
  38. $sheets = $this->ExcelReader->sheets();
  39. $this->assertSame(3, $sheets);
  40. $array = $this->ExcelReader->dumpToArray();
  41. $expected = [
  42. ['A', 'B', 'C'],
  43. ['titleA', 'titleB', 'titleC'],
  44. ];
  45. $this->assertSame($expected, $array);
  46. $array = $this->ExcelReader->dumpToArray(1);
  47. //debug($array);
  48. $expected = [
  49. ['A1', '0'],
  50. ['A2', '1'],
  51. ['A3', '2'],
  52. ['A4', '3'],
  53. ];
  54. $this->assertSame($expected, $array);
  55. $array = $this->ExcelReader->dumpToArray(2);
  56. $expected = [
  57. ];
  58. $this->assertSame($expected, $array);
  59. $file = $path . 'excel_1995.xls';
  60. //TODO
  61. }
  62. }