Completed
Push — master ( ccf1c4...6b1cb7 )
by Agaletskiy
03:57
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\MockObject\MockObject;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\Validator\ConstraintViolationListInterface;
10
use Symfony\Component\Validator\Validator\ValidatorInterface;
11
12
/**
13
 * @group unit
14
 * @covers \Lamoda\AtolClient\Converter\ObjectConverter
15
 */
16
class ObjectConverterTest extends TestCase
17
{
18
    /**
19
     * @var SerializerInterface | MockObject
20
     */
21
    private $serializer;
22
23
    /**
24
     * @var ValidatorInterface | MockObject
25
     */
26
    private $validator;
27
28
    /**
29
     * @var ObjectConverter
30
     */
31
    private $objectConverter;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected function setUp()
37
    {
38
        $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...
39
        $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...
40
        $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...
41
    }
42
43
    public function testGetRequestString()
44
    {
45
        $object = (object) [];
46
        $json = '{}';
47
48
        /* @see ObjectConverter::assertValid() */
49
        $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...
50
            ->expects($this->once())
51
            ->method('validate')
52
            ->with($object)
53
            ->willReturn([]);
54
55
        /* @see ObjectConverter::serializeBodyObject() */
56
        $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...
57
            ->expects($this->once())
58
            ->method('serialize')
59
            ->with($object, 'atol_client', $this->anything())
60
            ->willReturn($json);
61
62
        $result = $this->objectConverter->getRequestString($object);
63
        $this->assertSame($json, $result);
64
    }
65
66
    /**
67
     * @expectedException \Lamoda\AtolClient\Exception\ParseException
68
     */
69
    public function testGetRequestStringParseException()
70
    {
71
        /* @see ObjectConverter::assertValid() */
72
        $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...
73
            ->method('validate')
74
            ->willReturn([]);
75
76
        /* @see ObjectConverter::serializeBodyObject() */
77
        $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...
78
            ->expects($this->once())
79
            ->method('serialize')
80
            ->willThrowException(new \RuntimeException());
81
82
        $this->objectConverter->getRequestString((object) []);
83
    }
84
85
    /**
86
     * @expectedException \Lamoda\AtolClient\Exception\ValidationException
87
     * @expectedExceptionCode 2
88
     */
89
    public function testGetRequestStringInvalid()
90
    {
91
        $object = (object) [];
92
        $errors = $this->createMock(ConstraintViolationListInterface::class);
93
94
        /* @see ObjectConverter::assertValid() */
95
        $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...
96
            ->expects($this->once())
97
            ->method('validate')
98
            ->with($object)
99
            ->willReturn($errors);
100
        $errors
101
            ->expects($this->once())
102
            ->method('count')
103
            ->willReturn(1);
104
105
        $this->objectConverter->getRequestString($object);
106
    }
107
108
    public function testGetResponseObject()
109
    {
110
        $class = 'class';
111
        $json = 'json';
112
        $object = (object) [];
113
114
        /* @see ObjectConverter::deserialize() */
115
        $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...
116
            ->expects($this->once())
117
            ->method('deserialize')
118
            ->with($json, $class, 'atol_client')
119
            ->willReturn($object);
120
        /* @see ObjectConverter::assertValid() */
121
        $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...
122
            ->expects($this->once())
123
            ->method('validate')
124
            ->with($object)
125
            ->willReturn([]);
126
127
        $result = $this->objectConverter->getResponseObject($class, $json);
128
        $this->assertSame($object, $result);
129
    }
130
131
    /**
132
     * @expectedException \Lamoda\AtolClient\Exception\ParseException
133
     * @expectedExceptionCode 2
134
     */
135
    public function testGetResponseObjectParseException()
136
    {
137
        /* @see ObjectConverter::deserialize() */
138
        $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...
139
            ->method('deserialize')
140
            ->willThrowException(new \RuntimeException());
141
142
        $this->objectConverter->getResponseObject('class', 'json');
143
    }
144
145
    /**
146
     * @expectedException \Lamoda\AtolClient\Exception\ValidationException
147
     * @expectedExceptionCode 2
148
     */
149
    public function testGetResponseObjectInvalid()
150
    {
151
        $errors = $this->createMock(ConstraintViolationListInterface::class);
152
153
        /* @see ObjectConverter::assertValid() */
154
        $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...
155
            ->method('deserialize')
156
            ->willReturn((object) []);
157
        $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...
158
            ->method('validate')
159
            ->willReturn($errors);
160
        $errors
161
            ->expects($this->once())
162
            ->method('count')
163
            ->willReturn(1);
164
165
        $this->objectConverter->getResponseObject('class', 'json');
166
    }
167
}
168