2016-12-04 22:26:49 +05:30
|
|
|
<?php
|
|
|
|
namespace tests\codeception\common\_support\amqp;
|
|
|
|
|
|
|
|
use Codeception\Exception\ModuleException;
|
|
|
|
use Codeception\Module;
|
|
|
|
use Codeception\Module\Yii2;
|
|
|
|
|
|
|
|
class Helper extends Module {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks that message is created.
|
|
|
|
*
|
|
|
|
* ```php
|
|
|
|
* <?php
|
|
|
|
* // check that at least 1 message was created
|
|
|
|
* $I->seeAmqpMessageIsCreated();
|
|
|
|
*
|
|
|
|
* // check that only 3 messages were created
|
|
|
|
* $I->seeAmqpMessageIsCreated(3);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param string|null $exchange
|
|
|
|
* @param int|null $num
|
|
|
|
*/
|
|
|
|
public function seeAmqpMessageIsCreated($exchange = null, $num = null) {
|
|
|
|
if ($num === null) {
|
|
|
|
$this->assertNotEmpty($this->grabSentAmqpMessages($exchange), 'message were created');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-29 16:50:28 +05:30
|
|
|
$this->assertCount(
|
2016-12-04 22:26:49 +05:30
|
|
|
$num,
|
2017-05-29 16:50:28 +05:30
|
|
|
$this->grabSentAmqpMessages($exchange),
|
2016-12-04 22:26:49 +05:30
|
|
|
'number of created messages is equal to ' . $num
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks that no messages was created
|
|
|
|
*
|
|
|
|
* @param string|null $exchange
|
|
|
|
*/
|
|
|
|
public function dontSeeAmqpMessageIsCreated($exchange = null) {
|
|
|
|
$this->seeAmqpMessageIsCreated($exchange, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns last sent message
|
|
|
|
*
|
|
|
|
* @param string|null $exchange
|
|
|
|
* @return \PhpAmqpLib\Message\AMQPMessage
|
|
|
|
*/
|
|
|
|
public function grabLastSentAmqpMessage($exchange = null) {
|
|
|
|
$this->seeAmqpMessageIsCreated();
|
|
|
|
$messages = $this->grabSentAmqpMessages($exchange);
|
|
|
|
|
|
|
|
return end($messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns array of all sent amqp messages.
|
|
|
|
* Each message is `\PhpAmqpLib\Message\AMQPMessage` instance.
|
|
|
|
* Useful to perform additional checks using `Asserts` module.
|
|
|
|
*
|
|
|
|
* @param string|null $exchange
|
|
|
|
* @return \PhpAmqpLib\Message\AMQPMessage[]
|
|
|
|
* @throws ModuleException
|
|
|
|
*/
|
|
|
|
public function grabSentAmqpMessages($exchange = null) {
|
|
|
|
$amqp = $this->grabComponent('amqp');
|
|
|
|
if (!$amqp instanceof TestComponent) {
|
|
|
|
throw new ModuleException($this, 'AMQP module is not mocked, can\'t test messages');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $amqp->getSentMessages($exchange);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function grabComponent(string $component) {
|
|
|
|
return $this->getYii2()->grabComponent($component);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getYii2() : Yii2 {
|
|
|
|
$yii2 = $this->getModule('Yii2');
|
|
|
|
if (!$yii2 instanceof Yii2) {
|
|
|
|
throw new ModuleException($this, 'Yii2 module must be configured');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $yii2;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|