You are here

class ContentAwareGeneratorTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony-cmf/routing/Tests/Routing/ContentAwareGeneratorTest.php \Symfony\Cmf\Component\Routing\Tests\Routing\ContentAwareGeneratorTest

Hierarchy

  • class \Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase extends \Symfony\Cmf\Component\Routing\Test\PHPUnit_Framework_TestCase

Expanded class hierarchy of ContentAwareGeneratorTest

File

vendor/symfony-cmf/routing/Tests/Routing/ContentAwareGeneratorTest.php, line 19

Namespace

Symfony\Cmf\Component\Routing\Tests\Routing
View source
class ContentAwareGeneratorTest extends CmfUnitTestCase {
  protected $contentDocument;
  protected $routeDocument;
  protected $routeCompiled;
  protected $provider;

  /**
   * @var ContentAwareGenerator
   */
  protected $generator;
  protected $context;
  public function setUp() {
    $this->contentDocument = $this
      ->buildMock('Symfony\\Cmf\\Component\\Routing\\RouteReferrersReadInterface');
    $this->routeDocument = $this
      ->buildMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Routing\\RouteMock', array(
      'getDefaults',
      'compile',
    ));
    $this->routeCompiled = $this
      ->buildMock('Symfony\\Component\\Routing\\CompiledRoute');
    $this->provider = $this
      ->buildMock('Symfony\\Cmf\\Component\\Routing\\RouteProviderInterface');
    $this->context = $this
      ->buildMock('Symfony\\Component\\Routing\\RequestContext');
    $this->generator = new TestableContentAwareGenerator($this->provider);
  }
  public function testGenerateFromContent() {
    $this->provider
      ->expects($this
      ->never())
      ->method('getRouteByName');
    $this->contentDocument
      ->expects($this
      ->once())
      ->method('getRoutes')
      ->will($this
      ->returnValue(array(
      $this->routeDocument,
    )));
    $this->routeDocument
      ->expects($this
      ->once())
      ->method('compile')
      ->will($this
      ->returnValue($this->routeCompiled));
    $this
      ->assertEquals('result_url', $this->generator
      ->generate($this->contentDocument));
  }
  public function testGenerateFromContentId() {
    $this->provider
      ->expects($this
      ->never())
      ->method('getRouteByName');
    $contentRepository = $this
      ->buildMock('Symfony\\Cmf\\Component\\Routing\\ContentRepositoryInterface', array(
      'findById',
      'getContentId',
    ));
    $contentRepository
      ->expects($this
      ->once())
      ->method('findById')
      ->with('/content/id')
      ->will($this
      ->returnValue($this->contentDocument));
    $this->generator
      ->setContentRepository($contentRepository);
    $this->contentDocument
      ->expects($this
      ->once())
      ->method('getRoutes')
      ->will($this
      ->returnValue(array(
      $this->routeDocument,
    )));
    $this->routeDocument
      ->expects($this
      ->once())
      ->method('compile')
      ->will($this
      ->returnValue($this->routeCompiled));
    $this
      ->assertEquals('result_url', $this->generator
      ->generate('', array(
      'content_id' => '/content/id',
    )));
  }
  public function testGenerateEmptyRouteString() {
    $this->provider
      ->expects($this
      ->never())
      ->method('getRouteByName');
    $this->contentDocument
      ->expects($this
      ->once())
      ->method('getRoutes')
      ->will($this
      ->returnValue(array(
      $this->routeDocument,
    )));
    $this->routeDocument
      ->expects($this
      ->once())
      ->method('compile')
      ->will($this
      ->returnValue($this->routeCompiled));
    $this
      ->assertEquals('result_url', $this->generator
      ->generate($this->contentDocument));
  }
  public function testGenerateRouteMultilang() {
    $route_en = $this
      ->buildMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Routing\\RouteMock', array(
      'getDefaults',
      'compile',
      'getContent',
    ));
    $route_en
      ->setLocale('en');
    $route_de = $this->routeDocument;
    $route_de
      ->setLocale('de');
    $this->contentDocument
      ->expects($this
      ->once())
      ->method('getRoutes')
      ->will($this
      ->returnValue(array(
      $route_en,
      $route_de,
    )));
    $route_en
      ->expects($this
      ->once())
      ->method('getContent')
      ->will($this
      ->returnValue($this->contentDocument));
    $route_en
      ->expects($this
      ->never())
      ->method('compile');
    $route_de
      ->expects($this
      ->once())
      ->method('compile')
      ->will($this
      ->returnValue($this->routeCompiled));
    $this
      ->assertEquals('result_url', $this->generator
      ->generate($route_en, array(
      '_locale' => 'de',
    )));
  }
  public function testGenerateRouteMultilangDefaultLocale() {
    $route = $this
      ->buildMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Routing\\RouteMock');
    $route
      ->expects($this
      ->any())
      ->method('compile')
      ->will($this
      ->returnValue($this->routeCompiled));
    $route
      ->expects($this
      ->any())
      ->method('getRequirement')
      ->with('_locale')
      ->will($this
      ->returnValue('de|en'));
    $route
      ->expects($this
      ->any())
      ->method('getDefault')
      ->with('_locale')
      ->will($this
      ->returnValue('en'));
    $this->routeCompiled
      ->expects($this
      ->any())
      ->method('getVariables')
      ->will($this
      ->returnValue(array()));
    $this
      ->assertEquals('result_url', $this->generator
      ->generate($route, array(
      '_locale' => 'en',
    )));
  }
  public function testGenerateRouteMultilangLocaleNomatch() {
    $route_en = $this
      ->buildMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Routing\\RouteMock', array(
      'getDefaults',
      'compile',
      'getContent',
    ));
    $route_en
      ->setLocale('en');
    $route_de = $this->routeDocument;
    $route_de
      ->setLocale('de');
    $this->contentDocument
      ->expects($this
      ->once())
      ->method('getRoutes')
      ->will($this
      ->returnValue(array(
      $route_en,
      $route_de,
    )));
    $route_en
      ->expects($this
      ->once())
      ->method('getContent')
      ->will($this
      ->returnValue($this->contentDocument));
    $route_en
      ->expects($this
      ->once())
      ->method('compile')
      ->will($this
      ->returnValue($this->routeCompiled));
    $route_de
      ->expects($this
      ->never())
      ->method('compile');
    $this
      ->assertEquals('result_url', $this->generator
      ->generate($route_en, array(
      '_locale' => 'fr',
    )));
  }
  public function testGenerateNoncmfRouteMultilang() {
    $route_en = $this
      ->buildMock('Symfony\\Component\\Routing\\Route', array(
      'getDefaults',
      'compile',
      'getContent',
    ));
    $route_en
      ->expects($this
      ->once())
      ->method('compile')
      ->will($this
      ->returnValue($this->routeCompiled));
    $this
      ->assertEquals('result_url', $this->generator
      ->generate($route_en, array(
      '_locale' => 'de',
    )));
  }
  public function testGenerateRoutenameMultilang() {
    $name = 'foo/bar';
    $route_en = $this
      ->buildMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Routing\\RouteMock', array(
      'getDefaults',
      'compile',
      'getContent',
    ));
    $route_en
      ->setLocale('en');
    $route_de = $this->routeDocument;
    $route_de
      ->setLocale('de');
    $this->provider
      ->expects($this
      ->once())
      ->method('getRouteByName')
      ->with($name)
      ->will($this
      ->returnValue($route_en));
    $this->contentDocument
      ->expects($this
      ->once())
      ->method('getRoutes')
      ->will($this
      ->returnValue(array(
      $route_en,
      $route_de,
    )));
    $route_en
      ->expects($this
      ->once())
      ->method('getContent')
      ->will($this
      ->returnValue($this->contentDocument));
    $route_en
      ->expects($this
      ->never())
      ->method('compile');
    $route_de
      ->expects($this
      ->once())
      ->method('compile')
      ->will($this
      ->returnValue($this->routeCompiled));
    $this
      ->assertEquals('result_url', $this->generator
      ->generate($name, array(
      '_locale' => 'de',
    )));
  }

  /**
   * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
   */
  public function testGenerateRoutenameMultilangNotFound() {
    $name = 'foo/bar';
    $this->provider
      ->expects($this
      ->once())
      ->method('getRouteByName')
      ->with($name)
      ->will($this
      ->returnValue(null));
    $this->generator
      ->generate($name, array(
      '_locale' => 'de',
    ));
  }
  public function testGenerateDocumentMultilang() {
    $route_en = $this
      ->buildMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Routing\\RouteMock', array(
      'getDefaults',
      'compile',
    ));
    $route_en
      ->setLocale('en');
    $route_de = $this->routeDocument;
    $route_de
      ->setLocale('de');
    $this->contentDocument
      ->expects($this
      ->once())
      ->method('getRoutes')
      ->will($this
      ->returnValue(array(
      $route_en,
      $route_de,
    )));
    $route_en
      ->expects($this
      ->never())
      ->method('compile');
    $route_de
      ->expects($this
      ->once())
      ->method('compile')
      ->will($this
      ->returnValue($this->routeCompiled));
    $this
      ->assertEquals('result_url', $this->generator
      ->generate($this->contentDocument, array(
      '_locale' => 'de',
    )));
  }
  public function testGenerateDocumentMultilangLocaleNomatch() {
    $route_en = $this
      ->buildMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Routing\\RouteMock', array(
      'getDefaults',
      'compile',
    ));
    $route_en
      ->setLocale('en');
    $route_de = $this->routeDocument;
    $route_de
      ->setLocale('de');
    $this->contentDocument
      ->expects($this
      ->once())
      ->method('getRoutes')
      ->will($this
      ->returnValue(array(
      $route_en,
      $route_de,
    )));
    $route_en
      ->expects($this
      ->once())
      ->method('compile')
      ->will($this
      ->returnValue($this->routeCompiled));
    $route_de
      ->expects($this
      ->never())
      ->method('compile');
    $this
      ->assertEquals('result_url', $this->generator
      ->generate($this->contentDocument, array(
      '_locale' => 'fr',
    )));
  }

  /**
   * Generate without any information.
   *
   * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
   */
  public function testGenerateNoContent() {
    $this->generator
      ->generate('', array());
  }

  /**
   * Generate with an object that is neither a route nor route aware.
   *
   * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
   */
  public function testGenerateInvalidContent() {
    $this->generator
      ->generate($this);
  }

  /**
   * Generate with a content_id but there is no content repository.
   *
   * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
   */
  public function testGenerateNoContentRepository() {
    $this->provider
      ->expects($this
      ->never())
      ->method('getRouteByName');
    $this->generator
      ->generate('', array(
      'content_id' => '/content/id',
    ));
  }

  /**
   * Generate with content_id but the content is not found.
   *
   * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
   */
  public function testGenerateNoContentFoundInRepository() {
    $this->provider
      ->expects($this
      ->never())
      ->method('getRouteByName');
    $contentRepository = $this
      ->buildMock('Symfony\\Cmf\\Component\\Routing\\ContentRepositoryInterface', array(
      'findById',
      'getContentId',
    ));
    $contentRepository
      ->expects($this
      ->once())
      ->method('findById')
      ->with('/content/id')
      ->will($this
      ->returnValue(null));
    $this->generator
      ->setContentRepository($contentRepository);
    $this->generator
      ->generate('', array(
      'content_id' => '/content/id',
    ));
  }

  /**
   * Generate with content_id but the object at id is not route aware.
   *
   * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
   */
  public function testGenerateWrongContentClassInRepository() {
    $this->provider
      ->expects($this
      ->never())
      ->method('getRouteByName');
    $contentRepository = $this
      ->buildMock('Symfony\\Cmf\\Component\\Routing\\ContentRepositoryInterface', array(
      'findById',
      'getContentId',
    ));
    $contentRepository
      ->expects($this
      ->once())
      ->method('findById')
      ->with('/content/id')
      ->will($this
      ->returnValue($this));
    $this->generator
      ->setContentRepository($contentRepository);
    $this->generator
      ->generate('', array(
      'content_id' => '/content/id',
    ));
  }

  /**
   * Generate from a content that has no routes associated.
   *
   * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
   */
  public function testGenerateNoRoutes() {
    $this->contentDocument
      ->expects($this
      ->once())
      ->method('getRoutes')
      ->will($this
      ->returnValue(array()));
    $this->generator
      ->generate($this->contentDocument);
  }

  /**
   * Generate from a content that returns something that is not a route as route.
   *
   * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
   */
  public function testGenerateInvalidRoute() {
    $this->contentDocument
      ->expects($this
      ->once())
      ->method('getRoutes')
      ->will($this
      ->returnValue(array(
      $this,
    )));
    $this->generator
      ->generate($this->contentDocument);
  }
  public function testGetLocaleAttribute() {
    $this->generator
      ->setDefaultLocale('en');
    $attributes = array(
      '_locale' => 'fr',
    );
    $this
      ->assertEquals('fr', $this->generator
      ->getLocale($attributes));
  }
  public function testGetLocaleDefault() {
    $this->generator
      ->setDefaultLocale('en');
    $attributes = array();
    $this
      ->assertEquals('en', $this->generator
      ->getLocale($attributes));
  }
  public function testGetLocaleContext() {
    $this->generator
      ->setDefaultLocale('en');
    $this->generator
      ->getContext()
      ->setParameter('_locale', 'de');
    $attributes = array();
    $this
      ->assertEquals('de', $this->generator
      ->getLocale($attributes));
  }
  public function testSupports() {
    $this
      ->assertTrue($this->generator
      ->supports(''));
    $this
      ->assertTrue($this->generator
      ->supports(null));
    $this
      ->assertTrue($this->generator
      ->supports($this->contentDocument));
    $this
      ->assertFalse($this->generator
      ->supports($this));
  }
  public function testGetRouteDebugMessage() {
    $this
      ->assertContains('/some/content', $this->generator
      ->getRouteDebugMessage(null, array(
      'content_id' => '/some/content',
    )));
    $this
      ->assertContains('Route aware content Symfony\\Cmf\\Component\\Routing\\Tests\\Routing\\RouteAware', $this->generator
      ->getRouteDebugMessage(new RouteAware()));
    $this
      ->assertContains('/some/content', $this->generator
      ->getRouteDebugMessage('/some/content'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CmfUnitTestCase::buildMock protected function
ContentAwareGeneratorTest::$contentDocument protected property
ContentAwareGeneratorTest::$context protected property
ContentAwareGeneratorTest::$generator protected property
ContentAwareGeneratorTest::$provider protected property
ContentAwareGeneratorTest::$routeCompiled protected property
ContentAwareGeneratorTest::$routeDocument protected property
ContentAwareGeneratorTest::setUp public function
ContentAwareGeneratorTest::testGenerateDocumentMultilang public function
ContentAwareGeneratorTest::testGenerateDocumentMultilangLocaleNomatch public function
ContentAwareGeneratorTest::testGenerateEmptyRouteString public function
ContentAwareGeneratorTest::testGenerateFromContent public function
ContentAwareGeneratorTest::testGenerateFromContentId public function
ContentAwareGeneratorTest::testGenerateInvalidContent public function Generate with an object that is neither a route nor route aware.
ContentAwareGeneratorTest::testGenerateInvalidRoute public function Generate from a content that returns something that is not a route as route.
ContentAwareGeneratorTest::testGenerateNoContent public function Generate without any information.
ContentAwareGeneratorTest::testGenerateNoContentFoundInRepository public function Generate with content_id but the content is not found.
ContentAwareGeneratorTest::testGenerateNoContentRepository public function Generate with a content_id but there is no content repository.
ContentAwareGeneratorTest::testGenerateNoncmfRouteMultilang public function
ContentAwareGeneratorTest::testGenerateNoRoutes public function Generate from a content that has no routes associated.
ContentAwareGeneratorTest::testGenerateRouteMultilang public function
ContentAwareGeneratorTest::testGenerateRouteMultilangDefaultLocale public function
ContentAwareGeneratorTest::testGenerateRouteMultilangLocaleNomatch public function
ContentAwareGeneratorTest::testGenerateRoutenameMultilang public function
ContentAwareGeneratorTest::testGenerateRoutenameMultilangNotFound public function @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
ContentAwareGeneratorTest::testGenerateWrongContentClassInRepository public function Generate with content_id but the object at id is not route aware.
ContentAwareGeneratorTest::testGetLocaleAttribute public function
ContentAwareGeneratorTest::testGetLocaleContext public function
ContentAwareGeneratorTest::testGetLocaleDefault public function
ContentAwareGeneratorTest::testGetRouteDebugMessage public function
ContentAwareGeneratorTest::testSupports public function