|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\Component\HttpKernel\Tests\Controller; |
| 13 | + |
| 14 | +use Symfony\Component\HttpKernel\Controller\ArgumentResolverManager; |
| 15 | + |
| 16 | +class ArgumentResolverManagerTest extends \PHPUnit_Framework_TestCase |
| 17 | +{ |
| 18 | + protected $manager; |
| 19 | + protected $resolver1; |
| 20 | + protected $resolver2; |
| 21 | + protected $request; |
| 22 | + |
| 23 | + public function setUp() |
| 24 | + { |
| 25 | + $this->manager = new ArgumentResolverManager(); |
| 26 | + $this->resolver1 = $this->getMock('Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface'); |
| 27 | + $this->resolver2 = $this->getMock('Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface'); |
| 28 | + $this->manager->addResolver($this->resolver1); |
| 29 | + $this->manager->addResolver($this->resolver2); |
| 30 | + |
| 31 | + $this->request = $this->getMock('Symfony\Component\HttpFoundation\Request'); |
| 32 | + } |
| 33 | + |
| 34 | + public function testGetArgumentsFirstResolverAccepts() |
| 35 | + { |
| 36 | + $this->resolver1->expects($this->any())->method('accepts')->will($this->returnValue(true)); |
| 37 | + $this->resolver1->expects($this->any()) |
| 38 | + ->method('resolve') |
| 39 | + ->will($this->returnValue('resolved_value')); |
| 40 | + |
| 41 | + $controller = $this->getControllerWithOneArgument(); |
| 42 | + |
| 43 | + $arguments = $this->manager->getArguments($this->request, $controller); |
| 44 | + $this->assertEquals(array('resolved_value'), $arguments); |
| 45 | + } |
| 46 | + |
| 47 | + public function testGetArgumentsSecondResolverAccepts() |
| 48 | + { |
| 49 | + $this->resolver1->expects($this->any())->method('accepts')->will($this->returnValue(false)); |
| 50 | + $this->resolver2->expects($this->any())->method('accepts')->will($this->returnValue(true)); |
| 51 | + $this->resolver2->expects($this->any()) |
| 52 | + ->method('resolve') |
| 53 | + ->will($this->returnValue('resolved_value')); |
| 54 | + |
| 55 | + $controller = $this->getControllerWithOneArgument(); |
| 56 | + |
| 57 | + $arguments = $this->manager->getArguments($this->request, $controller); |
| 58 | + $this->assertEquals(array('resolved_value'), $arguments); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @expectedException RuntimeException |
| 63 | + */ |
| 64 | + public function testGetArgumentsFailsIfNoResolverAccepts() |
| 65 | + { |
| 66 | + $this->resolver1->expects($this->any())->method('accepts')->will($this->returnValue(false)); |
| 67 | + $this->resolver2->expects($this->any())->method('accepts')->will($this->returnValue(false)); |
| 68 | + |
| 69 | + $controller = $this->getControllerWithOneArgument(); |
| 70 | + $this->manager->getArguments($this->request, $controller); |
| 71 | + } |
| 72 | + |
| 73 | + public function testGetArgumentResolvingMultipleArguments() |
| 74 | + { |
| 75 | + $this->resolver1->expects($this->any()) |
| 76 | + ->method('accepts') |
| 77 | + ->will($this->onConsecutiveCalls(false, true, true)); |
| 78 | + $this->resolver1->expects($this->any()) |
| 79 | + ->method('resolve') |
| 80 | + ->will($this->onConsecutiveCalls('1st resolved by 1', '2nd resolved by 1')); |
| 81 | + |
| 82 | + $this->resolver2->expects($this->any()) |
| 83 | + ->method('accepts') |
| 84 | + ->will($this->onConsecutiveCalls(true, false, true)); |
| 85 | + $this->resolver2->expects($this->any()) |
| 86 | + ->method('resolve') |
| 87 | + ->will($this->onConsecutiveCalls('1st resolved by 2', '2nd resolved by 2')); |
| 88 | + |
| 89 | + $controller = function ($a, $b, $c) { }; |
| 90 | + |
| 91 | + $arguments = $this->manager->getArguments($this->request, $controller); |
| 92 | + $this->assertEquals(array('1st resolved by 2<
5ABE
/span>', '1st resolved by 1', '2nd resolved by 1'), $arguments); |
| 93 | + } |
| 94 | + |
| 95 | + protected function getControllerWithOneArgument() |
| 96 | + { |
| 97 | + return function ($a) { }; |
| 98 | + } |
| 99 | +} |
0 commit comments