BehaviorRegistryTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 CakePHP(tm) v 3.0.0
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  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->getEventManager();
  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\Error\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 \Cake\Error\Exception
  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 hasMethod()
  119. *
  120. * @return void
  121. */
  122. public function testHasMethod() {
  123. Plugin::load('TestPlugin');
  124. $this->Behaviors->load('TestPlugin.PersisterOne');
  125. $this->Behaviors->load('Sluggable');
  126. $this->assertTrue($this->Behaviors->hasMethod('slugify'));
  127. $this->assertTrue($this->Behaviors->hasMethod('SLUGIFY'));
  128. $this->assertTrue($this->Behaviors->hasMethod('persist'));
  129. $this->assertTrue($this->Behaviors->hasMethod('PERSIST'));
  130. $this->assertFalse($this->Behaviors->hasMethod('__construct'));
  131. $this->assertFalse($this->Behaviors->hasMethod('config'));
  132. $this->assertFalse($this->Behaviors->hasMethod('implementedEvents'));
  133. $this->assertFalse($this->Behaviors->hasMethod('nope'));
  134. $this->assertFalse($this->Behaviors->hasMethod('beforeFind'));
  135. $this->assertFalse($this->Behaviors->hasMethod('noSlug'));
  136. }
  137. /**
  138. * Test hasFinder() method.
  139. *
  140. * @return void
  141. */
  142. public function testHasFinder() {
  143. $this->Behaviors->load('Sluggable');
  144. $this->assertTrue($this->Behaviors->hasFinder('noSlug'));
  145. $this->assertTrue($this->Behaviors->hasFinder('noslug'));
  146. $this->assertTrue($this->Behaviors->hasFinder('NOSLUG'));
  147. $this->assertFalse($this->Behaviors->hasFinder('slugify'));
  148. $this->assertFalse($this->Behaviors->hasFinder('beforeFind'));
  149. $this->assertFalse($this->Behaviors->hasFinder('nope'));
  150. }
  151. /**
  152. * test call
  153. *
  154. * Setup a behavior, then replace it with a mock to verify methods are called.
  155. * use dummy return values to verify the return value makes it back
  156. *
  157. * @return void
  158. */
  159. public function testCall() {
  160. $this->Behaviors->load('Sluggable');
  161. $mockedBehavior = $this->getMock('Behavior', ['slugify']);
  162. $this->Behaviors->set('Sluggable', $mockedBehavior);
  163. $mockedBehavior
  164. ->expects($this->once())
  165. ->method('slugify')
  166. ->with(['some value'])
  167. ->will($this->returnValue('some-thing'));
  168. $return = $this->Behaviors->call('slugify', [['some value']]);
  169. $this->assertSame('some-thing', $return);
  170. }
  171. /**
  172. * Test errors on unknown methods.
  173. *
  174. * @expectedException \Cake\Error\Exception
  175. * @expectedExceptionMessage Cannot call "nope"
  176. */
  177. public function testCallError() {
  178. $this->Behaviors->load('Sluggable');
  179. $this->Behaviors->call('nope');
  180. }
  181. /**
  182. * test call finder
  183. *
  184. * Setup a behavior, then replace it with a mock to verify methods are called.
  185. * use dummy return values to verify the return value makes it back
  186. *
  187. * @return void
  188. */
  189. public function testCallFinder() {
  190. $this->Behaviors->load('Sluggable');
  191. $mockedBehavior = $this->getMock('Behavior', ['findNoSlug']);
  192. $this->Behaviors->set('Sluggable', $mockedBehavior);
  193. $query = $this->getMock('Cake\ORM\Query', [], [null, null]);
  194. $mockedBehavior
  195. ->expects($this->once())
  196. ->method('findNoSlug')
  197. ->with($query, [])
  198. ->will($this->returnValue('example'));
  199. $return = $this->Behaviors->callFinder('noSlug', [$query, []]);
  200. $this->assertSame('example', $return);
  201. }
  202. /**
  203. * Test errors on unknown methods.
  204. *
  205. * @expectedException \Cake\Error\Exception
  206. * @expectedExceptionMessage Cannot call finder "nope"
  207. */
  208. public function testCallFinderError() {
  209. $this->Behaviors->load('Sluggable');
  210. $this->Behaviors->callFinder('nope');
  211. }
  212. }