DispatcherFilterTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 Cake\Error\Exception
  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. */
  71. public function testMatchesWithFor() {
  72. $request = new Request(['url' => '/articles/view']);
  73. $event = new Event('Dispatcher.beforeDispatch', $this, compact('request'));
  74. $filter = new DispatcherFilter(['for' => '/articles']);
  75. $this->assertTrue($filter->matches($event));
  76. $request = new Request(['url' => '/blog/articles']);
  77. $event = new Event('Dispatcher.beforeDispatch', $this, compact('request'));
  78. $this->assertFalse($filter->matches($event), 'Does not start with /articles');
  79. $request = new Request(['url' => '/articles/edit/1']);
  80. $event = new Event('Dispatcher.beforeDispatch', $this, compact('request'));
  81. $filter = new DispatcherFilter(['for' => 'preg:#^/articles/edit/\d+$#']);
  82. $this->assertTrue($filter->matches($event));
  83. $request = new Request(['url' => '/blog/articles/edit/1']);
  84. $event = new Event('Dispatcher.beforeDispatch', $this, compact('request'));
  85. $this->assertFalse($filter->matches($event), 'Does not start with /articles');
  86. }
  87. /**
  88. * Test matching with when option.
  89. *
  90. * @return void
  91. */
  92. public function testMatchesWithWhen() {
  93. $matcher = function ($request, $response) {
  94. $this->assertInstanceOf('Cake\Network\Request', $request);
  95. $this->assertInstanceOf('Cake\Network\Response', $response);
  96. return true;
  97. };
  98. $request = new Request(['url' => '/articles/view']);
  99. $response = new Response();
  100. $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request'));
  101. $filter = new DispatcherFilter(['when' => $matcher]);
  102. $this->assertTrue($filter->matches($event));
  103. $matcher = function() {
  104. return false;
  105. };
  106. $filter = new DispatcherFilter(['when' => $matcher]);
  107. $this->assertFalse($filter->matches($event));
  108. }
  109. /**
  110. * Test matching with for & when option.
  111. *
  112. * @return void
  113. */
  114. public function testMatchesWithForAndWhen() {
  115. $request = new Request(['url' => '/articles/view']);
  116. $response = new Response();
  117. $matcher = function () {
  118. return true;
  119. };
  120. $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request'));
  121. $filter = new DispatcherFilter(['for' => '/admin', 'when' => $matcher]);
  122. $this->assertFalse($filter->matches($event));
  123. $filter = new DispatcherFilter(['for' => '/articles', 'when' => $matcher]);
  124. $this->assertTrue($filter->matches($event));
  125. $matcher = function() {
  126. return false;
  127. };
  128. $filter = new DispatcherFilter(['for' => '/admin', 'when' => $matcher]);
  129. $this->assertFalse($filter->matches($event));
  130. $filter = new DispatcherFilter(['for' => '/articles', 'when' => $matcher]);
  131. $this->assertFalse($filter->matches($event));
  132. }
  133. /**
  134. * Test event bindings have use condition checker
  135. *
  136. * @return void
  137. */
  138. public function testImplementedEventsMethodName() {
  139. $request = new Request(['url' => '/articles/view']);
  140. $response = new Response();
  141. $beforeEvent = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request'));
  142. $afterEvent = new Event('Dispatcher.afterDispatch', $this, compact('response', 'request'));
  143. $filter = $this->getMock('Cake\Routing\DispatcherFilter', ['beforeDispatch', 'afterDispatch']);
  144. $filter->expects($this->at(0))
  145. ->method('beforeDispatch')
  146. ->with($beforeEvent);
  147. $filter->expects($this->at(1))
  148. ->method('afterDispatch')
  149. ->with($afterEvent);
  150. $filter->handle($beforeEvent);
  151. $filter->handle($afterEvent);
  152. }
  153. /**
  154. * Test handle applies for conditions
  155. *
  156. * @return void
  157. */
  158. public function testHandleAppliesFor() {
  159. $request = new Request(['url' => '/articles/view']);
  160. $response = new Response();
  161. $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request'));
  162. $filter = $this->getMock(
  163. 'Cake\Routing\DispatcherFilter',
  164. ['beforeDispatch'],
  165. [['for' => '/admin']]
  166. );
  167. $filter->expects($this->never())
  168. ->method('beforeDispatch');
  169. $filter->handle($event);
  170. }
  171. /**
  172. * Test handle applies when conditions
  173. *
  174. * @return void
  175. */
  176. public function testHandleAppliesWhen() {
  177. $request = new Request(['url' => '/articles/view']);
  178. $response = new Response();
  179. $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request'));
  180. $matcher = function() {
  181. return false;
  182. };
  183. $filter = $this->getMock(
  184. 'Cake\Routing\DispatcherFilter',
  185. ['beforeDispatch'],
  186. [['when' => $matcher]]
  187. );
  188. $filter->expects($this->never())
  189. ->method('beforeDispatch');
  190. $filter->handle($event);
  191. }
  192. }