1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Small\CodeGeneration\Creation\Src\Validation\Constraints; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\ConstraintCreator; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FileFactory; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FindReplaceFactory; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File\Writer; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Small\ConfigTest; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\ConstraintCreator |
16
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\AbstractCreator |
17
|
|
|
*/ |
18
|
|
|
class ConstraintCreatorTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @test |
22
|
|
|
* @small |
23
|
|
|
*/ |
24
|
|
|
public function itCanCreateANewFileObjectWithTheCorrectContent() |
25
|
|
|
{ |
26
|
|
|
$newObjectFqn = 'EdmondsCommerce\\DoctrineStaticMeta\\Validation\\Constraints\\IsBlueConstraint'; |
27
|
|
|
$file = $this->getConstraintCreator() |
28
|
|
|
->createTargetFileObject($newObjectFqn) |
29
|
|
|
->getTargetFile(); |
30
|
|
|
$expected = '<?php declare(strict_types=1); |
31
|
|
|
|
32
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Validation\Constraints; |
33
|
|
|
|
34
|
|
|
use Symfony\Component\Validator\Constraint; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* This is a template constraint. The Constraint does very little other than specify a message and imply a |
38
|
|
|
* ConstraintValidator with the same FQN as this class, but with `Validator` appended. |
39
|
|
|
* |
40
|
|
|
* @see https://symfony.com/doc/2.6/cookbook/validation/custom_constraint.html |
41
|
|
|
* |
42
|
|
|
* Suggest using the `./bin/doctrine dsm:override:create` command to create a new |
43
|
|
|
* override straight after you generate the file. |
44
|
|
|
* |
45
|
|
|
* Then you can edit it and update your override as required using |
46
|
|
|
* `./bin/doctrine dsm:overrides:update -a fromProject` |
47
|
|
|
* |
48
|
|
|
* And then reapply after a new build using |
49
|
|
|
* `./bin/doctrine dsm:overrides:update -a toProject` |
50
|
|
|
* |
51
|
|
|
*/ |
52
|
|
|
class IsBlueConstraint extends Constraint |
53
|
|
|
{ |
54
|
|
|
public const VALUE_TYPE = \'(template value type)\'; |
55
|
|
|
|
56
|
|
|
public const MESSAGE = \'The value %s is not a valid \' . self::VALUE_TYPE; |
57
|
|
|
|
58
|
|
|
public $message = self::MESSAGE; |
59
|
|
|
}'; |
60
|
|
|
$actual = $file->getContents(); |
61
|
|
|
self::assertSame($expected, $actual); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function getConstraintCreator(): ConstraintCreator |
65
|
|
|
{ |
66
|
|
|
$namespaceHelper = new NamespaceHelper(); |
67
|
|
|
$config = new Config(ConfigTest::SERVER); |
68
|
|
|
|
69
|
|
|
return new ConstraintCreator( |
70
|
|
|
new FileFactory($namespaceHelper, $config), |
71
|
|
|
$namespaceHelper, |
72
|
|
|
new Writer(), |
73
|
|
|
$config, |
74
|
|
|
new FindReplaceFactory() |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|