Skip to content

[Newsletter] Refactor code and Cover Model/Observer class by Unit Test #25997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions app/code/Magento/Newsletter/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,50 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Newsletter\Model;

use Magento\Newsletter\Model\ResourceModel\Queue\Collection;
use Magento\Newsletter\Model\ResourceModel\Queue\CollectionFactory;

/**
* Newsletter module observer
*
* @SuppressWarnings(PHPMD.LongVariable)
*/
class Observer
{
/**
* Number of queue
*/
private const COUNT_OF_QUEUE = 3;

/**
* Number of subscriptions
*/
private const COUNT_OF_SUBSCRIPTIONS = 20;

/**
* First page in collection
*/
private const FIRST_PAGE = 1;

/**
* Queue collection factory
*
* @var \Magento\Newsletter\Model\ResourceModel\Queue\CollectionFactory
* @var CollectionFactory
*/
protected $_queueCollectionFactory;

/**
* Construct
*
* @param \Magento\Newsletter\Model\ResourceModel\Queue\CollectionFactory $queueCollectionFactory
* @param CollectionFactory $queueCollectionFactory
*/
public function __construct(
\Magento\Newsletter\Model\ResourceModel\Queue\CollectionFactory $queueCollectionFactory
CollectionFactory $queueCollectionFactory
) {
$this->_queueCollectionFactory = $queueCollectionFactory;
}
Expand All @@ -37,13 +58,11 @@ public function __construct(
*/
public function scheduledSend()
{
$countOfQueue = 3;
$countOfSubscriptions = 20;

/** @var \Magento\Newsletter\Model\ResourceModel\Queue\Collection $collection */
/** @var Collection $collection */
$collection = $this->_queueCollectionFactory->create();
$collection->setPageSize($countOfQueue)->setCurPage(1)->addOnlyForSendingFilter()->load();
$collection->setPageSize(self::COUNT_OF_QUEUE)
->setCurPage(self::FIRST_PAGE)->addOnlyForSendingFilter()->load();

$collection->walk('sendPerSubscriber', [$countOfSubscriptions]);
$collection->walk('sendPerSubscriber', [self::COUNT_OF_SUBSCRIPTIONS]);
}
}
63 changes: 63 additions & 0 deletions app/code/Magento/Newsletter/Test/Unit/Model/ObserverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Newsletter\Test\Unit\Model;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Newsletter\Model\Observer;
use Magento\Newsletter\Model\ResourceModel\Queue\Collection;
use Magento\Newsletter\Model\ResourceModel\Queue\CollectionFactory;
use PHPUnit\Framework\TestCase;

class ObserverTest extends TestCase
{
/**
* @var Observer
*/
private $model;

/**
* @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $collectionFactoryMock;

/**
* Setup environment for test
*/
protected function setUp()
{
$objectManager = new ObjectManagerHelper($this);

$this->collectionFactoryMock = $this->createPartialMock(
CollectionFactory::class,
['create']
);

$this->model = $objectManager->getObject(
Observer::class,
[
'queueCollectionFactory' => $this->collectionFactoryMock
]
);
}

/**
* Test scheduledSend() method
*/
public function testScheduledSend()
{
$collectionMock = $this->createMock(Collection::class);
$this->collectionFactoryMock->expects($this->once())->method('create')->willReturn($collectionMock);
$collectionMock->expects($this->once())->method('setPageSize')->with(3)->willReturnSelf();
$collectionMock->expects($this->once())->method('setCurPage')->with(1)->willReturnSelf();
$collectionMock->expects($this->once())->method('addOnlyForSendingFilter')->willReturnSelf();
$collectionMock->expects($this->once())->method('load')->willReturnSelf();
$collectionMock->expects($this->once())->method('walk')->with('sendPerSubscriber', [20]);

$this->model->scheduledSend();
}
}