Passed
Pull Request — master (#11)
by Michel
03:52
created

QueryNormalizerTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 131
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 6

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A testNormalizeWithStdClass() 0 9 1
A testNormalizeWithOptions() 0 12 1
A testNormalizeWithCircularReference() 0 13 1
A testNormalizeWithJsonSerializable() 0 12 1
A testInvalidArgumentExceptionThrownOnInvalidClass() 0 7 1
A testLogicExceptionThrownOnInvalidNormalizer() 0 10 1
A testSupportsDenormalizationReturnsFalse() 0 4 1
A testIfDenormalizeThrowsLogicException() 0 4 1
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
        $object->foo = 'bar';
0 ignored issues
show
Bug introduced by
The property foo does not seem to exist in ArrayObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
105
106
        $this->normalizer->normalize($object);
107
    }
108
109
    /**
110
     * @expectedException \Symfony\Component\Serializer\Exception\LogicException
111
     * @expectedExceptionMessage Cannot normalize object because injected serializer is not a normalizer
112
     * @return void
113
     */
114
    public function testLogicExceptionThrownOnInvalidNormalizer(): void
115
    {
116
        $object = new \stdClass();
117
        $object->foo = 'bar';
118
119
        $serializerMock = Mockery::mock('\Symfony\Component\Serializer\SerializerInterface');
120
        $this->normalizer->setSerializer($serializerMock);
121
122
        $this->normalizer->normalize($object);
123
    }
124
    
125
    /**
126
     * @return void
127
     */
128
    public function testSupportsDenormalizationReturnsFalse(): void
129
    {
130
        $this->assertFalse($this->normalizer->supportsDenormalization('foo', 'foo', 'foo'));
131
    }
132
133
    /**
134
     * @expectedException \Symfony\Component\Serializer\Exception\LogicException
135
     * @expectedExceptionMessage Cannot denormalize with "TBolier\RethinkQL\Serializer\QueryNormalizer".
136
     * @return void
137
     */
138
    public function testIfDenormalizeThrowsLogicException(): void
139
    {
140
        $this->normalizer->denormalize('foo', 'bar');
141
    }
142
}
143