QueueManagerTest::mockQueueManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace SfCod\QueueBundle\Tests\Service;
4
5
use PHPUnit\Framework\TestCase;
6
use SfCod\QueueBundle\Connector\ConnectorInterface;
7
use SfCod\QueueBundle\Queue\QueueInterface;
8
use SfCod\QueueBundle\Service\QueueManager;
9
10
/**
11
 * Class QueueManagerTest
12
 *
13
 * @author Virchenko Maksim <[email protected]>
14
 *
15
 * @package SfCod\QueueBundle\Tests\Service
16
 */
17
class QueueManagerTest extends TestCase
18
{
19
    /**
20
     * Test manager connection
21
     */
22
    public function testConnection()
23
    {
24
        $driver = uniqid('driver_');
25
        $connectionName = uniqid('connection_');
26
27
        $queueManager = $this->mockQueueManager($driver, $connectionName);
28
29
        self::assertFalse($queueManager->connected($connectionName));
30
31
        $queue = $queueManager->connection($connectionName);
32
33
        self::assertInstanceOf(QueueInterface::class, $queue);
34
        self::assertTrue($queueManager->connected($connectionName));
35
    }
36
37
    /**
38
     * Mock queue manager
39
     *
40
     * @param string $driver
41
     * @param string $connectionName
42
     *
43
     * @return QueueManager
44
     */
45
    private function mockQueueManager(string $driver, string $connectionName): QueueManager
46
    {
47
        $config = [
48
            'driver' => $driver,
49
            'collection' => uniqid('collection_'),
50
            'queue' => uniqid('queue_'),
51
            'expire' => rand(60, 3600),
52
            'limit' => rand(1, 10),
53
        ];
54
55
        $queue = $this->createMock(QueueInterface::class);
56
        $queue
57
            ->expects(self::once())
58
            ->method('setConnectionName')
59
            ->with(self::equalTo($connectionName))
60
            ->will(self::returnSelf());
61
62
        $connector = $this->createMock(ConnectorInterface::class);
63
        $connector
64
            ->expects(self::once())
65
            ->method('connect')
66
            ->with(self::equalTo($config))
67
            ->willReturn($queue);
68
69
        $queueManager = new QueueManager();
70
71
        $queueManager->addConnector($driver, $connector);
0 ignored issues
show
Documentation introduced by
$connector is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<SfCod\QueueBundle...tor\ConnectorInterface>.

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...
72
        $queueManager->addConnection($config, $connectionName);
73
74
        return $queueManager;
75
    }
76
}
77