PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/cakephp/cakephp/tests/TestCase/ORM/AssociationTest.php

https://gitlab.com/vannh/portal_training
PHP | 329 lines | 186 code | 29 blank | 114 comment | 0 complexity | deba44c82e6920851f5541228f3b5714 MD5 | raw file
  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\Core\Plugin;
  17. use Cake\ORM\Association;
  18. use Cake\ORM\Table;
  19. use Cake\ORM\TableRegistry;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * A Test double used to assert that default tables are created
  23. *
  24. */
  25. class TestTable extends Table
  26. {
  27. public function initialize(array $config = [])
  28. {
  29. $this->schema(['id' => ['type' => 'integer']]);
  30. }
  31. public function findPublished($query)
  32. {
  33. return $query->applyOptions(['this' => 'worked']);
  34. }
  35. }
  36. /**
  37. * Tests Association class
  38. *
  39. */
  40. class AssociationTest extends TestCase
  41. {
  42. /**
  43. * Set up
  44. *
  45. * @return void
  46. */
  47. public function setUp()
  48. {
  49. parent::setUp();
  50. $this->source = new TestTable;
  51. $config = [
  52. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  53. 'foreignKey' => 'a_key',
  54. 'conditions' => ['field' => 'value'],
  55. 'dependent' => true,
  56. 'sourceTable' => $this->source,
  57. 'joinType' => 'INNER'
  58. ];
  59. $this->association = $this->getMock(
  60. '\Cake\ORM\Association',
  61. [
  62. '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
  63. 'saveAssociated', 'eagerLoader', 'type'
  64. ],
  65. ['Foo', $config]
  66. );
  67. }
  68. /**
  69. * Tear down
  70. *
  71. * @return void
  72. */
  73. public function tearDown()
  74. {
  75. parent::tearDown();
  76. TableRegistry::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. * @return void
  94. */
  95. public function testName()
  96. {
  97. $this->assertEquals('Foo', $this->association->name());
  98. $this->association->name('Bar');
  99. $this->assertEquals('Bar', $this->association->name());
  100. }
  101. /**
  102. * Tests that cascadeCallbacks() returns the correct configured value
  103. *
  104. * @return void
  105. */
  106. public function testCascadeCallbacks()
  107. {
  108. $this->assertSame(false, $this->association->cascadeCallbacks());
  109. $this->association->cascadeCallbacks(true);
  110. $this->assertSame(true, $this->association->cascadeCallbacks());
  111. }
  112. /**
  113. * Tests that name() returns the correct configured value
  114. *
  115. * @return void
  116. */
  117. public function testForeignKey()
  118. {
  119. $this->assertEquals('a_key', $this->association->foreignKey());
  120. $this->association->foreignKey('another_key');
  121. $this->assertEquals('another_key', $this->association->foreignKey());
  122. }
  123. /**
  124. * Tests that conditions() returns the correct configured value
  125. *
  126. * @return void
  127. */
  128. public function testConditions()
  129. {
  130. $this->assertEquals(['field' => 'value'], $this->association->conditions());
  131. $conds = ['another_key' => 'another value'];
  132. $this->association->conditions($conds);
  133. $this->assertEquals($conds, $this->association->conditions());
  134. }
  135. /**
  136. * Tests that canBeJoined() returns the correct configured value
  137. *
  138. * @return void
  139. */
  140. public function testCanBeJoined()
  141. {
  142. $this->assertTrue($this->association->canBeJoined());
  143. }
  144. /**
  145. * Tests that target() returns the correct Table object
  146. *
  147. * @return void
  148. */
  149. public function testTarget()
  150. {
  151. $table = $this->association->target();
  152. $this->assertInstanceOf(__NAMESPACE__ . '\TestTable', $table);
  153. $other = new Table;
  154. $this->association->target($other);
  155. $this->assertSame($other, $this->association->target());
  156. }
  157. /**
  158. * Tests that target() returns the correct Table object for plugins
  159. *
  160. * @return void
  161. */
  162. public function testTargetPlugin()
  163. {
  164. Plugin::load('TestPlugin');
  165. $config = [
  166. 'className' => 'TestPlugin.Comments',
  167. 'foreignKey' => 'a_key',
  168. 'conditions' => ['field' => 'value'],
  169. 'dependent' => true,
  170. 'sourceTable' => $this->source,
  171. 'joinType' => 'INNER'
  172. ];
  173. $this->association = $this->getMock(
  174. '\Cake\ORM\Association',
  175. ['type', 'eagerLoader', 'cascadeDelete', 'isOwningSide', 'saveAssociated'],
  176. ['ThisAssociationName', $config]
  177. );
  178. $table = $this->association->target();
  179. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $table);
  180. $this->assertTrue(
  181. TableRegistry::exists('TestPlugin.ThisAssociationName'),
  182. 'The association class will use this registry key'
  183. );
  184. $this->assertFalse(TableRegistry::exists('TestPlugin.Comments'), 'The association class will NOT use this key');
  185. $this->assertFalse(TableRegistry::exists('Comments'), 'Should also not be set');
  186. $this->assertFalse(TableRegistry::exists('ThisAssociationName'), 'Should also not be set');
  187. $plugin = TableRegistry::get('TestPlugin.ThisAssociationName');
  188. $this->assertSame($table, $plugin, 'Should be an instance of TestPlugin.Comments');
  189. $this->assertSame('TestPlugin.ThisAssociationName', $table->registryAlias());
  190. $this->assertSame('comments', $table->table());
  191. $this->assertSame('ThisAssociationName', $table->alias());
  192. }
  193. /**
  194. * Tests that source() returns the correct Table object
  195. *
  196. * @return void
  197. */
  198. public function testSource()
  199. {
  200. $table = $this->association->source();
  201. $this->assertSame($this->source, $table);
  202. $other = new Table;
  203. $this->association->source($other);
  204. $this->assertSame($other, $this->association->source());
  205. }
  206. /**
  207. * Tests joinType method
  208. *
  209. * @return void
  210. */
  211. public function testJoinType()
  212. {
  213. $this->assertEquals('INNER', $this->association->joinType());
  214. $this->association->joinType('LEFT');
  215. $this->assertEquals('LEFT', $this->association->joinType());
  216. }
  217. /**
  218. * Tests property method
  219. *
  220. * @return void
  221. */
  222. public function testProperty()
  223. {
  224. $this->assertEquals('foo', $this->association->property());
  225. $this->association->property('thing');
  226. $this->assertEquals('thing', $this->association->property());
  227. }
  228. /**
  229. * Tests strategy method
  230. *
  231. * @return void
  232. */
  233. public function testStrategy()
  234. {
  235. $this->assertEquals('join', $this->association->strategy());
  236. $this->association->strategy('select');
  237. $this->assertEquals('select', $this->association->strategy());
  238. $this->association->strategy('subquery');
  239. $this->assertEquals('subquery', $this->association->strategy());
  240. }
  241. /**
  242. * Tests that providing an invalid strategy throws an exception
  243. *
  244. * @expectedException \InvalidArgumentException
  245. * @return void
  246. */
  247. public function testInvalidStrategy()
  248. {
  249. $this->association->strategy('anotherThing');
  250. $this->assertEquals('subquery', $this->association->strategy());
  251. }
  252. /**
  253. * Tests test finder() method as getter and setter
  254. *
  255. * @return void
  256. */
  257. public function testFinderMethod()
  258. {
  259. $this->assertEquals('all', $this->association->finder());
  260. $this->assertEquals('published', $this->association->finder('published'));
  261. $this->assertEquals('published', $this->association->finder());
  262. }
  263. /**
  264. * Tests that `finder` is a valid option for the association constructor
  265. *
  266. * @return void
  267. */
  268. public function testFinderInConstructor()
  269. {
  270. $config = [
  271. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  272. 'foreignKey' => 'a_key',
  273. 'conditions' => ['field' => 'value'],
  274. 'dependent' => true,
  275. 'sourceTable' => $this->source,
  276. 'joinType' => 'INNER',
  277. 'finder' => 'published'
  278. ];
  279. $assoc = $this->getMock(
  280. '\Cake\ORM\Association',
  281. ['type', 'eagerLoader', 'cascadeDelete', 'isOwningSide', 'saveAssociated'],
  282. ['Foo', $config]
  283. );
  284. $this->assertEquals('published', $assoc->finder());
  285. }
  286. /**
  287. * Tests that the defined custom finder is used when calling find
  288. * in the association
  289. *
  290. * @return void
  291. */
  292. public function testCustomFinderIsUsed()
  293. {
  294. $this->association->finder('published');
  295. $this->assertEquals(
  296. ['this' => 'worked'],
  297. $this->association->find()->getOptions()
  298. );
  299. }
  300. }