SqlConsumerTest::getSerializer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
namespace Ajir\RabbitMqSqlBundle\Tests\AMQP;
3
4
use Exception;
5
use InvalidArgumentException;
6
use JMS\Serializer\SerializerBuilder;
7
use Ajir\RabbitMqSqlBundle\AMQP\SqlConsumer;
8
use Ajir\RabbitMqSqlBundle\DataTransformer\DataTransformerInterface;
9
use Ajir\RabbitMqSqlBundle\DataStructure\Message\AMQPMessage as GnaaMessage;
10
use PhpAmqpLib\Message\AMQPMessage;
11
12
/**
13
 * Class SqlConsumerTest
14
 *
15
 * @author Florian Ajir <[email protected]>
16
 */
17
class SqlConsumerTest extends \PHPUnit_Framework_TestCase
18
{
19
    public function testExecuteWithoutError()
20
    {
21
        $transformer = $this->getTransformerMock();
22
        $persister = $this->getPersisterMock();
23
        $serializer = $this->getSerializer();
24
        $logger = $this->getLoggerMock();
25
        $logger
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Log\LoggerInterface>.

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...
26
            ->expects($this->once())
27
            ->method('info')
28
        ;
29
        $consumer = new SqlConsumer($transformer, $persister, $serializer);
0 ignored issues
show
Bug introduced by
It seems like $transformer defined by $this->getTransformerMock() on line 21 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Ajir\RabbitMqSqlBundle\A...Consumer::__construct() does only seem to accept object<Ajir\RabbitMqSqlB...taTransformerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
30
        $consumer->setLogger($logger);
31
        $data = new GnaaMessage();
32
        $data
33
            ->setType(uniqid())
34
            ->setData($serializer->serialize(array(), 'json'));
35
        $msg = new AMQPMessage();
36
        $msg->setBody($serializer->serialize($data, 'json'));
37
        $result = $consumer->execute($msg);
38
        $this->assertTrue($result);
39
    }
40
41
    /**
42
     * @param int $nbCallsPrepare
43
     * @param \PHPUnit_Framework_MockObject_Stub $returned
44
     *
45
     * @return \PHPUnit_Framework_MockObject_MockObject|DataTransformerInterface
46
     */
47
    private function getTransformerMock($nbCallsPrepare = 1, $returned = null)
48
    {
49
        if (is_null($returned)) {
50
            $returned = $this->returnValue(array());
51
        }
52
        $transformer = $this->getMock('Ajir\RabbitMqSqlBundle\DataTransformer\DataTransformerInterface');
53
        $transformer
54
            ->expects($this->exactly($nbCallsPrepare))
55
            ->method('prepare')
56
            ->will($returned);
57
58
        return $transformer;
59
    }
60
61
    /**
62
     * @return \Ajir\RabbitMqSqlBundle\Persister\PersisterInterface
63
     */
64
    private function getPersisterMock()
65
    {
66
        $persister = $this->getMock('Ajir\RabbitMqSqlBundle\Persister\PersisterInterface');
67
68
        return $persister;
69
    }
70
71
    /**
72
     * @return \Psr\Log\LoggerInterface
73
     */
74
    private function getLoggerMock()
75
    {
76
        $logger = $this
77
            ->getMockBuilder('Psr\Log\LoggerInterface')
78
            ->disableOriginalConstructor()
79
            ->getMock();
80
81
        return $logger;
82
    }
83
84
    /**
85
     * @return \JMS\Serializer\Serializer
86
     */
87
    private function getSerializer()
88
    {
89
        $serializer = SerializerBuilder::create()->build();
90
91
        return $serializer;
92
    }
93
94
    /**
95
     *
96
     */
97 View Code Duplication
    public function testExecuteWithInvalidArgumentThrew()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        $transformer = $this->getTransformerMock(1, $this->throwException(new InvalidArgumentException()));
100
        $persister = $this->getPersisterMock();
101
        $serializer = $this->getSerializer();
102
        $logger = $this->getLoggerMock();
103
        $logger
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Log\LoggerInterface>.

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...
104
            ->expects($this->once())
105
            ->method('info')
106
        ;
107
        $logger
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Log\LoggerInterface>.

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...
108
            ->expects($this->once())
109
            ->method('warning')
110
        ;
111
        $consumer = new SqlConsumer($transformer, $persister, $serializer);
0 ignored issues
show
Bug introduced by
It seems like $transformer defined by $this->getTransformerMoc...idArgumentException())) on line 99 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Ajir\RabbitMqSqlBundle\A...Consumer::__construct() does only seem to accept object<Ajir\RabbitMqSqlB...taTransformerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
112
        $consumer->setLogger($logger);
113
        $data = new GnaaMessage();
114
        $data
115
            ->setType(uniqid())
116
            ->setData($serializer->serialize(array(), 'json'));
117
        $msg = new AMQPMessage($serializer->serialize($data, 'json'));
118
        $result = $consumer->execute($msg);
119
        $this->assertFalse($result);
120
    }
121
122
    /**
123
     *
124
     */
125 View Code Duplication
    public function testExecuteWithExceptionThrew()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        $transformer = $this->getTransformerMock(1, $this->throwException(new Exception()));
128
        $persister = $this->getPersisterMock();
129
        $serializer = $this->getSerializer();
130
        $logger = $this->getLoggerMock();
131
        $logger
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Log\LoggerInterface>.

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...
132
            ->expects($this->once())
133
            ->method('info')
134
        ;
135
        $logger
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Log\LoggerInterface>.

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...
136
            ->expects($this->once())
137
            ->method('error')
138
        ;
139
        $consumer = new SqlConsumer($transformer, $persister, $serializer);
0 ignored issues
show
Bug introduced by
It seems like $transformer defined by $this->getTransformerMoc...tion(new \Exception())) on line 127 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Ajir\RabbitMqSqlBundle\A...Consumer::__construct() does only seem to accept object<Ajir\RabbitMqSqlB...taTransformerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
140
        $consumer->setLogger($logger);
141
        $data = new GnaaMessage();
142
        $data
143
            ->setType(uniqid())
144
            ->setData($serializer->serialize(array(), 'json'));
145
        $msg = new AMQPMessage();
146
        $msg->body = $serializer->serialize($data, 'json');
147
        $this->setExpectedException('Exception');
148
        $consumer->execute($msg);
149
    }
150
151
    /**
152
     *
153
     */
154
    public function testExecuteWithIgnoredType()
155
    {
156
        $type = 'galenical';
157
        $transformer = $this->getTransformerMock(0);
158
        $persister = $this->getPersisterMock();
159
        $serializer = $this->getSerializer();
160
        $logger = $this->getLoggerMock();
161
        $logger
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Log\LoggerInterface>.

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...
162
            ->expects($this->once())
163
            ->method('info')
164
        ;
165
        $consumer = new SqlConsumer($transformer, $persister, $serializer, array($type));
0 ignored issues
show
Bug introduced by
It seems like $transformer defined by $this->getTransformerMock(0) on line 157 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Ajir\RabbitMqSqlBundle\A...Consumer::__construct() does only seem to accept object<Ajir\RabbitMqSqlB...taTransformerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
166
        $consumer->setLogger($logger);
167
        $data = new GnaaMessage();
168
        $data
169
            ->setType($type)
170
            ->setData($serializer->serialize(array(), 'json'));
171
        $msg = new AMQPMessage($serializer->serialize($data, 'json'));
172
        $result = $consumer->execute($msg);
173
        $this->assertTrue($result);
174
    }
175
}
176