Skip to content

Commit 9714eb2

Browse files
committed
review: add proper integration tests
1 parent 1878de3 commit 9714eb2

10 files changed

Lines changed: 423 additions & 113 deletions

File tree

Api/Data/MessageInterface.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,75 @@
55

66
interface MessageInterface
77
{
8+
public const FIELD_GROUP = 'group';
9+
public const FIELD_JOB = 'job';
10+
public const FIELD_TARGET = 'target';
11+
public const FIELD_ADDITIONAL_DATA = 'additional_data';
12+
public const FIELD_STATUS = 'status';
13+
14+
public const DEFAULT_GROUP = 'default';
15+
16+
public const STATUS_PENDING = 'pending';
17+
public const STATUS_PROCESSING = 'processing';
18+
public const STATUS_SUCCESS = 'success';
19+
public const STATUS_ERROR = 'error';
20+
21+
/**
22+
* "group" getter
23+
*
24+
* @return string
25+
*/
26+
public function getGroup();
27+
28+
/**
29+
* "group" setter
30+
*
31+
* @param string $group
32+
* @return \Discorgento\Queue\Api\Data\MessageInterface
33+
*/
34+
public function setGroup($group): self;
35+
36+
/**
37+
* "job" getter
38+
*
39+
* @return string|null
40+
*/
41+
public function getJob();
42+
43+
/**
44+
* "job" setter
45+
*
46+
* @param string $job
47+
* @return \Discorgento\Queue\Api\Data\MessageInterface
48+
*/
49+
public function setJob($job): self;
50+
51+
/**
52+
* "target" getter
53+
*
54+
* @return string|null
55+
*/
56+
public function getTarget();
57+
58+
/**
59+
* "target" setter
60+
*
61+
* @param string $target
62+
* @return \Discorgento\Queue\Api\Data\MessageInterface
63+
*/
64+
public function setTarget($target): self;
65+
866
/**
967
* Set additional data that can be used by the job later
1068
*
1169
* @param array|null $additionalData
12-
* @return self
70+
* @return \Discorgento\Queue\Api\Data\MessageInterface
1371
*/
1472
public function setAdditionalData($additionalData);
1573

1674
/**
1775
* Retrieve additional data array
76+
*
1877
* @return array
1978
*/
2079
public function getAdditionalData();

Command/ClearCommand.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* @author IronPlane Team
4+
* @copyright Copyright (c) 2023 IronPlane (https://www.ironplane.com)
5+
*/
6+
7+
namespace Discorgento\Queue\Command;
8+
9+
use Discorgento\Queue\Model\MessageRepository;
10+
use Discorgento\Queue\Model\ResourceModel\Message\CollectionFactory as MessagesCollectionFactory;
11+
12+
class ClearCommand
13+
{
14+
/** @var MessagesCollectionFactory */
15+
private $messagesCollectionFactory;
16+
17+
/** @var MessageRepository */
18+
private $messageRepository;
19+
20+
// phpcs:ignore
21+
public function __construct(
22+
MessagesCollectionFactory $messagesCollectionFactory,
23+
MessageRepository $messageRepository
24+
) {
25+
$this->messagesCollectionFactory = $messagesCollectionFactory;
26+
$this->messageRepository = $messageRepository;
27+
}
28+
29+
/**
30+
* Clear the queue jobs
31+
*
32+
* @return int Amount of messages cleared
33+
*/
34+
public function execute(): int
35+
{
36+
$messageCleanerIterator = $this->iterator();
37+
foreach ($messageCleanerIterator as $clearedMessage) {
38+
// iterator is cleaning the queue
39+
}
40+
41+
return $messageCleanerIterator->getReturn();
42+
}
43+
44+
/**
45+
* Clear the queue jobs
46+
*
47+
* @return \Generator
48+
*/
49+
public function iterator(): \Generator
50+
{
51+
$messages = $this->messagesCollectionFactory->create();
52+
$total = $messages->getSize();
53+
54+
foreach ($messages as $message) {
55+
$this->messageRepository->delete($message);
56+
yield $message;
57+
}
58+
59+
return $total;
60+
}
61+
62+
/**
63+
* Get the total amount of messages in the queue
64+
*
65+
* @return int
66+
*/
67+
public function getTotal() : int
68+
{
69+
return $this->messagesCollectionFactory->create()->getSize();
70+
}
71+
}

Console/Command/Clear.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
namespace Discorgento\Queue\Console\Command;
55

66
use Discorgento\Core\Helper\Data as CoreHelper;
7-
use Discorgento\Queue\Model\MessageRepository;
8-
use Discorgento\Queue\Model\ResourceModel\Message\CollectionFactory as MessagesCollectionFactory;
7+
use Discorgento\Queue\Command\ClearCommand;
98
use Symfony\Component\Console\Command\Command;
109
use Symfony\Component\Console\Helper\ProgressBarFactory;
1110
use Symfony\Component\Console\Input\InputInterface;
@@ -14,35 +13,31 @@
1413

1514
class Clear extends Command
1615
{
16+
/** @var ClearCommand */
17+
private $clearCommand;
18+
1719
/** @var ConfirmationQuestionFactory */
1820
private $confirmationQuestionFactory;
1921

2022
/** @var CoreHelper */
2123
private $coreHelper;
2224

23-
/** @var MessagesCollectionFactory */
24-
private $messagesCollectionFactory;
25-
26-
/** @var MessageRepository */
27-
private $messageRepository;
28-
2925
/** @var ProgressBarFactory */
3026
private $progressBarFactory;
3127

3228
// phpcs:ignore
3329
public function __construct(
30+
ClearCommand $clearCommand,
3431
ConfirmationQuestionFactory $confirmationQuestionFactory,
3532
CoreHelper $coreHelper,
36-
MessagesCollectionFactory $messagesCollectionFactory,
37-
MessageRepository $messageRepository,
3833
ProgressBarFactory $progressBarFactory,
3934
string $name = null
4035
) {
4136
parent::__construct($name);
37+
38+
$this->clearCommand = $clearCommand;
4239
$this->confirmationQuestionFactory = $confirmationQuestionFactory;
4340
$this->coreHelper = $coreHelper;
44-
$this->messagesCollectionFactory = $messagesCollectionFactory;
45-
$this->messageRepository = $messageRepository;
4641
$this->progressBarFactory = $progressBarFactory;
4742
}
4843

@@ -62,10 +57,8 @@ protected function configure()
6257
*/
6358
protected function execute(InputInterface $input, OutputInterface $output)
6459
{
65-
$messages = $this->messagesCollectionFactory->create();
66-
67-
$totalMessages = $messages->getSize();
68-
if ($totalMessages < 1) {
60+
$total = $this->clearCommand->getTotal();
61+
if ($total < 1) {
6962
$output->writeln("<error>There's no pending jobs.</error>");
7063

7164
return 0;
@@ -87,11 +80,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
8780

8881
$progressBar = $this->progressBarFactory->create([
8982
'output' => $output,
90-
'max' => $totalMessages,
83+
'max' => $total,
9184
]);
9285

93-
foreach ($messages as $message) {
94-
$this->messageRepository->delete($message);
86+
foreach ($this->clearCommand->iterator() as $clearedMessage) {
9587
$progressBar->advance();
9688
}
9789

Model/Message.php

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@
44
namespace Discorgento\Queue\Model;
55

66
use Discorgento\Queue\Api\Data\MessageInterface;
7-
use Discorgento\Queue\Model\ResourceModel\Message as MessageResourceModel;
8-
use Magento\Framework\DataObject\IdentityInterface;
97
use Magento\Framework\Model\AbstractModel;
8+
use Magento\Framework\Serialize\SerializerInterface;
109

11-
class Message extends AbstractModel implements MessageInterface, IdentityInterface
10+
class Message extends AbstractModel implements MessageInterface
1211
{
13-
public const CACHE_TAG = 'discorgento_queue';
14-
15-
public const STATUS_PENDING = 'pending';
16-
1712
/** @var SerializerInterface */
1813
private $serializer;
1914

@@ -30,36 +25,66 @@ public function __construct(
3025
$this->serializer = $serializer;
3126
}
3227

33-
public const STATUS_PROCESSING = 'processing';
28+
/**
29+
* @inheritDoc
30+
*/
31+
public function getGroup()
32+
{
33+
return $this->getData(self::FIELD_GROUP) ?: self::DEFAULT_GROUP;
34+
}
3435

35-
public const STATUS_SUCCESS = 'success';
36+
/**
37+
* @inheritDoc
38+
*/
39+
public function setGroup($group): self
40+
{
41+
return $this->setData(self::FIELD_GROUP, $group);
42+
}
3643

37-
public const STATUS_ERROR = 'error';
44+
/**
45+
* @inheritDoc
46+
*/
47+
public function getJob()
48+
{
49+
return $this->getData(self::FIELD_JOB);
50+
}
3851

39-
/** @inheritDoc */
40-
protected function _construct()
52+
/**
53+
* @inheritDoc
54+
*/
55+
public function setJob($job): self
4156
{
42-
$this->_init(MessageResourceModel::class);
57+
return $this->setData(self::FIELD_JOB, $job);
4358
}
4459

45-
/** @inheritDoc */
46-
public function getIdentities()
60+
/**
61+
* @inheritDoc
62+
*/
63+
public function getTarget()
64+
{
65+
return $this->getData(self::FIELD_TARGET);
66+
}
67+
68+
/**
69+
* @inheritDoc
70+
*/
71+
public function setTarget($target): self
4772
{
48-
return [self::CACHE_TAG . "_{$this->getId()}"];
73+
return $this->setData(self::FIELD_TARGET, $target);
4974
}
5075

5176
/** @inheritDoc */
5277
public function setAdditionalData($additionalData)
5378
{
5479
$encodedData = $this->serializer->serialize($additionalData ?: []);
5580

56-
return $this->setData('additional_data', $encodedData);
81+
return $this->setData(self::FIELD_ADDITIONAL_DATA, $encodedData);
5782
}
5883

5984
/** @inheritDoc */
6085
public function getAdditionalData()
6186
{
62-
$encodedData = (string) $this->getData('additional_data');
87+
$encodedData = (string) $this->getData(self::FIELD_ADDITIONAL_DATA);
6388

6489
return $this->serializer->unserialize($encodedData, true) ?: [];
6590
}

Model/MessageManagement.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\Exception\LocalizedException;
1313
use Magento\Framework\Filesystem\Driver\File as FileDriver;
1414
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\Framework\Serialize\SerializerInterface;
1516
use Magento\Framework\Stdlib\DateTime\DateTime;
1617
use Psr\Log\LoggerInterface;
1718

@@ -40,6 +41,9 @@ class MessageManagement implements MessageManagementInterface
4041
/** @var SearchCriteriaBuilder */
4142
private $searchCriteriaBuilder;
4243

44+
/** @var SerializerInterface */
45+
private $serializer;
46+
4347
// phpcs:ignore
4448
public function __construct(
4549
DateTime $date,
@@ -48,7 +52,8 @@ public function __construct(
4852
MessageRepositoryInterface $messageRepository,
4953
ObjectManagerInterface $objectManager,
5054
ScopeConfigInterface $scopeConfig,
51-
SearchCriteriaBuilder $searchCriteriaBuilder
55+
SearchCriteriaBuilder $searchCriteriaBuilder,
56+
SerializerInterface $serializer
5257
) {
5358
$this->date = $date;
5459
$this->fileDriver = $fileDriver;
@@ -57,6 +62,7 @@ public function __construct(
5762
$this->objectManager = $objectManager;
5863
$this->scopeConfig = $scopeConfig;
5964
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
65+
$this->serializer = $serializer;
6066
}
6167

6268
/** @inheritDoc */
@@ -77,7 +83,7 @@ public function process(MessageInterface $message)
7783
);
7884

7985
if (is_array($result)) {
80-
$result = json_encode($result, JSON_PRETTY_PRINT);
86+
$result = $this->serializer->serialize($result);
8187
}
8288

8389
$status = Message::STATUS_SUCCESS;

0 commit comments

Comments
 (0)