Skip to content

Commit 2ab1d26

Browse files
ENGCOM-6449: [Newsletter] Refactor code and Cover Model/Observer class by Unit Test #25997
2 parents ff52bbe + ef72b82 commit 2ab1d26

File tree

2 files changed

+91
-9
lines changed

2 files changed

+91
-9
lines changed

app/code/Magento/Newsletter/Model/Observer.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,50 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Newsletter\Model;
710

11+
use Magento\Newsletter\Model\ResourceModel\Queue\Collection;
12+
use Magento\Newsletter\Model\ResourceModel\Queue\CollectionFactory;
13+
814
/**
915
* Newsletter module observer
1016
*
1117
* @SuppressWarnings(PHPMD.LongVariable)
1218
*/
1319
class Observer
1420
{
21+
/**
22+
* Number of queue
23+
*/
24+
private const COUNT_OF_QUEUE = 3;
25+
26+
/**
27+
* Number of subscriptions
28+
*/
29+
private const COUNT_OF_SUBSCRIPTIONS = 20;
30+
31+
/**
32+
* First page in collection
33+
*/
34+
private const FIRST_PAGE = 1;
35+
1536
/**
1637
* Queue collection factory
1738
*
18-
* @var \Magento\Newsletter\Model\ResourceModel\Queue\CollectionFactory
39+
* @var CollectionFactory
1940
*/
2041
protected $_queueCollectionFactory;
2142

2243
/**
2344
* Construct
2445
*
25-
* @param \Magento\Newsletter\Model\ResourceModel\Queue\CollectionFactory $queueCollectionFactory
46+
* @param CollectionFactory $queueCollectionFactory
2647
*/
2748
public function __construct(
28-
\Magento\Newsletter\Model\ResourceModel\Queue\CollectionFactory $queueCollectionFactory
49+
CollectionFactory $queueCollectionFactory
2950
) {
3051
$this->_queueCollectionFactory = $queueCollectionFactory;
3152
}
@@ -37,13 +58,11 @@ public function __construct(
3758
*/
3859
public function scheduledSend()
3960
{
40-
$countOfQueue = 3;
41-
$countOfSubscriptions = 20;
42-
43-
/** @var \Magento\Newsletter\Model\ResourceModel\Queue\Collection $collection */
61+
/** @var Collection $collection */
4462
$collection = $this->_queueCollectionFactory->create();
45-
$collection->setPageSize($countOfQueue)->setCurPage(1)->addOnlyForSendingFilter()->load();
63+
$collection->setPageSize(self::COUNT_OF_QUEUE)
64+
->setCurPage(self::FIRST_PAGE)->addOnlyForSendingFilter()->load();
4665

47-
$collection->walk('sendPerSubscriber', [$countOfSubscriptions]);
66+
$collection->walk('sendPerSubscriber', [self::COUNT_OF_SUBSCRIPTIONS]);
4867
}
4968
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Newsletter\Test\Unit\Model;
9+
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
11+
use Magento\Newsletter\Model\Observer;
12+
use Magento\Newsletter\Model\ResourceModel\Queue\Collection;
13+
use Magento\Newsletter\Model\ResourceModel\Queue\CollectionFactory;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class ObserverTest extends TestCase
17+
{
18+
/**
19+
* @var Observer
20+
*/
21+
private $model;
22+
23+
/**
24+
* @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $collectionFactoryMock;
27+
28+
/**
29+
* Setup environment for test
30+
*/
31+
protected function setUp()
32+
{
33+
$objectManager = new ObjectManagerHelper($this);
34+
35+
$this->collectionFactoryMock = $this->createPartialMock(
36+
CollectionFactory::class,
37+
['create']
38+
);
39+
40+
$this->model = $objectManager->getObject(
41+
Observer::class,
42+
[
43+
'queueCollectionFactory' => $this->collectionFactoryMock
44+
]
45+
);
46+
}
47+
48+
/**
49+
* Test scheduledSend() method
50+
*/
51+
public function testScheduledSend()
52+
{
53+
$collectionMock = $this->createMock(Collection::class);
54+
$this->collectionFactoryMock->expects($this->once())->method('create')->willReturn($collectionMock);
55+
$collectionMock->expects($this->once())->method('setPageSize')->with(3)->willReturnSelf();
56+
$collectionMock->expects($this->once())->method('setCurPage')->with(1)->willReturnSelf();
57+
$collectionMock->expects($this->once())->method('addOnlyForSendingFilter')->willReturnSelf();
58+
$collectionMock->expects($this->once())->method('load')->willReturnSelf();
59+
$collectionMock->expects($this->once())->method('walk')->with('sendPerSubscriber', [20]);
60+
61+
$this->model->scheduledSend();
62+
}
63+
}

0 commit comments

Comments
 (0)