Passed
Pull Request — master (#11)
by Michel
02:36
created

testNormalizeWithJsonSerializable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
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);
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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