-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathJsonDecodingTraitTest.php
64 lines (49 loc) · 1.62 KB
/
JsonDecodingTraitTest.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
<?php
namespace Cucumber\Messages;
use Cucumber\Messages\DecodingException\MalformedJsonException;
use Cucumber\Messages\DecodingException\SchemaViolationException;
use Cucumber\Messages\DecodingException\UnexpectedDecodingException;
use JsonSerializable;
use PHPUnit\Framework\TestCase;
use RuntimeException;
class JsonDecodingTraitTest extends TestCase
{
public function testItCanDecodeSomeJsonToAnObject(): void
{
JsonDecodingTraitTestImpl::fromJson('[]');
self::expectNotToPerformAssertions();
}
public function testItThrowsIfGivenInvalidJson(): void
{
$this->expectException(MalformedJsonException::class);
JsonDecodingTraitTestImpl::fromJson('{');
}
public function testItThrowsIfGivenValidJsonThatIsNotArray(): void
{
$this->expectException(SchemaViolationException::class);
JsonDecodingTraitTestImpl::fromJson('true');
}
public function testItRethrowsUnexpectedErrors(): void
{
$this->expectException(UnexpectedDecodingException::class);
// 'oops' is hardcoded in BrokenJsonDecodingTraitTestImpl
$this->expectExceptionMessage('Unexpected decoding error: "oops"');
BrokenJsonDecodingTraitTestImpl::fromJson('[]');
}
}
class JsonDecodingTraitTestImpl implements JsonSerializable
{
use JsonEncodingTrait;
public static function fromArray(array $arr): self
{
return new self();
}
}
class BrokenJsonDecodingTraitTestImpl implements JsonSerializable
{
use JsonEncodingTrait;
public static function fromArray(array $arr): self
{
throw new RuntimeException('oops');
}
}