From 2d90d6265f2da0a0dd658f841ca39fb799f40240 Mon Sep 17 00:00:00 2001 From: Thierry THONGKHAM Date: Fri, 4 Nov 2016 10:51:28 +0100 Subject: [PATCH] [FEATURE#33794] Changement du rabbitcontext - Refactor du rabbitcontext --- src/RabbitContext.php | 82 ++++--------------------------------------- 1 file changed, 7 insertions(+), 75 deletions(-) diff --git a/src/RabbitContext.php b/src/RabbitContext.php index beeb2f8..39f31d7 100644 --- a/src/RabbitContext.php +++ b/src/RabbitContext.php @@ -9,43 +9,8 @@ class RabbitContext extends BaseContext { public static $vhosts = ["/test-behat"]; - private $channel; - private $connection; private $response; - public static function createAMQPConnection() - { - $rmq_url = getenv("RABBITMQ_URL"); - $rmq_vhost = getenv("RABBITMQ_VHOST"); - - if (false === $rmq_url) { - throw new \Exception('initializeDryRunQueue: RABBITMQ_URL is not defined'); - } - - if (false === $rmq_vhost) { - throw new \Exception('initializeDryRunQueue: RABBITMQ_VHOST is not defined'); - } - - $config = parse_url($rmq_url); - foreach (["host", "port", "user", "pass"] as $config_key) { - if (!isset($config[$config_key])) { - throw new \Exception("initializeDryRunQueue: Invalid RABBITMQ_URL: cannot resolve {$config_key}"); - } - } - - $connection = new AMQPConnection( - $config["host"], - $config["port"], - $config["user"], - $config["pass"], - $rmq_vhost - ); - - $channel = $connection->channel(); - - return [$connection, $channel]; - } - /** * @BeforeSuite */ @@ -189,50 +154,15 @@ public function jeTraiteJobsAvecLeConsumer($nb_jobs, $consumer) self::$silex_app['rabbit.consumer'][$consumer]->consume($nb_jobs); } - /** - * @Then /^je ferme la connexion rabbitmq$/ - */ - public function jeFermeLaConnexionRabbitmq() - { - $this->channel->close(); - $this->connection->close(); - } - - /** - * @Then /^je crée une connexion rabbitmq$/ - */ - public function jeCreeUneConnexionRabbitmq() - { - list($this->connection, $this->channel) = self::createAMQPConnection(); - } - - /** - * @Then /^je crée l'exchange "([^"]*)" de type "([^"]*)"$/ - */ - public function jeCreeLexchangeDeType($exchange_name, $exchange_type) - { - $this->channel->exchange_declare($exchange_name, $exchange_type, false, true, false); - } - - /** - * @Then /^je déclare la file "([^"]*)" avec la clé de routage "([^"]*)" en liaison avec l'exchange "([^"]*)"$/ - */ - public function jeDeclareLaFileAvecLaCleDeRoutageEnLiaisonAvecLexchange( - $queue = null, - $routing_key = null, - $exchange = null - ) - { - $this->channel->queue_declare($queue, false, true, false, false); - $this->channel->queue_bind($queue, $exchange, $routing_key); - } - /** * @Then /^il doit y avoir un message dans la file "([^"]*)"$/ */ public function ilDoitYAvoirUnMessageDansLaFile($queue = null) { - $response_msg = $this->channel->basic_get($queue); + $app = self::$silex_app; + $channel = $app["rabbit.connection"]['default']->channel(); + + $response_msg = $channel->basic_get($queue); $parsed_response = json_decode($response_msg->body); if (empty($parsed_response)) { throw new \Exception("{$parsed_response}"); @@ -251,7 +181,9 @@ public function ilDoitYAvoirUnMessageDansLaFileAvecLeCorpsContenuDans($queue = n $body = file_get_contents($this->results_path . $body); $parsed_wanted = json_decode($body); - $response_msg = $this->channel->basic_get($queue); + $app = self::$silex_app; + $channel = $app["rabbit.connection"]['default']->channel(); + $response_msg = $channel->basic_get($queue); $parsed_response = json_decode($response_msg->body); $this->response[$queue] = $parsed_response;