Passed
Pull Request — master (#17)
by Nicolas
03:18
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
namespace Puzzle\AMQP\Contexts;
4
5
use Puzzle\AMQP\Messages\Message;
6
use Puzzle\AMQP\Consumers\Insomniac;
7
use Puzzle\AMQP\Workers\Worker;
8
use Puzzle\AMQP\Workers\WorkerContext;
9
use Puzzle\AMQP\ReadableMessage;
10
use Psr\Log\LoggerAwareTrait;
11
use Psr\Log\NullLogger;
12
use Puzzle\AMQP\Workers\ProcessorInterfaceAdapter;
13
use Puzzle\AMQP\Messages\ContentType;
14
use Puzzle\AMQP\Messages\Bodies\Json;
15
16
class ConsumeContext extends AbstractRabbitMQContext implements Worker
17
{
18
    use LoggerAwareTrait;
19
    
20
    private
21
        $consumedMessages;
22
    
23
    public function __construct($path)
24
    {
25
        parent::__construct($path);
26
        
27
        $this->logger = new NullLogger();
28
        $this->consumedMessages = [];
29
    }
30
    
31
    /**
32
     * @Given The queue :queue contains the text message :bodyContent
33
     */
34
    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...
35
    {
36
        // FIXME Use RabbitMQCTL instead
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "Use RabbitMQCTL instead"
Loading history...
37
        
38
        $message = new Message(self::TEXT_ROUTING_KEY);
39
        $message->setText($bodyContent);
40
        
41
        $this->client->publish($this->exchange, $message);
42
    }
43
44
    /**
45
     * @Given The queue :queue contains the json message :bodyContent
46
     */
47
    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...
48
    {
49
        // FIXME Use RabbitMQCTL instead
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "Use RabbitMQCTL instead"
Loading history...
50
        
51
        $message = new Message(self::JSON_ROUTING_KEY);
52
        
53
        $body = new Json();
54
        $body->changeContentWithJson($bodyContent);
55
        $message->setBody($body);
56
        
57
        $this->client->publish($this->exchange, $message);
58
    }
59
    
60
    /**
61
     * @When I consume all the messages in the queue :queue
62
     */
63
    public function iConsumeAllTheMessagesInTheQueue($queue)
64
    {
65
        $this->consumedMessages = [];
66
        $workerContext = new WorkerContext(
67
            function() {
68
                return $this;
69
            },
70
            $consumer = new Insomniac(),
71
            $queue
72
        );
73
        
74
        $processor = new ProcessorInterfaceAdapter($workerContext);
75
        $consumer->consume($processor, $this->client, $workerContext);
76
    }
77
    
78
    public function process(ReadableMessage $message)
79
    {
80
        $this->consumedMessages[] = $message;
81
    }
82
    
83
    /**
84
     * @Then /I have consumed (\d+) messages?/
85
     */
86
    public function iHaveConsumedMessage($nbMessages)
87
    {
88
        \PHPUnit_Framework_Assert::assertSame((int) $nbMessages, count($this->consumedMessages));
89
    }
90
    
91
    /**
92
     * @Then the message is a text one
93
     */
94
    public function theMessageIsATextOne()
95
    {
96
        $this->theMessageIs(self::TEXT_ROUTING_KEY, ContentType::TEXT);
97
    }
98
    
99
    /**
100
     * @Then the message is a json one
101
     */
102
    public function theMessageIsAJsonOne()
103
    {
104
        $this->theMessageIs(self::JSON_ROUTING_KEY, ContentType::JSON);
105
    }
106
    
107
    private function theMessageIs($routingKey, $contentType)
108
    {
109
        $firstMessage = $this->consumedMessages[0];
110
        
111
        \PHPUnit_Framework_Assert::assertSame($firstMessage->getRoutingKeyFromHeader(), $routingKey);
112
        \PHPUnit_Framework_Assert::assertSame($firstMessage->getContentType(), $contentType);
113
    }
114
    
115
    /**
116
     * @Then the message contains the json :jsonString
117
     */
118
    public function theMessageContainsTheJson($jsonString)
119
    {
120
        $this->theMessageContains(json_decode($jsonString, true));
121
    }
122
    
123
    /**
124
     * @Then the message contains :bodyContent
125
     */
126
    public function theMessageContains($bodyContent)
127
    {
128
        $firstMessage = $this->consumedMessages[0];
129
        
130
        \PHPUnit_Framework_Assert::assertSame($firstMessage->getBodyInOriginalFormat(), $bodyContent);
131
    }
132
133
    /**
134
     * @Then one of the messages is a text one
135
     */
136
    public function oneOfTheMessagesIsATextOne()
137
    {
138
        $this->oneOfTheMessagesIs(ContentType::TEXT, self::TEXT_ROUTING_KEY);
139
    }
140
141
    /**
142
     * @Then one of the messages is a json one
143
     */
144
    public function oneOfTheMessagesIsAJsonOne()
145
    {
146
        $this->oneOfTheMessagesIs(ContentType::JSON, self::JSON_ROUTING_KEY);
147
    }
148
    
149
    private function oneOfTheMessagesIs($contentType, $routingKey)
150
    {
151
        $found = null;
152
        
153
        foreach($this->consumedMessages as $message)
154
        {
155
            if($message->getContentType() === $contentType)
156
            {
157
                $found = $message;
158
                break;
159
            }
160
        }
161
        
162
        \PHPUnit_Framework_Assert::assertNotNull($found);
163
        \PHPUnit_Framework_Assert::assertSame($routingKey, $found->getRoutingKeyFromHeader());
164
    }
165
    
166
    /**
167
     * @Then one of the messages contains the json :jsonString
168
     */
169
    public function oneOfTheMessagesContainsTheJson($jsonString)
170
    {
171
        $this->oneOfTheMessagesContains(json_decode($jsonString, true));
172
    }
173
    
174
    /**
175
     * @Then one of the messages contains :bodyContent
176
     */
177
    public function oneOfTheMessagesContains($bodyContent)
178
    {
179
        $found = false;
180
        
181
        foreach($this->consumedMessages as $message)
182
        {
183
            if($message->getBodyInOriginalFormat() === $bodyContent)
184
            {
185
                $found = true;
186
                break;
187
            }
188
        }
189
        
190
        \PHPUnit_Framework_Assert::assertTrue($found);
191
    }
192
}
193