AssociationTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. }
  25. /**
  26. * Tests Association class
  27. *
  28. */
  29. class AssociationTest extends \Cake\TestSuite\TestCase {
  30. /**
  31. * Set up
  32. *
  33. * @return void
  34. */
  35. public function setUp() {
  36. parent::setUp();
  37. $this->source = new TestTable;
  38. $config = [
  39. 'className' => '\Cake\Test\TestCase\ORM\TestTable',
  40. 'foreignKey' => 'a_key',
  41. 'conditions' => ['field' => 'value'],
  42. 'dependent' => true,
  43. 'sourceTable' => $this->source,
  44. 'joinType' => 'INNER'
  45. ];
  46. $this->association = $this->getMock(
  47. '\Cake\ORM\Association',
  48. [
  49. '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
  50. 'save', 'eagerLoader', 'type'
  51. ],
  52. ['Foo', $config]
  53. );
  54. }
  55. /**
  56. * Tear down
  57. *
  58. * @return void
  59. */
  60. public function tearDown() {
  61. parent::tearDown();
  62. TableRegistry::clear();
  63. }
  64. /**
  65. * Tests that _options acts as a callback where subclasses can add their own
  66. * initialization code based on the passed configuration array
  67. *
  68. * @return void
  69. */
  70. public function testOptionsIsCalled() {
  71. $options = ['foo' => 'bar'];
  72. $this->association->expects($this->once())->method('_options')->with($options);
  73. $this->association->__construct('Name', $options);
  74. }
  75. /**
  76. * Tests that name() returns the correct configure association name
  77. *
  78. * @return void
  79. */
  80. public function testName() {
  81. $this->assertEquals('Foo', $this->association->name());
  82. $this->association->name('Bar');
  83. $this->assertEquals('Bar', $this->association->name());
  84. }
  85. /**
  86. * Tests that name() returns the correct configured value
  87. *
  88. * @return void
  89. */
  90. public function testForeignKey() {
  91. $this->assertEquals('a_key', $this->association->foreignKey());
  92. $this->association->foreignKey('another_key');
  93. $this->assertEquals('another_key', $this->association->foreignKey());
  94. }
  95. /**
  96. * Tests that conditions() returns the correct configured value
  97. *
  98. * @return void
  99. */
  100. public function testConditions() {
  101. $this->assertEquals(['field' => 'value'], $this->association->conditions());
  102. $conds = ['another_key' => 'another value'];
  103. $this->association->conditions($conds);
  104. $this->assertEquals($conds, $this->association->conditions());
  105. }
  106. /**
  107. * Tests that canBeJoined() returns the correct configured value
  108. *
  109. * @return void
  110. */
  111. public function testCanBeJoined() {
  112. $this->assertTrue($this->association->canBeJoined());
  113. }
  114. /**
  115. * Tests that target() returns the correct Table object
  116. *
  117. * @return void
  118. */
  119. public function testTarget() {
  120. $table = $this->association->target();
  121. $this->assertInstanceOf(__NAMESPACE__ . '\TestTable', $table);
  122. $other = new Table;
  123. $this->association->target($other);
  124. $this->assertSame($other, $this->association->target());
  125. }
  126. /**
  127. * Tests that source() returns the correct Table object
  128. *
  129. * @return void
  130. */
  131. public function testSource() {
  132. $table = $this->association->source();
  133. $this->assertSame($this->source, $table);
  134. $other = new Table;
  135. $this->association->source($other);
  136. $this->assertSame($other, $this->association->source());
  137. }
  138. /**
  139. * Tests joinType method
  140. *
  141. * @return void
  142. */
  143. public function testJoinType() {
  144. $this->assertEquals('INNER', $this->association->joinType());
  145. $this->association->joinType('LEFT');
  146. $this->assertEquals('LEFT', $this->association->joinType());
  147. }
  148. /**
  149. * Tests property method
  150. *
  151. * @return void
  152. */
  153. public function testProperty() {
  154. $this->assertEquals('foo', $this->association->property());
  155. $this->association->property('thing');
  156. $this->assertEquals('thing', $this->association->property());
  157. }
  158. /**
  159. * Tests strategy method
  160. *
  161. * @return void
  162. */
  163. public function testStrategy() {
  164. $this->assertEquals('join', $this->association->strategy());
  165. $this->association->strategy('select');
  166. $this->assertEquals('select', $this->association->strategy());
  167. $this->association->strategy('subquery');
  168. $this->assertEquals('subquery', $this->association->strategy());
  169. }
  170. /**
  171. * Tests that providing an invalid strategy throws an exception
  172. *
  173. * @expectedException \InvalidArgumentException
  174. * @return void
  175. */
  176. public function testInvalidStrategy() {
  177. $this->association->strategy('anotherThing');
  178. $this->assertEquals('subquery', $this->association->strategy());
  179. }
  180. }