You are here

class PhpGeneratorDumperTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/routing/Tests/Generator/Dumper/PhpGeneratorDumperTest.php \Symfony\Component\Routing\Tests\Generator\Dumper\PhpGeneratorDumperTest

Hierarchy

  • class \Symfony\Component\Routing\Tests\Generator\Dumper\PhpGeneratorDumperTest extends \Symfony\Component\Routing\Tests\Generator\Dumper\PHPUnit_Framework_TestCase

Expanded class hierarchy of PhpGeneratorDumperTest

File

vendor/symfony/routing/Tests/Generator/Dumper/PhpGeneratorDumperTest.php, line 19

Namespace

Symfony\Component\Routing\Tests\Generator\Dumper
View source
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.' . $this
      ->getName() . '.php';
    @unlink($this->testTmpFilepath);
  }
  protected function tearDown() {
    parent::tearDown();
    @unlink($this->testTmpFilepath);
    $this->routeCollection = null;
    $this->generatorDumper = null;
    $this->testTmpFilepath = null;
  }
  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(new RequestContext('/app.php'));
    $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(new RequestContext('/app.php'));
    $projectUrlGenerator
      ->generate('Test', array());
  }

  /**
   * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
   */
  public function testGenerateNonExistingRoute() {
    $this->routeCollection
      ->add('Test', new Route('/test'));
    file_put_contents($this->testTmpFilepath, $this->generatorDumper
      ->dump(array(
      'class' => 'NonExistingRoutesUrlGenerator',
    )));
    include $this->testTmpFilepath;
    $projectUrlGenerator = new \NonExistingRoutesUrlGenerator(new RequestContext());
    $url = $projectUrlGenerator
      ->generate('NonExisting', 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(new RequestContext());
    $url = $projectUrlGenerator
      ->generate('Test', array());
    $this
      ->assertEquals($url, '/testing');
  }
  public function testDumpWithSchemeRequirement() {
    $this->routeCollection
      ->add('Test1', new Route('/testing', array(), array(), array(), '', array(
      'ftp',
      'https',
    )));
    file_put_contents($this->testTmpFilepath, $this->generatorDumper
      ->dump(array(
      'class' => 'SchemeUrlGenerator',
    )));
    include $this->testTmpFilepath;
    $projectUrlGenerator = new \SchemeUrlGenerator(new RequestContext('/app.php'));
    $absoluteUrl = $projectUrlGenerator
      ->generate('Test1', array(), true);
    $relativeUrl = $projectUrlGenerator
      ->generate('Test1', array(), false);
    $this
      ->assertEquals($absoluteUrl, 'ftp://localhost/app.php/testing');
    $this
      ->assertEquals($relativeUrl, 'ftp://localhost/app.php/testing');
    $projectUrlGenerator = new \SchemeUrlGenerator(new RequestContext('/app.php', 'GET', 'localhost', 'https'));
    $absoluteUrl = $projectUrlGenerator
      ->generate('Test1', array(), true);
    $relativeUrl = $projectUrlGenerator
      ->generate('Test1', array(), false);
    $this
      ->assertEquals($absoluteUrl, 'https://localhost/app.php/testing');
    $this
      ->assertEquals($relativeUrl, '/app.php/testing');
  }

}

Members