1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TBolier\RethinkQL\UnitTest\Serializer; |
5
|
|
|
|
6
|
|
|
use Mockery; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Symfony\Component\Serializer\Serializer; |
9
|
|
|
use TBolier\RethinkQL\Query\Options; |
10
|
|
|
use TBolier\RethinkQL\Serializer\QueryNormalizer; |
11
|
|
|
|
12
|
|
|
class QueryNormalizerTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var QueryNormalizer |
16
|
|
|
*/ |
17
|
|
|
private $normalizer; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public function setUp(): void |
23
|
|
|
{ |
24
|
|
|
$this->normalizer = new QueryNormalizer(); |
25
|
|
|
|
26
|
|
|
$serializer = new Serializer([$this->normalizer]); |
27
|
|
|
|
28
|
|
|
$this->normalizer->setSerializer($serializer); |
29
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function testNormalizeWithStdClass(): void |
36
|
|
|
{ |
37
|
|
|
$object = new \stdClass(); |
38
|
|
|
$object->foo = 'bar'; |
39
|
|
|
|
40
|
|
|
$data = $this->normalizer->normalize($object); |
41
|
|
|
|
42
|
|
|
$this->assertEquals(['foo' => 'bar'], $data); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function testNormalizeWithOptions(): void |
49
|
|
|
{ |
50
|
|
|
$object = new Options(); |
51
|
|
|
$object->setDb('foobar'); |
52
|
|
|
|
53
|
|
|
$expectedObject = new \stdClass(); |
54
|
|
|
$expectedObject->db = [0 => 14, 1 => ['foobar']]; |
55
|
|
|
|
56
|
|
|
$data = $this->normalizer->normalize($object); |
57
|
|
|
|
58
|
|
|
$this->assertEquals($expectedObject, $data); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException |
63
|
|
|
* @expectedExceptionMessage A circular reference has been detected when serializing the object of class "stdClass" (configured limit: 1) |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function testNormalizeWithCircularReference(): void |
67
|
|
|
{ |
68
|
|
|
$object = new \stdClass(); |
69
|
|
|
$object->foo = 'bar'; |
70
|
|
|
|
71
|
|
|
$context = [ |
72
|
|
|
'circular_reference_limit' => [ |
73
|
|
|
spl_object_hash($object) => 1 |
74
|
|
|
] |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
$this->normalizer->normalize($object, null, $context); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
public function testNormalizeWithJsonSerializable(): void |
84
|
|
|
{ |
85
|
|
|
$expectedReturn = ['foo' => 'bar']; |
86
|
|
|
|
87
|
|
|
$object = Mockery::mock('\JsonSerializable'); |
88
|
|
|
$object->shouldReceive('jsonSerialize')->andReturn($expectedReturn); |
|
|
|
|
89
|
|
|
|
90
|
|
|
|
91
|
|
|
$data = $this->normalizer->normalize($object); |
92
|
|
|
|
93
|
|
|
$this->assertEquals($expectedReturn, $data); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException |
98
|
|
|
* @expectedExceptionMessage The ArrayObject must implement "JsonSerializable" |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
public function testInvalidArgumentExceptionThrownOnInvalidClass(): void |
102
|
|
|
{ |
103
|
|
|
$object = new \ArrayObject(); |
104
|
|
|
|
105
|
|
|
$this->normalizer->normalize($object); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @expectedException \Symfony\Component\Serializer\Exception\LogicException |
110
|
|
|
* @expectedExceptionMessage Cannot normalize object because injected serializer is not a normalizer |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
public function testLogicExceptionThrownOnInvalidNormalizer(): void |
114
|
|
|
{ |
115
|
|
|
$object = new \stdClass(); |
116
|
|
|
$object->foo = 'bar'; |
117
|
|
|
|
118
|
|
|
$serializerMock = Mockery::mock('\Symfony\Component\Serializer\SerializerInterface'); |
119
|
|
|
$this->normalizer->setSerializer($serializerMock); |
120
|
|
|
|
121
|
|
|
$this->normalizer->normalize($object); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public function testSupportsDenormalizationReturnsFalse(): void |
128
|
|
|
{ |
129
|
|
|
$this->assertFalse($this->normalizer->supportsDenormalization('foo', 'foo', 'foo')); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @expectedException \Symfony\Component\Serializer\Exception\LogicException |
134
|
|
|
* @expectedExceptionMessage Cannot denormalize with "TBolier\RethinkQL\Serializer\QueryNormalizer". |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
|
|
public function testIfDenormalizeThrowsLogicException(): void |
138
|
|
|
{ |
139
|
|
|
$this->normalizer->denormalize('foo', 'bar'); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: