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