Completed
Pull Request — v2 (#16)
by Faouzi
02:28
created

FeatureContext::createExchangeAndQueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace TestContext;
4
5
use Behat\Behat\Context\SnippetAcceptingContext;
6
use ETNA\FeatureContext\BaseContext;
7
use PhpAmqpLib\Message\AMQPMessage;
8
9
/**
10
 * Features context
11
 */
12
class FeatureContext extends BaseContext implements SnippetAcceptingContext
13
{
14
    /**
15
     * @BeforeScenario
16
     */
17
    public static function createExchangeAndQueue()
18
    {
19
        $channel = self::$silex_app['rabbit.producer']['producer_a']->getChannel();
20
        $channel->exchange_declare('etna', 'direct', false, true, false);
21
22
        $queue_opt = self::$silex_app['rmq.queues']["queue_a"];
23
        $channel->queue_declare(
24
            $queue_opt["name"],
25
            $queue_opt["passive"],
26
            $queue_opt["durable"],
27
            $queue_opt["exclusive"],
28
            $queue_opt["auto_delete"]
29
        );
30
31
        $channel->queue_bind($queue_opt['name'], $queue_opt['exchange'], $queue_opt['routing.key']);
32
    }
33
34
    /**
35
     * @AfterScenario @consumer
36
     */
37
    public static function flushMessages()
38
    {
39
        self::$silex_app['ConsumerA']->flushMessages();
40
        self::$silex_app['rabbit.consumer']['consumer_a']->purge();
41
        self::$silex_app['rabbit.consumer']['consumer_a']->resetConsumed();
42
    }
43
44
    /**
45
     * @Then la connection :connection devrait être définie
46
     */
47 View Code Duplication
    public function laConnectionDevraitEtreDefinie($connection)
48
    {
49
        if (false === isset(self::$silex_app['rabbit.connection'][$connection])) {
50
            throw new \Exception("Connection {$connection} is not defined");
51
        }
52
53
        return self::$silex_app['rabbit.connection'][$connection];
54
    }
55
56
    /**
57
     * @Then la connection :conn devrait être une instance de :class_name
58
     */
59
    public function laConnectionDevraitEtreUneInstanceDe($conn, $class_name)
60
    {
61
        $connection = $this->laConnectionDevraitEtreDefinie($conn);
62
        if (false === $connection instanceof $class_name) {
63
            $actual_class = get_class($connection);
64
            throw new \Exception(
65
                "Expected connection {$conn} to be instance of {$class_name}. Instance of {$actual_class} instead."
66
            );
67
        }
68
    }
69
70
    /**
71
     * @Then le producer :producer devrait être défini
72
     */
73 View Code Duplication
    public function leProducerDevraitEtreDefini($producer)
74
    {
75
        if (false === isset(self::$silex_app['rabbit.producer'][$producer])) {
76
            throw new \Exception("Producer {$producer} is not defined");
77
        }
78
79
        return self::$silex_app['rabbit.producer'][$producer];
80
    }
81
82
    /**
83
     * @Then le producer :producer devrait être une instance de :class_name
84
     */
85
    public function leProducerDevraitEtreUneInstanceDe($producer_name, $class_name)
86
    {
87
        $producer = $this->leProducerDevraitEtreDefini($producer_name);
88
        if (false === $producer instanceof $class_name) {
89
            $class = get_class($producer);
90
            throw new \Exception(
91
                "Expected producer {$producer_name} to be instance of {$class_name}. Instance of {$class} instead."
92
            );
93
        }
94
    }
95
96
    /**
97
     * @When je publie un job via le producer :producer_name avec le corps contenu dans :body_file
98
     */
99
    public function jePublieUnJobViaLeProducerAvecLeCorpsContenuDans($producer_name, $body_file)
100
    {
101
        $body = file_get_contents($this->requests_path . $body_file);
102
        if (!$body) {
103
            throw new \Exception("File not found : {$this->requests_path}${body_file}");
104
        }
105
106
        $producer    = $this->leProducerDevraitEtreDefini($producer_name);
107
        $routing_key = self::$silex_app['rabbit.producers'][$producer_name]['queue_options']['routing_keys'][0];
108
        $producer->publish($body, $routing_key);
109
    }
110
111
    /**
112
     * @Then le consumer :consumer devrait être défini
113
     */
114 View Code Duplication
    public function leConsumerDevraitEtreDefini($consumer)
115
    {
116
        if (false === isset(self::$silex_app['rabbit.consumer'][$consumer])) {
117
            throw new \Exception("Consumer {$consumer} is not defined");
118
        }
119
120
        return self::$silex_app['rabbit.consumer'][$consumer];
121
    }
122
123
    /**
124
     * @Then le consumer :consumer_name devrait être une instance de :class_name
125
     */
126
    public function leConsumerDevraitEtreUneInstanceDe($consumer_name, $class_name)
127
    {
128
        $consumer = $this->leConsumerDevraitEtreDefini($consumer_name);
129
        if (false === $consumer instanceof $class_name) {
130
            $class = get_class($consumer);
131
            throw new \Exception(
132
                "Expected consumer {$consumer_name} to be instance of {$class_name}. Instance of {$class} instead."
133
            );
134
        }
135
    }
136
137
    /**
138
     * @Given que je publie un message pour le consumer :consumer_name avec le corps contenu dans :body_file
139
     */
140
    public function queJePublieUnMessagePourLeConsumerAvecLeCorpsContenuDans($consumer_name, $body_file)
141
    {
142
        $body = file_get_contents($this->requests_path . $body_file);
143
        if (!$body) {
144
            throw new \Exception("File not found : {$this->requests_path}${body_file}");
145
        }
146
147
        $consumer    = $this->leConsumerDevraitEtreDefini($consumer_name);
148
        $channel     = $consumer->getChannel();
149
        $exchange    = self::$silex_app['rabbit.consumers'][$consumer_name]['exchange_options']['name'];
150
        $routing_key = self::$silex_app['rabbit.consumers'][$consumer_name]['queue_options']['routing_keys'][0];
151
        $message     = new AMQPMessage($body, ["Content-Type" => "application/json"]);
152
153
        $channel->basic_publish($message, $exchange, $routing_key);
154
    }
155
156
    /**
157
     * @When le consumer :consumer_name consomme :nb_messages message
158
     */
159
    public function leConsumerConsommeMessage($consumer_name, $nb_messages)
160
    {
161
        $consumer = $this->leConsumerDevraitEtreDefini($consumer_name);
162
163
        $consumer->consume($nb_messages);
164
    }
165
166
    /**
167
     * @Then le consumer :consumer_name devrait avoir consommé :nb_messages message avec le corps contenu dans :result_file
168
     */
169
    public function leConsumerDevraitAvoirConsommeMessageAvecLeCorpsContenuDans(
170
        $consumer_name,
171
        $nb_messages,
172
        $result_file
173
    ) {
174
        $body = file_get_contents($this->results_path . $result_file);
175
        if (!$body) {
176
            throw new \Exception("File not found : {$this->results_path}${result_file}");
177
        }
178
        $callback_name     = self::$silex_app['rabbit.consumers'][$consumer_name]['callback'];
179
        $consumed_messages = self::$silex_app[$callback_name]->getMessages();
180
181
        $actual_count = count($consumed_messages);
182
        if (intval($nb_messages) !== $actual_count) {
183
            throw new \Exception("Expected {$nb_messages} consumed messages, got {$actual_count}");
184
        }
185
186
        if (json_decode($body, true) !== $consumed_messages) {
187
            $actual_body = json_encode($consumed_messages);
188
189
            throw new \Exception("Expected consumed messages to be {$body}, got {$actual_body}");
190
        }
191
    }
192
}
193