Skip to content

Commit 4283cc6

Browse files
rogyarjignesh-baldha
authored andcommitted
Added unit test for newsletter problem model
1 parent 8858fea commit 4283cc6

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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\Data\Collection\AbstractDb;
11+
use Magento\Framework\Model\Context;
12+
use Magento\Framework\Model\ResourceModel\AbstractResource;
13+
use Magento\Framework\Registry;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use Magento\Newsletter\Model\Problem as ProblemModel;
16+
use Magento\Newsletter\Model\Queue;
17+
use Magento\Newsletter\Model\ResourceModel\Problem;
18+
use Magento\Newsletter\Model\Subscriber;
19+
use Magento\Newsletter\Model\SubscriberFactory;
20+
21+
class ProblemTest extends \PHPUnit\Framework\TestCase
22+
{
23+
/**
24+
* @var Context|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $contextMock;
27+
28+
/**
29+
* @var Registry|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
private $registryMock;
32+
33+
/**
34+
* @var SubscriberFactory|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
private $subscriberFactoryMock;
37+
38+
/**
39+
* @var Subscriber|\PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
private $subscriberMock;
42+
43+
/**
44+
* @var AbstractResource|\PHPUnit_Framework_MockObject_MockObject
45+
*/
46+
private $abstractResourceMock;
47+
48+
/**
49+
* @var AbstractDb|\PHPUnit_Framework_MockObject_MockObject
50+
*/
51+
private $abstractDbMock;
52+
53+
/**
54+
* @var ObjectManager
55+
*/
56+
protected $objectManager;
57+
58+
/**
59+
* @var ProblemModel
60+
*/
61+
private $problemModel;
62+
63+
protected function setUp()
64+
{
65+
$this->contextMock = $this->getMockBuilder(Context::class)
66+
->disableOriginalConstructor()
67+
->getMock();
68+
$this->registryMock = $this->getMockBuilder(Registry::class)
69+
->disableOriginalConstructor()
70+
->getMock();
71+
$this->subscriberFactoryMock = $this->getMockBuilder(SubscriberFactory::class)
72+
->getMock();
73+
$this->subscriberMock = $this->getMockBuilder(Subscriber::class)
74+
->disableOriginalConstructor()
75+
->getMock();
76+
$this->abstractResourceMock = $this->getMockBuilder(Problem::class)
77+
->disableOriginalConstructor()
78+
->getMock();
79+
$this->abstractDbMock = $this->getMockBuilder(AbstractDb::class)
80+
->disableOriginalConstructor()
81+
->getMock();
82+
83+
$this->abstractResourceMock->expects($this->any())
84+
->method('getIdFieldName')
85+
->willReturn('id');
86+
87+
$this->objectManager = new ObjectManager($this);
88+
89+
$this->problemModel = $this->objectManager->getObject(
90+
ProblemModel::class,
91+
[
92+
'context' => $this->contextMock,
93+
'registry' => $this->registryMock,
94+
'subscriberFactory' => $this->subscriberFactoryMock,
95+
'resource' => $this->abstractResourceMock,
96+
'resourceCollection' => $this->abstractDbMock,
97+
'data' => [],
98+
]
99+
);
100+
}
101+
102+
public function testAddSubscriberData()
103+
{
104+
$subscriberId = 1;
105+
$this->subscriberMock->expects($this->once())
106+
->method('getId')
107+
->willReturn($subscriberId);
108+
109+
$result = $this->problemModel->addSubscriberData($this->subscriberMock);
110+
111+
self::assertEquals($result, $this->problemModel);
112+
self::assertEquals($subscriberId, $this->problemModel->getSubscriberId());
113+
}
114+
115+
public function testAddQueueData()
116+
{
117+
$queueId = 1;
118+
$queueMock = $this->getMockBuilder(Queue::class)
119+
->disableOriginalConstructor()
120+
->getMock();
121+
$queueMock->expects($this->once())
122+
->method('getId')
123+
->willReturn($queueId);
124+
125+
$result = $this->problemModel->addQueueData($queueMock);
126+
127+
self::assertEquals($result, $this->problemModel);
128+
self::assertEquals($queueId, $this->problemModel->getQueueId());
129+
}
130+
131+
public function testAddErrorData()
132+
{
133+
$exceptionMessage = 'Some message';
134+
$exceptionCode = 111;
135+
$exception = new \Exception($exceptionMessage, $exceptionCode);
136+
137+
$result = $this->problemModel->addErrorData($exception);
138+
139+
self::assertEquals($result, $this->problemModel);
140+
self::assertEquals($exceptionMessage, $this->problemModel->getProblemErrorText());
141+
self::assertEquals($exceptionCode, $this->problemModel->getProblemErrorCode());
142+
}
143+
144+
public function testGetSubscriberWithNoSubscriberId()
145+
{
146+
self::assertNull($this->problemModel->getSubscriber());
147+
}
148+
149+
public function testGetSubscriber()
150+
{
151+
$this->setSubscriber();
152+
self::assertEquals($this->subscriberMock, $this->problemModel->getSubscriber());
153+
}
154+
155+
public function testUnsubscribeWithNoSubscriber()
156+
{
157+
$this->subscriberMock->expects($this->never())
158+
->method('__call')
159+
->with($this->equalTo('setSubscriberStatus'));
160+
161+
$result = $this->problemModel->unsubscribe();
162+
163+
self::assertEquals($this->problemModel, $result);
164+
}
165+
166+
public function testUnsubscribe()
167+
{
168+
$this->setSubscriber();
169+
$this->subscriberMock->expects($this->at(1))
170+
->method('__call')
171+
->with($this->equalTo('setSubscriberStatus'), $this->equalTo([Subscriber::STATUS_UNSUBSCRIBED]))
172+
->willReturnSelf();
173+
$this->subscriberMock->expects($this->at(2))
174+
->method('__call')
175+
->with($this->equalTo('setIsStatusChanged'))
176+
->willReturnSelf();
177+
$this->subscriberMock->expects($this->once())
178+
->method('save');
179+
180+
$result = $this->problemModel->unsubscribe();
181+
182+
self::assertEquals($this->problemModel, $result);
183+
}
184+
185+
/**
186+
* Sets subscriber to the Problem model
187+
*/
188+
private function setSubscriber()
189+
{
190+
$subscriberId = 1;
191+
$this->problemModel->setSubscriberId($subscriberId);
192+
$this->subscriberFactoryMock->expects($this->once())
193+
->method('create')
194+
->willReturn($this->subscriberMock);
195+
$this->subscriberMock->expects($this->once())
196+
->method('load')
197+
->with($subscriberId)
198+
->willReturnSelf();
199+
}
200+
}

0 commit comments

Comments
 (0)