Skip to content
This repository
Browse code

Refactored the router to use composition instead of inheritance

  • Loading branch information...
commit c0e0d95134a0c60144f3f87d56ce451e12124021 1 parent 2ba57be
Christophe Coevoet authored July 31, 2011
2  DependencyInjection/BeSimpleI18nRoutingExtension.php
@@ -23,7 +23,6 @@ public function load(array $configs, ContainerBuilder $container)
23 23
         $loader->load('routing.xml');
24 24
 
25 25
         $this->addClassesToCompile(array(
26  
-            'BeSimple\\I18nRoutingBundle\\Routing\\Generator\\UrlGenerator',
27 26
             'BeSimple\\I18nRoutingBundle\\Routing\\Router',
28 27
         ));
29 28
 
@@ -67,7 +66,6 @@ public function load(array $configs, ContainerBuilder $container)
67 66
      *
68 67
      * @param type $cacheDriver
69 68
      * @param ContainerBuilder $container
70  
-     *
71 69
      * @return Definition
72 70
      */
73 71
     protected function getCacheDefinition($cacheDriver, ContainerBuilder $container)
19  DependencyInjection/Compiler/OverrideRoutingCompilerPass.php
@@ -4,6 +4,7 @@
4 4
 
5 5
 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6 6
 use Symfony\Component\DependencyInjection\ContainerBuilder;
  7
+use Symfony\Component\DependencyInjection\Alias;
7 8
 
8 9
 class OverrideRoutingCompilerPass implements CompilerPassInterface
9 10
 {
@@ -12,6 +13,22 @@ public function process(ContainerBuilder $container)
12 13
         $container->setParameter('routing.loader.xml.class', 'BeSimple\\I18nRoutingBundle\\Routing\\Loader\\XmlFileLoader');
13 14
         $container->setParameter('routing.loader.yml.class', 'BeSimple\\I18nRoutingBundle\\Routing\\Loader\\YamlFileLoader');
14 15
 
15  
-        $container->setAlias('router', 'i18n_routing.router');
  16
+        if (!$container->hasDefinition('be_simple_i18n_routing.router')) {
  17
+            return;
  18
+        }
  19
+
  20
+        if ($container->hasAlias('router')) {
  21
+            // router is an alias.
  22
+            // Register a private alias for this service to inject it as the parent
  23
+            $container->setAlias('be_simple_i18n_routing.router.parent', new Alias((string) $container->getAlias('router'), false));
  24
+        } else {
  25
+            // router is a definition.
  26
+            // Register it again as a private service to inject it as the parent
  27
+            $definition = $container->getDefinition('router');
  28
+            $definition->setPublic(false);
  29
+            $container->setDefinition('be_simple_i18n_routing.router.parent', $definition);
  30
+        }
  31
+
  32
+        $container->setAlias('router', 'be_simple_i18n_routing.router');
16 33
     }
17 34
 }
22  Resources/config/routing.xml
@@ -5,30 +5,16 @@
5 5
     xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6 6
 
7 7
     <parameters>
8  
-        <parameter key="i18n_routing.router.class">BeSimple\I18nRoutingBundle\Routing\Router</parameter>
9  
-        <parameter key="i18n_routing.generator_base.class">BeSimple\I18nRoutingBundle\Routing\Generator\UrlGenerator</parameter>
10 8
         <parameter key="i18n_routing.translator.doctrine.class">BeSimple\I18nRoutingBundle\Routing\Translator\DoctrineDBALTranslator</parameter>
11 9
         <parameter key="i18n_routing.translator.doctrine.schemalistener.class">BeSimple\I18nRoutingBundle\Routing\Translator\DoctrineDBAL\SchemaListener</parameter>
12 10
         <parameter key="i18n_routing.translator.translation.class">BeSimple\I18nRoutingBundle\Routing\Translator\TranslationTranslator</parameter>
  11
+        <parameter key="be_simple_i18n_routing.router.class">BeSimple\I18nRoutingBundle\Routing\Router</parameter>
13 12
     </parameters>
14 13
 
15 14
     <services>
16  
-        <service id="i18n_routing.router" class="%i18n_routing.router.class%">
17  
-            <argument type="service" id="i18n_routing.translator" on-invalid="ignore" />
18  
-            <argument type="service" id="service_container" />
19  
-            <argument>%router.resource%</argument>
20  
-            <argument type="collection">
21  
-                <argument key="cache_dir">%kernel.cache_dir%</argument>
22  
-                <argument key="debug">%kernel.debug%</argument>
23  
-                <argument key="generator_class">%router.options.generator_class%</argument>
24  
-                <argument key="generator_base_class">%i18n_routing.generator_base.class%</argument>
25  
-                <argument key="generator_dumper_class">%router.options.generator_dumper_class%</argument>
26  
-                <argument key="generator_cache_class">%router.options.generator.cache_class%</argument>
27  
-                <argument key="matcher_class">%router.options.matcher_class%</argument>
28  
-                <argument key="matcher_base_class">%router.options.matcher_base_class%</argument>
29  
-                <argument key="matcher_dumper_class">%router.options.matcher_dumper_class%</argument>
30  
-                <argument key="matcher_cache_class">%router.options.matcher.cache_class%</argument>
31  
-            </argument>
  15
+        <service id="be_simple_i18n_routing.router" class="%be_simple_i18n_routing.router.class%">
  16
+            <argument type="service" id="be_simple_i18n_routing.router.parent" />
  17
+            <argument type="service" id="i18n_routing.translator" on-invalid="null" />
32 18
         </service>
33 19
 
34 20
         <service id="router.cache_warmer" class="%router.cache_warmer.class%" public="false">
22  Routing/Generator/UrlGenerator.php
... ...
@@ -1,22 +0,0 @@
1  
-<?php
2  
-
3  
-namespace BeSimple\I18nRoutingBundle\Routing\Generator;
4  
-
5  
-use Symfony\Component\Routing\Generator\UrlGenerator as BaseUrlGenerator;
6  
-use Symfony\Component\Routing\Route;
7  
-use Symfony\Component\Routing\RouteCollection;
8  
-
9  
-/**
10  
- * @author Francis Besset <francis.besset@gmail.com>
11  
- */
12  
-class UrlGenerator extends BaseUrlGenerator
13  
-{
14  
-    public function generateI18n($name, $locale, $parameters = array(), $absolute = false)
15  
-    {
16  
-        try {
17  
-            return $this->generate($name.'.'.$locale, $parameters, $absolute);
18  
-        } catch (\InvalidArgumentException $e) {
19  
-            throw new \InvalidArgumentException(sprintf('I18nRoute "%s" (%s) does not exist.', $name, $locale));
20  
-        }
21  
-    }
22  
-}
66  Routing/Router.php
@@ -2,13 +2,14 @@
2 2
 
3 3
 namespace BeSimple\I18nRoutingBundle\Routing;
4 4
 
  5
+use Symfony\Component\Routing\RouterInterface;
  6
+use Symfony\Component\Routing\Exception\RouteNotFoundException;
5 7
 use BeSimple\I18nRoutingBundle\Routing\Translator\AttributeTranslatorInterface;
6 8
 use Symfony\Component\DependencyInjection\ContainerInterface;
7 9
 use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
8 10
 use Symfony\Component\Routing\RequestContext;
9  
-use Symfony\Bundle\FrameworkBundle\Routing\Router as BaseRouter;
10 11
 
11  
-class Router extends BaseRouter
  12
+class Router implements RouterInterface
12 13
 {
13 14
     /**
14 15
      * @var AttributeTranslatorInterface
@@ -16,25 +17,19 @@ class Router extends BaseRouter
16 17
     private $translator;
17 18
 
18 19
     /**
19  
-     * Constructor.
20  
-     *
21  
-     * Available options:
22  
-     *
23  
-     *   * See Router class
24  
-     *
25  
-     * @param AttributeTranslatorInterface $translator
26  
-     * @param ContainerInterface $container A ContainerInterface instance
27  
-     * @param mixed              $resource  The main resource to load
28  
-     * @param array              $options   An array of options
29  
-     * @param array              $context   The context
30  
-     * @param array              $defaults  The default values
  20
+     * @var RouterInterface
  21
+     */
  22
+    private $router;
  23
+
  24
+    /**
  25
+     * Constructor
31 26
      *
32  
-     * @throws \InvalidArgumentException When unsupported option is provided
  27
+     * @param \Symfony\Component\Routing\RouterInterface $router
  28
+     * @param Translator\AttributeTranslatorInterface|null $translator
33 29
      */
34  
-    public function __construct(AttributeTranslatorInterface $translator = null, ContainerInterface $container, $resource, array $options = array(), RequestContext $context = null, array $defaults = array())
  30
+    public function __construct(RouterInterface $router, AttributeTranslatorInterface $translator = null)
35 31
     {
36  
-        parent::__construct($container, $resource, $options, $context, $defaults);
37  
-
  32
+        $this->router = $router;
38 33
         $this->translator = $translator;
39 34
     }
40 35
 
@@ -76,14 +71,14 @@ public function generate($name, $parameters = array(), $absolute = false)
76 71
         }
77 72
 
78 73
         try {
79  
-            return parent::generate($name, $parameters, $absolute);
80  
-        } catch (\InvalidArgumentException $e) {
  74
+            return $this->router->generate($name, $parameters, $absolute);
  75
+        } catch (RouteNotFoundException $e) {
81 76
             if ($this->getContext()->hasParameter('_locale')) {
82 77
                 // at this point here we would never have $parameters['translate'] due to condition before
83 78
                 return $this->generateI18n($name, $this->getContext()->getParameter('_locale'), $parameters, $absolute);
84  
-            } else {
85  
-                throw $e;
86 79
             }
  80
+
  81
+            throw $e;
87 82
         }
88 83
     }
89 84
 
@@ -92,7 +87,7 @@ public function generate($name, $parameters = array(), $absolute = false)
92 87
      */
93 88
     public function match($url)
94 89
     {
95  
-        $match = parent::match($url);
  90
+        $match = $this->router->match($url);
96 91
 
97 92
         // if a _locale parameter isset remove the .locale suffix that is appended to each route in I18nRoute
98 93
         if (!empty($match['_locale']) && preg_match('#^(.+)\.'.preg_quote($match['_locale'], '#').'+$#', $match['_route'], $route)) {
@@ -111,6 +106,21 @@ public function match($url)
111 106
         return $match;
112 107
     }
113 108
 
  109
+    public function getRouteCollection()
  110
+    {
  111
+        return $this->router->getRouteCollection();
  112
+    }
  113
+
  114
+    public function setContext(RequestContext $context)
  115
+    {
  116
+        $this->router->setContext($context);
  117
+    }
  118
+
  119
+    public function getContext()
  120
+    {
  121
+        return $this->router->getContext();
  122
+    }
  123
+
114 124
     /**
115 125
      * Generates a I18N URL from the given parameter
116 126
      *
@@ -121,10 +131,14 @@ public function match($url)
121 131
      *
122 132
      * @return string The generated URL
123 133
      *
124  
-     * @throws \InvalidArgumentException When the route doesn't exists
  134
+     * @throws RouteNotFoundException When the route doesn't exists
125 135
      */
126  
-    protected function generateI18n($name, $locale, $parameters, $absolute)
  136
+    private function generateI18n($name, $locale, $parameters, $absolute)
127 137
     {
128  
-        return $this->getGenerator()->generateI18n($name, $locale, $parameters, $absolute);
  138
+        try {
  139
+            return $this->router->generate($name.'.'.$locale, $parameters, $absolute);
  140
+        } catch (RouteNotFoundException $e) {
  141
+            throw new RouteNotFoundException(sprintf('I18nRoute "%s" (%s) does not exist.', $name, $locale));
  142
+        }
129 143
     }
130 144
 }
80  Tests/Routing/Generator/UrlGeneratorTest.php
... ...
@@ -1,80 +0,0 @@
1  
-<?php
2  
-
3  
-namespace BeSimple\I18nRoutingBundle\Tests\Routing\Generator;
4  
-
5  
-use BeSimple\I18nRoutingBundle\Routing\Generator\UrlGenerator;
6  
-use BeSimple\I18nRoutingBundle\Routing\I18nRoute;
7  
-use Symfony\Component\Routing\RouteCollection;
8  
-use Symfony\Component\Routing\RequestContext;
9  
-
10  
-/**
11  
- * @author Francis Besset <francis.besset@gmail.com>
12  
- */
13  
-class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
14  
-{
15  
-    public function testException()
16  
-    {
17  
-        $generator = $this->getGenerator();
18  
-
19  
-        try {
20  
-            $generator->generateI18n('test', 'fr', array());
21  
-
22  
-            $this->fail('An expected exception has not been raised.');
23  
-        } catch (\InvalidArgumentException $e) {
24  
-            $this->assertEquals('I18nRoute "test" (fr) does not exist.', $e->getMessage());
25  
-        }
26  
-    }
27  
-
28  
-    /**
29  
-     * @dataProvider provideI18nRouteData
30  
-     */
31  
-    public function testGenerate($name, $locales)
32  
-    {
33  
-        $route = $this->getRoute($name, $locales);
34  
-        $generator = $this->getGenerator($route->getCollection());
35  
-
36  
-        foreach ($locales as $locale => $pattern) {
37  
-            $this->assertEquals($pattern, $generator->generateI18n($name, $locale));
38  
-        }
39  
-    }
40  
-
41  
-    public function provideI18nRouteData()
42  
-    {
43  
-        return array(
44  
-            array(
45  
-                'test',
46  
-                array(
47  
-                    'en' => '/welcome',
48  
-                    'fr' => '/bienvenue',
49  
-                    'de' => '/willkommen',
50  
-                ),
51  
-            ),
52  
-
53  
-            array(
54  
-                'test_bis',
55  
-                array(
56  
-                    'en_GB' => '/welcome',
57  
-                    'fr_FR' => '/bienvenue',
58  
-                    'de_DE' => '/willkommen',
59  
-                ),
60  
-            ),
61  
-        );
62  
-    }
63  
-
64  
-    private function getGenerator(RouteCollection $collection = null)
65  
-    {
66  
-        $collection = $collection ?: $this->getCollection();
67  
-
68  
-        return new UrlGenerator($collection, new RequestContext());
69  
-    }
70  
-
71  
-    private function getCollection()
72  
-    {
73  
-        return new RouteCollection();
74  
-    }
75  
-
76  
-    public function getRoute($name, $locales)
77  
-    {
78  
-        return new I18nRoute($name, $locales);
79  
-    }
80  
-}
197  Tests/Routing/RouterTest.php
@@ -3,60 +3,54 @@
3 3
 namespace BeSimple\I18nRoutingBundle\Tests\Routing;
4 4
 
5 5
 use BeSimple\I18nRoutingBundle\Routing\Router;
6  
-use BeSimple\I18nRoutingBundle\Routing\I18nRoute;
7  
-use Symfony\Component\HttpFoundation\Session;
8  
-use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
9 6
 
10 7
 class RouterTest extends \PHPUnit_Framework_TestCase
11 8
 {
12  
-    private $router;
13  
-    private $translator;
14  
-
15  
-    public function setUp()
16  
-    {
17  
-        $this->translator = $this->getMock('BeSimple\I18nRoutingBundle\Routing\Translator\AttributeTranslatorInterface');
18  
-
19  
-        $container    = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
20  
-        $this->router = $this->getMock('BeSimple\I18nRoutingBundle\Routing\Router', array('getMatcher', 'getGenerator'), array(
21  
-            $this->translator,
22  
-            $container,
23  
-            null,
24  
-        ));
25  
-    }
26  
-
27 9
     public function testMatchLocaleRoute()
28 10
     {
29  
-        $route = new I18nRoute('test', array('en' => '/foo', 'de' => 'bar'));
30  
-        $this->expectMatchCollection($route->getCollection());
  11
+        $parentRouter = $this->getMock('Symfony\Component\Routing\RouterInterface');
  12
+        $parentRouter
  13
+            ->expects($this->at(0))
  14
+            ->method('match')
  15
+            ->with($this->equalTo('/foo'))
  16
+            ->will($this->returnValue(array('_route' => 'test.en', '_locale' => 'en')))
  17
+        ;
  18
+        $parentRouter
  19
+            ->expects($this->at(1))
  20
+            ->method('match')
  21
+            ->with($this->equalTo('/bar'))
  22
+            ->will($this->returnValue(array('_route' => 'test.de', '_locale' => 'de')))
  23
+        ;
31 24
 
32  
-        $data = $this->router->match('/foo');
  25
+        $router = new Router($parentRouter);
33 26
 
  27
+        $data = $router->match('/foo');
34 28
         $this->assertEquals('en', $data['_locale']);
35 29
         $this->assertEquals('test', $data['_route']);
36 30
 
37  
-        $data = $this->router->match('/bar');
38  
-
  31
+        $data = $router->match('/bar');
39 32
         $this->assertEquals('de', $data['_locale']);
40 33
         $this->assertEquals('test', $data['_route']);
41 34
     }
42 35
 
43 36
     public function testMatchTranslateStringField()
44 37
     {
45  
-        $requestName  = "beberlei";
46  
-        $originalName = "Benjamin";
47  
-
48  
-        $route = new I18nRoute('test', array('en' => '/foo/{name}'), array('_translate' => 'name'));
49  
-        $this->expectMatchCollection($route->getCollection());
50  
-
51  
-        $this->translator
52  
-             ->expects($this->once())
53  
-             ->method('translate')
54  
-             ->with($this->equalTo('test'), $this->equalTo('en'), $this->equalTo('name'), $this->equalTo($requestName))
55  
-             ->will($this->returnValue($originalName))
  38
+        $parentRouter = $this->getMock('Symfony\Component\Routing\RouterInterface');
  39
+        $parentRouter->expects($this->any())
  40
+            ->method('match')
  41
+            ->with($this->equalTo('/foo/beberlei'))
  42
+            ->will($this->returnValue(array('_route' => 'test.en', '_locale' => 'en', '_translate' => 'name', 'name' => 'beberlei')))
  43
+        ;
  44
+        $translator = $this->getMock('BeSimple\I18nRoutingBundle\Routing\Translator\AttributeTranslatorInterface');
  45
+        $translator
  46
+            ->expects($this->once())
  47
+            ->method('translate')
  48
+            ->with($this->equalTo('test'), $this->equalTo('en'), $this->equalTo('name'), $this->equalTo('beberlei'))
  49
+            ->will($this->returnValue('Benjamin'))
56 50
         ;
  51
+        $router = new Router($parentRouter, $translator);
57 52
 
58  
-        $data = $this->router->match('/foo/beberlei');
59  
-
  53
+        $data = $router->match('/foo/beberlei');
60 54
         $this->assertEquals('en', $data['_locale']);
61 55
         $this->assertEquals('test', $data['_route']);
62 56
         $this->assertEquals('Benjamin', $data['name']);
@@ -64,85 +58,60 @@ public function testMatchTranslateStringField()
64 58
 
65 59
     public function testGenerateI18n()
66 60
     {
67  
-        $absolute  = false;
68  
-        $generator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface', array('generateI18n', 'generate', 'setContext', 'getContext'));
69  
-        $generator->expects($this->once())
70  
-            ->method('generateI18n')
71  
-            ->with($this->equalTo('test_route'), $this->equalTo('en'), $this->equalTo(array('foo' => 'bar')), $this->equalTo($absolute))
72  
-        ;
73  
-
74  
-        $this->router
75  
-            ->expects($this->once())
76  
-            ->method('getGenerator')
77  
-            ->will($this->returnValue($generator))
  61
+        $parentRouter = $this->getMock('Symfony\Component\Routing\RouterInterface');
  62
+        $parentRouter->expects($this->once())
  63
+            ->method('generate')
  64
+            ->with($this->equalTo('test_route.en'), $this->equalTo(array('foo' => 'bar')), $this->equalTo(false))
78 65
         ;
  66
+        $router = new Router($parentRouter);
79 67
 
80  
-        $this->router->generate('test_route', array('foo' => 'bar', 'locale' => 'en'), $absolute);
  68
+        $router->generate('test_route', array('foo' => 'bar', 'locale' => 'en'), false);
81 69
     }
82 70
 
83 71
     public function testGenerateDefault()
84 72
     {
85  
-        $absolute  = false;
86  
-        $generator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface', array('generateI18n', 'generate', 'setContext', 'getContext'));
87  
-        $generator->expects($this->once())
  73
+        $parentRouter = $this->getMock('Symfony\Component\Routing\RouterInterface');
  74
+        $parentRouter->expects($this->once())
88 75
             ->method('generate')
89  
-            ->with($this->equalTo('test_route'), $this->equalTo(array('foo' => 'bar')), $this->equalTo($absolute))
90  
-        ;
91  
-
92  
-        $this->router
93  
-            ->expects($this->once())
94  
-            ->method('getGenerator')
95  
-            ->will($this->returnValue($generator))
  76
+            ->with($this->equalTo('test_route'), $this->equalTo(array('foo' => 'bar')), $this->equalTo(false))
96 77
         ;
  78
+        $router = new Router($parentRouter);
97 79
 
98  
-        $this->router->generate('test_route', array('foo' => 'bar'), $absolute);
  80
+        $router->generate('test_route', array('foo' => 'bar'), false);
99 81
     }
100 82
 
101 83
     public function testGenerateI18nTranslated()
102 84
     {
103  
-        $originalValue  = 'bar';
104  
-        $localizedValue = 'baz';
105  
-
106  
-        $absolute  = false;
107  
-        $generator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface', array('generateI18n', 'generate', 'setContext', 'getContext'));
108  
-        $generator->expects($this->once())
109  
-            ->method('generateI18n')
110  
-            ->with($this->equalTo('test_route'), $this->equalTo('en'), $this->equalTo(array('foo' => $localizedValue)), $this->equalTo($absolute))
  85
+        $parentRouter = $this->getMock('Symfony\Component\Routing\RouterInterface');
  86
+        $parentRouter->expects($this->once())
  87
+            ->method('generate')
  88
+            ->with($this->equalTo('test_route.en'), $this->equalTo(array('foo' => 'baz')), $this->equalTo(false))
111 89
         ;
112  
-
113  
-        $this->translator
  90
+        $translator = $this->getMock('BeSimple\I18nRoutingBundle\Routing\Translator\AttributeTranslatorInterface');
  91
+        $translator
114 92
             ->expects($this->once())
115 93
             ->method('reverseTranslate')
116  
-            ->with($this->equalTo('test_route'), $this->equalTo('en'), $this->equalTo('foo'), $this->equalTo($originalValue))
117  
-            ->will($this->returnValue($localizedValue))
  94
+            ->with($this->equalTo('test_route'), $this->equalTo('en'), $this->equalTo('foo'), $this->equalTo('bar'))
  95
+            ->will($this->returnValue('baz'))
118 96
         ;
  97
+        $router = new Router($parentRouter, $translator);
119 98
 
120  
-        $this->router
121  
-            ->expects($this->once())
122  
-            ->method('getGenerator')
123  
-            ->will($this->returnValue($generator))
124  
-        ;
125  
-
126  
-        $this->router->generate('test_route', array('foo' => $originalValue, 'translate' => 'foo', 'locale' => 'en'), $absolute);
  99
+        $router->generate('test_route', array('foo' => 'bar', 'translate' => 'foo', 'locale' => 'en'), false);
127 100
     }
128 101
 
129 102
     public function testGenerateI18nTranslatedContextLocale()
130 103
     {
131  
-        $originalValue  = 'bar';
132  
-        $localizedValue = 'baz';
133  
-
134  
-        $absolute  = false;
135  
-        $generator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface', array('generateI18n', 'generate', 'setContext', 'getContext'));
136  
-        $generator->expects($this->once())
137  
-            ->method('generateI18n')
138  
-            ->with($this->equalTo('test_route'), $this->equalTo('en'), $this->equalTo(array('foo' => $localizedValue)), $this->equalTo($absolute))
  104
+        $parentRouter = $this->getMock('Symfony\Component\Routing\RouterInterface');
  105
+        $parentRouter->expects($this->once())
  106
+            ->method('generate')
  107
+            ->with($this->equalTo('test_route.fr'), $this->equalTo(array('foo' => 'baz')), $this->equalTo(false))
139 108
         ;
140  
-
141  
-        $this->translator
  109
+        $translator = $this->getMock('BeSimple\I18nRoutingBundle\Routing\Translator\AttributeTranslatorInterface');
  110
+        $translator
142 111
             ->expects($this->once())
143 112
             ->method('reverseTranslate')
144  
-            ->with($this->equalTo('test_route'), $this->equalTo('en'), $this->equalTo('foo'), $this->equalTo($originalValue))
145  
-            ->will($this->returnValue($localizedValue))
  113
+            ->with($this->equalTo('test_route'), $this->equalTo('fr'), $this->equalTo('foo'), $this->equalTo('bar'))
  114
+            ->will($this->returnValue('baz'))
146 115
         ;
147 116
 
148 117
         $context = $this->getMockBuilder('Symfony\Component\Routing\RequestContext')
@@ -158,25 +127,17 @@ public function testGenerateI18nTranslatedContextLocale()
158 127
         $context->expects($this->once())
159 128
             ->method('getParameter')
160 129
             ->with($this->equalTo('_locale'))
161  
-            ->will($this->returnValue('en'))
162  
-        ;
163  
-        $this->router = $this->getMock('BeSimple\I18nRoutingBundle\Routing\Router', array('getMatcher', 'getGenerator', 'getContext'), array(
164  
-            $this->translator,
165  
-            $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'),
166  
-            null,
167  
-        ));
168  
-        $this->router
169  
-            ->expects($this->once())
170  
-            ->method('getGenerator')
171  
-            ->will($this->returnValue($generator))
  130
+            ->will($this->returnValue('fr'))
172 131
         ;
173  
-        $this->router
  132
+        $parentRouter
174 133
             ->expects($this->any())
175 134
             ->method('getContext')
176 135
             ->will($this->returnValue($context))
177 136
         ;
178 137
 
179  
-        $this->router->generate('test_route', array('foo' => $originalValue, 'translate' => 'foo'), $absolute);
  138
+        $router = new Router($parentRouter, $translator);
  139
+
  140
+        $router->generate('test_route', array('foo' => 'bar', 'translate' => 'foo'), false);
180 141
     }
181 142
 
182 143
     /**
@@ -184,20 +145,26 @@ public function testGenerateI18nTranslatedContextLocale()
184 145
      */
185 146
     public function testGenerateI18nTranslatedWithoutLocale()
186 147
     {
187  
-        $originalValue  = 'bar';
188  
-        $absolute  = false;
  148
+        $parentRouter = $this->getMock('Symfony\Component\Routing\RouterInterface');
189 149
 
190  
-        $this->router->generate('test_route', array('foo' => $originalValue, 'translate' => 'foo'), $absolute);
191  
-    }
  150
+        $context = $this->getMockBuilder('Symfony\Component\Routing\RequestContext')
  151
+            ->disableOriginalConstructor()
  152
+            ->setMethods(array('getParameter', 'hasParameter'))
  153
+            ->getMock()
  154
+        ;
  155
+        $context->expects($this->once())
  156
+            ->method('hasParameter')
  157
+            ->with($this->equalTo('_locale'))
  158
+            ->will($this->returnValue(false))
  159
+        ;
  160
+        $parentRouter
  161
+            ->expects($this->any())
  162
+            ->method('getContext')
  163
+            ->will($this->returnValue($context))
  164
+        ;
192 165
 
193  
-    public function expectMatchCollection($collection)
194  
-    {
195  
-        $context = $this->getMock('Symfony\Component\Routing\RequestContext', array(), array(), '', false);
196  
-        $matcher = new \Symfony\Component\Routing\Matcher\UrlMatcher($collection, $context);
  166
+        $router = new Router($parentRouter);
197 167
 
198  
-        $this->router->expects($this->any())
199  
-            ->method('getMatcher')
200  
-            ->will($this->returnValue($matcher))
201  
-        ;
  168
+        $router->generate('test_route', array('foo' => 'bar', 'translate' => 'foo'), false);
202 169
     }
203 170
 }

0 notes on commit c0e0d95

Please sign in to comment.
Something went wrong with that request. Please try again.