|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\AbstractTest; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\Attribute\IpAddressFieldInterface; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\Attribute\IpAddressFieldTrait; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\ValidationException; |
|
10
|
|
|
|
|
11
|
|
|
class EntityValidatorTest extends AbstractTest |
|
12
|
|
|
{ |
|
13
|
|
|
public const WORK_DIR = AbstractTest::VAR_PATH.'/EntityValidatorTest'; |
|
14
|
|
|
|
|
15
|
|
|
public const TEST_ENTITY_SERVER = self::TEST_PROJECT_ROOT_NAMESPACE.'\\' |
|
16
|
|
|
.AbstractGenerator::ENTITIES_FOLDER_NAME.'\\Server'; |
|
17
|
|
|
|
|
18
|
|
|
public const TEST_ENTITY_SERVER_COPIED = self::TEST_ENTITY_SERVER.'Copied'; |
|
19
|
|
|
|
|
20
|
|
|
public const VALID_IP_ADDRESSES = [ |
|
21
|
|
|
'192.136.234.145', |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
public const INVALID_IP_ADDRESSES = [ |
|
25
|
|
|
'cheese', |
|
26
|
|
|
'192.136', |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var IpAddressFieldInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $testEntity; |
|
33
|
|
|
|
|
34
|
|
|
public function setup() |
|
35
|
|
|
{ |
|
36
|
|
|
parent::setup(); |
|
37
|
|
|
$this->getEntityGenerator()->generateEntity(self::TEST_ENTITY_SERVER); |
|
38
|
|
|
$this->getFieldGenerator()->setEntityHasField( |
|
39
|
|
|
self::TEST_ENTITY_SERVER, |
|
40
|
|
|
IpAddressFieldTrait::class |
|
41
|
|
|
); |
|
42
|
|
|
file_put_contents( |
|
43
|
|
|
self::WORK_DIR.'/src/Entities/ServerCopied.php', |
|
44
|
|
|
str_replace( |
|
45
|
|
|
[ |
|
46
|
|
|
'class Server', |
|
47
|
|
|
], |
|
48
|
|
|
[ |
|
49
|
|
|
'class ServerCopied', |
|
50
|
|
|
], |
|
51
|
|
|
file_get_contents(self::WORK_DIR.'/src/Entities/Server.php') |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
$entityValidator = $this->container->get(EntityValidator::class); |
|
55
|
|
|
$class = self::TEST_ENTITY_SERVER_COPIED; |
|
56
|
|
|
$this->testEntity = new $class($entityValidator); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testIsValid() |
|
60
|
|
|
{ |
|
61
|
|
|
foreach (self::VALID_IP_ADDRESSES as $ipAddress) { |
|
62
|
|
|
$this->testEntity->setIpAddress($ipAddress); |
|
63
|
|
|
$this->assertEquals($ipAddress, $this->testEntity->getIpAddress()); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testInvalid() |
|
68
|
|
|
{ |
|
69
|
|
|
foreach (self::INVALID_IP_ADDRESSES as $ipAddress) { |
|
70
|
|
|
$exception = null; |
|
71
|
|
|
try { |
|
72
|
|
|
$this->testEntity->setIpAddress($ipAddress); |
|
73
|
|
|
} catch (ValidationException $exception) { |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
$this->assertInstanceOf(ValidationException::class, $exception); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|