Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

start implementing ORM Route tests #429

Open
wants to merge 3 commits into
base: 2.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"php": "^7.1",
"symfony-cmf/routing": "^2.1.0",
"symfony/framework-bundle": "^2.8 || ^3.3 || ^4.0",
"twig/twig": "^1.35 || ^2.4.4"
"twig/twig": "^1.35 || ^2.4.4",
"symfony/symfony": "4.1.x"
},
"require-dev": {
"doctrine/phpcr-odm": "^1.4|^2.0",
Expand Down
25 changes: 21 additions & 4 deletions tests/Functional/Doctrine/Orm/OrmTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Functional\Doctrine\Orm;

use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route;
use Symfony\Cmf\Component\Testing\Functional\BaseTestCase as ComponentBaseTestCase;

Expand Down Expand Up @@ -40,25 +41,41 @@ protected function clearDb($model)
$this->getDm()->flush();
}

/**
* @return ObjectManager
*/
protected function getDm()
{
return $this->db('ORM')->getOm();
}

protected function createRoute($name, $path)
/**
* @param string $name
* @param string $path
*
* @param array $options
* @param bool $persist
*
* @return Route
*/
protected function createRoute($name, $path, $options = [], $persist = true)
{
// split path in static and variable part
preg_match('{^(.*?)(/[^/]*\{.*)?$}', $path, $paths);

$route = new Route();
$route = new Route($options);
$route->setName($name);
$route->setStaticPrefix($paths[1]);
if (isset($paths[2])) {
$route->setVariablePattern($paths[2]);
}

$this->getDm()->persist($route);
$this->getDm()->flush();
if ($persist) {
echo "\n + + + Persist the route + + + \n";
$this->getDm()->persist($route);
$this->getDm()->flush();
$this->getDm()->clear();
}

return $route;
}
Expand Down
163 changes: 163 additions & 0 deletions tests/Functional/Doctrine/Orm/RouteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Functional\Doctrine\Orm;

use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route;

/**
* @author Maximilian Berghoff <[email protected]>
*/
class RouteTest extends OrmTestCase
{
public function setUp()
{
parent::setUp();
$this->clearDb(Route::class);

$this->createRoute('root', '/test/');
}

public function testPersist()
{
$route = $this->createRoute('test', '/test/testroute', [], false);
$linkedRoute = $this->getDm()->getRepository(Route::class)->findByPath('/test/');

$route->setContent($linkedRoute); // this happens to be a referenceable node
$route->setPosition(87);
$route->setDefault('x', 'y');
$route->setRequirement('testreq', 'testregex');
$route->setOptions(['test' => 'value']);
$route->setOption('another', 'value2');


$this->getDm()->persist($route);
$this->getDm()->flush();

$this->assertEquals('test', $route->getName());

$this->getDm()->clear();

$route = $this->getDm()->getRepository(Route::class)->findByPath('/test/testroute');

$this->assertNotNull($route->getContent());
$this->assertEquals('/test/testroute', $route->getPath());

$this->assertEquals('y', $route->getDefault('x'));
$defaults = $route->getDefaults();
$this->assertArrayHasKey('x', $defaults);
$this->assertEquals('y', $defaults['x']);

$requirements = $route->getRequirements();
$this->assertArrayHasKey('testreq', $requirements);
$this->assertEquals('testregex', $requirements['testreq']);

$options = $route->getOptions();
$this->assertArrayHasKey('test', $options);
$this->assertEquals('value', $options['test']);
$this->assertArrayHasKey('another', $options);
$this->assertEquals('value2', $options['another']);
}

public function testPersistEmptyOptions()
{
$this->createRoute('testroute', '/test/empty');

$route = $this->getDm()->getRepository(Route::class)->findByPath('/test/empty');

$defaults = $route->getDefaults();
$this->assertCount(0, $defaults);

$requirements = $route->getRequirements();
$this->assertCount(0, $requirements);

$options = $route->getOptions();
$this->assertTrue(1 >= count($options)); // there is a default option for the compiler

return $route;
}


public function testConditionOption()
{
$route = $this->createRoute('testroute', '/test/conditionroute', [], false);
$route->setCondition('foobar');

$this->getDm()->persist($route);
$this->getDm()->flush();
$this->getDm()->clear();

$route = $this->getDm()->getRepository(Route::class)->findByPath('/test/conditionroute');

$this->assertEquals('foobar', $route->getCondition());
}

public function testRootRoute()
{
$root = $this->getDm()->getRepository(Route::class)->findByPath('/test/');
$this->assertEquals('/test/', $root->getPath());
}

public function testSetPath()
{
$root = $this->getDm()->getRepository(Route::class)->findByPath('/test/');
$this->assertEquals('/test/', $root->getStaticPrefix());
$root->setPath('/test/{test}');
$this->assertEquals('{test}', $root->getVariablePattern());
}

public function testSetPattern()
{
$root = $this->getDm()->getRepository(Route::class)->findByPath('/test');
$root->setVariablePattern('{test}');
$this->assertEquals('/test/{test}', $root->getPath());
$this->assertEquals('{test}', $root->getVariablePattern());
}

public function testCreateVariablePattern()
{
$this->createRoute('test', '/test/{test}');

$route = $this->getDm()->getRepository(Route::class)->findByPath('/test/{test}');
$this->assertEquals('/test/{test}', $route->getPath());
$this->assertEquals('{test}', $route->getVariablePattern());
}

/**
* @depends testPersistEmptyOptions
*
* @expectedException \InvalidArgumentException
*/
public function testSetPatternInvalid(Route $route)
{
$route->setPath('/test/impossible');
}

/**
* @expectedException \LogicException
*/
public function testInvalidIdPrefix()
{
$root = $this->getDm()->getRepository(Route::class)->findByPath('/test/');
$root->setPrefix('/test/changed'); // simulate a problem with the prefix setter listener
$this->assertEquals('/test/', $root->getPath());
}

/**
* @expectedException \LogicException
*/
public function testPrefixNonpersisted()
{
$route = new Route();
$route->getPath();
}

public function testDefaultFormat()
{
# $route = new Route(['add_format_pattern' => true]);
$this->createRoute('test', '/test/format');

$route = $this->getDm()->getRepository(Route::class)->findByPath('/test/format');

$this->assertEquals('/test/format.{_format}', $route->getPath());
}
}
85 changes: 0 additions & 85 deletions tests/Functional/Doctrine/Phpcr/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,89 +88,4 @@ public function testPersistEmptyOptions()

return $route;
}

public function testConditionOption()
{
$route = new Route();
$root = $this->getDm()->find(null, self::ROUTE_ROOT);

$route->setPosition($root, 'conditionroute');
$route->setCondition('foobar');

$this->getDm()->persist($route);
$this->getDm()->flush();
$this->getDm()->clear();

$route = $this->getDm()->find(null, self::ROUTE_ROOT.'/conditionroute');

$this->assertEquals('foobar', $route->getCondition());
}

public function testRootRoute()
{
$root = $this->getDm()->find(null, self::ROUTE_ROOT);
$this->assertEquals('/', $root->getPath());
}

public function testSetPath()
{
$root = $this->getDm()->find(null, self::ROUTE_ROOT);
$this->assertEquals('/', $root->getStaticPrefix());
$root->setPath('/{test}');
$this->assertEquals('{test}', $root->getVariablePattern());
}

public function testSetPattern()
{
$root = $this->getDm()->find(null, self::ROUTE_ROOT);
$root->setVariablePattern('{test}');
$this->assertEquals('/{test}', $root->getPath());
$this->assertEquals('{test}', $root->getVariablePattern());
}

/**
* @depends testPersistEmptyOptions
*
* @expectedException \InvalidArgumentException
*/
public function testSetPatternInvalid(Route $route)
{
$route->setPath('/impossible');
}

/**
* @expectedException \LogicException
*/
public function testInvalidIdPrefix()
{
$root = $this->getDm()->find(null, self::ROUTE_ROOT);
$root->setPrefix('/changed'); // simulate a problem with the prefix setter listener
$this->assertEquals('/', $root->getPath());
}

/**
* @expectedException \LogicException
*/
public function testPrefixNonpersisted()
{
$route = new Route();
$route->getPath();
}

public function testDefaultFormat()
{
$route = new Route(['add_format_pattern' => true]);

$root = $this->getDm()->find(null, self::ROUTE_ROOT);

$route->setPosition($root, 'format');
$this->getDm()->persist($route);
$this->getDm()->flush();

$this->getDm()->clear();

$route = $this->getDm()->find(null, self::ROUTE_ROOT.'/format');

$this->assertEquals('/format.{_format}', $route->getPath());
}
}