Skip to content

Commit

Permalink
Added cron and queue detection (#23)
Browse files Browse the repository at this point in the history
* Added cron and queue detection

* Added tests to cron & queue status
  • Loading branch information
andrii-onufriichuk authored Apr 7, 2023
1 parent b447860 commit 82b0557
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 0 deletions.
63 changes: 63 additions & 0 deletions Model/System/Message/Cron.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Rvvup\Payments\Model\System\Message;

use Magento\Cron\Model\ResourceModel\Schedule\Collection;
use Magento\Cron\Model\ResourceModel\Schedule\CollectionFactory;
use Magento\Framework\Notification\MessageInterface;

class Cron implements MessageInterface
{

/**
* @var CollectionFactory
*/
private CollectionFactory $collectionFactory;

/**
* @param CollectionFactory $collectionFactory
*/
public function __construct(
CollectionFactory $collectionFactory
) {
$this->collectionFactory = $collectionFactory;
}

/**
* Message identity
*/
private const MESSAGE_IDENTITY = 'rvvup_missing_crons';

public function getIdentity()
{
return self::MESSAGE_IDENTITY;
}

public function isDisplayed()
{
/** @var Collection $collection */
$collection = $this->collectionFactory->create();

return empty($collection->getAllIds());
}

public function getText()
{
$warning = "<b>WARNING</b>: ";
$message = $warning . 'Rvvup requires Magento cron jobs to be enabled in order to handle payment';
$message .= " events that complete asynchronously.";
$message .= "<br>It appears that cron jobs may not be configured on your Magento instance.";
$documentationLink = "https://articles.rvvup.com/getting-started-with-magento-and-rvvup";
$documentation = "<a href='$documentationLink'>our documentation</a>";
$message .= "<br>See " . $documentation . " for more information.";

return $message;
}

public function getSeverity()
{
return self::SEVERITY_NOTICE;
}
}
76 changes: 76 additions & 0 deletions Model/System/Message/Queue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Rvvup\Payments\Model\System\Message;

use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\Notification\MessageInterface;
use Magento\Cron\Model\ResourceModel\Schedule\Collection;
use Magento\Cron\Model\ResourceModel\Schedule\CollectionFactory;

class Queue implements MessageInterface
{

/**
* @var CollectionFactory
*/
private CollectionFactory $collectionFactory;

/**
* @var DeploymentConfig
*/
private DeploymentConfig $config;

/**
* @param CollectionFactory $collectionFactory
* @param DeploymentConfig $config
*/
public function __construct(
CollectionFactory $collectionFactory,
DeploymentConfig $config
) {
$this->collectionFactory = $collectionFactory;
$this->config = $config;
}

/**
* Message identity
*/
private const MESSAGE_IDENTITY = 'rvvup_missing_queues';

public function getIdentity()
{
return self::MESSAGE_IDENTITY;
}

public function isDisplayed()
{
/** @var Collection $collection */
$collection = $this->collectionFactory->create();

$collection->addFieldToFilter('job_code', ['eq' => 'consumers_runner']);

$rabbitMq = $this->config->get('amqp');

return empty($collection->getAllIds()) && !$rabbitMq;
}

public function getText()
{
$warning = "<b>WARNING</b>: ";
$message = $warning . "Rvvup requires Magento MessageQueue to be enabled in order to handle payment";
$message .= " events that complete asynchronously.";
$message .= "<br>It appears that this may not be configured on your Magento instance.";
$documentationLink = "https://articles.rvvup.com/getting-started-with-magento-and-rvvup";
$documentation = "<a href='$documentationLink'>our documentation</a>";
$message .= "<br>See " . $documentation . " for more information.";

return $message;
}

public function getSeverity()
{
return self::SEVERITY_NOTICE;
}
}
71 changes: 71 additions & 0 deletions Test/Unit/Model/System/CronTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Rvvup\Payments\Test\Unit\Model\System;

use Magento\Cron\Model\ResourceModel\Schedule\Collection;
use Magento\Cron\Model\ResourceModel\Schedule\CollectionFactory;
use PHPUnit\Framework\TestCase;
use Rvvup\Payments\Model\System\Message\Cron;

class CronTest extends TestCase
{
/**
* @var CollectionFactory
*/
private CollectionFactory $collectionFactory;

/**
* @var Cron $cron
*/
private $cron;

protected function setUp(): void
{
$this->collectionFactory = $this->getMockBuilder(CollectionFactory::class)->disableOriginalConstructor()->getMock();
$this->configureCollection();

$this->cron = new Cron(
$this->collectionFactory
);
}

private function configureCollection($array = []) {
$collection = $this->getMockBuilder(Collection::class)->disableOriginalConstructor()->getMock();
$collection->method('getAllIds')->willReturn($array);
$this->collectionFactory->method('create')->willReturn($collection);
}

/**
* @return void
*/
protected function tearDown(): void
{
$this->cron = null;
}

/**
* @return void
*/
public function testDisplayWithCronEnabled()
{
$this->configureCollection([1]);
$this->assertEquals(
true,
$this->cron->isDisplayed()
);
}

/**
* @return void
*/
public function testDisplayWithCronDisabled()
{
$this->configureCollection();
$this->assertEquals(
true,
$this->cron->isDisplayed()
);
}
}
96 changes: 96 additions & 0 deletions Test/Unit/Model/System/QueueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace Rvvup\Payments\Test\Unit\Model\System;

use Magento\Cron\Model\ResourceModel\Schedule\Collection;
use Magento\Cron\Model\ResourceModel\Schedule\CollectionFactory;
use Magento\Framework\App\DeploymentConfig;
use PHPUnit\Framework\TestCase;
use Rvvup\Payments\Model\System\Message\Queue;

class QueueTest extends TestCase
{
/**
* @var CollectionFactory
*/
private CollectionFactory $collectionFactory;

/**
* @var Queue $queue
*/
private $queue;

/**
* @var DeploymentConfig $config
*/
private $config;

/** @inheirtDoc */
protected function setUp(): void
{
$this->collectionFactory = $this->getMockBuilder(CollectionFactory::class)->disableOriginalConstructor()->getMock();
$this->config = $this->getMockBuilder(DeploymentConfig::class)->disableOriginalConstructor()->getMock();
$this->configure(false);

$this->queue = new Queue(
$this->collectionFactory,
$this->config
);
}

/** @inheirtDoc */
private function configure($amqp, array $ids = []) {
$collection = $this->getMockBuilder(Collection::class)->disableOriginalConstructor()->getMock();
$collection->method('getAllIds')->willReturn($ids);
$this->config->method('get')->willReturn($amqp);
$this->collectionFactory->method('create')->willReturn($collection);
}

/**
* @return void
*/
protected function tearDown(): void
{
$this->queue = null;
}

/**
* @return void
*/
public function testDisplayWithAmqpEnabled()
{
$this->configure(true);
$this->assertEquals(
true,
$this->queue->isDisplayed()
);
}

/**
* @return void
*/
public function testDisplayWithAmqpDisabled()
{
$this->configure(false);
$this->assertEquals(
true,
$this->queue->isDisplayed()
);
}


/**
* @return void
*/
public function testDisplayWithCronEnabled()
{
$this->configure(false, [1]);
$this->assertEquals(
true,
$this->queue->isDisplayed()
);
}
}

8 changes: 8 additions & 0 deletions etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@
type="Rvvup\Payments\Plugin\PaymentMethod"
sortOrder="1"/>
</type>
<type name="Magento\Framework\Notification\MessageList">
<arguments>
<argument name="messages" xsi:type="array">
<item name="rvvup_missing_queues" xsi:type="string">Rvvup\Payments\Model\System\Message\Queue</item>
<item name="rvvup_missing_crons" xsi:type="string">Rvvup\Payments\Model\System\Message\Cron</item>
</argument>
</arguments>
</type>
</config>

0 comments on commit 82b0557

Please sign in to comment.