1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
use Behat\Behat\Context\ClosuredContextInterface, |
4
|
|
|
Behat\Behat\Context\TranslatedContextInterface, |
5
|
|
|
Behat\Behat\Context\BehatContext, |
6
|
|
|
Behat\Behat\Exception\PendingException; |
7
|
|
|
use Behat\Gherkin\Node\PyStringNode, |
8
|
|
|
Behat\Gherkin\Node\TableNode; |
9
|
|
|
|
10
|
|
|
require __DIR__ . "/../../vendor/autoload.php"; |
11
|
|
|
|
12
|
|
|
// |
13
|
|
|
// Require 3rd-party libraries here: |
14
|
|
|
// |
15
|
|
|
// require_once 'PHPUnit/Autoload.php'; |
16
|
|
|
// require_once 'PHPUnit/Framework/Assert/Functions.php'; |
17
|
|
|
// |
18
|
|
|
|
19
|
|
|
use ETNA\Silex\Provider\RabbitMQ\RabbitMQServiceProvider; |
20
|
|
|
use Silex\Application; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Features context. |
24
|
|
|
*/ |
25
|
|
|
class FeatureContext extends BehatContext |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Initializes context. |
29
|
|
|
* Every scenario gets its own context object. |
30
|
|
|
* |
31
|
|
|
* @param array $parameters context parameters (set them up through behat.yml) |
32
|
|
|
*/ |
33
|
|
|
public function __construct(array $parameters) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
// Initialize your context here |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
static private $vhosts = ["/test-behat", "/test-behat-named"]; |
|
|
|
|
39
|
|
|
|
40
|
|
|
use ETNA\FeatureContext\RabbitMQ; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @Given /^une application Silex$/ |
44
|
|
|
*/ |
45
|
|
|
public function uneApplicationSilex() |
46
|
|
|
{ |
47
|
|
|
$this->app = new Application(); |
|
|
|
|
48
|
|
|
$this->app->register(new RabbitMQServiceProvider()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @Given /^la configuration suivante :$/ |
53
|
|
|
*/ |
54
|
|
|
public function laConfigurationSuivante(PyStringNode $config) |
55
|
|
|
{ |
56
|
|
|
$config = json_decode($config->getRaw(), true); |
57
|
|
|
if (!$config && json_last_error()) { |
58
|
|
|
throw new PendingException("Invalid JSON"); |
59
|
|
|
} |
60
|
|
|
foreach ($config as $key => $value) { |
61
|
|
|
$this->app[$key] = $value; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @Given /^\$app\["amqp\.chan"\] == \$app\["amqp\.chans"\]\["default"\]$/ |
67
|
|
|
*/ |
68
|
|
|
public function chanEstUnAliasVersDefault() |
69
|
|
|
{ |
70
|
|
|
if ($this->app["amqp.chan"] !== $this->app["amqp.chans"]["default"]) { |
71
|
|
|
throw new Exception('$app["amqp.chan"] != $app["amqp.chans"]["default"]'); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @Given /^\$app\["amqp\.(\w+)"\]\["(\w+)"\] est du type ([\w\\]+)$/ |
77
|
|
|
*/ |
78
|
|
|
public function checkClass($type, $name, $class) |
79
|
|
|
{ |
80
|
|
|
if (!is_a($this->app["amqp.{$type}"][$name], $class)) { |
81
|
|
|
throw new Exception("\$app['amqp.{$type}']['{$name}'] n'est pas une instance de {$class}"); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @Given /^\$app\["amqp\.(\w+)"\]\["(\w+)"\]->(\w+)\(\) == "([^"]*)"$/ |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function checkGetterString($type, $name, $method, $value) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
if ($this->app["amqp.{$type}"][$name]->$method() != $value) { |
91
|
|
|
$value = $this->app["amqp.{$type}"][$name]->$method(); |
92
|
|
|
throw new Exception("\$app['amqp.{$type}'['{$name}']]->{$method}() = " . var_export($value, true)); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @Given /^\$app\["amqp\.(\w+)"\]\["(\w+)"\]->(\w+)\(\) == (true|false)$/ |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function checkGetterBoolean($type, $name, $method, $value) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
if ($this->app["amqp.{$type}"][$name]->$method() != (strtolower($value) == "true")) { |
102
|
|
|
$value = $this->app["amqp.{$type}"][$name]->$method(); |
103
|
|
|
throw new Exception("\$app['amqp.{$type}'['{$name}']]->{$method}() = " . var_export($value, true)); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @Given /^que je bind une file sur l\'exchange "([^"]*)"$/ |
109
|
|
|
*/ |
110
|
|
|
public function queJeBindUneFileSurLExchange($exchange) |
111
|
|
|
{ |
112
|
|
|
$this->channel = $this->app["amqp.exchanges"][$exchange]->getChannel(); |
|
|
|
|
113
|
|
|
$this->tmp_queue = $this->channel->queue_declare()[0]; |
|
|
|
|
114
|
|
|
$this->channel->queue_bind($this->tmp_queue, $exchange); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @Given /^que "([^"]*)" est (un exchange|une queue) réservé$/ |
119
|
|
|
*/ |
120
|
|
|
public function queEstUnTrucReserve($name, $type) |
121
|
|
|
{ |
122
|
|
|
try { |
123
|
|
|
$type = $type == 'un exchange' ? "amqp.exchanges" : "amqp.queues"; |
124
|
|
|
$this->app[$type][$name]->getChannel(); |
125
|
|
|
} catch (Exception $e) { |
126
|
|
|
$this->exception = $e->getMessage(); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @Given /^je devrais avoir une exception "([^"]*)"$/ |
132
|
|
|
*/ |
133
|
|
|
public function jeDevraisAvoirUneException($exception) |
134
|
|
|
{ |
135
|
|
|
if ($exception != $this->exception) { |
136
|
|
|
throw new Exception("Expected: '{$exception}'; got: '{$this->exception}'"); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @Given /^j\'envoie un message "(\w+)" dans l\'exchange "(\w+)"$/ |
142
|
|
|
*/ |
143
|
|
|
public function jEnvoieUnMessage($message, $exchange) |
144
|
|
|
{ |
145
|
|
|
$this->app["amqp.exchanges"][$exchange]->send($message); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @Given /^j\'envoie un message "(\w+)" dans la file "([^"]*)"$/ |
150
|
|
|
*/ |
151
|
|
|
public function jEnvoieUnMessageDansLaFile($message, $queue) |
152
|
|
|
{ |
153
|
|
|
$this->channel = $this->app["amqp.queues"][$queue]->getChannel(); |
|
|
|
|
154
|
|
|
$this->tmp_queue = $queue; |
155
|
|
|
$this->app["amqp.queues"][$queue]->send($message); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @Given /^il doit y avoir un message "([^"]*)" dans la file( "(\w+)")?$/ |
160
|
|
|
*/ |
161
|
|
|
public function ilDoitYAvoirUnMessageDansLaFile($message, $queue = null) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
$this->channel->basic_consume($this->tmp_queue, "behat", false, false, false, false, function ($msg) use ($message) { |
|
|
|
|
164
|
|
|
$msg->delivery_info['channel']->basic_cancel($msg->delivery_info['consumer_tag']); |
165
|
|
|
|
166
|
|
|
if (json_decode($msg->body) != $message) { |
167
|
|
|
throw new Exception("{$msg->body} != {$message}"); |
168
|
|
|
} |
169
|
|
|
}); |
170
|
|
|
$this->channel->wait(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @Given /^je fais un listen ma callback doit être appelé (\d+) fois$/ |
175
|
|
|
*/ |
176
|
|
|
public function jeFaisUnListen($nb) |
177
|
|
|
{ |
178
|
|
|
$this->app["amqp.queues"][$this->tmp_queue]->send("__QUIT__"); |
179
|
|
|
$nb++; |
180
|
|
|
|
181
|
|
|
$count = 0; |
|
|
|
|
182
|
|
|
$last_message = null; |
183
|
|
|
$this->app["amqp.queues"][$this->tmp_queue]->listen(function ($msg) use ($count, &$last_message) { |
184
|
|
|
$count++; |
185
|
|
|
$last_message = json_decode($msg->body); |
186
|
|
|
}); |
187
|
|
|
while ($nb--) { |
188
|
|
|
$this->channel->wait(); |
189
|
|
|
} |
190
|
|
|
if ($last_message != "__QUIT__") { |
191
|
|
|
throw new Exception("Il y a trop de message"); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.