AssociationTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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\Core\Plugin;
  17. use Cake\ORM\Association;
  18. use Cake\ORM\Table;
  19. use Cake\ORM\TableRegistry;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * A Test double used to assert that default tables are created
  23. *
  24. */
  25. class TestTable extends Table
  26. {
  27. public function initialize(array $config = [])
  28. {
  29. $this->schema(['id' => ['type' => 'integer']]);
  30. }
  31. public function findPublished($query)
  32. {
  33. return $query->applyOptions(['this' => 'worked']);
  34. }
  35. }
  36. /**
  37. * Tests Association class
  38. *
  39. */
  40. class AssociationTest extends TestCase
  41. {
  42. /**
  43. * Set up
  44. *
  45. * @return void
  46. */
  47. public function setUp()
  48. {
  49. parent::setUp();
  50. $this->source = new TestTable;
  51. $config = [
  52. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  53. 'foreignKey' => 'a_key',
  54. 'conditions' => ['field' => 'value'],
  55. 'dependent' => true,
  56. 'sourceTable' => $this->source,
  57. 'joinType' => 'INNER'
  58. ];
  59. $this->association = $this->getMock(
  60. '\Cake\ORM\Association',
  61. [
  62. '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
  63. 'saveAssociated', 'eagerLoader', 'type'
  64. ],
  65. ['Foo', $config]
  66. );
  67. }
  68. /**
  69. * Tear down
  70. *
  71. * @return void
  72. */
  73. public function tearDown()
  74. {
  75. parent::tearDown();
  76. TableRegistry::clear();
  77. }
  78. /**
  79. * Tests that _options acts as a callback where subclasses can add their own
  80. * initialization code based on the passed configuration array
  81. *
  82. * @return void
  83. */
  84. public function testOptionsIsCalled()
  85. {
  86. $options = ['foo' => 'bar'];
  87. $this->association->expects($this->once())->method('_options')->with($options);
  88. $this->association->__construct('Name', $options);
  89. }
  90. /**
  91. * Tests that name() returns the correct configure association name
  92. *
  93. * @return void
  94. */
  95. public function testName()
  96. {
  97. $this->assertEquals('Foo', $this->association->name());
  98. $this->association->name('Bar');
  99. $this->assertEquals('Bar', $this->association->name());
  100. }
  101. /**
  102. * Tests that cascadeCallbacks() returns the correct configured value
  103. *
  104. * @return void
  105. */
  106. public function testCascadeCallbacks()
  107. {
  108. $this->assertSame(false, $this->association->cascadeCallbacks());
  109. $this->association->cascadeCallbacks(true);
  110. $this->assertSame(true, $this->association->cascadeCallbacks());
  111. }
  112. /**
  113. * Tests the bindingKey method as a setter/getter
  114. *
  115. * @return void
  116. */
  117. public function testBindingKey()
  118. {
  119. $this->association->bindingKey('foo_id');
  120. $this->assertEquals('foo_id', $this->association->bindingKey());
  121. }
  122. /**
  123. * Tests the bindingKey() method when called with its defaults
  124. *
  125. * @return void
  126. */
  127. public function testBindingKeyDefault()
  128. {
  129. $this->source->primaryKey(['id', 'site_id']);
  130. $this->association
  131. ->expects($this->once())
  132. ->method('isOwningSide')
  133. ->will($this->returnValue(true));
  134. $result = $this->association->bindingKey();
  135. $this->assertEquals(['id', 'site_id'], $result);
  136. }
  137. /**
  138. * Tests the bindingKey() method when the association source is not the
  139. * owning side
  140. *
  141. * @return void
  142. */
  143. public function testBindingDefaultNoOwningSide()
  144. {
  145. $target = new Table;
  146. $target->primaryKey(['foo', 'site_id']);
  147. $this->association->target($target);
  148. $this->association
  149. ->expects($this->once())
  150. ->method('isOwningSide')
  151. ->will($this->returnValue(false));
  152. $result = $this->association->bindingKey();
  153. $this->assertEquals(['foo', 'site_id'], $result);
  154. }
  155. /**
  156. * Tests that name() returns the correct configured value
  157. *
  158. * @return void
  159. */
  160. public function testForeignKey()
  161. {
  162. $this->assertEquals('a_key', $this->association->foreignKey());
  163. $this->association->foreignKey('another_key');
  164. $this->assertEquals('another_key', $this->association->foreignKey());
  165. }
  166. /**
  167. * Tests that conditions() returns the correct configured value
  168. *
  169. * @return void
  170. */
  171. public function testConditions()
  172. {
  173. $this->assertEquals(['field' => 'value'], $this->association->conditions());
  174. $conds = ['another_key' => 'another value'];
  175. $this->association->conditions($conds);
  176. $this->assertEquals($conds, $this->association->conditions());
  177. }
  178. /**
  179. * Tests that canBeJoined() returns the correct configured value
  180. *
  181. * @return void
  182. */
  183. public function testCanBeJoined()
  184. {
  185. $this->assertTrue($this->association->canBeJoined());
  186. }
  187. /**
  188. * Tests that target() returns the correct Table object
  189. *
  190. * @return void
  191. */
  192. public function testTarget()
  193. {
  194. $table = $this->association->target();
  195. $this->assertInstanceOf(__NAMESPACE__ . '\TestTable', $table);
  196. $other = new Table;
  197. $this->association->target($other);
  198. $this->assertSame($other, $this->association->target());
  199. }
  200. /**
  201. * Tests that target() returns the correct Table object for plugins
  202. *
  203. * @return void
  204. */
  205. public function testTargetPlugin()
  206. {
  207. Plugin::load('TestPlugin');
  208. $config = [
  209. 'className' => 'TestPlugin.Comments',
  210. 'foreignKey' => 'a_key',
  211. 'conditions' => ['field' => 'value'],
  212. 'dependent' => true,
  213. 'sourceTable' => $this->source,
  214. 'joinType' => 'INNER'
  215. ];
  216. $this->association = $this->getMock(
  217. '\Cake\ORM\Association',
  218. ['type', 'eagerLoader', 'cascadeDelete', 'isOwningSide', 'saveAssociated'],
  219. ['ThisAssociationName', $config]
  220. );
  221. $table = $this->association->target();
  222. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $table);
  223. $this->assertTrue(
  224. TableRegistry::exists('TestPlugin.ThisAssociationName'),
  225. 'The association class will use this registry key'
  226. );
  227. $this->assertFalse(TableRegistry::exists('TestPlugin.Comments'), 'The association class will NOT use this key');
  228. $this->assertFalse(TableRegistry::exists('Comments'), 'Should also not be set');
  229. $this->assertFalse(TableRegistry::exists('ThisAssociationName'), 'Should also not be set');
  230. $plugin = TableRegistry::get('TestPlugin.ThisAssociationName');
  231. $this->assertSame($table, $plugin, 'Should be an instance of TestPlugin.Comments');
  232. $this->assertSame('TestPlugin.ThisAssociationName', $table->registryAlias());
  233. $this->assertSame('comments', $table->table());
  234. $this->assertSame('ThisAssociationName', $table->alias());
  235. }
  236. /**
  237. * Tests that source() returns the correct Table object
  238. *
  239. * @return void
  240. */
  241. public function testSource()
  242. {
  243. $table = $this->association->source();
  244. $this->assertSame($this->source, $table);
  245. $other = new Table;
  246. $this->association->source($other);
  247. $this->assertSame($other, $this->association->source());
  248. }
  249. /**
  250. * Tests joinType method
  251. *
  252. * @return void
  253. */
  254. public function testJoinType()
  255. {
  256. $this->assertEquals('INNER', $this->association->joinType());
  257. $this->association->joinType('LEFT');
  258. $this->assertEquals('LEFT', $this->association->joinType());
  259. }
  260. /**
  261. * Tests property method
  262. *
  263. * @return void
  264. */
  265. public function testProperty()
  266. {
  267. $this->assertEquals('foo', $this->association->property());
  268. $this->association->property('thing');
  269. $this->assertEquals('thing', $this->association->property());
  270. }
  271. /**
  272. * Tests strategy method
  273. *
  274. * @return void
  275. */
  276. public function testStrategy()
  277. {
  278. $this->assertEquals('join', $this->association->strategy());
  279. $this->association->strategy('select');
  280. $this->assertEquals('select', $this->association->strategy());
  281. $this->association->strategy('subquery');
  282. $this->assertEquals('subquery', $this->association->strategy());
  283. }
  284. /**
  285. * Tests that providing an invalid strategy throws an exception
  286. *
  287. * @expectedException \InvalidArgumentException
  288. * @return void
  289. */
  290. public function testInvalidStrategy()
  291. {
  292. $this->association->strategy('anotherThing');
  293. $this->assertEquals('subquery', $this->association->strategy());
  294. }
  295. /**
  296. * Tests test finder() method as getter and setter
  297. *
  298. * @return void
  299. */
  300. public function testFinderMethod()
  301. {
  302. $this->assertEquals('all', $this->association->finder());
  303. $this->assertEquals('published', $this->association->finder('published'));
  304. $this->assertEquals('published', $this->association->finder());
  305. }
  306. /**
  307. * Tests that `finder` is a valid option for the association constructor
  308. *
  309. * @return void
  310. */
  311. public function testFinderInConstructor()
  312. {
  313. $config = [
  314. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  315. 'foreignKey' => 'a_key',
  316. 'conditions' => ['field' => 'value'],
  317. 'dependent' => true,
  318. 'sourceTable' => $this->source,
  319. 'joinType' => 'INNER',
  320. 'finder' => 'published'
  321. ];
  322. $assoc = $this->getMock(
  323. '\Cake\ORM\Association',
  324. ['type', 'eagerLoader', 'cascadeDelete', 'isOwningSide', 'saveAssociated'],
  325. ['Foo', $config]
  326. );
  327. $this->assertEquals('published', $assoc->finder());
  328. }
  329. /**
  330. * Tests that the defined custom finder is used when calling find
  331. * in the association
  332. *
  333. * @return void
  334. */
  335. public function testCustomFinderIsUsed()
  336. {
  337. $this->association->finder('published');
  338. $this->assertEquals(
  339. ['this' => 'worked'],
  340. $this->association->find()->getOptions()
  341. );
  342. }
  343. }