AssociationTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 className() returns the correct association className
  103. *
  104. * @return void
  105. */
  106. public function testClassName()
  107. {
  108. $this->assertEquals('\Cake\Test\TestCase\ORM\TestTable', $this->association->className());
  109. }
  110. /**
  111. * Tests that className() returns the correct (unnormalized) className
  112. *
  113. * @return void
  114. */
  115. public function testClassNameUnnormalized()
  116. {
  117. $config = [
  118. 'className' => 'Test',
  119. ];
  120. $this->association = $this->getMock(
  121. '\Cake\ORM\Association',
  122. [
  123. '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
  124. 'saveAssociated', 'eagerLoader', 'type'
  125. ],
  126. ['Foo', $config]
  127. );
  128. $this->assertEquals('Test', $this->association->className());
  129. }
  130. /**
  131. * Tests that cascadeCallbacks() returns the correct configured value
  132. *
  133. * @return void
  134. */
  135. public function testCascadeCallbacks()
  136. {
  137. $this->assertSame(false, $this->association->cascadeCallbacks());
  138. $this->association->cascadeCallbacks(true);
  139. $this->assertSame(true, $this->association->cascadeCallbacks());
  140. }
  141. /**
  142. * Tests the bindingKey method as a setter/getter
  143. *
  144. * @return void
  145. */
  146. public function testBindingKey()
  147. {
  148. $this->association->bindingKey('foo_id');
  149. $this->assertEquals('foo_id', $this->association->bindingKey());
  150. }
  151. /**
  152. * Tests the bindingKey() method when called with its defaults
  153. *
  154. * @return void
  155. */
  156. public function testBindingKeyDefault()
  157. {
  158. $this->source->primaryKey(['id', 'site_id']);
  159. $this->association
  160. ->expects($this->once())
  161. ->method('isOwningSide')
  162. ->will($this->returnValue(true));
  163. $result = $this->association->bindingKey();
  164. $this->assertEquals(['id', 'site_id'], $result);
  165. }
  166. /**
  167. * Tests the bindingKey() method when the association source is not the
  168. * owning side
  169. *
  170. * @return void
  171. */
  172. public function testBindingDefaultNoOwningSide()
  173. {
  174. $target = new Table;
  175. $target->primaryKey(['foo', 'site_id']);
  176. $this->association->target($target);
  177. $this->association
  178. ->expects($this->once())
  179. ->method('isOwningSide')
  180. ->will($this->returnValue(false));
  181. $result = $this->association->bindingKey();
  182. $this->assertEquals(['foo', 'site_id'], $result);
  183. }
  184. /**
  185. * Tests that name() returns the correct configured value
  186. *
  187. * @return void
  188. */
  189. public function testForeignKey()
  190. {
  191. $this->assertEquals('a_key', $this->association->foreignKey());
  192. $this->association->foreignKey('another_key');
  193. $this->assertEquals('another_key', $this->association->foreignKey());
  194. }
  195. /**
  196. * Tests that conditions() returns the correct configured value
  197. *
  198. * @return void
  199. */
  200. public function testConditions()
  201. {
  202. $this->assertEquals(['field' => 'value'], $this->association->conditions());
  203. $conds = ['another_key' => 'another value'];
  204. $this->association->conditions($conds);
  205. $this->assertEquals($conds, $this->association->conditions());
  206. }
  207. /**
  208. * Tests that canBeJoined() returns the correct configured value
  209. *
  210. * @return void
  211. */
  212. public function testCanBeJoined()
  213. {
  214. $this->assertTrue($this->association->canBeJoined());
  215. }
  216. /**
  217. * Tests that target() returns the correct Table object
  218. *
  219. * @return void
  220. */
  221. public function testTarget()
  222. {
  223. $table = $this->association->target();
  224. $this->assertInstanceOf(__NAMESPACE__ . '\TestTable', $table);
  225. $other = new Table;
  226. $this->association->target($other);
  227. $this->assertSame($other, $this->association->target());
  228. }
  229. /**
  230. * Tests that target() returns the correct Table object for plugins
  231. *
  232. * @return void
  233. */
  234. public function testTargetPlugin()
  235. {
  236. Plugin::load('TestPlugin');
  237. $config = [
  238. 'className' => 'TestPlugin.Comments',
  239. 'foreignKey' => 'a_key',
  240. 'conditions' => ['field' => 'value'],
  241. 'dependent' => true,
  242. 'sourceTable' => $this->source,
  243. 'joinType' => 'INNER'
  244. ];
  245. $this->association = $this->getMock(
  246. '\Cake\ORM\Association',
  247. ['type', 'eagerLoader', 'cascadeDelete', 'isOwningSide', 'saveAssociated'],
  248. ['ThisAssociationName', $config]
  249. );
  250. $table = $this->association->target();
  251. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $table);
  252. $this->assertTrue(
  253. TableRegistry::exists('TestPlugin.ThisAssociationName'),
  254. 'The association class will use this registry key'
  255. );
  256. $this->assertFalse(TableRegistry::exists('TestPlugin.Comments'), 'The association class will NOT use this key');
  257. $this->assertFalse(TableRegistry::exists('Comments'), 'Should also not be set');
  258. $this->assertFalse(TableRegistry::exists('ThisAssociationName'), 'Should also not be set');
  259. $plugin = TableRegistry::get('TestPlugin.ThisAssociationName');
  260. $this->assertSame($table, $plugin, 'Should be an instance of TestPlugin.Comments');
  261. $this->assertSame('TestPlugin.ThisAssociationName', $table->registryAlias());
  262. $this->assertSame('comments', $table->table());
  263. $this->assertSame('ThisAssociationName', $table->alias());
  264. }
  265. /**
  266. * Tests that source() returns the correct Table object
  267. *
  268. * @return void
  269. */
  270. public function testSource()
  271. {
  272. $table = $this->association->source();
  273. $this->assertSame($this->source, $table);
  274. $other = new Table;
  275. $this->association->source($other);
  276. $this->assertSame($other, $this->association->source());
  277. }
  278. /**
  279. * Tests joinType method
  280. *
  281. * @return void
  282. */
  283. public function testJoinType()
  284. {
  285. $this->assertEquals('INNER', $this->association->joinType());
  286. $this->association->joinType('LEFT');
  287. $this->assertEquals('LEFT', $this->association->joinType());
  288. }
  289. /**
  290. * Tests property method
  291. *
  292. * @return void
  293. */
  294. public function testProperty()
  295. {
  296. $this->assertEquals('foo', $this->association->property());
  297. $this->association->property('thing');
  298. $this->assertEquals('thing', $this->association->property());
  299. }
  300. /**
  301. * Test that warning is shown if property name clashes with table field.
  302. *
  303. * @return void
  304. * @expectedException PHPUnit_Framework_Error_Warning
  305. * @expectedExceptionMessageRegExp /^Association property name "foo" clashes with field of same name of table "test"/
  306. */
  307. public function testPropertyNameClash()
  308. {
  309. $this->source->schema(['foo' => ['type' => 'string']]);
  310. $this->assertEquals('foo', $this->association->property());
  311. }
  312. /**
  313. * Test that warning is not shown if "propertyName" option is explicitly specified.
  314. *
  315. * @return void
  316. */
  317. public function testPropertyNameExplicitySet()
  318. {
  319. $this->source->schema(['foo' => ['type' => 'string']]);
  320. $config = [
  321. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  322. 'foreignKey' => 'a_key',
  323. 'conditions' => ['field' => 'value'],
  324. 'dependent' => true,
  325. 'sourceTable' => $this->source,
  326. 'joinType' => 'INNER',
  327. 'propertyName' => 'foo'
  328. ];
  329. $association = $this->getMock(
  330. '\Cake\ORM\Association',
  331. [
  332. '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
  333. 'saveAssociated', 'eagerLoader', 'type'
  334. ],
  335. ['Foo', $config]
  336. );
  337. $this->assertEquals('foo', $association->property());
  338. }
  339. /**
  340. * Tests strategy method
  341. *
  342. * @return void
  343. */
  344. public function testStrategy()
  345. {
  346. $this->assertEquals('join', $this->association->strategy());
  347. $this->association->strategy('select');
  348. $this->assertEquals('select', $this->association->strategy());
  349. $this->association->strategy('subquery');
  350. $this->assertEquals('subquery', $this->association->strategy());
  351. }
  352. /**
  353. * Tests that providing an invalid strategy throws an exception
  354. *
  355. * @expectedException \InvalidArgumentException
  356. * @return void
  357. */
  358. public function testInvalidStrategy()
  359. {
  360. $this->association->strategy('anotherThing');
  361. $this->assertEquals('subquery', $this->association->strategy());
  362. }
  363. /**
  364. * Tests test finder() method as getter and setter
  365. *
  366. * @return void
  367. */
  368. public function testFinderMethod()
  369. {
  370. $this->assertEquals('all', $this->association->finder());
  371. $this->assertEquals('published', $this->association->finder('published'));
  372. $this->assertEquals('published', $this->association->finder());
  373. }
  374. /**
  375. * Tests that `finder` is a valid option for the association constructor
  376. *
  377. * @return void
  378. */
  379. public function testFinderInConstructor()
  380. {
  381. $config = [
  382. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  383. 'foreignKey' => 'a_key',
  384. 'conditions' => ['field' => 'value'],
  385. 'dependent' => true,
  386. 'sourceTable' => $this->source,
  387. 'joinType' => 'INNER',
  388. 'finder' => 'published'
  389. ];
  390. $assoc = $this->getMock(
  391. '\Cake\ORM\Association',
  392. ['type', 'eagerLoader', 'cascadeDelete', 'isOwningSide', 'saveAssociated'],
  393. ['Foo', $config]
  394. );
  395. $this->assertEquals('published', $assoc->finder());
  396. }
  397. /**
  398. * Tests that the defined custom finder is used when calling find
  399. * in the association
  400. *
  401. * @return void
  402. */
  403. public function testCustomFinderIsUsed()
  404. {
  405. $this->association->finder('published');
  406. $this->assertEquals(
  407. ['this' => 'worked'],
  408. $this->association->find()->getOptions()
  409. );
  410. }
  411. /**
  412. * Tests that `locator` is a valid option for the association constructor
  413. *
  414. * @return void
  415. */
  416. public function testLocatorInConstructor()
  417. {
  418. $locator = $this->getMock('Cake\ORM\Locator\LocatorInterface');
  419. $config = [
  420. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  421. 'tableLocator' => $locator
  422. ];
  423. $assoc = $this->getMock(
  424. '\Cake\ORM\Association',
  425. ['type', 'eagerLoader', 'cascadeDelete', 'isOwningSide', 'saveAssociated'],
  426. ['Foo', $config]
  427. );
  428. $this->assertEquals($locator, $assoc->tableLocator());
  429. }
  430. }