AssociationTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. class TestTable extends Table
  25. {
  26. public function initialize(array $config = [])
  27. {
  28. $this->schema(['id' => ['type' => 'integer']]);
  29. }
  30. public function findPublished($query)
  31. {
  32. return $query->applyOptions(['this' => 'worked']);
  33. }
  34. }
  35. /**
  36. * Tests Association class
  37. */
  38. class AssociationTest extends TestCase
  39. {
  40. /**
  41. * @var \Cake\ORM\Association|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. public $association;
  44. /**
  45. * Set up
  46. *
  47. * @return void
  48. */
  49. public function setUp()
  50. {
  51. parent::setUp();
  52. $this->source = new TestTable;
  53. $config = [
  54. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  55. 'foreignKey' => 'a_key',
  56. 'conditions' => ['field' => 'value'],
  57. 'dependent' => true,
  58. 'sourceTable' => $this->source,
  59. 'joinType' => 'INNER'
  60. ];
  61. $this->association = $this->getMockBuilder('\Cake\ORM\Association')
  62. ->setMethods([
  63. '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
  64. 'saveAssociated', 'eagerLoader', 'type', 'requiresKeys'
  65. ])
  66. ->setConstructorArgs(['Foo', $config])
  67. ->getMock();
  68. }
  69. /**
  70. * Tear down
  71. *
  72. * @return void
  73. */
  74. public function tearDown()
  75. {
  76. parent::tearDown();
  77. TableRegistry::clear();
  78. }
  79. /**
  80. * Tests that _options acts as a callback where subclasses can add their own
  81. * initialization code based on the passed configuration array
  82. *
  83. * @return void
  84. */
  85. public function testOptionsIsCalled()
  86. {
  87. $options = ['foo' => 'bar'];
  88. $this->association->expects($this->once())->method('_options')->with($options);
  89. $this->association->__construct('Name', $options);
  90. }
  91. /**
  92. * Tests that name() returns the correct configure association name
  93. *
  94. * @return void
  95. */
  96. public function testName()
  97. {
  98. $this->assertEquals('Foo', $this->association->name());
  99. $this->association->name('Bar');
  100. $this->assertEquals('Bar', $this->association->name());
  101. }
  102. /**
  103. * Tests that className() returns the correct association className
  104. *
  105. * @return void
  106. */
  107. public function testClassName()
  108. {
  109. $this->assertEquals('\Cake\Test\TestCase\ORM\TestTable', $this->association->className());
  110. }
  111. /**
  112. * Tests that className() returns the correct (unnormalized) className
  113. *
  114. * @return void
  115. */
  116. public function testClassNameUnnormalized()
  117. {
  118. $config = [
  119. 'className' => 'Test',
  120. ];
  121. $this->association = $this->getMockBuilder('\Cake\ORM\Association')
  122. ->setMethods([
  123. '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
  124. 'saveAssociated', 'eagerLoader', 'type', 'requiresKeys'
  125. ])
  126. ->setConstructorArgs(['Foo', $config])
  127. ->getMock();
  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->getMockBuilder('\Cake\ORM\Association')
  246. ->setMethods([
  247. 'type', 'eagerLoader', 'cascadeDelete', 'isOwningSide', 'saveAssociated',
  248. 'requiresKeys'
  249. ])
  250. ->setConstructorArgs(['ThisAssociationName', $config])
  251. ->getMock();
  252. $table = $this->association->target();
  253. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $table);
  254. $this->assertTrue(
  255. TableRegistry::exists('TestPlugin.ThisAssociationName'),
  256. 'The association class will use this registry key'
  257. );
  258. $this->assertFalse(TableRegistry::exists('TestPlugin.Comments'), 'The association class will NOT use this key');
  259. $this->assertFalse(TableRegistry::exists('Comments'), 'Should also not be set');
  260. $this->assertFalse(TableRegistry::exists('ThisAssociationName'), 'Should also not be set');
  261. $plugin = TableRegistry::get('TestPlugin.ThisAssociationName');
  262. $this->assertSame($table, $plugin, 'Should be an instance of TestPlugin.Comments');
  263. $this->assertSame('TestPlugin.ThisAssociationName', $table->registryAlias());
  264. $this->assertSame('comments', $table->table());
  265. $this->assertSame('ThisAssociationName', $table->alias());
  266. }
  267. /**
  268. * Tests that source() returns the correct Table object
  269. *
  270. * @return void
  271. */
  272. public function testSource()
  273. {
  274. $table = $this->association->source();
  275. $this->assertSame($this->source, $table);
  276. $other = new Table;
  277. $this->association->source($other);
  278. $this->assertSame($other, $this->association->source());
  279. }
  280. /**
  281. * Tests joinType method
  282. *
  283. * @return void
  284. */
  285. public function testJoinType()
  286. {
  287. $this->assertEquals('INNER', $this->association->joinType());
  288. $this->association->joinType('LEFT');
  289. $this->assertEquals('LEFT', $this->association->joinType());
  290. }
  291. /**
  292. * Tests property method
  293. *
  294. * @return void
  295. */
  296. public function testProperty()
  297. {
  298. $this->assertEquals('foo', $this->association->property());
  299. $this->association->property('thing');
  300. $this->assertEquals('thing', $this->association->property());
  301. }
  302. /**
  303. * Test that warning is shown if property name clashes with table field.
  304. *
  305. * @return void
  306. * @expectedException \PHPUnit_Framework_Error_Warning
  307. * @expectedExceptionMessageRegExp /^Association property name "foo" clashes with field of same name of table "test"/
  308. */
  309. public function testPropertyNameClash()
  310. {
  311. $this->source->schema(['foo' => ['type' => 'string']]);
  312. $this->assertEquals('foo', $this->association->property());
  313. }
  314. /**
  315. * Test that warning is not shown if "propertyName" option is explicitly specified.
  316. *
  317. * @return void
  318. */
  319. public function testPropertyNameExplicitySet()
  320. {
  321. $this->source->schema(['foo' => ['type' => 'string']]);
  322. $config = [
  323. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  324. 'foreignKey' => 'a_key',
  325. 'conditions' => ['field' => 'value'],
  326. 'dependent' => true,
  327. 'sourceTable' => $this->source,
  328. 'joinType' => 'INNER',
  329. 'propertyName' => 'foo'
  330. ];
  331. $association = $this->getMockBuilder('\Cake\ORM\Association')
  332. ->setMethods([
  333. '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
  334. 'saveAssociated', 'eagerLoader', 'type', 'requiresKeys'
  335. ])
  336. ->setConstructorArgs(['Foo', $config])
  337. ->getMock();
  338. $this->assertEquals('foo', $association->property());
  339. }
  340. /**
  341. * Tests strategy method
  342. *
  343. * @return void
  344. */
  345. public function testStrategy()
  346. {
  347. $this->assertEquals('join', $this->association->strategy());
  348. $this->association->strategy('select');
  349. $this->assertEquals('select', $this->association->strategy());
  350. $this->association->strategy('subquery');
  351. $this->assertEquals('subquery', $this->association->strategy());
  352. }
  353. /**
  354. * Tests that providing an invalid strategy throws an exception
  355. *
  356. * @expectedException \InvalidArgumentException
  357. * @return void
  358. */
  359. public function testInvalidStrategy()
  360. {
  361. $this->association->strategy('anotherThing');
  362. $this->assertEquals('subquery', $this->association->strategy());
  363. }
  364. /**
  365. * Tests test finder() method as getter and setter
  366. *
  367. * @return void
  368. */
  369. public function testFinderMethod()
  370. {
  371. $this->assertEquals('all', $this->association->finder());
  372. $this->assertEquals('published', $this->association->finder('published'));
  373. $this->assertEquals('published', $this->association->finder());
  374. }
  375. /**
  376. * Tests that `finder` is a valid option for the association constructor
  377. *
  378. * @return void
  379. */
  380. public function testFinderInConstructor()
  381. {
  382. $config = [
  383. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  384. 'foreignKey' => 'a_key',
  385. 'conditions' => ['field' => 'value'],
  386. 'dependent' => true,
  387. 'sourceTable' => $this->source,
  388. 'joinType' => 'INNER',
  389. 'finder' => 'published'
  390. ];
  391. $assoc = $this->getMockBuilder('\Cake\ORM\Association')
  392. ->setMethods([
  393. 'type', 'eagerLoader', 'cascadeDelete', 'isOwningSide', 'saveAssociated',
  394. 'requiresKeys'
  395. ])
  396. ->setConstructorArgs(['Foo', $config])
  397. ->getMock();
  398. $this->assertEquals('published', $assoc->finder());
  399. }
  400. /**
  401. * Tests that the defined custom finder is used when calling find
  402. * in the association
  403. *
  404. * @return void
  405. */
  406. public function testCustomFinderIsUsed()
  407. {
  408. $this->association->finder('published');
  409. $this->assertEquals(
  410. ['this' => 'worked'],
  411. $this->association->find()->getOptions()
  412. );
  413. }
  414. /**
  415. * Tests that `locator` is a valid option for the association constructor
  416. *
  417. * @return void
  418. */
  419. public function testLocatorInConstructor()
  420. {
  421. $locator = $this->getMockBuilder('Cake\ORM\Locator\LocatorInterface')->getMock();
  422. $config = [
  423. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  424. 'tableLocator' => $locator
  425. ];
  426. $assoc = $this->getMockBuilder('\Cake\ORM\Association')
  427. ->setMethods([
  428. 'type', 'eagerLoader', 'cascadeDelete', 'isOwningSide', 'saveAssociated',
  429. 'requiresKeys'
  430. ])
  431. ->setConstructorArgs(['Foo', $config])
  432. ->getMock();
  433. $this->assertEquals($locator, $assoc->tableLocator());
  434. }
  435. }