Skip to content

Commit a9e17cf

Browse files
committed
Add Test for Generator
1 parent 1ec95ad commit a9e17cf

4 files changed

Lines changed: 131 additions & 33 deletions

File tree

DependencyInjection/Configuration.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
<?php
2+
/**
3+
* SimpleThings FormExtraBundle
4+
*
5+
* LICENSE
6+
*
7+
* This source file is subject to the new BSD license that is bundled
8+
* with this package in the file LICENSE.txt.
9+
* If you did not receive a copy of the license and are unable to
10+
* obtain it through the world-wide-web, please send an email
11+
* to kontakt@beberlei.de so I can send you a copy immediately.
12+
*/
213

314
namespace SimpleThings\FormExtraBundle\DependencyInjection;
415

Resources/config/client_validation.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
</service>
3131

3232
<service id="simple_things_form_extra.js_validation_constraints_generator" class="%simple_things_form_extra.js_validation_constraints_generator.class%">
33-
<argument type="service" id="validator" />
34-
<argument type="service" id="translator" />
33+
<argument type="service" id="validator.mapping.class_metadata_factory" />
34+
<argument type="service" id="translator" on-invalid="null" />
3535
<argument>%kernel.default_locale%</argument>
3636
</service>
3737
</services>
Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
<?php
2+
/**
3+
* SimpleThings FormExtraBundle
4+
*
5+
* LICENSE
6+
*
7+
* This source file is subject to the new BSD license that is bundled
8+
* with this package in the file LICENSE.txt.
9+
* If you did not receive a copy of the license and are unable to
10+
* obtain it through the world-wide-web, please send an email
11+
* to kontakt@beberlei.de so I can send you a copy immediately.
12+
*/
213

314
namespace SimpleThings\FormExtraBundle\Service;
415

5-
use Symfony\Component\Validator\ValidatorInterface;
16+
use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
617
use Symfony\Component\Validator\Constraint;
718
use Symfony\Component\Translation\Translator;
819

@@ -11,71 +22,79 @@
1122
*/
1223
class JsValidationConstraintsGenerator
1324
{
14-
1525
/**
16-
*
17-
* @var ValidatorInterface
26+
* @var ClassMetadataFactoryInterface
1827
*/
19-
protected $validator;
28+
protected $metadataFactory;
2029

2130
/**
22-
*
2331
* @var Translator
2432
*/
2533
protected $translator;
2634

2735
/**
28-
*
2936
* @var string
3037
*/
3138
protected $defaultLocale;
3239

3340
/**
34-
*
35-
* @param ValidatorInterface $validator
41+
* @param metadataFactoryInterface $metadataFactory
3642
*/
37-
public function __construct(ValidatorInterface $validator, Translator $translator, $defaultLocale)
43+
public function __construct(ClassMetadataFactoryInterface $metadataFactory, Translator $translator = null, $defaultLocale = null)
3844
{
39-
$this->validator = $validator;
45+
$this->metadataFactory = $metadataFactory;
4046
$this->translator = $translator;
4147
$this->defaultLocale = $defaultLocale;
4248
}
4349

4450
/**
45-
*
46-
* @param array $objects
51+
* @param array $classNames
4752
* @return string
4853
*/
49-
public function generate(array $objects)
54+
public function generate(array $classNames)
5055
{
51-
52-
$metadataFactory = $this->validator->getMetadataFactory();
5356
$data = array();
57+
foreach ($classNames as $className) {
58+
$data[$className] = $this->generateClass($className);
59+
}
60+
61+
return json_encode($data);
62+
}
5463

55-
foreach ($objects as $object) {
56-
$data[$object] = array();
64+
/**
65+
* Generate array representation of constraints that is exported to JSON.
66+
*
67+
* @todo export class constraints
68+
* @todo filter exported contraints?
69+
* @param string $className
70+
* @return array
71+
*/
72+
public function generateClass($className)
73+
{
74+
$data = array();
5775

58-
$metadata = $metadataFactory->getClassMetadata($object);
59-
$properties = $metadata->getConstrainedProperties();
76+
$metadata = $this->metadataFactory->getClassMetadata($className);
77+
$properties = $metadata->getConstrainedProperties();
6078

61-
foreach ($properties as $property) {
62-
$data[$object][$property] = array();
63-
$constraintsList = $metadata->getMemberMetadatas($property);
64-
foreach ($constraintsList as $constraints) {
65-
foreach ($constraints->constraints as $constraint) {
66-
$const = clone $constraint;
67-
$const->message = $this->translator->trans($const->message, array(), 'validators', $this->defaultLocale);
68-
$data[$object][$property][$this->getConstraintName($const)] = $const;
79+
foreach ($properties as $property) {
80+
$data[$property] = array();
81+
$constraintsList = $metadata->getMemberMetadatas($property);
82+
foreach ($constraintsList as $constraints) {
83+
foreach ($constraints->constraints as $constraint) {
84+
$const = clone $constraint;
85+
if ($this->translator) {
86+
$const->message = $this->translator->trans($const->message, array(), 'validations', $this->defaultLocale);
6987
}
88+
$data[$property][$this->getConstraintName($const)] = $const;
7089
}
7190
}
7291
}
7392

74-
return json_encode($data);
93+
return $data;
7594
}
7695

7796
/**
78-
*
97+
* @todo Only shorten symfony ones and underscore/camlize others?
7998
* @param Constraint $constraint
8099
* @return string
81100
*/
@@ -85,5 +104,5 @@ protected function getConstraintName(Constraint $constraint)
85104
$parts = explode('\\', $class);
86105
return lcfirst(array_pop($parts));
87106
}
88-
89107
}
108+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* SimpleThings FormExtraBundle
4+
*
5+
* LICENSE
6+
*
7+
* This source file is subject to the new BSD license that is bundled
8+
* with this package in the file LICENSE.txt.
9+
* If you did not receive a copy of the license and are unable to
10+
* obtain it through the world-wide-web, please send an email
11+
* to kontakt@beberlei.de so I can send you a copy immediately.
12+
*/
13+
14+
namespace SimpleThings\FormExtraBundle\Tests\Services;
15+
16+
use SimpleThings\FormExtraBundle\Service\JsValidationConstraintsGenerator;
17+
use Symfony\Component\Validator\Constraints\Min;
18+
use Symfony\Component\Validator\Mapping\ClassMetadata;
19+
20+
class JsValidationConstraintsGeneratorTest extends \PHPUnit_Framework_TestCase
21+
{
22+
private $generator;
23+
private $metadataFactory;
24+
25+
public function setUp()
26+
{
27+
$this->metadataFactory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
28+
$this->generator = new JsValidationConstraintsGenerator($this->metadataFactory);
29+
}
30+
31+
public function testGenerateEmpty()
32+
{
33+
$this->assertEquals("[]", $this->generator->generate(array()));
34+
}
35+
36+
public function testGenerate()
37+
{
38+
$constraint = new Min(array('limit' => 10));
39+
40+
$cm = new ClassMetadata(__NAMESPACE__ . '\\TestEntity');
41+
$cm->addPropertyConstraint('field1', $constraint);
42+
$this->metadataFactory->expects($this->once())->method('getClassMetadata')->will($this->returnValue($cm));
43+
44+
$data = $this->generator->generate(array(__NAMESPACE__ . '\\TestEntity'));
45+
$this->assertEquals('{"SimpleThings\\\\FormExtraBundle\\\\Tests\\\\Services\\\\TestEntity":{"field1":{"min":{"message":"This value should be {{ limit }} or more","invalidMessage":"This value should be a valid number","limit":10,"groups":["Default","TestEntity"]}}}}', $data);
46+
}
47+
48+
public function testGenerateClass()
49+
{
50+
$constraint = new Min(array('limit' => 10));
51+
52+
$cm = new ClassMetadata(__NAMESPACE__ . '\\TestEntity');
53+
$cm->addPropertyConstraint('field1', $constraint);
54+
$this->metadataFactory->expects($this->once())->method('getClassMetadata')->will($this->returnValue($cm));
55+
56+
$data = $this->generator->generateClass(__NAMESPACE__ . '\\TestEntity');
57+
58+
$this->assertArrayHasKey('field1', $data);
59+
$this->assertArrayHasKey('min', $data['field1']);
60+
$this->assertEquals($constraint, $data['field1']['min']);
61+
}
62+
}
63+
64+
class TestEntity
65+
{
66+
public $field1;
67+
}
68+

0 commit comments

Comments
 (0)