BehaviorRegistryTest.php 8.9 KB

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