|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace unit; |
| 4 | + |
| 5 | +use Symfony\Component\HttpFoundation\Request; |
| 6 | + |
| 7 | +class MatcherTest extends \PHPUnit_Framework_TestCase |
| 8 | +{ |
| 9 | + public function testPathMatching() |
| 10 | + { |
| 11 | + $this->matcher = new \Dflydev\Stack\Firewall\Matcher([ |
| 12 | + ['path' => '/'], |
| 13 | + ['path' => '/users/', 'exact_match' => true], |
| 14 | + ['path' => '/messages/', 'method' => 'POST'], |
| 15 | + ['path' => '/cats/', 'method' => 'PUT', 'exact_match' => true], |
| 16 | + ]); |
| 17 | + |
| 18 | + $this->assertMatch('/', Request::create('/test')); |
| 19 | + $this->assertMatch('/', Request::create('/')); |
| 20 | + |
| 21 | + $this->assertMatch('/users/', Request::create('/users/')); |
| 22 | + $this->assertMatch('/', Request::create('/users/123')); // no matching for exact_match |
| 23 | + |
| 24 | + $this->assertMatch('/messages/', Request::create('/messages/123', 'POST')); |
| 25 | + $this->assertMatch('/messages/', Request::create('/messages/', 'POST')); |
| 26 | + $this->assertMatch('/', Request::create('/messages/', 'GET')); |
| 27 | + |
| 28 | + $this->assertMatch('/cats/', Request::create('/cats/', 'PUT')); |
| 29 | + $this->assertMatch('/', Request::create('/cats/123', 'PUT')); |
| 30 | + $this->assertMatch('/', Request::create('/cats/', 'DELETE')); |
| 31 | + $this->assertMatch('/', Request::create('/cats/123', 'DELETE')); |
| 32 | + } |
| 33 | + |
| 34 | + public function testNotMatching() |
| 35 | + { |
| 36 | + $matcher = new \Dflydev\Stack\Firewall\Matcher([['path' => '/api/']]); |
| 37 | + $this->assertNull($matcher->match(Request::create('/blah'))); |
| 38 | + } |
| 39 | + |
| 40 | + public function testSpecificMethodMatchingOrder() |
| 41 | + { |
| 42 | + $this->matcher = new \Dflydev\Stack\Firewall\Matcher([ |
| 43 | + ['path' => '/users/', 'method' => 'POST'], |
| 44 | + ['path' => '/users/'], |
| 45 | + ]); |
| 46 | + |
| 47 | + $this->assertMatch('/users/', Request::create('/users/123')); |
| 48 | + } |
| 49 | + |
| 50 | + protected function assertMatch($expected, $request) |
| 51 | + { |
| 52 | + $this->assertEquals($expected, $this->matcher->match($request)['path']); |
| 53 | + } |
| 54 | +} |
0 commit comments