Completed
Pull Request — master (#17)
by Nicolas
04:26 queued 01:13
created

ConsumeContext::oneOfTheMessagesIs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
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
    public function theMessageIsATextOne()
94
    {
95
        $this->theMessageIs(self::TEXT_ROUTING_KEY, ContentType::TEXT);
96
    }
97
    
98
    /**
99
     * @Then the message is a json one
100
     */
101
    public function theMessageIsAJsonOne()
102
    {
103
        $this->theMessageIs(self::JSON_ROUTING_KEY, ContentType::JSON);
104
    }
105
    
106
    private function theMessageIs($routingKey, $contentType)
107
    {
108
        $firstMessage = $this->consumedMessages[0];
109
        
110
        \PHPUnit_Framework_Assert::assertSame($firstMessage->getRoutingKeyFromHeader(), $routingKey);
111
        \PHPUnit_Framework_Assert::assertSame($firstMessage->getContentType(), $contentType);
112
    }
113
    
114
    /**
115
     * @Then the message contains the json :jsonString
116
     */
117
    public function theMessageContainsTheJson($jsonString)
118
    {
119
        $this->theMessageContains(json_decode($jsonString, true));
120
    }
121
    
122
    /**
123
     * @Then the message contains :bodyContent
124
     */
125
    public function theMessageContains($bodyContent)
126
    {
127
        $firstMessage = $this->consumedMessages[0];
128
        
129
        \PHPUnit_Framework_Assert::assertSame($firstMessage->getBodyInOriginalFormat(), $bodyContent);
130
    }
131
132
    /**
133
     * @Then one of the messages is a text one
134
     */
135
    public function oneOfTheMessagesIsATextOne()
136
    {
137
        $this->oneOfTheMessagesIs(ContentType::TEXT, self::TEXT_ROUTING_KEY);
138
    }
139
140
    /**
141
     * @Then one of the messages is a json one
142
     */
143
    public function oneOfTheMessagesIsAJsonOne()
144
    {
145
        $this->oneOfTheMessagesIs(ContentType::JSON, self::JSON_ROUTING_KEY);
146
    }
147
    
148
    private function oneOfTheMessagesIs($contentType, $routingKey)
149
    {
150
        $found = null;
151
        
152
        foreach($this->consumedMessages as $message)
153
        {
154
            if($message->getContentType() === $contentType)
155
            {
156
                $found = $message;
157
                break;
158
            }
159
        }
160
        
161
        \PHPUnit_Framework_Assert::assertNotNull($found);
162
        \PHPUnit_Framework_Assert::assertSame($routingKey, $found->getRoutingKeyFromHeader());
163
    }
164
    
165
    /**
166
     * @Then one of the messages contains the json :jsonString
167
     */
168
    public function oneOfTheMessagesContainsTheJson($jsonString)
169
    {
170
        $this->oneOfTheMessagesContains(json_decode($jsonString, true));
171
    }
172
    
173
    /**
174
     * @Then one of the messages contains :bodyContent
175
     */
176
    public function oneOfTheMessagesContains($bodyContent)
177
    {
178
        $found = false;
179
        
180
        foreach($this->consumedMessages as $message)
181
        {
182
            if($message->getBodyInOriginalFormat() === $bodyContent)
183
            {
184
                $found = true;
185
                break;
186
            }
187
        }
188
        
189
        \PHPUnit_Framework_Assert::assertTrue($found);
190
    }
191
}
192