Selaa lähdekoodia

[Routing] add unit test for PhpGeneratorDumper class

Leszek 14 vuotta sitten
vanhempi
commit
1059f7cb82

+ 43 - 0
tests/Symfony/Tests/Component/Routing/Fixtures/dumper/php_generator1.php

@@ -0,0 +1,43 @@
+<?php
+
+/**
+ * ProjectUrlGenerator
+ *
+ * This class has been auto-generated
+ * by the Symfony Routing Component.
+ */
+class ProjectUrlGenerator extends Symfony\Component\Routing\Generator\UrlGenerator
+{
+    static protected $declaredRouteNames = array(
+       'Test' => true,
+    );
+
+    /**
+     * Constructor.
+     */
+    public function __construct(array $context = array(), array $defaults = array())
+    {
+        $this->context = $context;
+        $this->defaults = $defaults;
+    }
+
+    public function generate($name, array $parameters, $absolute = false)
+    {
+        if (!isset(self::$declaredRouteNames[$name])) {
+            throw new \InvalidArgumentException(sprintf('Route "%s" does not exist.', $name));
+        }
+
+        $escapedName = str_replace('.', '__', $name);
+
+        list($variables, $defaults, $requirements, $tokens) = $this->{'get'.$escapedName.'RouteInfo'}();
+
+        return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute);
+    }
+
+    protected function getTestRouteInfo()
+    {
+        $defaults = $this->defaults;
+
+        return array(array (), $defaults, array (), array (  0 =>   array (    0 => 'text',    1 => '/',    2 => 'testing',    3 => NULL,  ),));
+    }
+}

+ 37 - 0
tests/Symfony/Tests/Component/Routing/Fixtures/dumper/php_generator2.php

@@ -0,0 +1,37 @@
+<?php
+
+/**
+ * ProjectUrlGenerator
+ *
+ * This class has been auto-generated
+ * by the Symfony Routing Component.
+ */
+class ProjectUrlGenerator extends Symfony\Component\Routing\Generator\UrlGenerator
+{
+    static protected $declaredRouteNames = array(
+
+    );
+
+    /**
+     * Constructor.
+     */
+    public function __construct(array $context = array(), array $defaults = array())
+    {
+        $this->context = $context;
+        $this->defaults = $defaults;
+    }
+
+    public function generate($name, array $parameters, $absolute = false)
+    {
+        if (!isset(self::$declaredRouteNames[$name])) {
+            throw new \InvalidArgumentException(sprintf('Route "%s" does not exist.', $name));
+        }
+
+        $escapedName = str_replace('.', '__', $name);
+
+        list($variables, $defaults, $requirements, $tokens) = $this->{'get'.$escapedName.'RouteInfo'}();
+
+        return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute);
+    }
+
+}

+ 43 - 0
tests/Symfony/Tests/Component/Routing/Fixtures/dumper/php_generator3.php

@@ -0,0 +1,43 @@
+<?php
+
+/**
+ * FooGenerator
+ *
+ * This class has been auto-generated
+ * by the Symfony Routing Component.
+ */
+class FooGenerator extends FooGeneratorBase
+{
+    static protected $declaredRouteNames = array(
+       'Test' => true,
+    );
+
+    /**
+     * Constructor.
+     */
+    public function __construct(array $context = array(), array $defaults = array())
+    {
+        $this->context = $context;
+        $this->defaults = $defaults;
+    }
+
+    public function generate($name, array $parameters, $absolute = false)
+    {
+        if (!isset(self::$declaredRouteNames[$name])) {
+            throw new \InvalidArgumentException(sprintf('Route "%s" does not exist.', $name));
+        }
+
+        $escapedName = str_replace('.', '__', $name);
+
+        list($variables, $defaults, $requirements, $tokens) = $this->{'get'.$escapedName.'RouteInfo'}();
+
+        return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute);
+    }
+
+    protected function getTestRouteInfo()
+    {
+        $defaults = $this->defaults;
+
+        return array(array (), $defaults, array (), array (  0 =>   array (    0 => 'text',    1 => '/',    2 => 'testing',    3 => NULL,  ),));
+    }
+}

+ 44 - 0
tests/Symfony/Tests/Component/Routing/Fixtures/dumper/php_generator4.php

@@ -0,0 +1,44 @@
+<?php
+
+/**
+ * ProjectUrlGenerator
+ *
+ * This class has been auto-generated
+ * by the Symfony Routing Component.
+ */
+class ProjectUrlGenerator extends Symfony\Component\Routing\Generator\UrlGenerator
+{
+    static protected $declaredRouteNames = array(
+       'Test' => true,
+    );
+
+    /**
+     * Constructor.
+     */
+    public function __construct(array $context = array(), array $defaults = array())
+    {
+        $this->context = $context;
+        $this->defaults = $defaults;
+    }
+
+    public function generate($name, array $parameters, $absolute = false)
+    {
+        if (!isset(self::$declaredRouteNames[$name])) {
+            throw new \InvalidArgumentException(sprintf('Route "%s" does not exist.', $name));
+        }
+
+        $escapedName = str_replace('.', '__', $name);
+
+        list($variables, $defaults, $requirements, $tokens) = $this->{'get'.$escapedName.'RouteInfo'}();
+
+        return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute);
+    }
+
+    protected function getTestRouteInfo()
+    {
+        $defaults = $this->defaults;
+        $defaults['foo'] = 'bar';
+
+        return array(array (  'foo' => '{foo}',), $defaults, array (), array (  0 =>   array (    0 => 'variable',    1 => '/',    2 => '{foo}',    3 => 'foo',  ),  1 =>   array (    0 => 'text',    1 => '/',    2 => 'testing',    3 => NULL,  ),));
+    }
+}

+ 73 - 0
tests/Symfony/Tests/Component/Routing/Generator/Dumper/PhpGeneratorDumperTest.php

@@ -0,0 +1,73 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Tests\Component\Routing\Generator\Dumper\PhpGeneratorDumper;
+
+use Symfony\Component\Routing\RouteCollection;
+use Symfony\Component\Routing\Route;
+use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper;
+
+class PhpGeneratorDumperTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * @var RouteCollection
+     */
+    private $routeCollection;
+    
+    /**
+     * @var PhpGeneratorDumper
+     */
+    private $generatorDumper;
+    
+    /**
+     * @var string
+     */
+    static protected $fixturesPath;
+
+    static public function setUpBeforeClass()
+    {
+        self::$fixturesPath = realpath(__DIR__.'/../../Fixtures/');
+    }
+    
+    protected function setUp()
+    {
+        parent::setUp();
+
+        $this->routeCollection = new RouteCollection();
+        $this->generatorDumper = new PhpGeneratorDumper($this->routeCollection);
+    }
+
+    public function testDumpWithRoutes()
+    {
+        $this->routeCollection->add('Test', new Route('/testing'));
+        
+        $this->assertStringEqualsFile(self::$fixturesPath.'/dumper/php_generator1.php', $this->generatorDumper->dump()); 
+    }
+    
+    public function testDumpWithoutRoutes()
+    {
+        $this->assertStringEqualsFile(self::$fixturesPath.'/dumper/php_generator2.php', $this->generatorDumper->dump()); 
+    }
+    
+    public function testDumpWithClassNamesOptions()
+    {
+        $this->routeCollection->add('Test', new Route('/testing'));
+        
+        $this->assertStringEqualsFile(self::$fixturesPath.'/dumper/php_generator3.php', $this->generatorDumper->dump(array('class' => 'FooGenerator', 'base_class' => 'FooGeneratorBase')));
+    }
+
+    public function testDumpForRouteWithDefaults()
+    {
+        $this->routeCollection->add('Test', new Route('/testing/{foo}', array('foo' => 'bar')));
+        
+        $this->assertStringEqualsFile(self::$fixturesPath.'/dumper/php_generator4.php', $this->generatorDumper->dump());
+    }
+}