-
Notifications
You must be signed in to change notification settings - Fork 1
/
PhoneNumberTypeTest.phpt
163 lines (139 loc) · 4.51 KB
/
PhoneNumberTypeTest.phpt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types = 1);
namespace NepadaTests\PhoneNumberDoctrine;
use Brick\PhoneNumber\PhoneNumber;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use Mockery\MockInterface;
use Nepada\PhoneNumberDoctrine\PhoneNumberType;
use NepadaTests\TestCase;
use Tester\Assert;
require_once __DIR__ . '/../bootstrap.php';
/**
* @testCase
*/
class PhoneNumberTypeTest extends TestCase
{
private PhoneNumberType $type;
/**
* @var AbstractPlatform|MockInterface
*/
private AbstractPlatform $platform;
protected function setUp(): void
{
parent::setUp();
if (Type::hasType(PhoneNumber::class)) {
Type::overrideType(PhoneNumber::class, PhoneNumberType::class);
} else {
Type::addType(PhoneNumber::class, PhoneNumberType::class);
}
/** @var PhoneNumberType $type */
$type = Type::getType(PhoneNumber::class);
Assert::type(PhoneNumberType::class, $type);
$this->type = $type;
$this->platform = \Mockery::mock(AbstractPlatform::class);
}
public function testGetName(): void
{
Assert::same(PhoneNumber::class, $this->type->getName());
}
public function testRequiresSQLCommentHint(): void
{
Assert::true($this->type->requiresSQLCommentHint($this->platform));
}
public function testConvertToDatabaseValueFails(): void
{
Assert::exception(
function (): void {
$this->type->convertToDatabaseValue('foo', $this->platform);
},
ConversionException::class,
sprintf(
'Could not convert PHP value \'foo\'%%a?%% to type %%S?%%%s%%S?%%. Expected one of the following types: null, Brick\PhoneNumber\PhoneNumber, phone number string',
PhoneNumber::class,
),
);
}
/**
* @dataProvider getDataForConvertToDatabaseValue
* @param PhoneNumber|string|null $value
* @param string|null $expected
*/
public function testConvertToDatabaseValueSucceeds($value, ?string $expected): void
{
Assert::same($expected, $this->type->convertToDatabaseValue($value, $this->platform));
}
/**
* @return mixed[]
*/
protected function getDataForConvertToDatabaseValue(): array
{
return [
[
'value' => null,
'expected' => null,
],
[
'value' => PhoneNumber::parse('+420 800 123 456'),
'expected' => '+420800123456',
],
[
'value' => '+420 800 123 456',
'expected' => '+420800123456',
],
];
}
public function testConvertToPHPValueFails(): void
{
Assert::exception(
function (): void {
$this->type->convertToPHPValue('foo', $this->platform);
},
ConversionException::class,
sprintf('Could not convert database value "foo" to Doctrine Type %s', PhoneNumber::class),
);
}
/**
* @dataProvider getDataForConvertToPHPValue
* @param PhoneNumber|string|null $value
* @param PhoneNumber|null $expected
*/
public function testConvertToPHPValueSucceeds($value, ?PhoneNumber $expected): void
{
$actual = $this->type->convertToPHPValue($value, $this->platform);
if ($expected === null) {
Assert::null($actual);
} else {
Assert::type(PhoneNumber::class, $actual);
Assert::same((string) $expected, (string) $actual);
}
}
/**
* @return mixed[]
*/
protected function getDataForConvertToPHPValue(): array
{
return [
[
'value' => null,
'expected' => null,
],
[
'value' => PhoneNumber::parse('+420 800 123 456'),
'expected' => PhoneNumber::parse('+420800123456'),
],
[
'value' => '+420 800 123 456',
'expected' => PhoneNumber::parse('+420800123456'),
],
];
}
public function testGetSQLDeclaration(): void
{
$this->platform->shouldReceive('getVarcharTypeDeclarationSQL')->with(['length' => 21])->andReturn('MOCKVARCHAR');
$declaration = $this->type->getSQLDeclaration(['length' => 255], $this->platform);
Assert::same('MOCKVARCHAR', $declaration);
}
}
(new PhoneNumberTypeTest())->run();