BehaviorRegistryTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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->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\ORM\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->getMockBuilder('Cake\ORM\Behavior')
  162. ->setMethods(['slugify'])
  163. ->disableOriginalConstructor()
  164. ->getMock();
  165. $this->Behaviors->set('Sluggable', $mockedBehavior);
  166. $mockedBehavior
  167. ->expects($this->once())
  168. ->method('slugify')
  169. ->with(['some value'])
  170. ->will($this->returnValue('some-thing'));
  171. $return = $this->Behaviors->call('slugify', [['some value']]);
  172. $this->assertSame('some-thing', $return);
  173. }
  174. /**
  175. * Test errors on unknown methods.
  176. *
  177. * @expectedException \Cake\Error\Exception
  178. * @expectedExceptionMessage Cannot call "nope"
  179. */
  180. public function testCallError() {
  181. $this->Behaviors->load('Sluggable');
  182. $this->Behaviors->call('nope');
  183. }
  184. /**
  185. * test call finder
  186. *
  187. * Setup a behavior, then replace it with a mock to verify methods are called.
  188. * use dummy return values to verify the return value makes it back
  189. *
  190. * @return void
  191. */
  192. public function testCallFinder() {
  193. $this->Behaviors->load('Sluggable');
  194. $mockedBehavior = $this->getMockBuilder('Cake\ORM\Behavior')
  195. ->setMethods(['findNoSlug'])
  196. ->disableOriginalConstructor()
  197. ->getMock();
  198. $this->Behaviors->set('Sluggable', $mockedBehavior);
  199. $query = $this->getMock('Cake\ORM\Query', [], [null, null]);
  200. $mockedBehavior
  201. ->expects($this->once())
  202. ->method('findNoSlug')
  203. ->with($query, [])
  204. ->will($this->returnValue('example'));
  205. $return = $this->Behaviors->callFinder('noSlug', [$query, []]);
  206. $this->assertSame('example', $return);
  207. }
  208. /**
  209. * Test errors on unknown methods.
  210. *
  211. * @expectedException \Cake\Error\Exception
  212. * @expectedExceptionMessage Cannot call finder "nope"
  213. */
  214. public function testCallFinderError() {
  215. $this->Behaviors->load('Sluggable');
  216. $this->Behaviors->callFinder('nope');
  217. }
  218. }