|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gorynych\Tests\Adapter; |
|
6
|
|
|
|
|
7
|
|
|
use Gorynych\Adapter\SerializerAdapter; |
|
8
|
|
|
use Gorynych\Exception\NotDeserializableException; |
|
9
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\Serializer\Exception\NotEncodableValueException; |
|
13
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
14
|
|
|
|
|
15
|
|
|
class SerializerAdapterTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var SerializerInterface|MockObject */ |
|
18
|
|
|
private $serializerMock; |
|
19
|
|
|
|
|
20
|
|
|
public function setUp(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$this->serializerMock = $this->createMock(SerializerInterface::class); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testThrowsNotDeserializable(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$this->expectException(NotDeserializableException::class); |
|
28
|
|
|
|
|
29
|
|
|
$this->serializerMock |
|
30
|
|
|
->expects($this->once()) |
|
31
|
|
|
->method('deserialize') |
|
32
|
|
|
->with('foo', self::class, 'json') |
|
33
|
|
|
->willThrowException(new NotEncodableValueException()); |
|
34
|
|
|
|
|
35
|
|
|
$this->setUpAdapter()->deserialize('foo', self::class, 'json'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @dataProvider provideNormalizationCases |
|
40
|
|
|
* @param mixed $data |
|
41
|
|
|
*/ |
|
42
|
|
|
public function testCanNormalize($data, bool $expectedResult): void |
|
43
|
|
|
{ |
|
44
|
|
|
$this->assertSame($expectedResult, $this->setUpAdapter()->canNormalize($data)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return \Generator<array> |
|
49
|
|
|
*/ |
|
50
|
|
|
public function provideNormalizationCases(): \Generator |
|
51
|
|
|
{ |
|
52
|
|
|
yield 'object' => [new \stdClass(), true]; |
|
53
|
|
|
|
|
54
|
|
|
yield 'array of objects' => [[new \stdClass()], true]; |
|
55
|
|
|
|
|
56
|
|
|
yield 'scalar' => ['foo', false]; |
|
57
|
|
|
|
|
58
|
|
|
yield 'empty array' => [[], false]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @dataProvider provideDeserializationCases |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testCanDeserialize(string $type, bool $expectedResult): void |
|
65
|
|
|
{ |
|
66
|
|
|
$this->assertSame($expectedResult, $this->setUpAdapter()->canDeserialize($type)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return \Generator<array> |
|
71
|
|
|
*/ |
|
72
|
|
|
public function provideDeserializationCases(): \Generator |
|
73
|
|
|
{ |
|
74
|
|
|
yield 'existing class' => [self::class, true]; |
|
75
|
|
|
|
|
76
|
|
|
yield 'non existent class' => ['Test', false]; |
|
77
|
|
|
|
|
78
|
|
|
yield 'request' => [Request::class, false]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function setUpAdapter(): SerializerAdapter |
|
82
|
|
|
{ |
|
83
|
|
|
$reflection = new \ReflectionClass(SerializerAdapter::class); |
|
84
|
|
|
$serializerAdapter = $reflection->newInstanceWithoutConstructor(); |
|
85
|
|
|
|
|
86
|
|
|
$reflectionProperty = $reflection->getProperty('serializer'); |
|
87
|
|
|
$reflectionProperty->setAccessible(true); |
|
88
|
|
|
$reflectionProperty->setValue($serializerAdapter, $this->serializerMock); |
|
89
|
|
|
|
|
90
|
|
|
return $serializerAdapter; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|