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
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmf_routing:
2+
dynamic:
3+
persistence:
4+
phpcr:
5+
enabled: true
6+
route_basepath: /test/routing

Tests/Resources/app/config/cmf_routing.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ cmf_routing:
1212
Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\RedirectRoute: cmf_routing.redirect_controller:redirectAction
1313
templates_by_class:
1414
Symfony\Cmf\Component\Testing\Document\Content: TestBundle:Content:index.html.twig
15-
persistence:
16-
phpcr:
17-
enabled: true
18-
route_basepath: /test/routing
1915
locales:
2016
- en
2117
- de
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
$loader->import(CMF_TEST_CONFIG_DIR.'/default.php');
4+
$loader->import(CMF_TEST_CONFIG_DIR.'/phpcr_odm.php'); // todo find some way to don't have this in the ORM config
5+
$loader->import(CMF_TEST_CONFIG_DIR.'/doctrine_orm.php');
6+
$loader->import(CMF_TEST_CONFIG_DIR.'/sonata_admin.php');
7+
$loader->import(__DIR__.'/cmf_routing.yml');
8+
$loader->import(__DIR__.'/cmf_routing.orm.yml');

Tests/Resources/app/config/config.php renamed to Tests/Resources/app/config/config_phpcr.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
$loader->import(CMF_TEST_CONFIG_DIR.'/phpcr_odm.php');
55
$loader->import(CMF_TEST_CONFIG_DIR.'/sonata_admin.php');
66
$loader->import(__DIR__.'/cmf_routing.yml');
7+
$loader->import(__DIR__.'/cmf_routing.phpcr.yml');
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Unit\Doctrine\Orm;
4+
5+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\ContentRepository;
6+
7+
class ContentRepositoryTest extends \PHPUnit_Framework_TestCase
8+
{
9+
private $document;
10+
private $managerRegistry;
11+
private $objectManager;
12+
private $objectRepository;
13+
14+
public function setUp()
15+
{
16+
$this->document = new \stdClass;
17+
$this->objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
18+
$this->managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
19+
$this->objectRepository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository');
20+
}
21+
22+
public function testFindById()
23+
{
24+
$this->objectManager
25+
->expects($this->any())
26+
->method('getRepository')
27+
->with($this->equalTo('stdClass'))
28+
->will($this->returnValue($this->objectRepository))
29+
;
30+
31+
$this->objectRepository
32+
->expects($this->any())
33+
->method('find')
34+
->with(123)
35+
->will($this->returnValue($this->document))
36+
;
37+
38+
$this->managerRegistry
39+
->expects($this->any())
40+
->method('getManager')
41+
->will($this->returnValue($this->objectManager))
42+
;
43+
44+
$contentRepository = new ContentRepository($this->managerRegistry);
45+
$contentRepository->setManagerName('default');
46+
47+
$foundDocument = $contentRepository->findById('stdClass:123');
48+
49+
$this->assertSame($this->document, $foundDocument);
50+
}
51+
52+
/**
53+
* @dataProvider getFindCorrectModelAndIdData
54+
*/
55+
public function testFindCorrectModelAndId($input, $model, $id)
56+
{
57+
$this->objectManager
58+
->expects($this->any())
59+
->method('getRepository')
60+
->with($this->equalTo($model))
61+
->will($this->returnValue($this->objectRepository))
62+
;
63+
64+
$this->objectRepository
65+
->expects($this->any())
66+
->method('find')
67+
->with($id)
68+
;
69+
70+
$this->managerRegistry
71+
->expects($this->any())
72+
->method('getManager')
73+
->will($this->returnValue($this->objectManager))
74+
;
75+
76+
$contentRepository = new ContentRepository($this->managerRegistry);
77+
$contentRepository->setManagerName('default');
78+
79+
$foundDocument = $contentRepository->findById($input);
80+
}
81+
82+
public function getFindCorrectModelAndIdData()
83+
{
84+
return array(
85+
array('Acme\ContentBundle\Entity\Content:12', 'Acme\ContentBundle\Entity\Content', 12),
86+
array('Id\Contains\Colon:12:1', 'Id\Contains\Colon', '12:1'),
87+
array('Class\EndsWith\Number12:20', 'Class\EndsWith\Number12', 20),
88+
);
89+
}
90+
}

0 commit comments

Comments
 (0)