AssociationTest.php 6.6 KB

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