AssociationTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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\ORM\Association;
  17. use Cake\ORM\Table;
  18. use Cake\ORM\TableRegistry;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * A Test double used to assert that default tables are created
  22. *
  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. */
  39. class AssociationTest extends TestCase
  40. {
  41. /**
  42. * Set up
  43. *
  44. * @return void
  45. */
  46. public function setUp()
  47. {
  48. parent::setUp();
  49. $this->source = new TestTable;
  50. $config = [
  51. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  52. 'foreignKey' => 'a_key',
  53. 'conditions' => ['field' => 'value'],
  54. 'dependent' => true,
  55. 'sourceTable' => $this->source,
  56. 'joinType' => 'INNER'
  57. ];
  58. $this->association = $this->getMock(
  59. '\Cake\ORM\Association',
  60. [
  61. '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
  62. 'saveAssociated', 'eagerLoader', 'type'
  63. ],
  64. ['Foo', $config]
  65. );
  66. }
  67. /**
  68. * Tear down
  69. *
  70. * @return void
  71. */
  72. public function tearDown()
  73. {
  74. parent::tearDown();
  75. TableRegistry::clear();
  76. }
  77. /**
  78. * Tests that _options acts as a callback where subclasses can add their own
  79. * initialization code based on the passed configuration array
  80. *
  81. * @return void
  82. */
  83. public function testOptionsIsCalled()
  84. {
  85. $options = ['foo' => 'bar'];
  86. $this->association->expects($this->once())->method('_options')->with($options);
  87. $this->association->__construct('Name', $options);
  88. }
  89. /**
  90. * Tests that name() returns the correct configure association name
  91. *
  92. * @return void
  93. */
  94. public function testName()
  95. {
  96. $this->assertEquals('Foo', $this->association->name());
  97. $this->association->name('Bar');
  98. $this->assertEquals('Bar', $this->association->name());
  99. }
  100. /**
  101. * Tests that cascadeCallbacks() returns the correct configured value
  102. *
  103. * @return void
  104. */
  105. public function testCascadeCallbacks()
  106. {
  107. $this->assertSame(false, $this->association->cascadeCallbacks());
  108. $this->association->cascadeCallbacks(true);
  109. $this->assertSame(true, $this->association->cascadeCallbacks());
  110. }
  111. /**
  112. * Tests that name() returns the correct configured value
  113. *
  114. * @return void
  115. */
  116. public function testForeignKey()
  117. {
  118. $this->assertEquals('a_key', $this->association->foreignKey());
  119. $this->association->foreignKey('another_key');
  120. $this->assertEquals('another_key', $this->association->foreignKey());
  121. }
  122. /**
  123. * Tests that conditions() returns the correct configured value
  124. *
  125. * @return void
  126. */
  127. public function testConditions()
  128. {
  129. $this->assertEquals(['field' => 'value'], $this->association->conditions());
  130. $conds = ['another_key' => 'another value'];
  131. $this->association->conditions($conds);
  132. $this->assertEquals($conds, $this->association->conditions());
  133. }
  134. /**
  135. * Tests that canBeJoined() returns the correct configured value
  136. *
  137. * @return void
  138. */
  139. public function testCanBeJoined()
  140. {
  141. $this->assertTrue($this->association->canBeJoined());
  142. }
  143. /**
  144. * Tests that target() returns the correct Table object
  145. *
  146. * @return void
  147. */
  148. public function testTarget()
  149. {
  150. $table = $this->association->target();
  151. $this->assertInstanceOf(__NAMESPACE__ . '\TestTable', $table);
  152. $other = new Table;
  153. $this->association->target($other);
  154. $this->assertSame($other, $this->association->target());
  155. }
  156. /**
  157. * Tests that source() returns the correct Table object
  158. *
  159. * @return void
  160. */
  161. public function testSource()
  162. {
  163. $table = $this->association->source();
  164. $this->assertSame($this->source, $table);
  165. $other = new Table;
  166. $this->association->source($other);
  167. $this->assertSame($other, $this->association->source());
  168. }
  169. /**
  170. * Tests joinType method
  171. *
  172. * @return void
  173. */
  174. public function testJoinType()
  175. {
  176. $this->assertEquals('INNER', $this->association->joinType());
  177. $this->association->joinType('LEFT');
  178. $this->assertEquals('LEFT', $this->association->joinType());
  179. }
  180. /**
  181. * Tests property method
  182. *
  183. * @return void
  184. */
  185. public function testProperty()
  186. {
  187. $this->assertEquals('foo', $this->association->property());
  188. $this->association->property('thing');
  189. $this->assertEquals('thing', $this->association->property());
  190. }
  191. /**
  192. * Tests strategy method
  193. *
  194. * @return void
  195. */
  196. public function testStrategy()
  197. {
  198. $this->assertEquals('join', $this->association->strategy());
  199. $this->association->strategy('select');
  200. $this->assertEquals('select', $this->association->strategy());
  201. $this->association->strategy('subquery');
  202. $this->assertEquals('subquery', $this->association->strategy());
  203. }
  204. /**
  205. * Tests that providing an invalid strategy throws an exception
  206. *
  207. * @expectedException \InvalidArgumentException
  208. * @return void
  209. */
  210. public function testInvalidStrategy()
  211. {
  212. $this->association->strategy('anotherThing');
  213. $this->assertEquals('subquery', $this->association->strategy());
  214. }
  215. /**
  216. * Tests test finder() method as getter and setter
  217. *
  218. * @return void
  219. */
  220. public function testFinderMethod()
  221. {
  222. $this->assertEquals('all', $this->association->finder());
  223. $this->assertEquals('published', $this->association->finder('published'));
  224. $this->assertEquals('published', $this->association->finder());
  225. }
  226. /**
  227. * Tests that `finder` is a valid option for the association constructor
  228. *
  229. * @return void
  230. */
  231. public function testFinderInConstructor()
  232. {
  233. $config = [
  234. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  235. 'foreignKey' => 'a_key',
  236. 'conditions' => ['field' => 'value'],
  237. 'dependent' => true,
  238. 'sourceTable' => $this->source,
  239. 'joinType' => 'INNER',
  240. 'finder' => 'published'
  241. ];
  242. $assoc = $this->getMock(
  243. '\Cake\ORM\Association',
  244. [
  245. '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
  246. 'saveAssociated', 'eagerLoader', 'type'
  247. ],
  248. ['Foo', $config]
  249. );
  250. $this->assertEquals('published', $assoc->finder());
  251. }
  252. /**
  253. * Tests that the defined custom finder is used when calling find
  254. * in the association
  255. *
  256. * @return void
  257. */
  258. public function testCustomFinderIsUsed()
  259. {
  260. $this->association->finder('published');
  261. $this->assertEquals(
  262. ['this' => 'worked'],
  263. $this->association->find()->getOptions()
  264. );
  265. }
  266. }