Browse Source

[Routing] modified unit test for PhpGeneratorDumper class

Leszek 14 năm trước cách đây
mục cha
commit
2b820ac9d0

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

@@ -1,43 +0,0 @@
-<?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,  ),));
-    }
-}

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

@@ -1,37 +0,0 @@
-<?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);
-    }
-
-}

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

@@ -1,43 +0,0 @@
-<?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,  ),));
-    }
-}

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

@@ -1,44 +0,0 @@
-<?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,  ),));
-    }
-}

+ 53 - 11
tests/Symfony/Tests/Component/Routing/Generator/Dumper/PhpGeneratorDumperTest.php

@@ -45,29 +45,71 @@ class PhpGeneratorDumperTest extends \PHPUnit_Framework_TestCase
         $this->generatorDumper = new PhpGeneratorDumper($this->routeCollection);
     }
 
+    public function tearDown()
+    {
+        parent::tearDown();
+     
+        @unlink(self::$fixturesPath.'/dumper/php_generator.php');
+    }
+
     public function testDumpWithRoutes()
     {
-        $this->routeCollection->add('Test', new Route('/testing'));
+        $this->routeCollection->add('Test', new Route('/testing/{foo}'));
+        $this->routeCollection->add('Test2', new Route('/testing2'));
+        
+        file_put_contents(self::$fixturesPath.'/dumper/php_generator.php', $this->generatorDumper->dump());
+        include (self::$fixturesPath.'/dumper/php_generator.php');
+
+        $projectUrlGenerator = new \ProjectUrlGenerator(array(
+            'base_url' => '/app.php',
+            'method' => 'GET',
+            'host' => 'localhost',
+            'port' => 80,
+            'is_secure' => false
+        ));
         
-        $this->assertStringEqualsFile(self::$fixturesPath.'/dumper/php_generator1.php', $this->generatorDumper->dump()); 
+        $absoluteUrlWithParameter    = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), true);
+        $absoluteUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), true);
+        $relativeUrlWithParameter    = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), false);
+        $relativeUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), false);
+
+        $this->assertEquals($absoluteUrlWithParameter, 'http://localhost/app.php/testing/bar');
+        $this->assertEquals($absoluteUrlWithoutParameter, 'http://localhost/app.php/testing2');
+        $this->assertEquals($relativeUrlWithParameter, '/app.php/testing/bar');
+        $this->assertEquals($relativeUrlWithoutParameter, '/app.php/testing2');
     }
     
+    /**
+     * @expectedException \InvalidArgumentException
+     */
     public function testDumpWithoutRoutes()
     {
-        $this->assertStringEqualsFile(self::$fixturesPath.'/dumper/php_generator2.php', $this->generatorDumper->dump()); 
+        file_put_contents(self::$fixturesPath.'/dumper/php_generator.php', 
+            $this->generatorDumper->dump(array('class' => 'WithoutRoutesUrlGenerator')));
+        include (self::$fixturesPath.'/dumper/php_generator.php');
+
+        $projectUrlGenerator = new \WithoutRoutesUrlGenerator(array(
+            'base_url' => '/app.php',
+            'method' => 'GET',
+            'host' => 'localhost',
+            'port' => 80,
+            'is_secure' => false
+        ));
+       
+        $projectUrlGenerator->generate('Test', array());
     }
     
-    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());
+        file_put_contents(self::$fixturesPath.'/dumper/php_generator.php', 
+            $this->generatorDumper->dump(array('class' => 'DefaultRoutesUrlGenerator')));
+        include (self::$fixturesPath.'/dumper/php_generator.php');
+        
+        $projectUrlGenerator = new \DefaultRoutesUrlGenerator(array());
+        $url = $projectUrlGenerator->generate('Test', array());
+
+        $this->assertEquals($url, '/testing');
     }
 }