-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added cron and queue detection (#23)
* Added cron and queue detection * Added tests to cron & queue status
- Loading branch information
1 parent
b447860
commit 82b0557
Showing
5 changed files
with
314 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters