3 namespace Drupal\Tests\Core\Enhancer;
5 use Drupal\Core\Routing\Enhancer\ParamConversionEnhancer;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
8 use Symfony\Component\HttpFoundation\ParameterBag;
9 use Symfony\Component\HttpFoundation\Request;
10 use Symfony\Component\Routing\Route;
13 * @coversDefaultClass \Drupal\Core\Routing\Enhancer\ParamConversionEnhancer
16 class ParamConversionEnhancerTest extends UnitTestCase {
19 * @var \Drupal\Core\Routing\Enhancer\ParamConversionEnhancer
21 protected $paramConversionEnhancer;
24 * @var \Drupal\Core\ParamConverter\ParamConverterManagerInterface|\PHPUnit_Framework_MockObject_MockObject
26 protected $paramConverterManager;
31 protected function setUp() {
34 $this->paramConverterManager = $this->getMock('Drupal\Core\ParamConverter\ParamConverterManagerInterface');
35 $this->paramConversionEnhancer = new ParamConversionEnhancer($this->paramConverterManager);
41 public function testEnhance() {
42 $route = new Route('/test/{id}/{literal}/{null}');
46 'literal' => 'this is a literal',
50 RouteObjectInterface::ROUTE_OBJECT => $route,
53 $expected = $defaults;
54 $expected['id'] = 'something_better!';
55 $expected['_raw_variables'] = new ParameterBag($raw_variables);
57 $this->paramConverterManager->expects($this->once())
59 ->with($this->isType('array'))
60 ->will($this->returnValue($expected));
62 $result = $this->paramConversionEnhancer->enhance($defaults, new Request());
64 $this->assertEquals($expected, $result);
66 // Now run with the results as the new defaults to ensure that the
67 // conversion is just run once.
68 $result = $this->paramConversionEnhancer->enhance($result, new Request());
70 $this->assertEquals($expected, $result);
74 * @covers ::copyRawVariables
76 public function testCopyRawVariables() {
77 $route = new Route('/test/{id}');
79 RouteObjectInterface::ROUTE_OBJECT => $route,
82 // Set one default to mirror another by reference.
83 $defaults['bar'] = &$defaults['id'];
84 $this->paramConverterManager->expects($this->any())
86 ->with($this->isType('array'))
87 ->will($this->returnCallback(function ($defaults) {
88 // Convert the mirrored default to another value.
89 $defaults['bar'] = '2';
92 $expected = new ParameterBag(['id' => 1]);
93 $result = $this->paramConversionEnhancer->enhance($defaults, new Request());
94 $this->assertEquals($result['_raw_variables'], $expected);