BehaviorTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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\Behavior;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * Test Stub.
  20. */
  21. class TestBehavior extends Behavior {
  22. /**
  23. * Test for event bindings.
  24. */
  25. public function beforeFind() {
  26. }
  27. }
  28. /**
  29. * Test Stub.
  30. */
  31. class Test2Behavior extends Behavior {
  32. /**
  33. * Test for event bindings.
  34. */
  35. public function beforeFind() {
  36. }
  37. /**
  38. * Test finder
  39. */
  40. public function findFoo() {
  41. }
  42. /**
  43. * Test method
  44. */
  45. public function doSomething() {
  46. }
  47. }
  48. /**
  49. * Test3Behavior
  50. */
  51. class Test3Behavior extends Behavior {
  52. /**
  53. * Test for event bindings.
  54. */
  55. public function beforeFind() {
  56. }
  57. /**
  58. * Test finder
  59. */
  60. public function findFoo() {
  61. }
  62. /**
  63. * Test method
  64. */
  65. public function doSomething() {
  66. }
  67. /**
  68. * Test method to ensure it is ignored as a callable method.
  69. */
  70. public function verifyConfig() {
  71. return parent::verifyConfig();
  72. }
  73. /**
  74. * implementedEvents
  75. *
  76. * This class does pretend to implement beforeFind
  77. *
  78. * @return void
  79. */
  80. public function implementedEvents() {
  81. return ['Model.beforeFind' => 'beforeFind'];
  82. }
  83. /**
  84. * implementedFinders
  85. */
  86. public function implementedFinders() {
  87. }
  88. /**
  89. * implementedMethods
  90. */
  91. public function implementedMethods() {
  92. }
  93. /**
  94. * Expose protected method for testing
  95. *
  96. * Since this is public - it'll show up as callable which is a side-effect
  97. *
  98. * @return array
  99. */
  100. public function testReflectionCache() {
  101. return $this->_reflectionCache();
  102. }
  103. }
  104. /**
  105. * Behavior test case
  106. */
  107. class BehaviorTest extends TestCase {
  108. /**
  109. * Test the side effects of the constructor.
  110. *
  111. * @return void
  112. */
  113. public function testConstructor() {
  114. $table = $this->getMock('Cake\ORM\Table');
  115. $config = ['key' => 'value'];
  116. $behavior = new TestBehavior($table, $config);
  117. $this->assertEquals($config, $behavior->config());
  118. }
  119. public function testReflectionCache() {
  120. $table = $this->getMock('Cake\ORM\Table');
  121. $behavior = new Test3Behavior($table);
  122. $expected = [
  123. 'finders' => [
  124. 'foo' => 'findFoo'
  125. ],
  126. 'methods' => [
  127. 'doSomething' => 'doSomething',
  128. 'testReflectionCache' => 'testReflectionCache'
  129. ]
  130. ];
  131. $this->assertEquals($expected, $behavior->testReflectionCache());
  132. }
  133. /**
  134. * Test the default behavior of implementedEvents
  135. *
  136. * @return void
  137. */
  138. public function testImplementedEvents() {
  139. $table = $this->getMock('Cake\ORM\Table');
  140. $behavior = new TestBehavior($table);
  141. $expected = [
  142. 'Model.beforeFind' => 'beforeFind'
  143. ];
  144. $this->assertEquals($expected, $behavior->implementedEvents());
  145. }
  146. /**
  147. * Test that implementedEvents uses the priority setting.
  148. *
  149. * @return void
  150. */
  151. public function testImplementedEventsWithPriority() {
  152. $table = $this->getMock('Cake\ORM\Table');
  153. $behavior = new TestBehavior($table, ['priority' => 10]);
  154. $expected = [
  155. 'Model.beforeFind' => [
  156. 'priority' => 10,
  157. 'callable' => 'beforeFind'
  158. ]
  159. ];
  160. $this->assertEquals($expected, $behavior->implementedEvents());
  161. }
  162. /**
  163. * testImplementedMethods
  164. *
  165. * @return void
  166. */
  167. public function testImplementedMethods() {
  168. $table = $this->getMock('Cake\ORM\Table');
  169. $behavior = new Test2Behavior($table);
  170. $expected = [
  171. 'doSomething' => 'doSomething'
  172. ];
  173. $this->assertEquals($expected, $behavior->implementedMethods());
  174. }
  175. /**
  176. * testImplementedMethodsAliased
  177. *
  178. * @return void
  179. */
  180. public function testImplementedMethodsAliased() {
  181. $table = $this->getMock('Cake\ORM\Table');
  182. $behavior = new Test2Behavior($table, [
  183. 'implementedMethods' => [
  184. 'aliased' => 'doSomething'
  185. ]
  186. ]);
  187. $expected = [
  188. 'aliased' => 'doSomething'
  189. ];
  190. $this->assertEquals($expected, $behavior->implementedMethods());
  191. }
  192. /**
  193. * testImplementedMethodsDisabled
  194. *
  195. * @return void
  196. */
  197. public function testImplementedMethodsDisabled() {
  198. $table = $this->getMock('Cake\ORM\Table');
  199. $behavior = new Test2Behavior($table, [
  200. 'implementedMethods' => []
  201. ]);
  202. $expected = [];
  203. $this->assertEquals($expected, $behavior->implementedMethods());
  204. }
  205. /**
  206. * testImplementedFinders
  207. *
  208. * @return void
  209. */
  210. public function testImplementedFinders() {
  211. $table = $this->getMock('Cake\ORM\Table');
  212. $behavior = new Test2Behavior($table);
  213. $expected = [
  214. 'foo' => 'findFoo'
  215. ];
  216. $this->assertEquals($expected, $behavior->implementedFinders());
  217. }
  218. /**
  219. * testImplementedFindersAliased
  220. *
  221. * @return void
  222. */
  223. public function testImplementedFindersAliased() {
  224. $table = $this->getMock('Cake\ORM\Table');
  225. $behavior = new Test2Behavior($table, [
  226. 'implementedFinders' => [
  227. 'aliased' => 'findFoo'
  228. ]
  229. ]);
  230. $expected = [
  231. 'aliased' => 'findFoo'
  232. ];
  233. $this->assertEquals($expected, $behavior->implementedFinders());
  234. }
  235. /**
  236. * testImplementedFindersDisabled
  237. *
  238. * @return void
  239. */
  240. public function testImplementedFindersDisabled() {
  241. $table = $this->getMock('Cake\ORM\Table');
  242. $behavior = new Test2Behavior($table, [
  243. 'implementedFinders' => []
  244. ]);
  245. $expected = [];
  246. $this->assertEquals($expected, $behavior->implementedFinders());
  247. }
  248. /**
  249. * testVerifyConfig
  250. *
  251. * Don't expect an exception to be thrown
  252. *
  253. * @return void
  254. */
  255. public function testVerifyConfig() {
  256. $table = $this->getMock('Cake\ORM\Table');
  257. $behavior = new Test2Behavior($table);
  258. $behavior->verifyConfig();
  259. $this->assertTrue(true, 'No exception thrown');
  260. }
  261. /**
  262. * testVerifyConfigImplementedFindersOverriden
  263. *
  264. * Simply don't expect an exception to be thrown
  265. *
  266. * @return void
  267. */
  268. public function testVerifyConfigImplementedFindersOverriden() {
  269. $table = $this->getMock('Cake\ORM\Table');
  270. $behavior = new Test2Behavior($table, [
  271. 'implementedFinders' => [
  272. 'aliased' => 'findFoo'
  273. ]
  274. ]);
  275. $behavior->verifyConfig();
  276. $this->assertTrue(true, 'No exception thrown');
  277. }
  278. /**
  279. * testVerifyImplementedFindersInvalid
  280. *
  281. * @expectedException \Cake\Error\Exception
  282. * @expectedExceptionMessage The method findNotDefined is not callable on class Cake\Test\TestCase\ORM\Test2Behavior
  283. *
  284. * @return void
  285. */
  286. public function testVerifyImplementedFindersInvalid() {
  287. $table = $this->getMock('Cake\ORM\Table');
  288. $behavior = new Test2Behavior($table, [
  289. 'implementedFinders' => [
  290. 'aliased' => 'findNotDefined'
  291. ]
  292. ]);
  293. $behavior->verifyConfig();
  294. }
  295. /**
  296. * testVerifyConfigImplementedMethodsOverriden
  297. *
  298. * Don't expect an exception to be thrown
  299. *
  300. * @return void
  301. */
  302. public function testVerifyConfigImplementedMethodsOverriden() {
  303. $table = $this->getMock('Cake\ORM\Table');
  304. $behavior = new Test2Behavior($table);
  305. $behavior = new Test2Behavior($table, [
  306. 'implementedMethods' => [
  307. 'aliased' => 'doSomething'
  308. ]
  309. ]);
  310. $behavior->verifyConfig();
  311. $this->assertTrue(true, 'No exception thrown');
  312. }
  313. /**
  314. * testVerifyImplementedMethodsInvalid
  315. *
  316. * @expectedException \Cake\Error\Exception
  317. * @expectedExceptionMessage The method iDoNotExist is not callable on class Cake\Test\TestCase\ORM\Test2Behavior
  318. *
  319. * @return void
  320. */
  321. public function testVerifyImplementedMethodsInvalid() {
  322. $table = $this->getMock('Cake\ORM\Table');
  323. $behavior = new Test2Behavior($table, [
  324. 'implementedMethods' => [
  325. 'aliased' => 'iDoNotExist'
  326. ]
  327. ]);
  328. $behavior->verifyConfig();
  329. }
  330. }