BehaviorRegistryTest.php 12 KB

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