Преглед на файлове

Merge remote branch 'l3l0/routing-tests'

* l3l0/routing-tests:
  [Routing] added tearDown with cleanup the file system tmp directory
  [Routing] moved clean up the directory from tearDown to setUp. Removed setUpBeforeClass method
  [Routing] modified place when we store temporary file
  [Routing] modified unit test for PhpGeneratorDumper class
  [Routing] add unit test for PhpGeneratorDumper class
  [Routing] added more tests for UrlGenerator class
Fabien Potencier преди 14 години
родител
ревизия
a95f72fff3

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

@@ -0,0 +1,110 @@
+<?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
+     */
+    private $testTmpFilepath;
+
+    protected function setUp()
+    {
+        parent::setUp();
+
+        $this->routeCollection = new RouteCollection();
+        $this->generatorDumper = new PhpGeneratorDumper($this->routeCollection);
+        $this->testTmpFilepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'php_generator.php';
+        @unlink($this->testTmpFilepath);
+    }
+
+    protected function tearDown()
+    {
+        parent::tearDown();
+        
+        @unlink($this->testTmpFilepath);
+    }
+
+    public function testDumpWithRoutes()
+    {
+        $this->routeCollection->add('Test', new Route('/testing/{foo}'));
+        $this->routeCollection->add('Test2', new Route('/testing2'));
+        
+        file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump());
+        include ($this->testTmpFilepath);
+
+        $projectUrlGenerator = new \ProjectUrlGenerator(array(
+            'base_url' => '/app.php',
+            'method' => 'GET',
+            'host' => 'localhost',
+            'port' => 80,
+            'is_secure' => false
+        ));
+        
+        $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()
+    {
+        file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'WithoutRoutesUrlGenerator')));
+        include ($this->testTmpFilepath);
+
+        $projectUrlGenerator = new \WithoutRoutesUrlGenerator(array(
+            'base_url' => '/app.php',
+            'method' => 'GET',
+            'host' => 'localhost',
+            'port' => 80,
+            'is_secure' => false
+        ));
+       
+        $projectUrlGenerator->generate('Test', array());
+    }
+    
+    public function testDumpForRouteWithDefaults()
+    {
+        $this->routeCollection->add('Test', new Route('/testing/{foo}', array('foo' => 'bar')));
+        
+        file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'DefaultRoutesUrlGenerator')));
+        include ($this->testTmpFilepath);
+        
+        $projectUrlGenerator = new \DefaultRoutesUrlGenerator(array());
+        $url = $projectUrlGenerator->generate('Test', array());
+
+        $this->assertEquals($url, '/testing');
+    }
+}

+ 98 - 1
tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php

@@ -89,4 +89,101 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
 
         $this->assertEquals('https://localhost:8080/app.php/testing', $url);
     }
-}
+
+    public function testRelativeUrlWithoutParameters()
+    {
+        $this->routeCollection->add('test', new Route('/testing'));
+        $this->generator->setContext(array(
+            'base_url'=>'/app.php',
+            'method'=>'GET',
+            'host'=>'localhost',
+            'port'=>80,
+            'is_secure'=>false));
+
+        $url = $this->generator->generate('test', array(), false);
+
+        $this->assertEquals('/app.php/testing', $url);
+    }
+    
+    public function testRelativeUrlWithParameter()
+    {
+        $this->routeCollection->add('test', new Route('/testing/{foo}'));
+        $this->generator->setContext(array(
+            'base_url'=>'/app.php',
+            'method'=>'GET',
+            'host'=>'localhost',
+            'port'=>80,
+            'is_secure'=>false));
+
+        $url = $this->generator->generate('test', array('foo' => 'bar'), false);
+
+        $this->assertEquals('/app.php/testing/bar', $url);
+    }
+    
+    public function testRelativeUrlWithExtraParameters()
+    {
+        $this->routeCollection->add('test', new Route('/testing'));
+        $this->generator->setContext(array(
+            'base_url'=>'/app.php',
+            'method'=>'GET',
+            'host'=>'localhost',
+            'port'=>80,
+            'is_secure'=>false));
+
+        $url = $this->generator->generate('test', array('foo' => 'bar'), false);
+
+        $this->assertEquals('/app.php/testing?foo=bar', $url);
+    }
+    
+    public function testAbsoluteUrlWithExtraParameters()
+    {
+        $this->routeCollection->add('test', new Route('/testing'));
+        $this->generator->setContext(array(
+            'base_url'=>'/app.php',
+            'method'=>'GET',
+            'host'=>'localhost',
+            'port'=>80,
+            'is_secure'=>false));
+
+        $url = $this->generator->generate('test', array('foo' => 'bar'), true);
+
+        $this->assertEquals('http://localhost/app.php/testing?foo=bar', $url);
+    }
+    
+    /**
+     * @expectedException \InvalidArgumentException
+     */
+    public function testGenerateWithoutRoutes()
+    {       
+        $this->generator->generate('test', array(), true);
+    }
+    
+    /**
+     * @expectedException \InvalidArgumentException
+     */
+    public function testGenerateForRouteWithoutManditoryParameter()
+    {
+        $this->routeCollection->add('test', new Route('/testing/{foo}'));
+        $this->generator->generate('test', array(), true);
+    }
+    
+    /**
+     * @expectedException \InvalidArgumentException
+     */
+    public function testGenerateForRouteWithInvalidOptionalParameter()
+    {
+        $route = new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+'));
+        $this->routeCollection->add('test', $route);
+        $this->generator->generate('test', array('foo' => 'bar'), true);
+    }
+    
+    /**
+     * @expectedException \InvalidArgumentException
+     */
+    public function testGenerateForRouteWithInvalidManditoryParameter()
+    {
+        $route = new Route('/testing/{foo}', array(), array('foo' => 'd+'));
+        $this->routeCollection->add('test', $route);
+        $this->generator->generate('test', array('foo' => 'bar'), true);
+    } 
+}