OneToManyReaderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace Ddeboer\DataImport\Tests\Reader;
  3. use Ddeboer\DataImport\Reader\ArrayReader;
  4. use Ddeboer\DataImport\Reader\OneToManyReader;
  5. /**
  6. * Class OneToManyReaderTest
  7. * @package Ddeboer\DataImport\Tests\Reader
  8. * @author Aydin Hassan <aydin@hotmail.co.uk>
  9. */
  10. class OneToManyReaderTest extends \PHPUnit_Framework_TestCase
  11. {
  12. public function testReaderMergesOneToMany()
  13. {
  14. $leftData = array(
  15. array(
  16. 'OrderId' => 1,
  17. 'Price' => 30,
  18. ),
  19. array(
  20. 'OrderId' => 2,
  21. 'Price' => 15,
  22. ),
  23. );
  24. $rightData = array(
  25. array(
  26. 'OrderId' => 1,
  27. 'Name' => 'Super Cool Item 1',
  28. ),
  29. array(
  30. 'OrderId' => 1,
  31. 'Name' => 'Super Cool Item 2',
  32. ),
  33. array(
  34. 'OrderId' => 2,
  35. 'Name' => 'Super Cool Item 1',
  36. ),
  37. );
  38. $leftReader = new ArrayReader($leftData);
  39. $rightReader = new ArrayReader($rightData);
  40. $oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId', 'OrderId');
  41. $expected = array(
  42. array(
  43. 'OrderId' => 1,
  44. 'Price' => 30,
  45. 'items' => array(
  46. array(
  47. 'OrderId' => 1,
  48. 'Name' => 'Super Cool Item 1',
  49. ),
  50. array(
  51. 'OrderId' => 1,
  52. 'Name' => 'Super Cool Item 2',
  53. ),
  54. ),
  55. ),
  56. array(
  57. 'OrderId' => 2,
  58. 'Price' => 15,
  59. 'items' => array(
  60. array(
  61. 'OrderId' => 2,
  62. 'Name' => 'Super Cool Item 1',
  63. ),
  64. )
  65. ),
  66. );
  67. $i = 0;
  68. foreach($oneToManyReader as $row) {
  69. $this->assertEquals($row, $expected[$i++]);
  70. }
  71. }
  72. public function testIfRightReaderIdFieldIsMissingLeftIsUsed()
  73. {
  74. $leftData = array(
  75. array(
  76. 'OrderId' => 1,
  77. 'Price' => 30,
  78. ),
  79. array(
  80. 'OrderId' => 2,
  81. 'Price' => 15,
  82. ),
  83. );
  84. $rightData = array(
  85. array(
  86. 'OrderId' => 1,
  87. 'Name' => 'Super Cool Item 1',
  88. ),
  89. array(
  90. 'OrderId' => 1,
  91. 'Name' => 'Super Cool Item 2',
  92. ),
  93. array(
  94. 'OrderId' => 2,
  95. 'Name' => 'Super Cool Item 1',
  96. ),
  97. );
  98. $leftReader = new ArrayReader($leftData);
  99. $rightReader = new ArrayReader($rightData);
  100. $oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId');
  101. $expected = array(
  102. array(
  103. 'OrderId' => 1,
  104. 'Price' => 30,
  105. 'items' => array(
  106. array(
  107. 'OrderId' => 1,
  108. 'Name' => 'Super Cool Item 1',
  109. ),
  110. array(
  111. 'OrderId' => 1,
  112. 'Name' => 'Super Cool Item 2',
  113. ),
  114. ),
  115. ),
  116. array(
  117. 'OrderId' => 2,
  118. 'Price' => 15,
  119. 'items' => array(
  120. array(
  121. 'OrderId' => 2,
  122. 'Name' => 'Super Cool Item 1',
  123. ),
  124. )
  125. ),
  126. );
  127. $i = 0;
  128. foreach($oneToManyReader as $row) {
  129. $this->assertEquals($row, $expected[$i++]);
  130. }
  131. }
  132. public function testReaderThrowsExceptionIfNestKeyExistsInLeftReaderRow()
  133. {
  134. $leftData = array(
  135. array(
  136. 'OrderId' => 1,
  137. 'Price' => 30,
  138. 'items' => null,
  139. ),
  140. );
  141. $rightData = array(
  142. array(
  143. 'OrderId' => 1,
  144. 'Name' => 'Super Cool Item 1',
  145. ),
  146. );
  147. $leftReader = new ArrayReader($leftData);
  148. $rightReader = new ArrayReader($rightData);
  149. $oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId');
  150. $this->setExpectedException('Ddeboer\DataImport\Exception\ReaderException', 'Left Row: "0" Reader already contains a field named "items". Please choose a different nest key field');
  151. $oneToManyReader->rewind();
  152. $oneToManyReader->current();
  153. }
  154. public function testReaderThrowsExceptionIfIdFieldDoesNotExistInLeftRow()
  155. {
  156. $leftData = array(
  157. array(
  158. 'Price' => 30,
  159. ),
  160. );
  161. $rightData = array(
  162. array(
  163. 'Name' => 'Super Cool Item 1',
  164. ),
  165. );
  166. $leftReader = new ArrayReader($leftData);
  167. $rightReader = new ArrayReader($rightData);
  168. $oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId');
  169. $this->setExpectedException('Ddeboer\DataImport\Exception\ReaderException', 'Row: "0" has no field named "OrderId"');
  170. $oneToManyReader->rewind();
  171. $oneToManyReader->current();
  172. }
  173. public function testReaderThrowsExceptionIfIdFieldDoesNotExistInRightRow()
  174. {
  175. $leftData = array(
  176. array(
  177. 'OrderId' => 1,
  178. 'Price' => 30,
  179. ),
  180. );
  181. $rightData = array(
  182. array(
  183. 'Name' => 'Super Cool Item 1',
  184. ),
  185. );
  186. $leftReader = new ArrayReader($leftData);
  187. $rightReader = new ArrayReader($rightData);
  188. $oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId');
  189. $this->setExpectedException('Ddeboer\DataImport\Exception\ReaderException', 'Row: "0" has no field named "OrderId"');
  190. $oneToManyReader->rewind();
  191. $oneToManyReader->current();
  192. }
  193. public function testGetKeysReturnsLeftReaderColumnsMergedWithNestKey()
  194. {
  195. $leftReader = new ArrayReader(array(array('col1' => 'data1', 'col2' => 'data2'), array('data3', 'data4')));
  196. $rightReader = new ArrayReader(array());
  197. $oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId');
  198. $this->assertSame(array('col1', 'col2', 'items'), $oneToManyReader->getFields());
  199. }
  200. public function testCountReturnsTheCountOfTheLeftReader()
  201. {
  202. $leftReader = new ArrayReader(array());
  203. $rightReader = new ArrayReader(array());
  204. $oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId');
  205. $this->assertEquals(0, $oneToManyReader->count());
  206. $leftReader = new ArrayReader(array(array(), array(), array()));
  207. $oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId');
  208. $this->assertEquals(3, $oneToManyReader->count());
  209. }
  210. /**
  211. * This is probably a limitation - but it's not need for current implementation
  212. * @dataProvider outOfOrderRowProvider
  213. */
  214. public function testOutOfOrderRowsInRightReaderAreNotNested($leftData, $rightData, $expected)
  215. {
  216. //var_dump($leftData);
  217. //var_dump($rightData);
  218. $leftReader = new ArrayReader($leftData);
  219. $rightReader = new ArrayReader($rightData);
  220. $oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId');
  221. $i = 0;
  222. foreach($oneToManyReader as $row) {
  223. $this->assertEquals($row, $expected[$i++]);
  224. }
  225. }
  226. public function outOfOrderRowProvider()
  227. {
  228. return array(
  229. 'skip-first-right-row' => array(
  230. 'left' => array(
  231. array(
  232. 'OrderId' => 3,
  233. 'Price' => 30,
  234. ),
  235. array(
  236. 'OrderId' => 2,
  237. 'Price' => 15,
  238. ),
  239. ),
  240. 'right' => array(
  241. array(
  242. 'OrderId' => 2,
  243. 'Name' => 'Super Cool Item 1',
  244. ),
  245. array(
  246. 'OrderId' => 1,
  247. 'Name' => 'Super Cool Item 1',
  248. ),
  249. array(
  250. 'OrderId' => 1,
  251. 'Name' => 'Super Cool Item 2',
  252. ),
  253. ),
  254. 'expected' => array(
  255. array(
  256. 'OrderId' => 3,
  257. 'Price' => 30,
  258. 'items' => array()
  259. ),
  260. array(
  261. 'OrderId' => 2,
  262. 'Price' => 15,
  263. 'items' => array(
  264. array(
  265. 'OrderId' => 2,
  266. 'Name' => 'Super Cool Item 1',
  267. ),
  268. )
  269. ),
  270. ),
  271. ),
  272. 'skip-out-of-order' => array(
  273. 'left' => array(
  274. array(
  275. 'OrderId' => 1,
  276. 'Price' => 30,
  277. ),
  278. array(
  279. 'OrderId' => 2,
  280. 'Price' => 15,
  281. ),
  282. ),
  283. 'right' => array(
  284. array(
  285. 'OrderId' => 0,
  286. 'Name' => 'Super Cool Item 1',
  287. ),
  288. array(
  289. 'OrderId' => 2,
  290. 'Name' => 'Super Cool Item 2',
  291. ),
  292. array(
  293. 'OrderId' => 1,
  294. 'Name' => 'Super Cool Item 3',
  295. ),
  296. ),
  297. 'expected' => array(
  298. array(
  299. 'OrderId' => 1,
  300. 'Price' => 30,
  301. 'items' => array()
  302. ),
  303. array(
  304. 'OrderId' => 2,
  305. 'Price' => 15,
  306. 'items' => array(),
  307. ),
  308. ),
  309. ),
  310. );
  311. }
  312. }