AssociationCollectionTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\ORM\AssociationCollection;
  17. use Cake\ORM\Association\BelongsTo;
  18. use Cake\ORM\Association\BelongsToMany;
  19. use Cake\ORM\Entity;
  20. use Cake\ORM\Locator\LocatorInterface;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * AssociationCollection test case.
  24. */
  25. class AssociationCollectionTest extends TestCase
  26. {
  27. /**
  28. * @var AssociationCollection
  29. */
  30. public $associations;
  31. /**
  32. * setup
  33. *
  34. * @return void
  35. */
  36. public function setUp()
  37. {
  38. parent::setUp();
  39. $this->associations = new AssociationCollection();
  40. }
  41. /**
  42. * Test the constructor.
  43. *
  44. * @return void
  45. */
  46. public function testConstructor()
  47. {
  48. $this->assertSame($this->getTableLocator(), $this->associations->getTableLocator());
  49. $tableLocator = $this->createMock(LocatorInterface::class);
  50. $associations = new AssociationCollection($tableLocator);
  51. $this->assertSame($tableLocator, $associations->getTableLocator());
  52. }
  53. /**
  54. * Test the simple add/has and get methods.
  55. *
  56. * @return void
  57. */
  58. public function testAddHasRemoveAndGet()
  59. {
  60. $this->assertFalse($this->associations->has('users'));
  61. $this->assertFalse($this->associations->has('Users'));
  62. $this->assertNull($this->associations->get('users'));
  63. $this->assertNull($this->associations->get('Users'));
  64. $belongsTo = new BelongsTo('');
  65. $this->assertSame($belongsTo, $this->associations->add('Users', $belongsTo));
  66. $this->assertTrue($this->associations->has('users'));
  67. $this->assertTrue($this->associations->has('Users'));
  68. $this->assertSame($belongsTo, $this->associations->get('users'));
  69. $this->assertSame($belongsTo, $this->associations->get('Users'));
  70. $this->assertNull($this->associations->remove('Users'));
  71. $this->assertFalse($this->associations->has('users'));
  72. $this->assertFalse($this->associations->has('Users'));
  73. $this->assertNull($this->associations->get('users'));
  74. $this->assertNull($this->associations->get('Users'));
  75. }
  76. /**
  77. * Test the load method.
  78. *
  79. * @return void
  80. */
  81. public function testLoad()
  82. {
  83. $this->associations->load(BelongsTo::class, 'Users');
  84. $this->assertTrue($this->associations->has('Users'));
  85. $this->assertInstanceOf(BelongsTo::class, $this->associations->get('Users'));
  86. $this->assertSame($this->associations->getTableLocator(), $this->associations->get('Users')->getTableLocator());
  87. }
  88. /**
  89. * Test the load method with custom locator.
  90. *
  91. * @return void
  92. */
  93. public function testLoadCustomLocator()
  94. {
  95. $locator = $this->createMock(LocatorInterface::class);
  96. $this->associations->load(BelongsTo::class, 'Users', [
  97. 'tableLocator' => $locator
  98. ]);
  99. $this->assertTrue($this->associations->has('Users'));
  100. $this->assertInstanceOf(BelongsTo::class, $this->associations->get('Users'));
  101. $this->assertSame($locator, $this->associations->get('Users')->getTableLocator());
  102. }
  103. /**
  104. * Test load invalid class.
  105. *
  106. * @return void
  107. * @expectedException InvalidArgumentException
  108. * @expectedExceptionMessage The association must extend `Cake\ORM\Association` class, `stdClass` given.
  109. */
  110. public function testLoadInvalid()
  111. {
  112. $this->associations->load('stdClass', 'Users');
  113. }
  114. /**
  115. * Test removeAll method
  116. *
  117. * @return void
  118. */
  119. public function testRemoveAll()
  120. {
  121. $this->assertEmpty($this->associations->keys());
  122. $belongsTo = new BelongsTo('');
  123. $this->assertSame($belongsTo, $this->associations->add('Users', $belongsTo));
  124. $belongsToMany = new BelongsToMany('');
  125. $this->assertSame($belongsToMany, $this->associations->add('Cart', $belongsToMany));
  126. $this->associations->removeAll();
  127. $this->assertEmpty($this->associations->keys());
  128. }
  129. /**
  130. * Test getting associations by property.
  131. *
  132. * @return void
  133. */
  134. public function testGetByProperty()
  135. {
  136. $table = $this->getMockBuilder('Cake\ORM\Table')
  137. ->setMethods(['table'])
  138. ->getMock();
  139. $table->setSchema([]);
  140. $belongsTo = new BelongsTo('Users', [
  141. 'sourceTable' => $table
  142. ]);
  143. $this->assertEquals('user', $belongsTo->getProperty());
  144. $this->associations->add('Users', $belongsTo);
  145. $this->assertNull($this->associations->get('user'));
  146. $this->assertSame($belongsTo, $this->associations->getByProperty('user'));
  147. }
  148. /**
  149. * Test associations with plugin names.
  150. *
  151. * @return void
  152. */
  153. public function testAddHasRemoveGetWithPlugin()
  154. {
  155. $this->assertFalse($this->associations->has('Photos.Photos'));
  156. $this->assertFalse($this->associations->has('Photos'));
  157. $belongsTo = new BelongsTo('');
  158. $this->assertSame($belongsTo, $this->associations->add('Photos.Photos', $belongsTo));
  159. $this->assertTrue($this->associations->has('Photos'));
  160. $this->assertFalse($this->associations->has('Photos.Photos'));
  161. }
  162. /**
  163. * Test keys()
  164. *
  165. * @return void
  166. */
  167. public function testKeys()
  168. {
  169. $belongsTo = new BelongsTo('');
  170. $this->associations->add('Users', $belongsTo);
  171. $this->associations->add('Categories', $belongsTo);
  172. $this->assertEquals(['users', 'categories'], $this->associations->keys());
  173. $this->associations->remove('Categories');
  174. $this->assertEquals(['users'], $this->associations->keys());
  175. }
  176. /**
  177. * Data provider for AssociationCollection::getByType
  178. */
  179. public function associationCollectionType()
  180. {
  181. return [
  182. ['BelongsTo', 'BelongsToMany'],
  183. ['belongsTo', 'belongsToMany'],
  184. ['belongsto', 'belongstomany']
  185. ];
  186. }
  187. /**
  188. * Test getting association names by getByType.
  189. *
  190. * @param string $belongsToStr
  191. * @param string $belongsToManyStr
  192. * @dataProvider associationCollectionType
  193. */
  194. public function testGetByType($belongsToStr, $belongsToManyStr)
  195. {
  196. $belongsTo = new BelongsTo('');
  197. $this->associations->add('Users', $belongsTo);
  198. $belongsToMany = new BelongsToMany('');
  199. $this->associations->add('Tags', $belongsToMany);
  200. $this->assertSame([$belongsTo], $this->associations->getByType($belongsToStr));
  201. $this->assertSame([$belongsToMany], $this->associations->getByType($belongsToManyStr));
  202. $this->assertSame([$belongsTo, $belongsToMany], $this->associations->getByType([$belongsToStr, $belongsToManyStr]));
  203. }
  204. /**
  205. * Type should return empty array.
  206. *
  207. * @return void
  208. */
  209. public function hasTypeReturnsEmptyArray()
  210. {
  211. foreach (['HasMany', 'hasMany', 'FooBar', 'DoesNotExist'] as $value) {
  212. $this->assertSame([], $this->associations->getByType($value));
  213. }
  214. }
  215. /**
  216. * test cascading deletes.
  217. *
  218. * @return void
  219. */
  220. public function testCascadeDelete()
  221. {
  222. $mockOne = $this->getMockBuilder('Cake\ORM\Association\BelongsTo')
  223. ->setConstructorArgs([''])
  224. ->getMock();
  225. $mockTwo = $this->getMockBuilder('Cake\ORM\Association\HasMany')
  226. ->setConstructorArgs([''])
  227. ->getMock();
  228. $entity = new Entity();
  229. $options = ['option' => 'value'];
  230. $this->associations->add('One', $mockOne);
  231. $this->associations->add('Two', $mockTwo);
  232. $mockOne->expects($this->once())
  233. ->method('cascadeDelete')
  234. ->with($entity, $options);
  235. $mockTwo->expects($this->once())
  236. ->method('cascadeDelete')
  237. ->with($entity, $options);
  238. $this->assertNull($this->associations->cascadeDelete($entity, $options));
  239. }
  240. /**
  241. * Test saving parent associations
  242. *
  243. * @return void
  244. */
  245. public function testSaveParents()
  246. {
  247. $table = $this->getMockBuilder('Cake\ORM\Table')
  248. ->setMethods(['table'])
  249. ->getMock();
  250. $table->setSchema([]);
  251. $mockOne = $this->getMockBuilder('Cake\ORM\Association\BelongsTo')
  252. ->setMethods(['saveAssociated'])
  253. ->setConstructorArgs(['Parent', [
  254. 'sourceTable' => $table,
  255. ]])
  256. ->getMock();
  257. $mockTwo = $this->getMockBuilder('Cake\ORM\Association\HasMany')
  258. ->setMethods(['saveAssociated'])
  259. ->setConstructorArgs(['Child', [
  260. 'sourceTable' => $table
  261. ]])
  262. ->getMock();
  263. $this->associations->add('Parent', $mockOne);
  264. $this->associations->add('Child', $mockTwo);
  265. $entity = new Entity();
  266. $entity->set('parent', ['key' => 'value']);
  267. $entity->set('child', ['key' => 'value']);
  268. $options = ['option' => 'value'];
  269. $mockOne->expects($this->once())
  270. ->method('saveAssociated')
  271. ->with($entity, $options)
  272. ->will($this->returnValue(true));
  273. $mockTwo->expects($this->never())
  274. ->method('saveAssociated');
  275. $result = $this->associations->saveParents(
  276. $table,
  277. $entity,
  278. ['Parent', 'Child'],
  279. $options
  280. );
  281. $this->assertTrue($result, 'Save should work.');
  282. }
  283. /**
  284. * Test saving filtered parent associations.
  285. *
  286. * @return void
  287. */
  288. public function testSaveParentsFiltered()
  289. {
  290. $table = $this->getMockBuilder('Cake\ORM\Table')
  291. ->setMethods(['table'])
  292. ->getMock();
  293. $table->setSchema([]);
  294. $mockOne = $this->getMockBuilder('Cake\ORM\Association\BelongsTo')
  295. ->setMethods(['saveAssociated'])
  296. ->setConstructorArgs(['Parents', [
  297. 'sourceTable' => $table,
  298. ]])
  299. ->getMock();
  300. $mockTwo = $this->getMockBuilder('Cake\ORM\Association\BelongsTo')
  301. ->setMethods(['saveAssociated'])
  302. ->setConstructorArgs(['Categories', [
  303. 'sourceTable' => $table
  304. ]])
  305. ->getMock();
  306. $this->associations->add('Parents', $mockOne);
  307. $this->associations->add('Categories', $mockTwo);
  308. $entity = new Entity();
  309. $entity->set('parent', ['key' => 'value']);
  310. $entity->set('category', ['key' => 'value']);
  311. $options = ['atomic' => true];
  312. $mockOne->expects($this->once())
  313. ->method('saveAssociated')
  314. ->with($entity, ['atomic' => true, 'associated' => ['Others']])
  315. ->will($this->returnValue(true));
  316. $mockTwo->expects($this->never())
  317. ->method('saveAssociated');
  318. $result = $this->associations->saveParents(
  319. $table,
  320. $entity,
  321. ['Parents' => ['associated' => ['Others']]],
  322. $options
  323. );
  324. $this->assertTrue($result, 'Save should work.');
  325. }
  326. /**
  327. * Test saving filtered child associations.
  328. *
  329. * @return void
  330. */
  331. public function testSaveChildrenFiltered()
  332. {
  333. $table = $this->getMockBuilder('Cake\ORM\Table')
  334. ->setMethods(['table'])
  335. ->getMock();
  336. $table->setSchema([]);
  337. $mockOne = $this->getMockBuilder('Cake\ORM\Association\HasMany')
  338. ->setMethods(['saveAssociated'])
  339. ->setConstructorArgs(['Comments', [
  340. 'sourceTable' => $table,
  341. ]])
  342. ->getMock();
  343. $mockTwo = $this->getMockBuilder('Cake\ORM\Association\HasOne')
  344. ->setMethods(['saveAssociated'])
  345. ->setConstructorArgs(['Profiles', [
  346. 'sourceTable' => $table
  347. ]])
  348. ->getMock();
  349. $this->associations->add('Comments', $mockOne);
  350. $this->associations->add('Profiles', $mockTwo);
  351. $entity = new Entity();
  352. $entity->set('comments', ['key' => 'value']);
  353. $entity->set('profile', ['key' => 'value']);
  354. $options = ['atomic' => true];
  355. $mockOne->expects($this->once())
  356. ->method('saveAssociated')
  357. ->with($entity, $options + ['associated' => ['Other']])
  358. ->will($this->returnValue(true));
  359. $mockTwo->expects($this->never())
  360. ->method('saveAssociated');
  361. $result = $this->associations->saveChildren(
  362. $table,
  363. $entity,
  364. ['Comments' => ['associated' => ['Other']]],
  365. $options
  366. );
  367. $this->assertTrue($result, 'Should succeed.');
  368. }
  369. /**
  370. * Test exceptional case.
  371. *
  372. */
  373. public function testErrorOnUnknownAlias()
  374. {
  375. $this->expectException(\InvalidArgumentException::class);
  376. $this->expectExceptionMessage('Cannot save Profiles, it is not associated to Users');
  377. $table = $this->getMockBuilder('Cake\ORM\Table')
  378. ->setMethods(['save'])
  379. ->setConstructorArgs([['alias' => 'Users']])
  380. ->getMock();
  381. $entity = new Entity();
  382. $entity->set('profile', ['key' => 'value']);
  383. $this->associations->saveChildren(
  384. $table,
  385. $entity,
  386. ['Profiles'],
  387. ['atomic' => true]
  388. );
  389. }
  390. /**
  391. * Tests the normalizeKeys method
  392. *
  393. * @return void
  394. */
  395. public function testNormalizeKeys()
  396. {
  397. $this->assertSame([], $this->associations->normalizeKeys([]));
  398. $this->assertSame([], $this->associations->normalizeKeys(false));
  399. $assocs = ['a', 'b', 'd' => ['something']];
  400. $expected = ['a' => [], 'b' => [], 'd' => ['something']];
  401. $this->assertSame($expected, $this->associations->normalizeKeys($assocs));
  402. $belongsTo = new BelongsTo('');
  403. $this->associations->add('users', $belongsTo);
  404. $this->associations->add('categories', $belongsTo);
  405. $expected = ['users' => [], 'categories' => []];
  406. $this->assertSame($expected, $this->associations->normalizeKeys(true));
  407. }
  408. /**
  409. * Ensure that the association collection can be iterated.
  410. *
  411. * @return void
  412. */
  413. public function testAssociationsCanBeIterated()
  414. {
  415. $belongsTo = new BelongsTo('');
  416. $this->associations->add('Users', $belongsTo);
  417. $belongsToMany = new BelongsToMany('');
  418. $this->associations->add('Cart', $belongsToMany);
  419. $expected = ['users' => $belongsTo, 'cart' => $belongsToMany];
  420. $result = iterator_to_array($this->associations, true);
  421. $this->assertSame($expected, $result);
  422. }
  423. }