BehaviorTest.php 8.1 KB

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