AssociationTest.php 6.7 KB

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