AssociationCollectionTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.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\TestSuite\TestCase;
  21. /**
  22. * AssociationCollection test case.
  23. */
  24. class AssociationCollectionTest extends TestCase {
  25. /**
  26. * setup
  27. *
  28. * @return void
  29. */
  30. public function setUp() {
  31. parent::setUp();
  32. $this->associations = new AssociationCollection();
  33. }
  34. /**
  35. * Test the simple add/has and get methods.
  36. *
  37. * @return void
  38. */
  39. public function testAddHasRemoveAndGet() {
  40. $this->assertFalse($this->associations->has('users'));
  41. $this->assertFalse($this->associations->has('Users'));
  42. $this->assertNull($this->associations->get('users'));
  43. $this->assertNull($this->associations->get('Users'));
  44. $belongsTo = new BelongsTo([]);
  45. $this->assertSame($belongsTo, $this->associations->add('Users', $belongsTo));
  46. $this->assertTrue($this->associations->has('users'));
  47. $this->assertTrue($this->associations->has('Users'));
  48. $this->assertSame($belongsTo, $this->associations->get('users'));
  49. $this->assertSame($belongsTo, $this->associations->get('Users'));
  50. $this->assertNull($this->associations->remove('Users'));
  51. $this->assertFalse($this->associations->has('users'));
  52. $this->assertFalse($this->associations->has('Users'));
  53. $this->assertNull($this->associations->get('users'));
  54. $this->assertNull($this->associations->get('Users'));
  55. }
  56. /**
  57. * Test removeAll method
  58. *
  59. * @return void
  60. */
  61. public function testRemoveAll() {
  62. $this->assertEmpty($this->associations->keys());
  63. $belongsTo = new BelongsTo([]);
  64. $this->assertSame($belongsTo, $this->associations->add('Users', $belongsTo));
  65. $belongsToMany = new BelongsToMany([]);
  66. $this->assertSame($belongsToMany, $this->associations->add('Cart', $belongsToMany));
  67. $this->associations->removeAll();
  68. $this->assertEmpty($this->associations->keys());
  69. }
  70. /**
  71. * Test getting associations by property.
  72. *
  73. * @return void
  74. */
  75. public function testGetByProperty() {
  76. $belongsTo = new BelongsTo('Users', []);
  77. $this->assertEquals('user', $belongsTo->property());
  78. $this->associations->add('Users', $belongsTo);
  79. $this->assertNull($this->associations->get('user'));
  80. $this->assertSame($belongsTo, $this->associations->getByProperty('user'));
  81. }
  82. /**
  83. * Test associations with plugin names.
  84. *
  85. * @return void
  86. */
  87. public function testAddHasRemoveGetWithPlugin() {
  88. $this->assertFalse($this->associations->has('Photos.Photos'));
  89. $this->assertFalse($this->associations->has('Photos'));
  90. $belongsTo = new BelongsTo([]);
  91. $this->assertSame($belongsTo, $this->associations->add('Photos.Photos', $belongsTo));
  92. $this->assertTrue($this->associations->has('Photos'));
  93. $this->assertFalse($this->associations->has('Photos.Photos'));
  94. }
  95. /**
  96. * Test keys()
  97. *
  98. * @return void
  99. */
  100. public function testKeys() {
  101. $belongsTo = new BelongsTo([]);
  102. $this->associations->add('Users', $belongsTo);
  103. $this->associations->add('Categories', $belongsTo);
  104. $this->assertEquals(['users', 'categories'], $this->associations->keys());
  105. $this->associations->remove('Categories');
  106. $this->assertEquals(['users'], $this->associations->keys());
  107. }
  108. /**
  109. * Test getting association names by type.
  110. *
  111. * @return void
  112. */
  113. public function testType() {
  114. $belongsTo = new BelongsTo([]);
  115. $this->associations->add('Users', $belongsTo);
  116. $belongsToMany = new BelongsToMany([]);
  117. $this->associations->add('Tags', $belongsToMany);
  118. $this->assertSame([$belongsTo], $this->associations->type('BelongsTo'));
  119. $this->assertSame([$belongsToMany], $this->associations->type('BelongsToMany'));
  120. $this->assertSame([], $this->associations->type('HasMany'));
  121. }
  122. /**
  123. * test cascading deletes.
  124. *
  125. * @return void
  126. */
  127. public function testCascadeDelete() {
  128. $mockOne = $this->getMock('Cake\ORM\Association\BelongsTo', [], [[]]);
  129. $mockTwo = $this->getMock('Cake\ORM\Association\HasMany', [], [[]]);
  130. $entity = new Entity();
  131. $options = ['option' => 'value'];
  132. $this->associations->add('One', $mockOne);
  133. $this->associations->add('Two', $mockTwo);
  134. $mockOne->expects($this->once())
  135. ->method('cascadeDelete')
  136. ->with($entity, $options);
  137. $mockTwo->expects($this->once())
  138. ->method('cascadeDelete')
  139. ->with($entity, $options);
  140. $this->assertNull($this->associations->cascadeDelete($entity, $options));
  141. }
  142. /**
  143. * Test saving parent associations
  144. *
  145. * @return void
  146. */
  147. public function testSaveParents() {
  148. $table = $this->getMock('Cake\ORM\Table', [], [[]]);
  149. $mockOne = $this->getMock(
  150. 'Cake\ORM\Association\BelongsTo',
  151. ['saveAssociated'],
  152. ['Parent', [
  153. 'sourceTable' => $table,
  154. ]]);
  155. $mockTwo = $this->getMock(
  156. 'Cake\ORM\Association\HasMany',
  157. ['saveAssociated'],
  158. ['Child', [
  159. 'sourceTable' => $table
  160. ]]);
  161. $this->associations->add('Parent', $mockOne);
  162. $this->associations->add('Child', $mockTwo);
  163. $entity = new Entity();
  164. $entity->set('parent', ['key' => 'value']);
  165. $entity->set('child', ['key' => 'value']);
  166. $options = ['option' => 'value'];
  167. $mockOne->expects($this->once())
  168. ->method('saveAssociated')
  169. ->with($entity, $options)
  170. ->will($this->returnValue(true));
  171. $mockTwo->expects($this->never())
  172. ->method('saveAssociated');
  173. $result = $this->associations->saveParents(
  174. $table,
  175. $entity,
  176. ['Parent', 'Child'],
  177. $options
  178. );
  179. $this->assertTrue($result, 'Save should work.');
  180. }
  181. /**
  182. * Test saving filtered parent associations.
  183. *
  184. * @return void
  185. */
  186. public function testSaveParentsFiltered() {
  187. $table = $this->getMock('Cake\ORM\Table', [], [[]]);
  188. $mockOne = $this->getMock(
  189. 'Cake\ORM\Association\BelongsTo',
  190. ['saveAssociated'],
  191. ['Parents', [
  192. 'sourceTable' => $table,
  193. ]]);
  194. $mockTwo = $this->getMock(
  195. 'Cake\ORM\Association\BelongsTo',
  196. ['saveAssociated'],
  197. ['Categories', [
  198. 'sourceTable' => $table
  199. ]]);
  200. $this->associations->add('Parents', $mockOne);
  201. $this->associations->add('Categories', $mockTwo);
  202. $entity = new Entity();
  203. $entity->set('parent', ['key' => 'value']);
  204. $entity->set('category', ['key' => 'value']);
  205. $options = ['atomic' => true];
  206. $mockOne->expects($this->once())
  207. ->method('saveAssociated')
  208. ->with($entity, ['atomic' => true, 'associated' => ['Others']])
  209. ->will($this->returnValue(true));
  210. $mockTwo->expects($this->never())
  211. ->method('saveAssociated');
  212. $result = $this->associations->saveParents(
  213. $table,
  214. $entity,
  215. ['Parents' => ['associated' => ['Others']]],
  216. $options
  217. );
  218. $this->assertTrue($result, 'Save should work.');
  219. }
  220. /**
  221. * Test saving filtered child associations.
  222. *
  223. * @return void
  224. */
  225. public function testSaveChildrenFiltered() {
  226. $table = $this->getMock('Cake\ORM\Table', [], [[]]);
  227. $mockOne = $this->getMock(
  228. 'Cake\ORM\Association\HasMany',
  229. ['saveAssociated'],
  230. ['Comments', [
  231. 'sourceTable' => $table,
  232. ]]);
  233. $mockTwo = $this->getMock(
  234. 'Cake\ORM\Association\HasOne',
  235. ['saveAssociated'],
  236. ['Profiles', [
  237. 'sourceTable' => $table
  238. ]]);
  239. $this->associations->add('Comments', $mockOne);
  240. $this->associations->add('Profiles', $mockTwo);
  241. $entity = new Entity();
  242. $entity->set('comments', ['key' => 'value']);
  243. $entity->set('profile', ['key' => 'value']);
  244. $options = ['atomic' => true];
  245. $mockOne->expects($this->once())
  246. ->method('saveAssociated')
  247. ->with($entity, $options + ['associated' => ['Other']])
  248. ->will($this->returnValue(true));
  249. $mockTwo->expects($this->never())
  250. ->method('saveAssociated');
  251. $result = $this->associations->saveChildren(
  252. $table,
  253. $entity,
  254. ['Comments' => ['associated' => ['Other']]],
  255. $options
  256. );
  257. $this->assertTrue($result, 'Should succeed.');
  258. }
  259. /**
  260. * Test exceptional case.
  261. *
  262. * @expectedException \InvalidArgumentException
  263. * @expectedExceptionMessage Cannot save Profiles, it is not associated to Users
  264. */
  265. public function testErrorOnUnknownAlias() {
  266. $table = $this->getMock(
  267. 'Cake\ORM\Table',
  268. ['save'],
  269. [['alias' => 'Users']]);
  270. $entity = new Entity();
  271. $entity->set('profile', ['key' => 'value']);
  272. $this->associations->saveChildren(
  273. $table,
  274. $entity,
  275. ['Profiles'],
  276. ['atomic' => true]
  277. );
  278. }
  279. /**
  280. * Tests the normalizeKeys method
  281. *
  282. * @return void
  283. */
  284. public function testNormalizeKeys() {
  285. $this->assertSame([], $this->associations->normalizeKeys([]));
  286. $this->assertSame([], $this->associations->normalizeKeys(false));
  287. $assocs = ['a', 'b', 'd' => ['something']];
  288. $expected = ['a' => [], 'b' => [], 'd' => ['something']];
  289. $this->assertSame($expected, $this->associations->normalizeKeys($assocs));
  290. $belongsTo = new BelongsTo([]);
  291. $this->associations->add('users', $belongsTo);
  292. $this->associations->add('categories', $belongsTo);
  293. $expected = ['users' => [], 'categories' => []];
  294. $this->assertSame($expected, $this->associations->normalizeKeys(true));
  295. }
  296. }