|
| 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 | + |
| 17 | +use Cake\Event\Event; |
| 18 | +use Cake\Network\Request; |
| 19 | +use Cake\Network\Response; |
| 20 | +use Cake\Routing\DispatcherFilter; |
| 21 | +use Cake\TestSuite\TestCase; |
| 22 | + |
| 23 | +/** |
| 24 | + * Dispatcher filter test. |
| 25 | + */ |
| 26 | +class DispatcherFilterTest extends TestCase { |
| 27 | + |
| 28 | +/** |
| 29 | + * Test that the constructor takes config. |
| 30 | + * |
| 31 | + * @return void |
| 32 | + */ |
| 33 | + public function testConstructConfig() { |
| 34 | + $filter = new DispatcherFilter(['one' => 'value', 'on' => '/blog']); |
| 35 | + $this->assertEquals('value', $filter->config('one')); |
| 36 | + } |
| 37 | + |
| 38 | +/** |
| 39 | + * Test constructor error invalid when |
| 40 | + * |
| 41 | + * @expectedException Cake\Error\Exception |
| 42 | + * @expectedExceptionMessage "when" conditions must be a callable. |
| 43 | + * @return void |
| 44 | + */ |
| 45 | + public function testConstructorInvalidWhen() { |
| 46 | + new DispatcherFilter(['when' => 'nope']); |
| 47 | + } |
| 48 | + |
| 49 | +/** |
| 50 | + * Test basic matching with for option. |
| 51 | + * |
| 52 | + * @return void |
| 53 | + */ |
| 54 | + public function testMatchesWithFor() { |
| 55 | + $request = new Request(['url' => '/articles/view']); |
| 56 | + $event = new Event('Dispatcher.beforeDispatch', $this, compact('request')); |
| 57 | + $filter = new DispatcherFilter(['for' => '/articles']); |
| 58 | + $this->assertTrue($filter->matches($event)); |
| 59 | + |
| 60 | + $request = new Request(['url' => '/blog/articles']); |
| 61 | + $event = new Event('Dispatcher.beforeDispatch', $this, compact('request')); |
| 62 | + $this->assertFalse($filter->matches($event), 'Does not start with /articles'); |
| 63 | + } |
| 64 | + |
| 65 | +/** |
| 66 | + * Test matching with when option. |
| 67 | + * |
| 68 | + * @return void |
| 69 | + */ |
| 70 | + public function testMatchesWithWhen() { |
| 71 | + $matcher = function ($request, $response) { |
| 72 | + $this->assertInstanceOf('Cake\Network\Request', $request); |
| 73 | + $this->assertInstanceOf('Cake\Network\Response', $response); |
| 74 | + return true; |
| 75 | + }; |
| 76 | + |
| 77 | + $request = new Request(['url' => '/articles/view']); |
| 78 | + $response = new Response(); |
| 79 | + $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request')); |
| 80 | + $filter = new DispatcherFilter(['when' => $matcher]); |
| 81 | + $this->assertTrue($filter->matches($event)); |
| 82 | + |
| 83 | + $matcher = function() { |
| 84 | + return false; |
| 85 | + }; |
| 86 | + $filter = new DispatcherFilter(['when' => $matcher]); |
| 87 | + $this->assertFalse($filter->matches($event)); |
| 88 | + } |
| 89 | + |
| 90 | +/** |
| 91 | + * Test matching with for & when option. |
| 92 | + * |
| 93 | + * @return void |
| 94 | + */ |
| 95 | + public function testMatchesWithForAndWhen() { |
| 96 | + $request = new Request(['url' => '/articles/view']); |
| 97 | + $response = new Response(); |
| 98 | + |
| 99 | + $matcher = function () { |
| 100 | + return true; |
| 101 | + }; |
| 102 | + $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request')); |
| 103 | + $filter = new DispatcherFilter(['for' => '/admin', 'when' => $matcher]); |
| 104 | + $this->assertFalse($filter->matches($event)); |
| 105 | + |
| 106 | + $filter = new DispatcherFilter(['for' => '/articles', 'when' => $matcher]); |
| 107 | + $this->assertTrue($filter->matches($event)); |
| 108 | + |
| 109 | + $matcher = function() { |
| 110 | + return false; |
| 111 | + }; |
| 112 | + $filter = new DispatcherFilter(['for' => '/admin', 'when' => $matcher]); |
| 113 | + $this->assertFalse($filter->matches($event)); |
| 114 | + |
| 115 | + $filter = new DispatcherFilter(['for' => '/articles', 'when' => $matcher]); |
| 116 | + $this->assertFalse($filter->matches($event)); |
| 117 | + } |
| 118 | + |
| 119 | +/** |
| 120 | + * Test matching with when option. |
| 121 | + * |
| 122 | + * @expectedException \RuntimeException |
| 123 | + * @expectedExceptionMessage 'when' conditions must be a callable. |
| 124 | + * @return void |
| 125 | + */ |
| 126 | + public function testMatchesWithWhenInvalid() { |
| 127 | + $this->markTestIncomplete('not done'); |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | +/** |
| 132 | + * Test event bindings have use condition checker |
| 133 | + * |
| 134 | + * @return void |
| 135 | + */ |
| 136 | + public function testImplementedEventsMethodName() { |
| 137 | + $this->markTestIncomplete('not done'); |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | +/** |
| 142 | + * Test handle applies for conditions |
| 143 | + * |
| 144 | + * @return void |
| 145 | + */ |
| 146 | + public function testHandleAppliesFor() { |
| 147 | + $this->markTestIncomplete('not done'); |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | +/** |
| 152 | + * Test handle applies when conditions |
| 153 | + * |
| 154 | + * @return void |
| 155 | + */ |
| 156 | + public function testHandleAppliesWhen() { |
| 157 | + $this->markTestIncomplete('not done'); |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments