BehaviorTest.php 10 KB

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