cakephp /tests/TestCase/Routing/DispatcherFilterTest.php

Language PHP Lines 245
MD5 Hash e8a1f5efe8e0bc9778ec234f657acb05
Repository https://github.com/ceeram/cakephp.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/**
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @since         3.0.0
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
namespace Cake\Test\TestCase\Routing;

use Cake\Event\Event;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\Routing\DispatcherFilter;
use Cake\TestSuite\TestCase;

/**
 * Dispatcher filter test.
 */
class DispatcherFilterTest extends TestCase
{

    /**
     * Test that the constructor takes config.
     *
     * @return void
     */
    public function testConstructConfig()
    {
        $filter = new DispatcherFilter(['one' => 'value', 'on' => '/blog']);
        $this->assertEquals('value', $filter->config('one'));
    }

    /**
     * Test setting priority
     *
     * @return void
     */
    public function testConstructPriority()
    {
        $filter = new DispatcherFilter();
        $this->assertEquals(10, $filter->config('priority'));

        $filter = new DispatcherFilter(['priority' => 100]);
        $this->assertEquals(100, $filter->config('priority'));
    }

    /**
     * Test implemented events
     *
     * @return void
     */
    public function testImplementedEvents()
    {
        $filter = new DispatcherFilter(['priority' => 100]);
        $events = $filter->implementedEvents();
        $this->assertEquals(100, $events['Dispatcher.beforeDispatch']['priority']);
        $this->assertEquals(100, $events['Dispatcher.afterDispatch']['priority']);
    }

    /**
     * Test constructor error invalid when
     *
     * @expectedException \InvalidArgumentException
     * @expectedExceptionMessage "when" conditions must be a callable.
     * @return void
     */
    public function testConstructorInvalidWhen()
    {
        new DispatcherFilter(['when' => 'nope']);
    }

    /**
     * Test basic matching with for option.
     *
     * @return void
     * @triggers Dispatcher.beforeDispatch $this, compact('request')
     * @triggers Dispatcher.beforeDispatch $this, compact('request')
     * @triggers Dispatcher.beforeDispatch $this, compact('request')
     * @triggers Dispatcher.beforeDispatch $this, compact('request')
     */
    public function testMatchesWithFor()
    {
        $request = new Request(['url' => '/articles/view']);
        $event = new Event('Dispatcher.beforeDispatch', $this, compact('request'));
        $filter = new DispatcherFilter(['for' => '/articles']);
        $this->assertTrue($filter->matches($event));

        $request = new Request(['url' => '/blog/articles']);
        $event = new Event('Dispatcher.beforeDispatch', $this, compact('request'));
        $this->assertFalse($filter->matches($event), 'Does not start with /articles');

        $request = new Request(['url' => '/articles/edit/1']);
        $event = new Event('Dispatcher.beforeDispatch', $this, compact('request'));
        $filter = new DispatcherFilter(['for' => 'preg:#^/articles/edit/\d+$#']);
        $this->assertTrue($filter->matches($event));

        $request = new Request(['url' => '/blog/articles/edit/1']);
        $event = new Event('Dispatcher.beforeDispatch', $this, compact('request'));
        $this->assertFalse($filter->matches($event), 'Does not start with /articles');
    }

    /**
     * Test matching with when option.
     *
     * @return void
     * @triggers Dispatcher.beforeDispatch $this, compact('response', 'request')
     */
    public function testMatchesWithWhen()
    {
        $matcher = function ($request, $response) {
            $this->assertInstanceOf('Cake\Network\Request', $request);
            $this->assertInstanceOf('Cake\Network\Response', $response);
            return true;
        };

        $request = new Request(['url' => '/articles/view']);
        $response = new Response();
        $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request'));
        $filter = new DispatcherFilter(['when' => $matcher]);
        $this->assertTrue($filter->matches($event));

        $matcher = function () {
            return false;
        };
        $filter = new DispatcherFilter(['when' => $matcher]);
        $this->assertFalse($filter->matches($event));
    }

    /**
     * Test matching with for & when option.
     *
     * @return void
     * @triggers Dispatcher.beforeDispatch $this, compact('response', 'request')
     */
    public function testMatchesWithForAndWhen()
    {
        $request = new Request(['url' => '/articles/view']);
        $response = new Response();

        $matcher = function () {
            return true;
        };
        $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request'));
        $filter = new DispatcherFilter(['for' => '/admin', 'when' => $matcher]);
        $this->assertFalse($filter->matches($event));

        $filter = new DispatcherFilter(['for' => '/articles', 'when' => $matcher]);
        $this->assertTrue($filter->matches($event));

        $matcher = function () {
            return false;
        };
        $filter = new DispatcherFilter(['for' => '/admin', 'when' => $matcher]);
        $this->assertFalse($filter->matches($event));

        $filter = new DispatcherFilter(['for' => '/articles', 'when' => $matcher]);
        $this->assertFalse($filter->matches($event));
    }

    /**
     * Test event bindings have use condition checker
     *
     * @return void
     * @triggers Dispatcher.beforeDispatch $this, compact('response', 'request')
     * @triggers Dispatcher.afterDispatch $this, compact('response', 'request')
     */
    public function testImplementedEventsMethodName()
    {
        $request = new Request(['url' => '/articles/view']);
        $response = new Response();

        $beforeEvent = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request'));
        $afterEvent = new Event('Dispatcher.afterDispatch', $this, compact('response', 'request'));

        $filter = $this->getMock('Cake\Routing\DispatcherFilter', ['beforeDispatch', 'afterDispatch']);
        $filter->expects($this->at(0))
            ->method('beforeDispatch')
            ->with($beforeEvent);
        $filter->expects($this->at(1))
            ->method('afterDispatch')
            ->with($afterEvent);

        $filter->handle($beforeEvent);
        $filter->handle($afterEvent);
    }

    /**
     * Test handle applies for conditions
     *
     * @return void
     * @triggers Dispatcher.beforeDispatch $this, compact('response', 'request')
     */
    public function testHandleAppliesFor()
    {
        $request = new Request(['url' => '/articles/view']);
        $response = new Response();

        $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request'));

        $filter = $this->getMock(
            'Cake\Routing\DispatcherFilter',
            ['beforeDispatch'],
            [['for' => '/admin']]
        );
        $filter->expects($this->never())
            ->method('beforeDispatch');

        $filter->handle($event);
    }

    /**
     * Test handle applies when conditions
     *
     * @return void
     * @triggers Dispatcher.beforeDispatch $this, compact('response', 'request')
     */
    public function testHandleAppliesWhen()
    {
        $request = new Request(['url' => '/articles/view']);
        $response = new Response();

        $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request'));
        $matcher = function () {
            return false;
        };

        $filter = $this->getMock(
            'Cake\Routing\DispatcherFilter',
            ['beforeDispatch'],
            [['when' => $matcher]]
        );
        $filter->expects($this->never())
            ->method('beforeDispatch');

        $filter->handle($event);
    }
}
Back to Top