SingleImportTestingTrait.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. trait SingleImportTestingTrait {
  3. public function testGet()
  4. {
  5. $got = $this->loadedFile->get();
  6. $this->assertInstanceOf('Maatwebsite\Excel\Collections\RowCollection', $got);
  7. $this->assertCount(5, $got);
  8. }
  9. public function testGetWithColumns()
  10. {
  11. $columns = array('heading_one', 'heading_two');
  12. $got = $this->loadedFile->get($columns);
  13. $this->assertInstanceOf('Maatwebsite\Excel\Collections\RowCollection', $got);
  14. $this->assertCount(5, $got);
  15. }
  16. public function testAll()
  17. {
  18. $all = $this->loadedFile->all();
  19. $this->assertInstanceOf('Maatwebsite\Excel\Collections\RowCollection', $all);
  20. $this->assertCount(5, $all);
  21. }
  22. public function testFirst()
  23. {
  24. $first = $this->loadedFile->first();
  25. $this->assertInstanceOf('Maatwebsite\Excel\Collections\CellCollection', $first);
  26. // 3 columns
  27. $this->assertCount(3, $first);
  28. }
  29. public function testFirstWithColumns()
  30. {
  31. $columns = array('heading_one', 'heading_two');
  32. $first = $this->loadedFile->first($columns);
  33. $this->assertInstanceOf('Maatwebsite\Excel\Collections\CellCollection', $first);
  34. $this->assertCount(count($columns), $first);
  35. }
  36. public function testEach()
  37. {
  38. $me = $this;
  39. $this->loadedFile->each(function($cells) use($me) {
  40. $me->assertInstanceOf('Maatwebsite\Excel\Collections\CellCollection', $cells);
  41. });
  42. }
  43. public function testToArray()
  44. {
  45. $array = $this->loadedFile->toArray();
  46. $this->assertEquals(array(
  47. array(
  48. 'heading_one' => 'test',
  49. 'heading_two' => 'test',
  50. 'heading_three' => 'test',
  51. ),
  52. array(
  53. 'heading_one' => 'test',
  54. 'heading_two' => 'test',
  55. 'heading_three' => 'test',
  56. ),
  57. array(
  58. 'heading_one' => 'test',
  59. 'heading_two' => 'test',
  60. 'heading_three' => 'test',
  61. ),
  62. array(
  63. 'heading_one' => 'test',
  64. 'heading_two' => 'test',
  65. 'heading_three' => 'test',
  66. ),
  67. array(
  68. 'heading_one' => 'test',
  69. 'heading_two' => 'test',
  70. 'heading_three' => 'test',
  71. )
  72. ), $array);
  73. }
  74. public function testImportedHeadingsSlugged()
  75. {
  76. $first = $this->loadedFile->first()->toArray();
  77. $keys = array_keys($first);
  78. $this->assertEquals(array(
  79. 'heading_one',
  80. 'heading_two',
  81. 'heading_three'
  82. ), $keys);
  83. }
  84. public function testImportedHeadingsHashed()
  85. {
  86. Config::set('excel.import.heading', 'hashed');
  87. $loaded = $this->reload();
  88. $first = $loaded->first()->toArray();
  89. $keys = array_keys($first);
  90. $this->assertEquals(array(
  91. md5('heading one'),
  92. md5('heading two'),
  93. md5('heading three')
  94. ), $keys);
  95. }
  96. public function testImportedHeadingsNumeric()
  97. {
  98. Config::set('excel.import.heading', 'numeric');
  99. $loaded = $this->reload();
  100. $first = $loaded->first()->toArray();
  101. $keys = array_keys($first);
  102. $this->assertEquals(array(
  103. 0,
  104. 1,
  105. 2
  106. ), $keys);
  107. }
  108. public function testImportedHeadingsOriginal()
  109. {
  110. Config::set('excel.import.heading', 'original');
  111. $loaded = $this->reload();
  112. $first = $loaded->first()->toArray();
  113. $keys = array_keys($first);
  114. $this->assertEquals(array(
  115. 'heading one',
  116. 'heading two',
  117. 'heading three'
  118. ), $keys);
  119. }
  120. public function testRemember()
  121. {
  122. $remembered = $this->loadedFile->remember(10);
  123. $this->assertEquals($this->reader, $remembered);
  124. $this->assertEquals(10, $remembered->cacheMinutes);
  125. $this->assertTrue($remembered->remembered);
  126. }
  127. public function testByConfig()
  128. {
  129. $config = $this->loadedFile->byConfig('excel.import.sheets');
  130. $this->assertInstanceOf('Maatwebsite\Excel\Collections\SheetCollection', $config);
  131. }
  132. public function testByConfigCallback()
  133. {
  134. $me = $this;
  135. $config = $this->loadedFile->byConfig('excel.import.sheets', function($config) use($me)
  136. {
  137. $me->assertInstanceOf('Maatwebsite\Excel\Readers\ConfigReader', $config);
  138. });
  139. $this->assertInstanceOf('Maatwebsite\Excel\Collections\SheetCollection', $config);
  140. }
  141. public function testTake()
  142. {
  143. $taken = $this->loadedFile->take(2);
  144. $this->assertEquals(2, $taken->getLimit());
  145. $this->assertCount(2, $taken->get());
  146. }
  147. public function testSkip()
  148. {
  149. $taken = $this->loadedFile->skip(1);
  150. $this->assertEquals(1, $taken->getSkip());
  151. $this->assertCount(4, $taken->get());
  152. }
  153. public function testLimit()
  154. {
  155. $taken = $this->loadedFile->limit(2, 1);
  156. $this->assertEquals(2, $taken->getLimit());
  157. $this->assertEquals(1, $taken->getSkip());
  158. $this->assertCount(2, $taken->get());
  159. }
  160. public function testSelect()
  161. {
  162. $columns = array('heading_one', 'heading_two');
  163. $taken = $this->loadedFile->select($columns);
  164. $this->assertEquals($columns, $taken->columns);
  165. }
  166. public function testSetDateFormat()
  167. {
  168. $set = $this->loadedFile->setDateFormat('Y-m-d');
  169. $this->assertEquals('Y-m-d', $set->getDateFormat());
  170. }
  171. public function testFormatDates()
  172. {
  173. $set = $this->loadedFile->formatDates(true, 'Y-m-d');
  174. $this->assertTrue($set->needsDateFormatting());
  175. $this->assertEquals('Y-m-d', $set->getDateFormat());
  176. }
  177. public function testSetDateColumns()
  178. {
  179. $set = $this->loadedFile->setDateColumns('created_at', 'deleted_at');
  180. $this->assertTrue($set->needsDateFormatting());
  181. $this->assertEquals(array('created_at', 'deleted_at'), $set->getDateColumns());
  182. }
  183. public function testCalculate()
  184. {
  185. $set = $this->loadedFile->calculate();
  186. $this->assertTrue($set->needsCalculation());
  187. }
  188. public function testIgnoreEmpty()
  189. {
  190. $set = $this->loadedFile->ignoreEmpty();
  191. $this->assertTrue($set->needsIgnoreEmpty());
  192. }
  193. }