Skip to content

Commit a9800c6

Browse files
committed
Merge pull request #132 from dantleech/testing_component
[WIP] Integrating Testing Component
2 parents a4726a7 + 3d28959 commit a9800c6

37 files changed

+231
-364
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
phpunit.xml
22
composer.lock
3-
/vendor
4-
Tests/Functional/config/parameters.yml
5-
Tests/Functional/app.sqlite
3+
vendor
4+
Tests/Resources/app/cache
5+
Tests/Resources/app/logs

.travis.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@ env:
1111
- SYMFONY_VERSION=dev-master
1212

1313
before_script:
14-
- composer self-update
1514
- composer require symfony/framework-bundle:${SYMFONY_VERSION} --prefer-source
16-
- cp ./Tests/Functional/config/parameters.yml.dist ./Tests/Functional/config/parameters.yml
17-
- php Tests/Functional/console doctrine:phpcr:init:dbal
18-
- php Tests/Functional/console doctrine:phpcr:repository:init
15+
- vendor/symfony-cmf/testing/bin/travis/phpcr_odm_doctrine_dbal.sh
1916

2017
script: phpunit --coverage-text
2118

Tests/Functional/Admin/RouteAdminTest.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,26 @@
77
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Functional\Admin;
88

99
use Symfony\Cmf\Bundle\RoutingBundle\Admin\RouteAdmin;
10-
use Symfony\Cmf\Bundle\RoutingBundle\Tests\Functional\BaseTestCase;
1110
use Symfony\Component\Routing\Route;
11+
use Symfony\Cmf\Bundle\RoutingBundle\Tests\Functional\BaseTestCase;
1212

1313
class RouteAdminTest extends BaseTestCase
1414
{
1515
/**
1616
* @var RouteAdmin
1717
*/
18-
private static $routeAdmin;
18+
private $routeAdmin;
1919

2020
/**
2121
* @var \PHPUnit_Framework_MockObject_MockObject
2222
*/
2323
private $errorElement;
2424

25-
public static function setupBeforeClass(array $options = array(), $routebase = null)
26-
{
27-
parent::setUpBeforeClass($options, $routebase);
28-
self::$routeAdmin = self::$kernel->getContainer()->get('cmf_routing.route_admin');
29-
}
30-
3125
protected function setUp()
3226
{
27+
parent::setUp();
28+
$this->db('PHPCR')->createTestNode();
29+
$this->routeAdmin = $this->getContainer()->get('cmf_routing.route_admin');
3330
$this->errorElement = $this->getMockBuilder('Sonata\AdminBundle\Validator\ErrorElement')
3431
->disableOriginalConstructor()
3532
->getMock();
@@ -38,7 +35,7 @@ protected function setUp()
3835
public function testCorrectControllerPath()
3936
{
4037
$route = new Route('/', array('_controller' => 'FrameworkBundle:Redirect:redirect'));
41-
self::$routeAdmin->validate($this->errorElement, $route);
38+
$this->routeAdmin->validate($this->errorElement, $route);
4239
}
4340

4441
public function testControllerPathViolation()
@@ -55,7 +52,7 @@ public function testControllerPathViolation()
5552
$this->errorElement->expects($this->once())
5653
->method('end');
5754

58-
self::$routeAdmin->validate($this->errorElement, $route);
55+
$this->routeAdmin->validate($this->errorElement, $route);
5956
}
6057

6158
public function testTemplateViolation()
@@ -72,12 +69,12 @@ public function testTemplateViolation()
7269
$this->errorElement->expects($this->once())
7370
->method('end');
7471

75-
self::$routeAdmin->validate($this->errorElement, $route);
72+
$this->routeAdmin->validate($this->errorElement, $route);
7673
}
7774

7875
public function testCorrectTemplate()
7976
{
8077
$route = new Route('/', array('_template' => 'TwigBundle::layout.html.twig'));
81-
self::$routeAdmin->validate($this->errorElement, $route);
78+
$this->routeAdmin->validate($this->errorElement, $route);
8279
}
8380
}

Tests/Functional/AppKernel.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

Tests/Functional/BaseTestCase.php

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,39 @@
55
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;
66

77
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
8+
use Symfony\Cmf\Component\Testing\Functional\BaseTestCase as ComponentBaseTestCase;
9+
use PHPCR\Util\PathHelper;
10+
use Symfony\Cmf\Component\Testing\Document\Content;
811

9-
class BaseTestCase extends WebTestCase
12+
class BaseTestCase extends ComponentBaseTestCase
1013
{
11-
/**
12-
* @var \Doctrine\ODM\PHPCR\DocumentManager
13-
*/
14-
protected static $dm;
15-
16-
protected static function createKernel(array $options = array())
14+
protected function getDm()
1715
{
18-
return new AppKernel(
19-
isset($options['config']) ? $options['config'] : 'default.yml'
20-
);
16+
$dm = $this->db('PHPCR')->getOm();
17+
return $dm;
2118
}
2219

23-
/**
24-
* careful: the kernel is shut down after the first test, if you need the
25-
* kernel, recreate it.
26-
*
27-
* @param array $options passed to self:.createKernel
28-
* @param string $routebase base name for routes under /test to use
29-
*/
30-
public static function setupBeforeClass(array $options = array(), $routebase = null)
20+
protected function createRoute($path)
3121
{
32-
self::$kernel = self::createKernel($options);
33-
self::$kernel->init();
34-
self::$kernel->boot();
35-
36-
self::$dm = self::$kernel->getContainer()->get('doctrine_phpcr.odm.document_manager');
37-
38-
if (null == $routebase) {
39-
return;
40-
}
41-
42-
$session = self::$kernel->getContainer()->get('doctrine_phpcr.session');
43-
if ($session->nodeExists("/test/$routebase")) {
44-
$session->getNode("/test/$routebase")->remove();
45-
}
46-
if (! $session->nodeExists('/test')) {
47-
$session->getRootNode()->addNode('test', 'nt:unstructured');
48-
}
49-
$session->save();
50-
51-
$root = self::$dm->find(null, '/test');
22+
$parentPath = PathHelper::getParentPath($path);
23+
$parent = $this->getDm()->find(null, $parentPath);
24+
$name = PathHelper::getNodeName($path);
5225
$route = new Route;
53-
$route->setPosition($root, $routebase);
54-
self::$dm->persist($route);
55-
self::$dm->flush();
26+
$route->setPosition($parent, $name);
27+
$this->getDm()->persist($route);
28+
$this->getDm()->flush();
29+
30+
return $route;
31+
}
32+
33+
protected function createContent($path)
34+
{
35+
$content = new Content;
36+
$content->setId('/test/content');
37+
$content->setTitle('Foo Content');
38+
$this->getDm()->persist($content);
39+
$this->getDm()->flush();
40+
41+
return $content;
5642
}
5743
}

Tests/Functional/Controller/RedirectControllerTest.php

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Functional\Controller;
44

55
use PHPCR\Util\PathHelper;
6-
use Symfony\Cmf\Bundle\RoutingBundle\Tests\Functional\BaseTestCase;
76

87
use Symfony\Component\HttpFoundation\RedirectResponse;
98

109
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;
1110
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\RedirectRoute;
1211
use Symfony\Cmf\Bundle\RoutingBundle\Controller\RedirectController;
12+
use Symfony\Cmf\Bundle\RoutingBundle\Tests\Functional\BaseTestCase;
13+
use PHPCR\Util\NodeHelper;
1314

1415
class RedirectControllerTest extends BaseTestCase
1516
{
@@ -18,32 +19,35 @@ class RedirectControllerTest extends BaseTestCase
1819
/**
1920
* @var \Symfony\Cmf\Bundle\RoutingBundle\Controller\RedirectController
2021
*/
21-
protected static $controller;
22+
protected $controller;
2223

23-
public static function setupBeforeClass(array $options = array(), $routebase = null)
24+
public function setUp()
2425
{
25-
parent::setupBeforeClass(array(), PathHelper::getNodeName(self::ROUTE_ROOT));
26-
$router = self::$kernel->getContainer()->get('router');
27-
self::$controller = new RedirectController($router);
26+
parent::setUp();
27+
$this->db('PHPCR')->createTestNode();
28+
$this->createRoute(self::ROUTE_ROOT);
29+
30+
$router = $this->getContainer()->get('router');
31+
$this->controller = new RedirectController($router);
2832
}
2933

3034
public function testRedirectUri()
3135
{
32-
$root = self::$dm->find(null, self::ROUTE_ROOT);
36+
$root = $this->getDm()->find(null, self::ROUTE_ROOT);
3337

3438
$redirect = new RedirectRoute;
3539
$redirect->setPosition($root, 'redirectUri');
3640
$redirect->setUri('http://example.com/test-url');
3741
$redirect->setParameters(array('test' => 7)); // parameters should be ignored in this case
3842
$redirect->setPermanent(true);
39-
self::$dm->persist($redirect);
43+
$this->getDm()->persist($redirect);
4044

41-
self::$dm->flush();
45+
$this->getDm()->flush();
4246

43-
self::$dm->clear();
47+
$this->getDm()->clear();
4448

45-
$redirect = self::$dm->find(null, self::ROUTE_ROOT.'/redirectUri');
46-
$response = self::$controller->redirectAction($redirect);
49+
$redirect = $this->getDm()->find(null, self::ROUTE_ROOT.'/redirectUri');
50+
$response = $this->controller->redirectAction($redirect);
4751

4852
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
4953
$this->assertSame(301, $response->getStatusCode());
@@ -52,25 +56,27 @@ public function testRedirectUri()
5256

5357
public function testRedirectContent()
5458
{
55-
$root = self::$dm->find(null, self::ROUTE_ROOT);
59+
$content = $this->createContent('/test/content');
60+
61+
$root = $this->getDm()->find(null, self::ROUTE_ROOT);
5662

5763
$route = new Route;
58-
$route->setContent($root); // this happens to be a referenceable node
64+
$route->setContent($content);
5965
$route->setPosition($root, 'testroute');
60-
self::$dm->persist($route);
66+
$this->getDm()->persist($route);
6167

6268
$redirect = new RedirectRoute;
6369
$redirect->setPosition($root, 'redirectContent');
6470
$redirect->setRouteTarget($route);
6571
$redirect->setParameters(array('test' => 'content'));
66-
self::$dm->persist($redirect);
72+
$this->getDm()->persist($redirect);
6773

68-
self::$dm->flush();
74+
$this->getDm()->flush();
6975

70-
self::$dm->clear();
76+
$this->getDm()->clear();
7177

72-
$redirect = self::$dm->find(null, self::ROUTE_ROOT.'/redirectContent');
73-
$response = self::$controller->redirectAction($redirect);
78+
$redirect = $this->getDm()->find(null, self::ROUTE_ROOT.'/redirectContent');
79+
$response = $this->controller->redirectAction($redirect);
7480

7581
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $response);
7682
$this->assertSame(302, $response->getStatusCode());
@@ -79,21 +85,20 @@ public function testRedirectContent()
7985

8086
public function testRedirectName()
8187
{
82-
$root = self::$dm->find(null, self::ROUTE_ROOT);
88+
$root = $this->getDm()->find(null, self::ROUTE_ROOT);
8389

8490
$redirect = new RedirectRoute;
8591
$redirect->setPosition($root, 'redirectName');
8692
$redirect->setRouteName('symfony_route');
8793
$redirect->setParameters(array('param'=>7)); // parameters should be ignored in this case
88-
self::$dm->persist($redirect);
89-
90-
self::$dm->flush();
94+
$this->getDm()->persist($redirect);
9195

92-
self::$dm->clear();
96+
$this->getDm()->flush();
9397

94-
$redirect = self::$dm->find(null, self::ROUTE_ROOT.'/redirectName');
95-
$response = self::$controller->redirectAction($redirect);
98+
$this->getDm()->clear();
9699

100+
$redirect = $this->getDm()->find(null, self::ROUTE_ROOT.'/redirectName');
101+
$response = $this->controller->redirectAction($redirect);
97102
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $response);
98103
$this->assertSame(302, $response->getStatusCode());
99104
$this->assertSame('http://localhost/symfony_route_test?param=7', $response->getTargetUrl());

0 commit comments

Comments
 (0)