|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony CMF package. |
| 5 | + * |
| 6 | + * (c) 2011-2015 Symfony CMF |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Cmf\Component\Routing\Tests\Mapper; |
| 13 | + |
| 14 | +use Symfony\Cmf\Component\Routing\Enhancer\ConditionalEnhancer; |
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | +use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase; |
| 17 | + |
| 18 | +class ConditionalEnhancerTest extends CmfUnitTestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var Request |
| 22 | + */ |
| 23 | + private $request; |
| 24 | + |
| 25 | + public function setUp() |
| 26 | + { |
| 27 | + $this->request = Request::create('/test'); |
| 28 | + } |
| 29 | + |
| 30 | + public function testSecondMatch() |
| 31 | + { |
| 32 | + $defaults = array('foo' => 'bar'); |
| 33 | + $expected = array('matcher' => 'found'); |
| 34 | + |
| 35 | + $enhancer1 = $this->buildMock('\Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface'); |
| 36 | + $enhancer1->expects($this->never()) |
| 37 | + ->method('enhance'); |
| 38 | + $matcher1 = $this->buildMock('\Symfony\Component\HttpFoundation\RequestMatcherInterface'); |
| 39 | + $matcher1->expects($this->once()) |
| 40 | + ->method('matches') |
| 41 | + ->with($this->request) |
| 42 | + ->will($this->returnValue(false)); |
| 43 | + |
| 44 | + $enhancer2 = $this->buildMock('\Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface'); |
| 45 | + $enhancer2->expects($this->once()) |
| 46 | + ->method('enhance') |
| 47 | + ->with($defaults, $this->request) |
| 48 | + ->will($this->returnValue($expected)); |
| 49 | + $matcher2 = $this->buildMock('\Symfony\Component\HttpFoundation\RequestMatcherInterface'); |
| 50 | + $matcher2->expects($this->once()) |
| 51 | + ->method('matches') |
| 52 | + ->with($this->request) |
| 53 | + ->will($this->returnValue(true)); |
| 54 | + |
| 55 | + $enhancer = new ConditionalEnhancer(array( |
| 56 | + array( |
| 57 | + 'matcher' => $matcher1, |
| 58 | + 'enhancer' => $enhancer1, |
| 59 | + ), |
| 60 | + array( |
| 61 | + 'matcher' => $matcher2, |
| 62 | + 'enhancer' => $enhancer2, |
| 63 | + ), |
| 64 | + )); |
| 65 | + |
| 66 | + $this->assertEquals($expected, $enhancer->enhance($defaults, $this->request)); |
| 67 | + } |
| 68 | + |
| 69 | + public function testNoMatch() |
| 70 | + { |
| 71 | + $defaults = array('foo' => 'bar'); |
| 72 | + $expected = array('matcher' => 'found'); |
| 73 | + |
| 74 | + $enhancer1 = $this->buildMock('\Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface'); |
| 75 | + $enhancer1->expects($this->never()) |
| 76 | + ->method('enhance'); |
| 77 | + $matcher1 = $this->buildMock('\Symfony\Component\HttpFoundation\RequestMatcherInterface'); |
| 78 | + $matcher1->expects($this->once()) |
| 79 | + ->method('matches') |
| 80 | + ->with($this->request) |
| 81 | + ->will($this->returnValue(false)); |
| 82 | + |
| 83 | + $enhancer = new ConditionalEnhancer(array( |
| 84 | + array( |
| 85 | + 'matcher' => $matcher1, |
| 86 | + 'enhancer' => $enhancer1, |
| 87 | + ), |
| 88 | + )); |
| 89 | + |
| 90 | + $this->assertEquals($defaults, $enhancer->enhance($defaults, $this->request)); |
| 91 | + } |
| 92 | +} |
0 commit comments