BehaviorTest.php 11 KB

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