DispatcherFilterTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 constructor error invalid when
  36. *
  37. * @expectedException Cake\Error\Exception
  38. * @expectedExceptionMessage "when" conditions must be a callable.
  39. * @return void
  40. */
  41. public function testConstructorInvalidWhen() {
  42. new DispatcherFilter(['when' => 'nope']);
  43. }
  44. /**
  45. * Test basic matching with for option.
  46. *
  47. * @return void
  48. */
  49. public function testMatchesWithFor() {
  50. $request = new Request(['url' => '/articles/view']);
  51. $event = new Event('Dispatcher.beforeDispatch', $this, compact('request'));
  52. $filter = new DispatcherFilter(['for' => '/articles']);
  53. $this->assertTrue($filter->matches($event));
  54. $request = new Request(['url' => '/blog/articles']);
  55. $event = new Event('Dispatcher.beforeDispatch', $this, compact('request'));
  56. $this->assertFalse($filter->matches($event), 'Does not start with /articles');
  57. }
  58. /**
  59. * Test matching with when option.
  60. *
  61. * @return void
  62. */
  63. public function testMatchesWithWhen() {
  64. $matcher = function ($request, $response) {
  65. $this->assertInstanceOf('Cake\Network\Request', $request);
  66. $this->assertInstanceOf('Cake\Network\Response', $response);
  67. return true;
  68. };
  69. $request = new Request(['url' => '/articles/view']);
  70. $response = new Response();
  71. $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request'));
  72. $filter = new DispatcherFilter(['when' => $matcher]);
  73. $this->assertTrue($filter->matches($event));
  74. $matcher = function() {
  75. return false;
  76. };
  77. $filter = new DispatcherFilter(['when' => $matcher]);
  78. $this->assertFalse($filter->matches($event));
  79. }
  80. /**
  81. * Test matching with for & when option.
  82. *
  83. * @return void
  84. */
  85. public function testMatchesWithForAndWhen() {
  86. $request = new Request(['url' => '/articles/view']);
  87. $response = new Response();
  88. $matcher = function () {
  89. return true;
  90. };
  91. $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request'));
  92. $filter = new DispatcherFilter(['for' => '/admin', 'when' => $matcher]);
  93. $this->assertFalse($filter->matches($event));
  94. $filter = new DispatcherFilter(['for' => '/articles', 'when' => $matcher]);
  95. $this->assertTrue($filter->matches($event));
  96. $matcher = function() {
  97. return false;
  98. };
  99. $filter = new DispatcherFilter(['for' => '/admin', 'when' => $matcher]);
  100. $this->assertFalse($filter->matches($event));
  101. $filter = new DispatcherFilter(['for' => '/articles', 'when' => $matcher]);
  102. $this->assertFalse($filter->matches($event));
  103. }
  104. /**
  105. * Test matching with when option.
  106. *
  107. * @expectedException \RuntimeException
  108. * @expectedExceptionMessage 'when' conditions must be a callable.
  109. * @return void
  110. */
  111. public function testMatchesWithWhenInvalid() {
  112. $this->markTestIncomplete('not done');
  113. }
  114. /**
  115. * Test event bindings have use condition checker
  116. *
  117. * @return void
  118. */
  119. public function testImplementedEventsMethodName() {
  120. $this->markTestIncomplete('not done');
  121. }
  122. /**
  123. * Test handle applies for conditions
  124. *
  125. * @return void
  126. */
  127. public function testHandleAppliesFor() {
  128. $this->markTestIncomplete('not done');
  129. }
  130. /**
  131. * Test handle applies when conditions
  132. *
  133. * @return void
  134. */
  135. public function testHandleAppliesWhen() {
  136. $this->markTestIncomplete('not done');
  137. }
  138. }