Skip to content

Commit 938c188

Browse files
committed
Merge pull request #144 from WouterJ/orm_testing
[WIP] Added Orm testing
2 parents 02ee7a7 + daa1b36 commit 938c188

File tree

17 files changed

+328
-16
lines changed

17 files changed

+328
-16
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ env:
1313
before_script:
1414
- composer self-update
1515
- composer require symfony/symfony:${SYMFONY_VERSION} --prefer-source
16-
- vendor/symfony-cmf/testing/bin/travis/phpcr_odm_doctrine_dbal.sh
16+
- vendor/symfony-cmf/testing/bin/travis/phpcr_odm_doctrine_orm_dbal.sh
1717

1818
script: phpunit --coverage-text
1919

Doctrine/Orm/ContentRepository.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ protected function getModelAndId($identifier)
2626

2727
/**
2828
* {@inheritDoc}
29+
*
30+
* @param string $id The ID contains both model name and id, seperated by a colon.
31+
* The model name must not contain a colon. For instance, "Acme\Content:12"
32+
* tries to find the Acme\Content object where id = 12
2933
*/
3034
public function findById($id)
3135
{

Doctrine/Orm/Route.php

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,61 @@
55
use Symfony\Cmf\Bundle\RoutingBundle\Model\Route as RouteModel;
66

77
/**
8-
* ORM route version.
9-
* @author matteo caberlotto [email protected]
8+
* The ORM route version.
9+
*
10+
* @author matteo caberlotto <[email protected]>
11+
* @author Wouter J <[email protected]>
1012
*/
1113
class Route extends RouteModel
1214
{
15+
protected $name;
16+
protected $position = 0;
17+
1318
/**
14-
* {@inheritDoc}
19+
* Sets the name.
20+
*
21+
* @param string $name
22+
*
23+
* @return self
1524
*/
16-
protected $name;
25+
public function setName($name)
26+
{
27+
$this->name = $name;
28+
29+
return $this;
30+
}
31+
32+
/**
33+
* Gets the name.
34+
*
35+
* @return string
36+
*/
37+
public function getName()
38+
{
39+
return $this->name;
40+
}
41+
42+
/**
43+
* Sets the position.
44+
*
45+
* @param int $position
46+
*
47+
* @return self
48+
*/
49+
public function setPosition($position)
50+
{
51+
$this->position = $position;
52+
53+
return $this;
54+
}
1755

1856
/**
19-
* {@inheritDoc}
57+
* Gets the position.
58+
*
59+
* @return int
2060
*/
21-
protected $position;
61+
public function getPosition()
62+
{
63+
return $this->position;
64+
}
2265
}

Resources/config/doctrine-model/Route.orm.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
xsi:schemaLocation="https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">
44

55
<mapped-superclass name="Symfony\Cmf\Bundle\RoutingBundle\Model\Route">
6-
<field name="variablePattern" type="string"/>
6+
<field name="variablePattern" type="string" nullable="true"/>
77
<field name="addFormatPattern" type="boolean"/>
8-
<field name="staticPrefix" type="string"/>
8+
<field name="staticPrefix" type="string" nullable="true"/>
99

1010
<indexes>
1111
<index name="name_idx" columns="name"/>

Tests/Functional/Admin/RouteAdminTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class RouteAdminTest extends BaseTestCase
2222
*/
2323
private $errorElement;
2424

25-
protected function setUp()
25+
public function setUp()
2626
{
2727
parent::setUp();
2828
$this->db('PHPCR')->createTestNode();
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Functional\Doctrine\Orm;
4+
5+
use Symfony\Cmf\Component\Testing\Functional\BaseTestCase as ComponentBaseTestCase;
6+
use Symfony\Cmf\Component\Testing\Document\Content;
7+
8+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route;
9+
10+
class OrmTestCase extends ComponentBaseTestCase
11+
{
12+
protected function getKernelConfiguration()
13+
{
14+
return array(
15+
'environment' => 'orm',
16+
);
17+
}
18+
19+
protected function clearDb($model)
20+
{
21+
if (is_array($model)) {
22+
foreach ($model as $singleModel) {
23+
$this->clearDb($singleModel);
24+
}
25+
}
26+
27+
$items = $this->getDm()->getRepository($model)->findAll();
28+
29+
foreach ($items as $item) {
30+
$this->getDm()->remove($item);
31+
}
32+
33+
$this->getDm()->flush();
34+
}
35+
36+
protected function getDm()
37+
{
38+
return $this->db('ORM')->getOm();
39+
}
40+
41+
protected function createRoute($name, $path)
42+
{
43+
// split path in static and variable part
44+
preg_match('{^(.*?)(/[^/]*\{.*)?$}', $path, $paths);
45+
46+
$route = new Route();
47+
$route->setName($name);
48+
$route->setStaticPrefix($paths[1]);
49+
if (isset($paths[2])) {
50+
$route->setVariablePattern($paths[2]);
51+
}
52+
53+
$this->getDm()->persist($route);
54+
$this->getDm()->flush();
55+
56+
return $route;
57+
}
58+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Functional\Doctrine\Orm;
4+
5+
use Symfony\Component\HttpFoundation\Request;
6+
7+
class RouteProviderTest extends OrmTestCase
8+
{
9+
private $repository;
10+
11+
public function setUp()
12+
{
13+
parent::setUp();
14+
$this->clearDb('Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route');
15+
16+
$this->repository = $this->getContainer()->get('cmf_routing.route_provider');
17+
}
18+
19+
public function testGetRouteCollectionForRequest()
20+
{
21+
$this->createRoute('route1', '/test');
22+
$this->createRoute('route2', '/test/child');
23+
$this->createRoute('route3', '/test/child/testroutechild');
24+
25+
$this->getDm()->clear();
26+
27+
$routes = $this->repository->getRouteCollectionForRequest(Request::create('/test/child/testroutechild'));
28+
$this->assertCount(3, $routes);
29+
$this->assertContainsOnlyInstancesOf('Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route', $routes);
30+
}
31+
}

Tests/Functional/Routing/DynamicRouterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class DynamicRouterTest extends BaseTestCase
3030
public function setUp()
3131
{
3232
parent::setUp();
33+
3334
$this->db('PHPCR')->createTestNode();
3435
$this->createRoute(self::ROUTE_ROOT);
3536

Tests/Resources/app/AppKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public function configure()
2020

2121
public function registerContainerConfiguration(LoaderInterface $loader)
2222
{
23-
$loader->load(__DIR__.'/config/config.php');
23+
$loader->load(__DIR__.'/config/config_'.$this->environment.'.php');
2424
}
2525
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cmf_routing:
2+
dynamic:
3+
persistence:
4+
orm:
5+
enabled: true

0 commit comments

Comments
 (0)