Skip to content

Commit 55f1c26

Browse files
fix merge
1 parent 63fc63f commit 55f1c26

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

Tests/ConstraintViolationTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,66 @@ public function testToStringOmitsEmptyCodes()
108108

109109
$this->assertSame($expected, (string) $violation);
110110
}
111+
112+
public function testMessageCanBeStringableObject()
113+
{
114+
$message = new ToString();
115+
$violation = new ConstraintViolation(
116+
$message,
117+
(string) $message,
118+
[],
119+
'Root',
120+
'property.path',
121+
null
122+
);
123+
124+
$expected = <<<'EOF'
125+
Root.property.path:
126+
toString
127+
EOF;
128+
$this->assertSame($expected, (string) $violation);
129+
$this->assertSame((string) $message, $violation->getMessage());
130+
}
131+
132+
public function testMessageCannotBeArray()
133+
{
134+
$this->expectException(\TypeError::class);
135+
$violation = new ConstraintViolation(
136+
['cannot be an array'],
137+
'',
138+
[],
139+
'Root',
140+
'property.path',
141+
null
142+
);
143+
}
144+
145+
public function testMessageObjectMustBeStringable()
146+
{
147+
$this->expectException(\TypeError::class);
148+
$violation = new ConstraintViolation(
149+
new CustomArrayObject(),
150+
'',
151+
[],
152+
'Root',
153+
'property.path',
154+
null
155+
);
156+
}
157+
158+
public function testNonStringCode()
159+
{
160+
$violation = new ConstraintViolation(
161+
'42 cannot be used here',
162+
'this is the message template',
163+
[],
164+
['some_value' => 42],
165+
'some_value',
166+
null,
167+
null,
168+
42
169+
);
170+
171+
self::assertSame(42, $violation->getCode());
172+
}
111173
}

Tests/Fixtures/CustomArrayObject.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __construct(array $array = null)
2424
$this->array = $array ?: [];
2525
}
2626

27-
public function offsetExists($offset)
27+
public function offsetExists($offset): bool
2828
{
2929
return \array_key_exists($offset, $this->array);
3030
}
@@ -48,12 +48,12 @@ public function offsetUnset($offset)
4848
unset($this->array[$offset]);
4949
}
5050

51-
public function getIterator()
51+
public function getIterator(): \Traversable
5252
{
5353
return new \ArrayIterator($this->array);
5454
}
5555

56-
public function count()
56+
public function count(): int
5757
{
5858
return \count($this->array);
5959
}
@@ -63,7 +63,7 @@ public function __serialize(): array
6363
return $this->array;
6464
}
6565

66-
public function serialize()
66+
public function serialize(): string
6767
{
6868
return serialize($this->__serialize());
6969
}

Tests/Fixtures/ToString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ToString
1515
{
1616
public $data;
1717

18-
public function __toString()
18+
public function __toString(): string
1919
{
2020
return 'toString';
2121
}

0 commit comments

Comments
 (0)