Test Setup Failed
Push — master ( 298fd3...56daff )
by Matthew
17:14
created

JobManagerTest::setUpBeforeClass()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 29
nc 4
nop 0
1
<?php
2
3
namespace Dtc\QueueBundle\Tests\RabbitMQ;
4
5
use Dtc\QueueBundle\RabbitMQ\JobManager;
6
use Dtc\QueueBundle\Tests\FibonacciWorker;
7
use Dtc\QueueBundle\Tests\Model\BaseJobManagerTest;
8
use PhpAmqpLib\Connection\AMQPStreamConnection;
9
10
/**
11
 * @author David
12
 *
13
 * This test requires local beanstalkd running
14
 */
15
class JobManagerTest extends BaseJobManagerTest
16
{
17
    public static $connection;
18
19
    public static function setUpBeforeClass()
20
    {
21
        $className = 'Dtc\QueueBundle\RabbitMQ\Job';
22
23
        $host = getenv('RABBIT_MQ_HOST');
24
        $port = 5672;
25
        $user = 'guest';
26
        $pass = 'guest';
27
        $vhost = '/';
28
        self::$connection = new AMQPStreamConnection($host, $port, $user, $pass, $vhost);
29
30
        self::$jobManager = new JobManager();
31
        self::$jobManager->setAMQPConnection(self::$connection);
32
        self::$jobManager->setQueueArgs('dtc_queue', false, true, false, false, 255);
33
        self::$jobManager->setExchangeArgs('dtc_queue_exchange', 'direct', false, true, false);
34
        $channel = self::$connection->channel();
35
        $channel->queue_delete('dtc_queue');
36
        $channel->close();
37
        self::$worker = new FibonacciWorker();
38
        self::$worker->setJobClass($className);
39
        self::$jobManager->setupChannel();
40
        $channel = self::$jobManager->getChannel();
41
        $drained = 0;
42
        do {
43
            $message = $channel->basic_get('dtc_queue');
44
            if ($message) {
45
                $channel->basic_ack($message->delivery_info['delivery_tag']);
46
                ++$drained;
47
            }
48
        } while ($message);
49
50
        if ($drained) {
51
            echo "\nRabbitMQ - drained $drained prior to start of test";
52
        }
53
        parent::setUpBeforeClass();
54
    }
55
56
    /**
57
     * RabbitMQ has no ability to delete specific messages off the queue.
58
     */
59
    public function testDeleteJob()
60
    {
61
        $job = new self::$jobClass(self::$worker, false, null);
62
        $job->fibonacci(1);
63
        $this->assertNotNull($job->getId(), 'Job id should be generated');
64
65
        try {
66
            self::$jobManager->deleteJob($job);
67
            $this->fail('expected exception');
68
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
69
        }
70
        self::$jobManager->getJob();
71
    }
72
73 View Code Duplication
    public function testSaveJob()
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...
74
    {
75
        $job = new self::$jobClass(self::$worker, false, null);
76
        $job->fibonacci(1);
77
        $this->assertNotNull($job->getId(), 'Job id should be generated');
78
79
        $jobInQueue = self::$jobManager->getJob();
80
        $this->assertNotNull($jobInQueue, 'There should be a job.');
81
        $this->assertEquals(
82
            $job->getId(),
83
            $jobInQueue->getId(),
84
            'Job id returned by manager should be the same'
85
        );
86
    }
87
}
88