Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProblemTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 use Magento\Newsletter\Model\Problem as ProblemModel;
18 use Magento\Newsletter\Model\SubscriberFactory;
19 
23 class ProblemTest extends \PHPUnit\Framework\TestCase
24 {
28  private $contextMock;
29 
33  private $registryMock;
34 
38  private $subscriberFactoryMock;
39 
43  private $subscriberMock;
44 
48  private $resourceModelMock;
49 
53  private $abstractDbMock;
54 
58  protected $objectManager;
59 
63  private $problemModel;
64 
68  protected function setUp()
69  {
70  $this->contextMock = $this->getMockBuilder(Context::class)
71  ->disableOriginalConstructor()
72  ->getMock();
73  $this->registryMock = $this->getMockBuilder(Registry::class)
74  ->disableOriginalConstructor()
75  ->getMock();
76  $this->subscriberFactoryMock = $this->getMockBuilder(SubscriberFactory::class)
77  ->disableOriginalConstructor()
78  ->getMock();
79  $this->subscriberMock = $this->getMockBuilder(Subscriber::class)
80  ->disableOriginalConstructor()
81  ->getMock();
82  $this->resourceModelMock = $this->getMockBuilder(ProblemResource::class)
83  ->disableOriginalConstructor()
84  ->getMock();
85  $this->abstractDbMock = $this->getMockBuilder(AbstractDb::class)
86  ->disableOriginalConstructor()
87  ->getMock();
88 
89  $this->resourceModelMock->expects($this->any())
90  ->method('getIdFieldName')
91  ->willReturn('id');
92 
93  $this->objectManager = new ObjectManager($this);
94 
95  $this->problemModel = $this->objectManager->getObject(
96  ProblemModel::class,
97  [
98  'context' => $this->contextMock,
99  'registry' => $this->registryMock,
100  'subscriberFactory' => $this->subscriberFactoryMock,
101  'resource' => $this->resourceModelMock,
102  'resourceCollection' => $this->abstractDbMock,
103  'data' => [],
104  ]
105  );
106  }
107 
111  public function testAddSubscriberData()
112  {
113  $subscriberId = 1;
114  $this->subscriberMock->expects($this->once())
115  ->method('getId')
116  ->willReturn($subscriberId);
117 
118  $result = $this->problemModel->addSubscriberData($this->subscriberMock);
119 
120  self::assertEquals($result, $this->problemModel);
121  self::assertEquals($subscriberId, $this->problemModel->getSubscriberId());
122  }
123 
127  public function testAddQueueData()
128  {
129  $queueId = 1;
130  $queueMock = $this->getMockBuilder(Queue::class)
131  ->disableOriginalConstructor()
132  ->getMock();
133  $queueMock->expects($this->once())
134  ->method('getId')
135  ->willReturn($queueId);
136 
137  $result = $this->problemModel->addQueueData($queueMock);
138 
139  self::assertEquals($result, $this->problemModel);
140  self::assertEquals($queueId, $this->problemModel->getQueueId());
141  }
142 
146  public function testAddErrorData()
147  {
148  $exceptionMessage = 'Some message';
149  $exceptionCode = 111;
150  $exception = new \Exception($exceptionMessage, $exceptionCode);
151 
152  $result = $this->problemModel->addErrorData($exception);
153 
154  self::assertEquals($result, $this->problemModel);
155  self::assertEquals($exceptionMessage, $this->problemModel->getProblemErrorText());
156  self::assertEquals($exceptionCode, $this->problemModel->getProblemErrorCode());
157  }
158 
163  {
164  self::assertNull($this->problemModel->getSubscriber());
165  }
166 
170  public function testGetSubscriber()
171  {
172  $this->setSubscriber();
173  self::assertEquals($this->subscriberMock, $this->problemModel->getSubscriber());
174  }
175 
180  {
181  $this->subscriberMock->expects($this->never())
182  ->method('__call')
183  ->with($this->equalTo('setSubscriberStatus'));
184 
185  $result = $this->problemModel->unsubscribe();
186 
187  self::assertEquals($this->problemModel, $result);
188  }
189 
193  public function testUnsubscribe()
194  {
195  $this->setSubscriber();
196  $this->subscriberMock->expects($this->at(1))
197  ->method('__call')
198  ->with($this->equalTo('setSubscriberStatus'), $this->equalTo([Subscriber::STATUS_UNSUBSCRIBED]))
199  ->willReturnSelf();
200  $this->subscriberMock->expects($this->at(2))
201  ->method('__call')
202  ->with($this->equalTo('setIsStatusChanged'))
203  ->willReturnSelf();
204  $this->subscriberMock->expects($this->once())
205  ->method('save');
206 
207  $result = $this->problemModel->unsubscribe();
208 
209  self::assertEquals($this->problemModel, $result);
210  }
211 
215  private function setSubscriber()
216  {
217  $subscriberId = 1;
218  $this->problemModel->setSubscriberId($subscriberId);
219  $this->subscriberFactoryMock->expects($this->once())
220  ->method('create')
221  ->willReturn($this->subscriberMock);
222  $this->subscriberMock->expects($this->once())
223  ->method('load')
224  ->with($subscriberId)
225  ->willReturnSelf();
226  }
227 }