PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/TestCase/Routing/DispatcherFilterTest.php

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