DispatcherFilterTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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\Routing;
  16. use Cake\Event\Event;
  17. use Cake\Network\Request;
  18. use Cake\Network\Response;
  19. use Cake\Routing\DispatcherFilter;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Dispatcher filter test.
  23. */
  24. class DispatcherFilterTest extends TestCase {
  25. /**
  26. * Test that the constructor takes config.
  27. *
  28. * @return void
  29. */
  30. public function testConstructConfig() {
  31. $filter = new DispatcherFilter(['one' => 'value', 'on' => '/blog']);
  32. $this->assertEquals('value', $filter->config('one'));
  33. }
  34. /**
  35. * Test setting priority
  36. *
  37. * @return void
  38. */
  39. public function testConstructPriority() {
  40. $filter = new DispatcherFilter();
  41. $this->assertEquals(10, $filter->config('priority'));
  42. $filter = new DispatcherFilter(['priority' => 100]);
  43. $this->assertEquals(100, $filter->config('priority'));
  44. }
  45. /**
  46. * Test implemented events
  47. *
  48. * @return void
  49. */
  50. public function testImplementedEvents() {
  51. $filter = new DispatcherFilter(['priority' => 100]);
  52. $events = $filter->implementedEvents();
  53. $this->assertEquals(100, $events['Dispatcher.beforeDispatch']['priority']);
  54. $this->assertEquals(100, $events['Dispatcher.afterDispatch']['priority']);
  55. }
  56. /**
  57. * Test constructor error invalid when
  58. *
  59. * @expectedException \InvalidArgumentException
  60. * @expectedExceptionMessage "when" conditions must be a callable.
  61. * @return void
  62. */
  63. public function testConstructorInvalidWhen() {
  64. new DispatcherFilter(['when' => 'nope']);
  65. }
  66. /**
  67. * Test basic matching with for option.
  68. *
  69. * @return void
  70. * @triggers Dispatcher.beforeDispatch $this, compact('request')
  71. * @triggers Dispatcher.beforeDispatch $this, compact('request')
  72. * @triggers Dispatcher.beforeDispatch $this, compact('request')
  73. * @triggers Dispatcher.beforeDispatch $this, compact('request')
  74. */
  75. public function testMatchesWithFor() {
  76. $request = new Request(['url' => '/articles/view']);
  77. $event = new Event('Dispatcher.beforeDispatch', $this, compact('request'));
  78. $filter = new DispatcherFilter(['for' => '/articles']);
  79. $this->assertTrue($filter->matches($event));
  80. $request = new Request(['url' => '/blog/articles']);
  81. $event = new Event('Dispatcher.beforeDispatch', $this, compact('request'));
  82. $this->assertFalse($filter->matches($event), 'Does not start with /articles');
  83. $request = new Request(['url' => '/articles/edit/1']);
  84. $event = new Event('Dispatcher.beforeDispatch', $this, compact('request'));
  85. $filter = new DispatcherFilter(['for' => 'preg:#^/articles/edit/\d+$#']);
  86. $this->assertTrue($filter->matches($event));
  87. $request = new Request(['url' => '/blog/articles/edit/1']);
  88. $event = new Event('Dispatcher.beforeDispatch', $this, compact('request'));
  89. $this->assertFalse($filter->matches($event), 'Does not start with /articles');
  90. }
  91. /**
  92. * Test matching with when option.
  93. *
  94. * @return void
  95. * @triggers Dispatcher.beforeDispatch $this, compact('response', 'request')
  96. */
  97. public function testMatchesWithWhen() {
  98. $matcher = function ($request, $response) {
  99. $this->assertInstanceOf('Cake\Network\Request', $request);
  100. $this->assertInstanceOf('Cake\Network\Response', $response);
  101. return true;
  102. };
  103. $request = new Request(['url' => '/articles/view']);
  104. $response = new Response();
  105. $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request'));
  106. $filter = new DispatcherFilter(['when' => $matcher]);
  107. $this->assertTrue($filter->matches($event));
  108. $matcher = function () {
  109. return false;
  110. };
  111. $filter = new DispatcherFilter(['when' => $matcher]);
  112. $this->assertFalse($filter->matches($event));
  113. }
  114. /**
  115. * Test matching with for & when option.
  116. *
  117. * @return void
  118. * @triggers Dispatcher.beforeDispatch $this, compact('response', 'request')
  119. */
  120. public function testMatchesWithForAndWhen() {
  121. $request = new Request(['url' => '/articles/view']);
  122. $response = new Response();
  123. $matcher = function () {
  124. return true;
  125. };
  126. $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request'));
  127. $filter = new DispatcherFilter(['for' => '/admin', 'when' => $matcher]);
  128. $this->assertFalse($filter->matches($event));
  129. $filter = new DispatcherFilter(['for' => '/articles', 'when' => $matcher]);
  130. $this->assertTrue($filter->matches($event));
  131. $matcher = function () {
  132. return false;
  133. };
  134. $filter = new DispatcherFilter(['for' => '/admin', 'when' => $matcher]);
  135. $this->assertFalse($filter->matches($event));
  136. $filter = new DispatcherFilter(['for' => '/articles', 'when' => $matcher]);
  137. $this->assertFalse($filter->matches($event));
  138. }
  139. /**
  140. * Test event bindings have use condition checker
  141. *
  142. * @return void
  143. * @triggers Dispatcher.beforeDispatch $this, compact('response', 'request')
  144. * @triggers Dispatcher.afterDispatch $this, compact('response', 'request')
  145. */
  146. public function testImplementedEventsMethodName() {
  147. $request = new Request(['url' => '/articles/view']);
  148. $response = new Response();
  149. $beforeEvent = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request'));
  150. $afterEvent = new Event('Dispatcher.afterDispatch', $this, compact('response', 'request'));
  151. $filter = $this->getMock('Cake\Routing\DispatcherFilter', ['beforeDispatch', 'afterDispatch']);
  152. $filter->expects($this->at(0))
  153. ->method('beforeDispatch')
  154. ->with($beforeEvent);
  155. $filter->expects($this->at(1))
  156. ->method('afterDispatch')
  157. ->with($afterEvent);
  158. $filter->handle($beforeEvent);
  159. $filter->handle($afterEvent);
  160. }
  161. /**
  162. * Test handle applies for conditions
  163. *
  164. * @return void
  165. * @triggers Dispatcher.beforeDispatch $this, compact('response', 'request')
  166. */
  167. public function testHandleAppliesFor() {
  168. $request = new Request(['url' => '/articles/view']);
  169. $response = new Response();
  170. $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request'));
  171. $filter = $this->getMock(
  172. 'Cake\Routing\DispatcherFilter',
  173. ['beforeDispatch'],
  174. [['for' => '/admin']]
  175. );
  176. $filter->expects($this->never())
  177. ->method('beforeDispatch');
  178. $filter->handle($event);
  179. }
  180. /**
  181. * Test handle applies when conditions
  182. *
  183. * @return void
  184. * @triggers Dispatcher.beforeDispatch $this, compact('response', 'request')
  185. */
  186. public function testHandleAppliesWhen() {
  187. $request = new Request(['url' => '/articles/view']);
  188. $response = new Response();
  189. $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request'));
  190. $matcher = function () {
  191. return false;
  192. };
  193. $filter = $this->getMock(
  194. 'Cake\Routing\DispatcherFilter',
  195. ['beforeDispatch'],
  196. [['when' => $matcher]]
  197. );
  198. $filter->expects($this->never())
  199. ->method('beforeDispatch');
  200. $filter->handle($event);
  201. }
  202. }