2017-11-27 04:59:15 +05:30
|
|
|
<?php
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace common\tests\_support\queue;
|
2017-11-27 04:59:15 +05:30
|
|
|
|
|
|
|
use Codeception\Exception\ModuleException;
|
|
|
|
use Codeception\Module;
|
|
|
|
use Codeception\Module\Yii2;
|
|
|
|
|
|
|
|
class CodeceptionQueueHelper extends Module {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns last sent message
|
|
|
|
*
|
|
|
|
* @return \yii\queue\JobInterface|null
|
|
|
|
*/
|
|
|
|
public function grabLastQueuedJob() {
|
|
|
|
$messages = $this->grabQueueJobs();
|
2018-07-08 20:50:19 +05:30
|
|
|
$last = end($messages);
|
|
|
|
if ($last === false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $last;
|
2017-11-27 04:59:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 \yii\queue\JobInterface[]
|
|
|
|
* @throws ModuleException
|
|
|
|
*/
|
|
|
|
public function grabQueueJobs() {
|
|
|
|
$amqp = $this->grabComponent('queue');
|
|
|
|
if (!$amqp instanceof Queue) {
|
|
|
|
throw new ModuleException($this, 'AMQP module is not mocked, can\'t test messages');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $amqp->getMessages();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|