Completed
Branch master (c3d959)
by Alexandre
09:54
created

AmqpAdapterTest::setUp()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 36
rs 8.8571
cc 3
eloc 22
nc 4
nop 0
1
<?php
2
3
use Heri\Bundle\JobQueueBundle\Tests\TestCase;
4
use Heri\Bundle\JobQueueBundle\Adapter as Adapter;
5
use Heri\Bundle\JobQueueBundle\Service\QueueService;
6
use Heri\Bundle\JobQueueBundle\Command\QueueListenCommand;
7
use Symfony\Bundle\FrameworkBundle\Console\Application;
8
use Symfony\Component\Console\Output\ConsoleOutput;
9
10
class AmqpAdapterTest extends TestCase
11
{
12
    /**
13
     * @var QueueService
14
     */
15
    protected $queue;
16
17
    /**
18
     * @var string
19
     */
20
    protected $queueName = 'my:queue';
21
22
    /**
23
     * @var int
24
     */
25
    protected $maxMessages = 1;
26
27
    /**
28
     * @var AMQPConnection
29
     */
30
    protected $connection;
31
32
    /**
33
     * @var AMQPChannel
34
     */
35
    protected $channel;
36
37
    /**
38
     * @var bool
39
     */
40
    protected $verbose = true;
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function setUp()
46
    {
47
        parent::setUp();
48
49
        if (!class_exists('PhpAmqpLib\Message\AMQPMessage')) {
50
            $this->markTestSkipped('AMQP not installed');
51
        }
52
53
        // set default rabbitmq configuration
54
        $adapter = new Adapter\AmqpAdapter(array(
55
            'host' => 'localhost',
56
            'port' => '5672',
57
            'user' => 'guest',
58
            'password' => 'guest',
59
        ));
60
61
        $this->queue = new QueueService(
62
            $this->container->get('logger'),
63
            $this->container->getParameter('jobqueue.config')
64
        );
65
        $this->queue->adapter = $adapter;
66
        $this->queue->attach($this->queueName.'1');
67
68
        $application = new Application($this->kernel);
69
        $application->add(new QueueListenCommand());
70
71
        $command = $application->find('jobqueue:listen');
72
        $this->queue->setCommand($command);
0 ignored issues
show
Compatibility introduced by
$command of type object<Symfony\Component\Console\Command\Command> is not a sub-type of object<Symfony\Bundle\Fr...\ContainerAwareCommand>. It seems like you assume a child class of the class Symfony\Component\Console\Command\Command to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
73
74
        if ($this->verbose) {
75
            $this->queue->setOutput(new ConsoleOutput());
76
        }
77
78
        $this->channel = $adapter->getChannel();
0 ignored issues
show
Documentation Bug introduced by
It seems like $adapter->getChannel() of type object<PhpAmqpLib\Connection\AMQPConnection> is incompatible with the declared type object<AMQPChannel> of property $channel.

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...
79
        $this->connection = $adapter->getConnection();
0 ignored issues
show
Documentation Bug introduced by
It seems like $adapter->getConnection() of type object<PhpAmqpLib\Connection\AMQPConnection> is incompatible with the declared type object<AMQPConnection> of property $connection.

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...
80
    }
81
82
    public function testPushAndReceive()
83
    {
84
        // Queue list command
85
        $command1 = array(
86
            'command' => 'list',
87
        );
88
        $this->queue->push($command1);
89
90
        // Queue demo:great command
91
        $command2 = [
92
            'command' => 'demo:great',
93
            'argument' => [
94
                'name' => 'Alexandre',
95
                '--yell' => true,
96
            ],
97
        ];
98
        $this->queue->push($command2);
99
100
        // Run list command using directly receive method
101
        $this->queue->receive($this->maxMessages);
102
103
        // Run demo:great command using listen method
104
        try {
105
            $this->queue->listen($this->queueName.'1');
106
            // $this->queue->receive($this->maxMessages);
107
        } catch (\Exception $e) {
108
            $this->assertRegExp(
109
                '/There are no commands defined in the "demo" namespace/',
110
                $e->getMessage(),
111
                'Command not found'
112
            );
113
        }
114
    }
115
116
    public function testPerf()
117
    {
118
        for ($i = 0; $i < 100; $i++) {
119
            // Queue list command
120
            $command = [
121
                'command' => 'list',
122
            ];
123
            $this->queue->push($command);
124
            $this->queue->receive(10);
125
        }
126
    }
127
128
    public function tearDown()
129
    {
130
        if ($this->channel) {
131
            $this->channel->queue_delete($this->queueName.'1');
132
            $this->channel->close();
133
        }
134
        if ($this->connection) {
135
            $this->connection->close();
136
        }
137
    }
138
}
139