DispatcherFilterTest.php 5.9 KB

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