Completed
Pull Request — master (#17)
by Nicolas
03:21
created

ConsumeContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
use Behat\Behat\Tester\Exception\PendingException;
4
use Puzzle\AMQP\Messages\Message;
5
use Puzzle\AMQP\Consumers\Insomniac;
6
use Puzzle\AMQP\Workers\Worker;
7
use Puzzle\AMQP\Workers\WorkerContext;
8
use Puzzle\AMQP\ReadableMessage;
9
use Psr\Log\LoggerAwareTrait;
10
use Psr\Log\NullLogger;
11
use Puzzle\AMQP\Workers\ProcessorInterfaceAdapter;
12
use Puzzle\AMQP\Messages\ContentType;
13
use Puzzle\AMQP\Messages\Bodies\Json;
14
15
class ConsumeContext extends AbstractRabbitMQContext implements Worker
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
16
{
17
    use LoggerAwareTrait;
18
    
19
    private
20
        $consumedMessages;
21
    
22
    public function __construct($path)
23
    {
24
        parent::__construct($path);
25
        
26
        $this->logger = new NullLogger();
27
        $this->consumedMessages = [];
28
    }
29
    
30
    /**
31
     * @Given The queue :queue contains the text message :bodyContent
32
     */
33
    public function theQueueContainsTheTextMessage($bodyContent, $queue)
0 ignored issues
show
Unused Code introduced by
The parameter $queue is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
    {
35
        // FIXME Use RabbitMQCTL instead
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "Use RabbitMQCTL instead"
Loading history...
36
        
37
        $message = new Message(self::TEXT_ROUTING_KEY);
38
        $message->setText($bodyContent);
39
        
40
        $this->client->publish($this->exchange, $message);
41
    }
42
43
    /**
44
     * @Given The queue :queue contains the json message :bodyContent
45
     */
46
    public function theQueueContainsTheJsonMessage($bodyContent, $queue)
0 ignored issues
show
Unused Code introduced by
The parameter $queue is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
    {
48
        // FIXME Use RabbitMQCTL instead
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "Use RabbitMQCTL instead"
Loading history...
49
        
50
        $message = new Message(self::JSON_ROUTING_KEY);
51
        
52
        $body = new Json();
53
        $body->changeContentWithJson($bodyContent);
54
        $message->setBody($body);
55
        
56
        $this->client->publish($this->exchange, $message);
57
    }
58
    
59
    /**
60
     * @When I consume all the messages in the queue :queue
61
     */
62
    public function iConsumeAllTheMessagesInTheQueue($queue)
63
    {
64
        $this->consumedMessages = [];
65
        $workerContext = new WorkerContext(
66
            function() {
67
                return $this;
68
            },
69
            $consumer = new Insomniac(),
70
            $queue
71
        );
72
        
73
        $processor = new ProcessorInterfaceAdapter($workerContext);
74
        $consumer->consume($processor, $this->client, $workerContext);
75
    }
76
    
77
    public function process(ReadableMessage $message)
78
    {
79
        $this->consumedMessages[] = $message;
80
    }
81
    
82
    /**
83
     * @Then /I have consumed (\d+) messages?/
84
     */
85
    public function iHaveConsumedMessage($nbMessages)
86
    {
87
        \PHPUnit_Framework_Assert::assertSame((int) $nbMessages, count($this->consumedMessages));
88
    }
89
    
90
    /**
91
     * @Then the message is a text one
92
     */
93 View Code Duplication
    public function theMessageIsATextOne()
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...
94
    {
95
        $firstMessage = $this->consumedMessages[0];
96
        
97
        \PHPUnit_Framework_Assert::assertSame($firstMessage->getRoutingKeyFromHeader(), self::TEXT_ROUTING_KEY);
98
        \PHPUnit_Framework_Assert::assertSame($firstMessage->getContentType(), ContentType::TEXT);
99
    }
100
    
101
    /**
102
     * @Then the message is a json one
103
     */
104 View Code Duplication
    public function theMessageIsAJsonOne()
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...
105
    {
106
        $firstMessage = $this->consumedMessages[0];
107
        
108
        \PHPUnit_Framework_Assert::assertSame($firstMessage->getRoutingKeyFromHeader(), self::JSON_ROUTING_KEY);
109
        \PHPUnit_Framework_Assert::assertSame($firstMessage->getContentType(), ContentType::JSON);
110
    }
111
    
112
    /**
113
     * @Then the message contains the json :jsonString
114
     */
115
    public function theMessageContainsTheJson($jsonString)
116
    {
117
        $this->theMessageContains(json_decode($jsonString, true));
118
    }
119
    
120
    /**
121
     * @Then the message contains :bodyContent
122
     */
123
    public function theMessageContains($bodyContent)
124
    {
125
        $firstMessage = $this->consumedMessages[0];
126
        
127
        \PHPUnit_Framework_Assert::assertSame($firstMessage->getBodyInOriginalFormat(), $bodyContent);
128
    }
129
130
    /**
131
     * @Then one of the messages is a text one
132
     */
133
    public function oneOfTheMessagesIsATextOne()
134
    {
135
        $this->oneOfTheMessagesIs(ContentType::TEXT, self::TEXT_ROUTING_KEY);
136
    }
137
138
    /**
139
     * @Then one of the messages is a json one
140
     */
141
    public function oneOfTheMessagesIsAJsonOne()
142
    {
143
        $this->oneOfTheMessagesIs(ContentType::JSON, self::JSON_ROUTING_KEY);
144
    }
145
    
146
    private function oneOfTheMessagesIs($contentType, $routingKey)
147
    {
148
        $found = null;
149
        
150
        foreach($this->consumedMessages as $message)
151
        {
152
            if($message->getContentType() === $contentType)
153
            {
154
                $found = $message;
155
                break;
156
            }
157
        }
158
        
159
        \PHPUnit_Framework_Assert::assertNotNull($found);
160
        \PHPUnit_Framework_Assert::assertSame($routingKey, $found->getRoutingKeyFromHeader());
161
    }
162
    
163
    /**
164
     * @Then one of the messages contains the json :jsonString
165
     */
166
    public function oneOfTheMessagesContainsTheJson($jsonString)
167
    {
168
        $this->oneOfTheMessagesContains(json_decode($jsonString, true));
169
    }
170
    
171
    /**
172
     * @Then one of the messages contains :bodyContent
173
     */
174
    public function oneOfTheMessagesContains($bodyContent)
175
    {
176
        $found = false;
177
        
178
        foreach($this->consumedMessages as $message)
179
        {
180
            if($message->getBodyInOriginalFormat() === $bodyContent)
181
            {
182
                $found = true;
183
                break;
184
            }
185
        }
186
        
187
        \PHPUnit_Framework_Assert::assertTrue($found);
188
    }
189
}
190