BehaviorRegistryTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\ORM\BehaviorRegistry;
  19. use Cake\ORM\Table;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Test case for BehaviorRegistry.
  23. */
  24. class BehaviorRegistryTest extends TestCase
  25. {
  26. /**
  27. * setup method.
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. $this->Table = new Table(['table' => 'articles']);
  35. $this->EventManager = $this->Table->eventManager();
  36. $this->Behaviors = new BehaviorRegistry($this->Table);
  37. Configure::write('App.namespace', 'TestApp');
  38. }
  39. /**
  40. * tearDown
  41. *
  42. * @return void
  43. */
  44. public function tearDown()
  45. {
  46. Plugin::unload();
  47. unset($this->Table, $this->EventManager, $this->Behaviors);
  48. parent::tearDown();
  49. }
  50. /**
  51. * Test loading behaviors.
  52. *
  53. * @return void
  54. */
  55. public function testLoad()
  56. {
  57. Plugin::load('TestPlugin');
  58. $config = ['alias' => 'Sluggable', 'replacement' => '-'];
  59. $result = $this->Behaviors->load('Sluggable', $config);
  60. $this->assertInstanceOf('TestApp\Model\Behavior\SluggableBehavior', $result);
  61. $this->assertEquals($config, $result->config());
  62. $result = $this->Behaviors->load('TestPlugin.PersisterOne');
  63. $this->assertInstanceOf('TestPlugin\Model\Behavior\PersisterOneBehavior', $result);
  64. }
  65. /**
  66. * Test load() binding listeners.
  67. *
  68. * @return void
  69. */
  70. public function testLoadBindEvents()
  71. {
  72. $result = $this->EventManager->listeners('Model.beforeFind');
  73. $this->assertCount(0, $result);
  74. $this->Behaviors->load('Sluggable');
  75. $result = $this->EventManager->listeners('Model.beforeFind');
  76. $this->assertCount(1, $result);
  77. $this->assertInstanceOf('TestApp\Model\Behavior\SluggableBehavior', $result[0]['callable'][0]);
  78. $this->assertEquals('beforeFind', $result[0]['callable'][1], 'Method name should match.');
  79. }
  80. /**
  81. * Test load() with enabled = false
  82. *
  83. * @return void
  84. */
  85. public function testLoadEnabledFalse()
  86. {
  87. $result = $this->EventManager->listeners('Model.beforeFind');
  88. $this->assertCount(0, $result);
  89. $this->Behaviors->load('Sluggable', ['enabled' => false]);
  90. $result = $this->EventManager->listeners('Model.beforeFind');
  91. $this->assertCount(0, $result);
  92. }
  93. /**
  94. * Test loading plugin behaviors
  95. *
  96. * @return void
  97. */
  98. public function testLoadPlugin()
  99. {
  100. Plugin::load('TestPlugin');
  101. $result = $this->Behaviors->load('TestPlugin.PersisterOne');
  102. $this->assertInstanceOf('TestPlugin\Model\Behavior\PersisterOneBehavior', $result);
  103. }
  104. /**
  105. * Test load() on undefined class
  106. *
  107. * @expectedException \Cake\ORM\Exception\MissingBehaviorException
  108. * @return void
  109. */
  110. public function testLoadMissingClass()
  111. {
  112. $this->Behaviors->load('DoesNotExist');
  113. }
  114. /**
  115. * Test load() duplicate method error
  116. *
  117. * @expectedException \LogicException
  118. * @expectedExceptionMessage TestApp\Model\Behavior\DuplicateBehavior contains duplicate method "slugify"
  119. * @return void
  120. */
  121. public function testLoadDuplicateMethodError()
  122. {
  123. $this->Behaviors->load('Sluggable');
  124. $this->Behaviors->load('Duplicate');
  125. }
  126. /**
  127. * Test load() duplicate method aliasing
  128. *
  129. * @return void
  130. */
  131. public function testLoadDuplicateMethodAliasing()
  132. {
  133. $this->Behaviors->load('Tree');
  134. $this->Behaviors->load('Duplicate', [
  135. 'implementedFinders' => [
  136. 'renamed' => 'findChildren',
  137. ],
  138. 'implementedMethods' => [
  139. 'renamed' => 'slugify',
  140. ]
  141. ]);
  142. $this->assertTrue($this->Behaviors->hasMethod('renamed'));
  143. }
  144. /**
  145. * Test load() duplicate finder error
  146. *
  147. * @expectedException \LogicException
  148. * @expectedExceptionMessage TestApp\Model\Behavior\DuplicateBehavior contains duplicate finder "children"
  149. * @return void
  150. */
  151. public function testLoadDuplicateFinderError()
  152. {
  153. $this->Behaviors->load('Tree');
  154. $this->Behaviors->load('Duplicate');
  155. }
  156. /**
  157. * Test load() duplicate finder aliasing
  158. *
  159. * @return void
  160. */
  161. public function testLoadDuplicateFinderAliasing()
  162. {
  163. $this->Behaviors->load('Tree');
  164. $this->Behaviors->load('Duplicate', [
  165. 'implementedFinders' => [
  166. 'renamed' => 'findChildren',
  167. ]
  168. ]);
  169. $this->assertTrue($this->Behaviors->hasFinder('renamed'));
  170. }
  171. /**
  172. * test hasMethod()
  173. *
  174. * @return void
  175. */
  176. public function testHasMethod()
  177. {
  178. Plugin::load('TestPlugin');
  179. $this->Behaviors->load('TestPlugin.PersisterOne');
  180. $this->Behaviors->load('Sluggable');
  181. $this->assertTrue($this->Behaviors->hasMethod('slugify'));
  182. $this->assertTrue($this->Behaviors->hasMethod('SLUGIFY'));
  183. $this->assertTrue($this->Behaviors->hasMethod('persist'));
  184. $this->assertTrue($this->Behaviors->hasMethod('PERSIST'));
  185. $this->assertFalse($this->Behaviors->hasMethod('__construct'));
  186. $this->assertFalse($this->Behaviors->hasMethod('config'));
  187. $this->assertFalse($this->Behaviors->hasMethod('implementedEvents'));
  188. $this->assertFalse($this->Behaviors->hasMethod('nope'));
  189. $this->assertFalse($this->Behaviors->hasMethod('beforeFind'));
  190. $this->assertFalse($this->Behaviors->hasMethod('noSlug'));
  191. }
  192. /**
  193. * Test hasFinder() method.
  194. *
  195. * @return void
  196. */
  197. public function testHasFinder()
  198. {
  199. $this->Behaviors->load('Sluggable');
  200. $this->assertTrue($this->Behaviors->hasFinder('noSlug'));
  201. $this->assertTrue($this->Behaviors->hasFinder('noslug'));
  202. $this->assertTrue($this->Behaviors->hasFinder('NOSLUG'));
  203. $this->assertFalse($this->Behaviors->hasFinder('slugify'));
  204. $this->assertFalse($this->Behaviors->hasFinder('beforeFind'));
  205. $this->assertFalse($this->Behaviors->hasFinder('nope'));
  206. }
  207. /**
  208. * test call
  209. *
  210. * Setup a behavior, then replace it with a mock to verify methods are called.
  211. * use dummy return values to verify the return value makes it back
  212. *
  213. * @return void
  214. */
  215. public function testCall()
  216. {
  217. $this->Behaviors->load('Sluggable');
  218. $mockedBehavior = $this->getMockBuilder('Cake\ORM\Behavior')
  219. ->setMethods(['slugify'])
  220. ->disableOriginalConstructor()
  221. ->getMock();
  222. $this->Behaviors->set('Sluggable', $mockedBehavior);
  223. $mockedBehavior
  224. ->expects($this->once())
  225. ->method('slugify')
  226. ->with(['some value'])
  227. ->will($this->returnValue('some-thing'));
  228. $return = $this->Behaviors->call('slugify', [['some value']]);
  229. $this->assertSame('some-thing', $return);
  230. }
  231. /**
  232. * Test errors on unknown methods.
  233. *
  234. * @expectedException \BadMethodCallException
  235. * @expectedExceptionMessage Cannot call "nope"
  236. */
  237. public function testCallError()
  238. {
  239. $this->Behaviors->load('Sluggable');
  240. $this->Behaviors->call('nope');
  241. }
  242. /**
  243. * test call finder
  244. *
  245. * Setup a behavior, then replace it with a mock to verify methods are called.
  246. * use dummy return values to verify the return value makes it back
  247. *
  248. * @return void
  249. */
  250. public function testCallFinder()
  251. {
  252. $this->Behaviors->load('Sluggable');
  253. $mockedBehavior = $this->getMockBuilder('Cake\ORM\Behavior')
  254. ->setMethods(['findNoSlug'])
  255. ->disableOriginalConstructor()
  256. ->getMock();
  257. $this->Behaviors->set('Sluggable', $mockedBehavior);
  258. $query = $this->getMock('Cake\ORM\Query', [], [null, null]);
  259. $mockedBehavior
  260. ->expects($this->once())
  261. ->method('findNoSlug')
  262. ->with($query, [])
  263. ->will($this->returnValue('example'));
  264. $return = $this->Behaviors->callFinder('noSlug', [$query, []]);
  265. $this->assertSame('example', $return);
  266. }
  267. /**
  268. * Test errors on unknown methods.
  269. *
  270. * @expectedException \BadMethodCallException
  271. * @expectedExceptionMessage Cannot call finder "nope"
  272. */
  273. public function testCallFinderError()
  274. {
  275. $this->Behaviors->load('Sluggable');
  276. $this->Behaviors->callFinder('nope');
  277. }
  278. /**
  279. * Test errors on unloaded behavior methods.
  280. *
  281. * @expectedException \BadMethodCallException
  282. * @expectedExceptionMessage Cannot call "slugify" it does not belong to any attached behavior.
  283. */
  284. public function testUnloadBehaviorThenCall()
  285. {
  286. $this->Behaviors->load('Sluggable');
  287. $this->Behaviors->unload('Sluggable');
  288. $this->Behaviors->call('slugify');
  289. }
  290. /**
  291. * Test errors on unloaded behavior finders.
  292. *
  293. * @expectedException \BadMethodCallException
  294. * @expectedExceptionMessage Cannot call finder "noslug" it does not belong to any attached behavior.
  295. */
  296. public function testUnloadBehaviorThenCallFinder()
  297. {
  298. $this->Behaviors->load('Sluggable');
  299. $this->Behaviors->unload('Sluggable');
  300. $this->Behaviors->callFinder('noSlug');
  301. }
  302. /**
  303. * Test that unloading then reloading a behavior does not throw any errors.
  304. *
  305. * @return void
  306. */
  307. public function testUnloadBehaviorThenReload()
  308. {
  309. $this->Behaviors->load('Sluggable');
  310. $this->Behaviors->unload('Sluggable');
  311. $this->assertEmpty($this->Behaviors->loaded());
  312. $this->Behaviors->load('Sluggable');
  313. $this->assertEquals(['Sluggable'], $this->Behaviors->loaded());
  314. }
  315. }