ProblemTest.php 6.4 KB

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