Passed
Pull Request — master (#3)
by Evgeny
06:38
created

ObjectConverterTest::testResponseToArrayParseException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Lamoda\AtolClient\Tests\Unit\Converter;
4
5
use Lamoda\AtolClient\Converter\ObjectConverter;
6
use JMS\Serializer\SerializerInterface;
7
use PHPUnit\Framework\TestCase;
8
use Symfony\Component\Validator\ConstraintViolationListInterface;
9
use Symfony\Component\Validator\Validator\ValidatorInterface;
10
11
/**
12
 * @group unit
13
 * @covers \Lamoda\AtolClient\Converter\ObjectConverter
14
 */
15
class ObjectConverterTest extends TestCase
16
{
17
    /**
18
     * @var SerializerInterface | \PHPUnit_Framework_MockObject_MockObject
19
     */
20
    private $serializer;
21
22
    /**
23
     * @var ValidatorInterface | \PHPUnit_Framework_MockObject_MockObject
24
     */
25
    private $validator;
26
27
    /**
28
     * @var ObjectConverter
29
     */
30
    private $objectConverter;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function setUp()
36
    {
37
        $this->serializer = $this->createMock(SerializerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\JMS\S...alizerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<JMS\Serializer\SerializerInterface> of property $serializer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
        $this->validator = $this->createMock(ValidatorInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...idatorInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...tor\ValidatorInterface> of property $validator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39
        $this->objectConverter = new ObjectConverter($this->serializer, $this->validator);
0 ignored issues
show
Documentation introduced by
$this->serializer is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<JMS\Serializer\SerializerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->validator is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...tor\ValidatorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40
    }
41
42
    public function testGetRequestString()
43
    {
44
        $object = (object) [];
45
        $json = '{}';
46
47
        /* @see ObjectConverter::assertValid() */
48
        $this->validator
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...tor\ValidatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
            ->expects($this->once())
50
            ->method('validate')
51
            ->with($object)
52
            ->willReturn([]);
53
54
        /* @see ObjectConverter::serializeBodyObject() */
55
        $this->serializer
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JMS\Serializer\SerializerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
            ->expects($this->once())
57
            ->method('serialize')
58
            ->with($object, 'atol_client', $this->anything())
59
            ->willReturn($json);
60
61
        $result = $this->objectConverter->getRequestString($object);
62
        $this->assertSame($json, $result);
63
    }
64
65
    /**
66
     * @expectedException \Lamoda\AtolClient\Exception\ParseException
67
     */
68
    public function testGetRequestStringParseException()
69
    {
70
        /* @see ObjectConverter::assertValid() */
71
        $this->validator
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...tor\ValidatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
            ->method('validate')
73
            ->willReturn([]);
74
75
        /* @see ObjectConverter::serializeBodyObject() */
76
        $this->serializer
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JMS\Serializer\SerializerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
            ->expects($this->once())
78
            ->method('serialize')
79
            ->willThrowException(new \RuntimeException());
80
81
        $this->objectConverter->getRequestString((object) []);
82
    }
83
84
    /**
85
     * @expectedException \Lamoda\AtolClient\Exception\ValidationException
86
     * @expectedExceptionCode 2
87
     */
88
    public function testGetRequestStringInvalid()
89
    {
90
        $object = (object) [];
91
        $errors = $this->createMock(ConstraintViolationListInterface::class);
92
93
        /* @see ObjectConverter::assertValid() */
94
        $this->validator
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...tor\ValidatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
            ->expects($this->once())
96
            ->method('validate')
97
            ->with($object)
98
            ->willReturn($errors);
99
        $errors
100
            ->expects($this->once())
101
            ->method('count')
102
            ->willReturn(1);
103
104
        $this->objectConverter->getRequestString($object);
105
    }
106
107
    public function testGetResponseObject()
108
    {
109
        $class = 'class';
110
        $json = 'json';
111
        $object = (object) [];
112
113
        /* @see ObjectConverter::deserialize() */
114
        $this->serializer
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JMS\Serializer\SerializerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
            ->expects($this->once())
116
            ->method('deserialize')
117
            ->with($json, $class, 'atol_client')
118
            ->willReturn($object);
119
        /* @see ObjectConverter::assertValid() */
120
        $this->validator
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...tor\ValidatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
121
            ->expects($this->once())
122
            ->method('validate')
123
            ->with($object)
124
            ->willReturn([]);
125
126
        $result = $this->objectConverter->getResponseObject($class, $json);
127
        $this->assertSame($object, $result);
128
    }
129
130
    /**
131
     * @expectedException \Lamoda\AtolClient\Exception\ParseException
132
     * @expectedExceptionCode 2
133
     */
134
    public function testGetResponseObjectParseException()
135
    {
136
        /* @see ObjectConverter::deserialize() */
137
        $this->serializer
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<JMS\Serializer\SerializerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
138
            ->method('deserialize')
139
            ->willThrowException(new \RuntimeException());
140
141
        $this->objectConverter->getResponseObject('class', 'json');
142
    }
143
144
    /**
145
     * @expectedException \Lamoda\AtolClient\Exception\ValidationException
146
     * @expectedExceptionCode 2
147
     */
148
    public function testGetResponseObjectInvalid()
149
    {
150
        $errors = $this->createMock(ConstraintViolationListInterface::class);
151
152
        /* @see ObjectConverter::assertValid() */
153
        $this->serializer
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<JMS\Serializer\SerializerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
154
            ->method('deserialize')
155
            ->willReturn((object) []);
156
        $this->validator
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...tor\ValidatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
157
            ->method('validate')
158
            ->willReturn($errors);
159
        $errors
160
            ->expects($this->once())
161
            ->method('count')
162
            ->willReturn(1);
163
164
        $this->objectConverter->getResponseObject('class', 'json');
165
    }
166
}
167