Skip to content

Commit 22ace07

Browse files
committed
Merge pull request #292 from EmmanuelVella/error-element
Migrate errorElement
2 parents 1bf534d + 4e0963a commit 22ace07

File tree

9 files changed

+223
-160
lines changed

9 files changed

+223
-160
lines changed

Admin/RouteAdmin.php

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
use Sonata\AdminBundle\Datagrid\DatagridMapper;
1515
use Sonata\AdminBundle\Datagrid\ListMapper;
1616
use Sonata\AdminBundle\Form\FormMapper;
17-
use Sonata\AdminBundle\Validator\ErrorElement;
1817
use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;
19-
use Symfony\Component\HttpFoundation\Request;
20-
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
2118
use Symfony\Cmf\Bundle\RoutingBundle\Model\Route;
2219
use PHPCR\Util\PathHelper;
2320

@@ -43,11 +40,6 @@ class RouteAdmin extends Admin
4340
*/
4441
protected $contentClass;
4542

46-
/**
47-
* @var ControllerResolverInterface
48-
*/
49-
protected $controllerResolver;
50-
5143
protected function configureListFields(ListMapper $listMapper)
5244
{
5345
$listMapper
@@ -117,11 +109,6 @@ public function setContentRoot($contentRoot)
117109
$this->contentRoot = $contentRoot;
118110
}
119111

120-
public function setControllerResolver($controllerResolver)
121-
{
122-
$this->controllerResolver = $controllerResolver;
123-
}
124-
125112
public function getExportFormats()
126113
{
127114
return array();
@@ -210,52 +197,6 @@ public function preUpdate($object)
210197
$object->setDefaults($defaults);
211198
}
212199

213-
public function validate(ErrorElement $errorElement, $object)
214-
{
215-
$defaults = $object->getDefaults();
216-
217-
if (isset($defaults['_controller'])) {
218-
$this->validateDefaultsController($errorElement, $object);
219-
}
220-
221-
if (isset($defaults['_template'])) {
222-
$this->validateDefaultsTemplate($errorElement, $object);
223-
}
224-
}
225-
226-
protected function validateDefaultsController(ErrorElement $errorElement, $object)
227-
{
228-
$defaults = $object->getDefaults();
229-
230-
$controller = $defaults['_controller'];
231-
232-
$request = new Request(array(), array(), array('_controller' => $controller));
233-
234-
try {
235-
$this->controllerResolver->getController($request);
236-
} catch (\LogicException $e) {
237-
$errorElement
238-
->with('defaults')
239-
->addViolation($e->getMessage())
240-
->end();
241-
}
242-
}
243-
244-
protected function validateDefaultsTemplate(ErrorElement $errorElement, $object)
245-
{
246-
$defaults = $object->getDefaults();
247-
248-
$templating = $this->getConfigurationPool()->getContainer()->get('templating');
249-
$template = $defaults['_template'];
250-
251-
if (false === $templating->exists($template)) {
252-
$errorElement
253-
->with('defaults')
254-
->addViolation(sprintf('Template "%s" does not exist.', $template))
255-
->end();
256-
}
257-
}
258-
259200
public function toString($object)
260201
{
261202
return $object instanceof Route && $object->getId()

DependencyInjection/CmfRoutingExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public function load(array $configs, ContainerBuilder $container)
4949
}
5050

5151
$this->setupFormTypes($config, $container, $loader);
52+
53+
$loader->load('validators.xml');
5254
}
5355

5456
public function setupFormTypes(array $config, ContainerBuilder $container, LoaderInterface $loader)

Resources/config/admin-phpcr.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
<call method="setRouteRoot">
3333
<argument>%cmf_routing.dynamic.persistence.phpcr.admin_basepath%</argument>
3434
</call>
35-
<call method="setControllerResolver">
36-
<argument type="service" id="controller_resolver" />
37-
</call>
3835
</service>
3936

4037
<service id="cmf_routing.redirect_route_admin" class="%cmf_routing.redirect_route_admin.class%">

Resources/config/validation.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
<class name="Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route">
88
<constraint name="Doctrine\Bundle\PHPCRBundle\Validator\Constraints\ValidPhpcrOdm" />
9+
<getter property="defaults">
10+
<constraint name="Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints\RouteDefaults" />
11+
</getter>
912
</class>
1013

1114
<class name="Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\RedirectRoute">

Resources/config/validators.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<parameters>
8+
<parameter key="cmf_routing.validator.route_defaults.class">Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints\RouteDefaultsValidator</parameter>
9+
</parameters>
10+
11+
<services>
12+
<service class="%cmf_routing.validator.route_defaults.class%" id="cmf_routing.validator.route_defaults">
13+
<argument id="controller_resolver" type="service"/>
14+
<argument id="templating" type="service"/>
15+
<tag name="validator.constraint_validator" alias="cmf_routing.validator.route_defaults" />
16+
</service>
17+
</services>
18+
</container>

Tests/Functional/Admin/RouteAdminTest.php

Lines changed: 0 additions & 98 deletions
This file was deleted.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2014 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Functional\Admin;
13+
14+
use Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints\RouteDefaults;
15+
use Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints\RouteDefaultsValidator;
16+
use Symfony\Component\Routing\Route;
17+
18+
class RouteDefaultsValidatorTest extends \PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* @var \PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $controllerResolver;
24+
25+
/**
26+
* @var \PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $templating;
29+
30+
/**
31+
* @var \PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
private $context;
34+
35+
/**
36+
* @var RouteDefaultsValidator
37+
*/
38+
private $validator;
39+
40+
/**
41+
* @var RouteDefaults
42+
*/
43+
private $constraint;
44+
45+
public function setUp()
46+
{
47+
$this->controllerResolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface');
48+
$this->templating = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
49+
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
50+
$this->constraint = new RouteDefaults();
51+
$this->validator = new RouteDefaultsValidator($this->controllerResolver, $this->templating);
52+
$this->validator->initialize($this->context);
53+
}
54+
55+
public function testCorrectControllerPath()
56+
{
57+
$this
58+
->context
59+
->expects($this->never())
60+
->method('addViolation')
61+
;
62+
63+
$this->validator->validate(array('_controller' => 'FrameworkBundle:Redirect:redirect'), $this->constraint);
64+
}
65+
66+
public function testControllerPathViolation()
67+
{
68+
$message = 'Invalid controller';
69+
70+
$this
71+
->controllerResolver
72+
->expects($this->any())
73+
->method('getController')
74+
->will($this->throwException(new \LogicException($message)))
75+
;
76+
77+
$this
78+
->context
79+
->expects($this->once())
80+
->method('addViolation')
81+
->with($this->equalTo($message))
82+
;
83+
84+
$this->validator->validate(array('_controller' => 'NotExistingBundle:Foo:bar'), $this->constraint);
85+
}
86+
87+
public function testCorrectTemplate()
88+
{
89+
$this
90+
->templating
91+
->expects($this->any())
92+
->method('exists')
93+
->will($this->returnValue(true))
94+
;
95+
96+
$this
97+
->context
98+
->expects($this->never())
99+
->method('addViolation')
100+
;
101+
102+
$this->validator->validate(array('_template' => 'TwigBundle::layout.html.twig'), $this->constraint);
103+
}
104+
105+
public function testTemplateViolation()
106+
{
107+
$this
108+
->templating
109+
->expects($this->any())
110+
->method('exists')
111+
->will($this->returnValue(false))
112+
;
113+
114+
$this
115+
->context
116+
->expects($this->once())
117+
->method('addViolation')
118+
->with($this->equalTo($this->constraint->message))
119+
;
120+
121+
$this->validator->validate(array('_template' => 'NotExistingBundle:Foo:bar.html.twig'), $this->constraint);
122+
}
123+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2014 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
16+
class RouteDefaults extends Constraint
17+
{
18+
public $message = 'Template "%name%" does not exist.';
19+
20+
public function validatedBy()
21+
{
22+
return 'cmf_routing.validator.route_defaults';
23+
}
24+
}

0 commit comments

Comments
 (0)