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;
|
2024-12-02 15:40:55 +05:30
|
|
|
use yii\queue\JobInterface;
|
2017-11-27 04:59:15 +05:30
|
|
|
|
|
|
|
class CodeceptionQueueHelper extends Module {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns last sent message
|
|
|
|
*/
|
2024-12-02 15:40:55 +05:30
|
|
|
public function grabLastQueuedJob(): ?JobInterface {
|
2017-11-27 04:59:15 +05:30
|
|
|
$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.
|
|
|
|
*
|
|
|
|
* @return \yii\queue\JobInterface[]
|
|
|
|
* @throws ModuleException
|
|
|
|
*/
|
2024-12-02 15:40:55 +05:30
|
|
|
public function grabQueueJobs(): array {
|
2017-11-27 04:59:15 +05:30
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|