generated from gsteel/php-template-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefinitionTest.php
165 lines (140 loc) · 6.19 KB
/
DefinitionTest.php
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
164
165
<?php
declare(strict_types=1);
namespace Prismic\DocumentType\Test\Unit;
use PHPUnit\Framework\TestCase;
use Prismic\DocumentType\Definition;
use Prismic\DocumentType\Exception\AssertionFailed;
use function json_encode;
use const JSON_PRETTY_PRINT;
use const JSON_THROW_ON_ERROR;
final class DefinitionTest extends TestCase
{
private Definition $type;
protected function setUp(): void
{
parent::setUp();
$this->type = Definition::new(
'custom',
'Custom Type',
true,
true,
'{"Foo":{}}',
);
}
public function testExpectedJsonStructure(): void
{
$expect = [
'id' => 'custom',
'label' => 'Custom Type',
'repeatable' => true,
'status' => true,
'json' => ['Foo' => []],
];
self::assertEquals($expect, $this->type->jsonSerialize());
}
public function testThatTheIdHasTheExpectedValue(): void
{
self::assertEquals('custom', $this->type->id());
}
public function testThatAlteringThePayloadYieldsANewInstance(): void
{
$clone = $this->type->withAlteredPayload('{"Bar":"Baz"}');
self::assertNotSame($this->type, $clone);
self::assertNotEquals($this->type->json(), $clone->json());
}
public function testThatAlteringTheLabelYieldsANewInstance(): void
{
$clone = $this->type->withNewLabel('New Label');
self::assertNotSame($this->type, $clone);
self::assertNotEquals($this->type->label(), $clone->label());
}
public function testThatAnEmptyLabelIsExceptional(): void
{
$this->expectException(AssertionFailed::class);
/** @psalm-suppress UnusedMethodCall, InvalidArgument */
$this->type->withNewLabel('');
}
public function testThatChangingActivationStateYieldsANewInstance(): void
{
$clone = $this->type->withActivationStatus(false);
self::assertNotSame($this->type, $clone);
self::assertNotEquals($this->type->isActive(), $clone->isActive());
}
public function testThatWithRepeatableStateYieldsANewInstance(): void
{
$clone = $this->type->withRepeatable();
self::assertNotSame($this->type, $clone);
self::assertEquals($this->type->isRepeatable(), $clone->isRepeatable());
}
public function testThatWithoutRepeatableStateYieldsANewInstance(): void
{
$clone = $this->type->withoutRepeatable();
self::assertNotSame($this->type, $clone);
self::assertNotEquals($this->type->isRepeatable(), $clone->isRepeatable());
}
public function testEqualityIsTrueForTheSameInstance(): void
{
self::assertTrue($this->type->equals($this->type));
}
public function testEqualityIsTrueForDifferentInstancesWithTheSameValues(): void
{
$a = $this->type->withActivationStatus(false);
$b = $this->type->withActivationStatus(false);
self::assertNotSame($a, $b);
self::assertTrue($a->equals($b));
}
public function testEqualityIsFalseForDifferentLabel(): void
{
$other = $this->type->withNewLabel('changed');
self::assertFalse($this->type->equals($other));
}
public function testEqualityIsFalseForDifferentActivationStatus(): void
{
$other = $this->type->withActivationStatus(false);
self::assertFalse($this->type->equals($other));
}
public function testEqualityIsFalseForDifferentRepeatableStatus(): void
{
$other = $this->type->withoutRepeatable();
self::assertFalse($this->type->equals($other));
}
public function testEqualityIsFalseForJsonWithAndWithoutWhitespace(): void
{
$input = ['foo' => 'bar'];
$plain = json_encode($input, JSON_THROW_ON_ERROR);
$pretty = json_encode($input, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
$a = $this->type->withAlteredPayload($plain);
$b = $this->type->withAlteredPayload($pretty);
self::assertFalse($a->equals($b));
}
/** @return array<string, array{0:array<string, mixed>}> */
public function invalidDefinitionArrayProvider(): array
{
return [
'Missing ID' => [['label' => 'foo', 'repeatable' => true, 'status' => true, 'json' => ['foo' => 'bar']]],
'Missing Label' => [['id' => 'bar', 'repeatable' => true, 'status' => true, 'json' => ['foo' => 'bar']]],
'Missing Repeat' => [['id' => 'bar', 'label' => 'foo', 'status' => true, 'json' => ['foo' => 'bar']]],
'Missing Status' => [['id' => 'bar', 'label' => 'foo', 'repeatable' => true, 'json' => ['foo' => 'bar']]],
'Missing Json' => [['id' => 'bar', 'label' => 'foo', 'repeatable' => true, 'status' => true]],
'ID is null' => [['id' => null, 'label' => 'foo', 'repeatable' => true, 'status' => true, 'json' => ['foo' => 'bar']]],
'ID is empty' => [['id' => '', 'label' => 'foo', 'repeatable' => true, 'status' => true, 'json' => ['foo' => 'bar']]],
'ID not string' => [['id' => 1, 'label' => 'foo', 'repeatable' => true, 'status' => true, 'json' => ['foo' => 'bar']]],
'Label is null' => [['id' => 'foo', 'label' => null, 'repeatable' => true, 'status' => true, 'json' => ['foo' => 'bar']]],
'Label is empty' => [['id' => 'foo', 'label' => '', 'repeatable' => true, 'status' => true, 'json' => ['foo' => 'bar']]],
'Label not string' => [['id' => 'foo', 'label' => 1, 'repeatable' => true, 'status' => true, 'json' => ['foo' => 'bar']]],
'Repeat not bool' => [['id' => 'foo', 'label' => 'bar', 'repeatable' => 1, 'status' => true, 'json' => ['foo' => 'bar']]],
'Status not bool' => [['id' => 'foo', 'label' => 'bar', 'repeatable' => true, 'status' => 1, 'json' => ['foo' => 'bar']]],
'Json not array' => [['id' => 'foo', 'label' => 'bar', 'repeatable' => true, 'status' => true, 'json' => 'whut?']],
];
}
/**
* @param array<string, mixed> $input
*
* @dataProvider invalidDefinitionArrayProvider
*/
public function testAssertionErrorIsThrownForInvalidDefinitionStructure(array $input): void
{
$this->expectException(AssertionFailed::class);
Definition::fromArray($input);
}
}