AssociationTest.php 25 KB

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