AssociationTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\ORM\Table;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * A Test double used to assert that default tables are created
  22. */
  23. class TestTable extends Table
  24. {
  25. public function initialize(array $config = [])
  26. {
  27. $this->setSchema(['id' => ['type' => 'integer']]);
  28. }
  29. public function findPublished($query)
  30. {
  31. return $query->applyOptions(['this' => 'worked']);
  32. }
  33. }
  34. /**
  35. * Tests Association class
  36. */
  37. class AssociationTest extends TestCase
  38. {
  39. /**
  40. * @var \Cake\ORM\Association|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. public $association;
  43. /**
  44. * Set up
  45. *
  46. * @return void
  47. */
  48. public function setUp()
  49. {
  50. parent::setUp();
  51. $this->source = new TestTable();
  52. $config = [
  53. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  54. 'foreignKey' => 'a_key',
  55. 'conditions' => ['field' => 'value'],
  56. 'dependent' => true,
  57. 'sourceTable' => $this->source,
  58. 'joinType' => 'INNER',
  59. ];
  60. $this->association = $this->getMockBuilder('\Cake\ORM\Association')
  61. ->setMethods([
  62. '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
  63. 'saveAssociated', 'eagerLoader', 'type', 'requiresKeys',
  64. ])
  65. ->setConstructorArgs(['Foo', $config])
  66. ->getMock();
  67. }
  68. /**
  69. * Tests that _options acts as a callback where subclasses can add their own
  70. * initialization code based on the passed configuration array
  71. *
  72. * @return void
  73. */
  74. public function testOptionsIsCalled()
  75. {
  76. $options = ['foo' => 'bar'];
  77. $this->association->expects($this->once())->method('_options')->with($options);
  78. $this->association->__construct('Name', $options);
  79. }
  80. /**
  81. * Tests that name() returns the correct configure association name
  82. *
  83. * @group deprecated
  84. * @return void
  85. */
  86. public function testName()
  87. {
  88. $this->deprecated(function () {
  89. $this->assertEquals('Foo', $this->association->name());
  90. $this->association->name('Bar');
  91. $this->assertEquals('Bar', $this->association->name());
  92. });
  93. }
  94. /**
  95. * Tests that setName()
  96. *
  97. * @return void
  98. */
  99. public function testSetName()
  100. {
  101. $this->assertEquals('Foo', $this->association->getName());
  102. $this->assertSame($this->association, $this->association->setName('Bar'));
  103. $this->assertEquals('Bar', $this->association->getName());
  104. }
  105. /**
  106. * Tests that setName() succeeds before the target table is resolved.
  107. *
  108. * @return void
  109. */
  110. public function testSetNameBeforeTarget()
  111. {
  112. $this->association->setName('Bar');
  113. $this->assertEquals('Bar', $this->association->getName());
  114. }
  115. /**
  116. * Tests that setName() fails after the target table is resolved.
  117. *
  118. * @return void
  119. */
  120. public function testSetNameAfterTarget()
  121. {
  122. $this->expectException(\InvalidArgumentException::class);
  123. $this->expectExceptionMessage('Association name does not match target table alias.');
  124. $this->association->getTarget();
  125. $this->association->setName('Bar');
  126. }
  127. /**
  128. * Tests that setName() succeeds if name equals target table alias.
  129. *
  130. * @return void
  131. */
  132. public function testSetNameToTargetAlias()
  133. {
  134. $alias = $this->association->getTarget()->getAlias();
  135. $this->association->setName($alias);
  136. $this->assertEquals($alias, $this->association->getName());
  137. }
  138. /**
  139. * Tests that className() returns the correct association className
  140. *
  141. * @group deprecated
  142. * @return void
  143. */
  144. public function testClassName()
  145. {
  146. $this->deprecated(function () {
  147. $this->assertEquals('\Cake\Test\TestCase\ORM\TestTable', $this->association->className());
  148. });
  149. }
  150. /**
  151. * Tests that setClassName() succeeds before the target table is resolved.
  152. *
  153. * @return void
  154. */
  155. public function testSetClassNameBeforeTarget()
  156. {
  157. $this->assertEquals('\Cake\Test\TestCase\ORM\TestTable', $this->association->getClassName());
  158. $this->assertSame($this->association, $this->association->setClassName('\TestApp\Model\Table\AuthorsTable'));
  159. $this->assertEquals('\TestApp\Model\Table\AuthorsTable', $this->association->getClassName());
  160. }
  161. /**
  162. * Tests that setClassName() fails after the target table is resolved.
  163. *
  164. * @return void
  165. */
  166. public function testSetClassNameAfterTarget()
  167. {
  168. $this->expectException(\InvalidArgumentException::class);
  169. $this->expectExceptionMessage('The class name doesn\'t match the target table\'s class name.');
  170. $this->association->getTarget();
  171. $this->association->setClassName('\TestApp\Model\Table\AuthorsTable');
  172. }
  173. /**
  174. * Tests that setClassName() fails after the target table is resolved.
  175. *
  176. * @return void
  177. */
  178. public function testSetClassNameWithShortSyntaxAfterTarget()
  179. {
  180. $this->expectException(\InvalidArgumentException::class);
  181. $this->expectExceptionMessage('The class name doesn\'t match the target table\'s class name.');
  182. $this->association->getTarget();
  183. $this->association->setClassName('Authors');
  184. }
  185. /**
  186. * Tests that setClassName() succeeds if name equals target table's class name.
  187. *
  188. * @return void
  189. */
  190. public function testSetClassNameToTargetClassName()
  191. {
  192. $className = get_class($this->association->getTarget());
  193. $this->association->setClassName($className);
  194. $this->assertEquals($className, $this->association->getClassName());
  195. }
  196. /**
  197. * Tests that setClassName() succeeds if the short name resolves to the target table's class name.
  198. *
  199. * @return void
  200. */
  201. public function testSetClassNameWithShortSyntaxToTargetClassName()
  202. {
  203. Configure::write('App.namespace', 'TestApp');
  204. $this->association->setClassName('\TestApp\Model\Table\AuthorsTable');
  205. $className = get_class($this->association->getTarget());
  206. $this->assertEquals('TestApp\Model\Table\AuthorsTable', $className);
  207. $this->association->setClassName('Authors');
  208. $this->assertEquals('Authors', $this->association->getClassName());
  209. }
  210. /**
  211. * Tests that className() returns the correct (unnormalized) className
  212. *
  213. * @return void
  214. */
  215. public function testClassNameUnnormalized()
  216. {
  217. $config = [
  218. 'className' => 'Test',
  219. ];
  220. $this->association = $this->getMockBuilder('\Cake\ORM\Association')
  221. ->setMethods([
  222. '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
  223. 'saveAssociated', 'eagerLoader', 'type', 'requiresKeys',
  224. ])
  225. ->setConstructorArgs(['Foo', $config])
  226. ->getMock();
  227. $this->assertEquals('Test', $this->association->getClassName());
  228. }
  229. /**
  230. * Tests that an exception is thrown when invalid target table is fetched
  231. * from a registry.
  232. *
  233. * @return void
  234. */
  235. public function testInvalidTableFetchedFromRegistry()
  236. {
  237. $this->expectException(\RuntimeException::class);
  238. $this->getTableLocator()->get('Test');
  239. $config = [
  240. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  241. ];
  242. $this->association = $this->getMockBuilder('\Cake\ORM\Association')
  243. ->setMethods([
  244. '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
  245. 'saveAssociated', 'eagerLoader', 'type', 'requiresKeys',
  246. ])
  247. ->setConstructorArgs(['Test', $config])
  248. ->getMock();
  249. $this->association->getTarget();
  250. }
  251. /**
  252. * Tests that a descendant table could be fetched from a registry.
  253. *
  254. * @return void
  255. */
  256. public function testTargetTableDescendant()
  257. {
  258. $this->getTableLocator()->get('Test', [
  259. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  260. ]);
  261. $className = '\Cake\ORM\Table';
  262. $config = [
  263. 'className' => $className,
  264. ];
  265. $this->association = $this->getMockBuilder('\Cake\ORM\Association')
  266. ->setMethods([
  267. '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
  268. 'saveAssociated', 'eagerLoader', 'type', 'requiresKeys',
  269. ])
  270. ->setConstructorArgs(['Test', $config])
  271. ->getMock();
  272. $target = $this->association->getTarget();
  273. $this->assertInstanceOf($className, $target);
  274. }
  275. /**
  276. * Tests that cascadeCallbacks() returns the correct configured value
  277. *
  278. * @group deprecated
  279. * @return void
  280. */
  281. public function testCascadeCallbacks()
  282. {
  283. $this->deprecated(function () {
  284. $this->assertFalse($this->association->cascadeCallbacks());
  285. $this->association->cascadeCallbacks(true);
  286. $this->assertTrue($this->association->cascadeCallbacks());
  287. });
  288. }
  289. /**
  290. * Tests that cascadeCallbacks() returns the correct configured value
  291. *
  292. * @return void
  293. */
  294. public function testSetCascadeCallbacks()
  295. {
  296. $this->assertFalse($this->association->getCascadeCallbacks());
  297. $this->assertSame($this->association, $this->association->setCascadeCallbacks(true));
  298. $this->assertTrue($this->association->getCascadeCallbacks());
  299. }
  300. /**
  301. * Tests the bindingKey method as a setter/getter
  302. *
  303. * @group deprecated
  304. * @return void
  305. */
  306. public function testBindingKey()
  307. {
  308. $this->deprecated(function () {
  309. $this->association->bindingKey('foo_id');
  310. $this->assertEquals('foo_id', $this->association->bindingKey());
  311. });
  312. }
  313. /**
  314. * Tests the bindingKey method as a setter/getter
  315. *
  316. * @return void
  317. */
  318. public function testSetBindingKey()
  319. {
  320. $this->assertSame($this->association, $this->association->setBindingKey('foo_id'));
  321. $this->assertEquals('foo_id', $this->association->getBindingKey());
  322. }
  323. /**
  324. * Tests the bindingKey() method when called with its defaults
  325. *
  326. * @return void
  327. */
  328. public function testBindingKeyDefault()
  329. {
  330. $this->source->setPrimaryKey(['id', 'site_id']);
  331. $this->association
  332. ->expects($this->once())
  333. ->method('isOwningSide')
  334. ->will($this->returnValue(true));
  335. $result = $this->association->getBindingKey();
  336. $this->assertEquals(['id', 'site_id'], $result);
  337. }
  338. /**
  339. * Tests the bindingKey() method when the association source is not the
  340. * owning side
  341. *
  342. * @return void
  343. */
  344. public function testBindingDefaultNoOwningSide()
  345. {
  346. $target = new Table();
  347. $target->setPrimaryKey(['foo', 'site_id']);
  348. $this->association->setTarget($target);
  349. $this->association
  350. ->expects($this->once())
  351. ->method('isOwningSide')
  352. ->will($this->returnValue(false));
  353. $result = $this->association->getBindingKey();
  354. $this->assertEquals(['foo', 'site_id'], $result);
  355. }
  356. /**
  357. * Tests that name() returns the correct configured value
  358. *
  359. * @group deprecated
  360. * @return void
  361. */
  362. public function testForeignKey()
  363. {
  364. $this->deprecated(function () {
  365. $this->assertEquals('a_key', $this->association->foreignKey());
  366. $this->association->foreignKey('another_key');
  367. $this->assertEquals('another_key', $this->association->foreignKey());
  368. });
  369. }
  370. /**
  371. * Tests setForeignKey()
  372. *
  373. * @return void
  374. */
  375. public function testSetForeignKey()
  376. {
  377. $this->assertEquals('a_key', $this->association->getForeignKey());
  378. $this->assertSame($this->association, $this->association->setForeignKey('another_key'));
  379. $this->assertEquals('another_key', $this->association->getForeignKey());
  380. }
  381. /**
  382. * Tests that conditions() returns the correct configured value
  383. *
  384. * @group deprecated
  385. * @return void
  386. */
  387. public function testConditions()
  388. {
  389. $this->deprecated(function () {
  390. $this->assertEquals(['field' => 'value'], $this->association->conditions());
  391. $conds = ['another_key' => 'another value'];
  392. $this->association->conditions($conds);
  393. $this->assertEquals($conds, $this->association->conditions());
  394. });
  395. }
  396. /**
  397. * Tests setConditions()
  398. *
  399. * @return void
  400. */
  401. public function testSetConditions()
  402. {
  403. $this->assertEquals(['field' => 'value'], $this->association->getConditions());
  404. $conds = ['another_key' => 'another value'];
  405. $this->assertSame($this->association, $this->association->setConditions($conds));
  406. $this->assertEquals($conds, $this->association->getConditions());
  407. }
  408. /**
  409. * Tests that canBeJoined() returns the correct configured value
  410. *
  411. * @return void
  412. */
  413. public function testCanBeJoined()
  414. {
  415. $this->assertTrue($this->association->canBeJoined());
  416. }
  417. /**
  418. * Tests that target() returns the correct Table object
  419. *
  420. * @group deprecated
  421. * @return void
  422. */
  423. public function testTarget()
  424. {
  425. $this->deprecated(function () {
  426. $table = $this->association->target();
  427. $this->assertInstanceOf(__NAMESPACE__ . '\TestTable', $table);
  428. $other = new Table();
  429. $this->association->target($other);
  430. $this->assertSame($other, $this->association->target());
  431. });
  432. }
  433. /**
  434. * Tests that setTarget()
  435. *
  436. * @return void
  437. */
  438. public function testSetTarget()
  439. {
  440. $table = $this->association->getTarget();
  441. $this->assertInstanceOf(__NAMESPACE__ . '\TestTable', $table);
  442. $other = new Table();
  443. $this->assertSame($this->association, $this->association->setTarget($other));
  444. $this->assertSame($other, $this->association->getTarget());
  445. }
  446. /**
  447. * Tests that target() returns the correct Table object for plugins
  448. *
  449. * @return void
  450. */
  451. public function testTargetPlugin()
  452. {
  453. $this->loadPlugins(['TestPlugin']);
  454. $config = [
  455. 'className' => 'TestPlugin.Comments',
  456. 'foreignKey' => 'a_key',
  457. 'conditions' => ['field' => 'value'],
  458. 'dependent' => true,
  459. 'sourceTable' => $this->source,
  460. 'joinType' => 'INNER',
  461. ];
  462. $this->association = $this->getMockBuilder('\Cake\ORM\Association')
  463. ->setMethods([
  464. 'type', 'eagerLoader', 'cascadeDelete', 'isOwningSide', 'saveAssociated',
  465. 'requiresKeys',
  466. ])
  467. ->setConstructorArgs(['ThisAssociationName', $config])
  468. ->getMock();
  469. $table = $this->association->getTarget();
  470. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $table);
  471. $this->assertTrue(
  472. $this->getTableLocator()->exists('TestPlugin.ThisAssociationName'),
  473. 'The association class will use this registry key'
  474. );
  475. $this->assertFalse($this->getTableLocator()->exists('TestPlugin.Comments'), 'The association class will NOT use this key');
  476. $this->assertFalse($this->getTableLocator()->exists('Comments'), 'Should also not be set');
  477. $this->assertFalse($this->getTableLocator()->exists('ThisAssociationName'), 'Should also not be set');
  478. $plugin = $this->getTableLocator()->get('TestPlugin.ThisAssociationName');
  479. $this->assertSame($table, $plugin, 'Should be an instance of TestPlugin.Comments');
  480. $this->assertSame('TestPlugin.ThisAssociationName', $table->getRegistryAlias());
  481. $this->assertSame('comments', $table->getTable());
  482. $this->assertSame('ThisAssociationName', $table->getAlias());
  483. $this->clearPlugins();
  484. }
  485. /**
  486. * Tests that source() returns the correct Table object
  487. *
  488. * @group deprecated
  489. * @return void
  490. */
  491. public function testSource()
  492. {
  493. $this->deprecated(function () {
  494. $table = $this->association->source();
  495. $this->assertSame($this->source, $table);
  496. $other = new Table();
  497. $this->association->source($other);
  498. $this->assertSame($other, $this->association->source());
  499. });
  500. }
  501. /**
  502. * Tests that source() returns the correct Table object
  503. *
  504. * @return void
  505. */
  506. public function testSetSource()
  507. {
  508. $table = $this->association->getSource();
  509. $this->assertSame($this->source, $table);
  510. $other = new Table();
  511. $this->assertSame($this->association, $this->association->setSource($other));
  512. $this->assertSame($other, $this->association->getSource());
  513. }
  514. /**
  515. * Tests joinType method
  516. *
  517. * @group deprecated
  518. * @return void
  519. */
  520. public function testJoinType()
  521. {
  522. $this->deprecated(function () {
  523. $this->assertEquals('INNER', $this->association->joinType());
  524. $this->association->joinType('LEFT');
  525. $this->assertEquals('LEFT', $this->association->joinType());
  526. });
  527. }
  528. /**
  529. * Tests setJoinType method
  530. *
  531. * @return void
  532. */
  533. public function testSetJoinType()
  534. {
  535. $this->assertEquals('INNER', $this->association->getJoinType());
  536. $this->assertSame($this->association, $this->association->setJoinType('LEFT'));
  537. $this->assertEquals('LEFT', $this->association->getJoinType());
  538. }
  539. /**
  540. * Tests dependent method
  541. *
  542. * @group deprecated
  543. * @return void
  544. */
  545. public function testDependent()
  546. {
  547. $this->deprecated(function () {
  548. $this->assertTrue($this->association->dependent());
  549. $this->association->dependent(false);
  550. $this->assertFalse($this->association->dependent());
  551. });
  552. }
  553. /**
  554. * Tests property method
  555. *
  556. * @group deprecated
  557. * @return void
  558. */
  559. public function testProperty()
  560. {
  561. $this->deprecated(function () {
  562. $this->assertEquals('foo', $this->association->property());
  563. $this->association->property('thing');
  564. $this->assertEquals('thing', $this->association->property());
  565. });
  566. }
  567. /**
  568. * Tests property method
  569. *
  570. * @return void
  571. */
  572. public function testSetProperty()
  573. {
  574. $this->assertEquals('foo', $this->association->getProperty());
  575. $this->assertSame($this->association, $this->association->setProperty('thing'));
  576. $this->assertEquals('thing', $this->association->getProperty());
  577. }
  578. /**
  579. * Test that warning is shown if property name clashes with table field.
  580. *
  581. * @return void
  582. */
  583. public function testPropertyNameClash()
  584. {
  585. $this->expectException(\PHPUnit\Framework\Error\Warning::class);
  586. $this->expectExceptionMessageRegExp('/^Association property name "foo" clashes with field of same name of table "test"/');
  587. $this->source->setSchema(['foo' => ['type' => 'string']]);
  588. $this->assertEquals('foo', $this->association->getProperty());
  589. }
  590. /**
  591. * Test that warning is not shown if "propertyName" option is explicitly specified.
  592. *
  593. * @return void
  594. */
  595. public function testPropertyNameExplicitySet()
  596. {
  597. $this->source->setSchema(['foo' => ['type' => 'string']]);
  598. $config = [
  599. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  600. 'foreignKey' => 'a_key',
  601. 'conditions' => ['field' => 'value'],
  602. 'dependent' => true,
  603. 'sourceTable' => $this->source,
  604. 'joinType' => 'INNER',
  605. 'propertyName' => 'foo',
  606. ];
  607. $association = $this->getMockBuilder('\Cake\ORM\Association')
  608. ->setMethods([
  609. '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
  610. 'saveAssociated', 'eagerLoader', 'type', 'requiresKeys',
  611. ])
  612. ->setConstructorArgs(['Foo', $config])
  613. ->getMock();
  614. $this->assertEquals('foo', $association->getProperty());
  615. }
  616. /**
  617. * Tests strategy method
  618. *
  619. * @group deprecated
  620. * @return void
  621. */
  622. public function testStrategy()
  623. {
  624. $this->deprecated(function () {
  625. $this->assertEquals('join', $this->association->strategy());
  626. $this->association->strategy('select');
  627. $this->assertEquals('select', $this->association->strategy());
  628. $this->association->strategy('subquery');
  629. $this->assertEquals('subquery', $this->association->strategy());
  630. });
  631. }
  632. /**
  633. * Tests strategy method
  634. *
  635. * @return void
  636. */
  637. public function testSetStrategy()
  638. {
  639. $this->assertEquals('join', $this->association->getStrategy());
  640. $this->association->setStrategy('select');
  641. $this->assertEquals('select', $this->association->getStrategy());
  642. $this->association->setStrategy('subquery');
  643. $this->assertEquals('subquery', $this->association->getStrategy());
  644. }
  645. /**
  646. * Tests that providing an invalid strategy throws an exception
  647. *
  648. * @return void
  649. */
  650. public function testInvalidStrategy()
  651. {
  652. $this->expectException(\InvalidArgumentException::class);
  653. $this->association->setStrategy('anotherThing');
  654. }
  655. /**
  656. * Tests test finder() method as getter and setter
  657. *
  658. * @group deprecated
  659. * @return void
  660. */
  661. public function testFinderMethod()
  662. {
  663. $this->deprecated(function () {
  664. $this->assertEquals('all', $this->association->finder());
  665. $this->assertEquals('published', $this->association->finder('published'));
  666. $this->assertEquals('published', $this->association->finder());
  667. });
  668. }
  669. /**
  670. * Tests test setFinder() method
  671. *
  672. * @return void
  673. */
  674. public function testSetFinderMethod()
  675. {
  676. $this->assertEquals('all', $this->association->getFinder());
  677. $this->assertSame($this->association, $this->association->setFinder('published'));
  678. $this->assertEquals('published', $this->association->getFinder());
  679. }
  680. /**
  681. * Tests that `finder` is a valid option for the association constructor
  682. *
  683. * @return void
  684. */
  685. public function testFinderInConstructor()
  686. {
  687. $config = [
  688. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  689. 'foreignKey' => 'a_key',
  690. 'conditions' => ['field' => 'value'],
  691. 'dependent' => true,
  692. 'sourceTable' => $this->source,
  693. 'joinType' => 'INNER',
  694. 'finder' => 'published',
  695. ];
  696. $assoc = $this->getMockBuilder('\Cake\ORM\Association')
  697. ->setMethods([
  698. 'type', 'eagerLoader', 'cascadeDelete', 'isOwningSide', 'saveAssociated',
  699. 'requiresKeys',
  700. ])
  701. ->setConstructorArgs(['Foo', $config])
  702. ->getMock();
  703. $this->assertEquals('published', $assoc->getFinder());
  704. }
  705. /**
  706. * Tests that the defined custom finder is used when calling find
  707. * in the association
  708. *
  709. * @return void
  710. */
  711. public function testCustomFinderIsUsed()
  712. {
  713. $this->association->setFinder('published');
  714. $this->assertEquals(
  715. ['this' => 'worked'],
  716. $this->association->find()->getOptions()
  717. );
  718. }
  719. /**
  720. * Tests that `locator` is a valid option for the association constructor
  721. *
  722. * @return void
  723. */
  724. public function testLocatorInConstructor()
  725. {
  726. $locator = $this->getMockBuilder('Cake\ORM\Locator\LocatorInterface')->getMock();
  727. $config = [
  728. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  729. 'tableLocator' => $locator,
  730. ];
  731. $assoc = $this->getMockBuilder('\Cake\ORM\Association')
  732. ->setMethods([
  733. 'type', 'eagerLoader', 'cascadeDelete', 'isOwningSide', 'saveAssociated',
  734. 'requiresKeys',
  735. ])
  736. ->setConstructorArgs(['Foo', $config])
  737. ->getMock();
  738. $this->assertEquals($locator, $assoc->getTableLocator());
  739. }
  740. }