Skip to content

Commit c059683

Browse files
author
Stanislav Idolov
authored
🔃 [EngCom] Public Pull Requests - 2.1-develop
Accepted Public Pull Requests: - #17667: [Backport] Added unit test for newsletter problem model (by @jignesh-baldha)
2 parents 8858fea + b085e8f commit c059683

File tree

1 file changed

+198
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)