contextMock = $this->getMockBuilder(Context::class) ->disableOriginalConstructor() ->getMock(); $this->registryMock = $this->getMockBuilder(Registry::class) ->disableOriginalConstructor() ->getMock(); $this->subscriberFactoryMock = $this->getMockBuilder(SubscriberFactory::class) ->disableOriginalConstructor() ->getMock(); $this->subscriberMock = $this->getMockBuilder(Subscriber::class) ->disableOriginalConstructor() ->getMock(); $this->resourceModelMock = $this->getMockBuilder(ProblemResource::class) ->disableOriginalConstructor() ->getMock(); $this->abstractDbMock = $this->getMockBuilder(AbstractDb::class) ->disableOriginalConstructor() ->getMock(); $this->resourceModelMock->expects($this->any()) ->method('getIdFieldName') ->willReturn('id'); $this->objectManager = new ObjectManager($this); $this->problemModel = $this->objectManager->getObject( ProblemModel::class, [ 'context' => $this->contextMock, 'registry' => $this->registryMock, 'subscriberFactory' => $this->subscriberFactoryMock, 'resource' => $this->resourceModelMock, 'resourceCollection' => $this->abstractDbMock, 'data' => [], ] ); } /** * @return void */ public function testAddSubscriberData() { $subscriberId = 1; $this->subscriberMock->expects($this->once()) ->method('getId') ->willReturn($subscriberId); $result = $this->problemModel->addSubscriberData($this->subscriberMock); self::assertEquals($result, $this->problemModel); self::assertEquals($subscriberId, $this->problemModel->getSubscriberId()); } /** * @return void */ public function testAddQueueData() { $queueId = 1; $queueMock = $this->getMockBuilder(Queue::class) ->disableOriginalConstructor() ->getMock(); $queueMock->expects($this->once()) ->method('getId') ->willReturn($queueId); $result = $this->problemModel->addQueueData($queueMock); self::assertEquals($result, $this->problemModel); self::assertEquals($queueId, $this->problemModel->getQueueId()); } /** * @return void */ public function testAddErrorData() { $exceptionMessage = 'Some message'; $exceptionCode = 111; $exception = new \Exception($exceptionMessage, $exceptionCode); $result = $this->problemModel->addErrorData($exception); self::assertEquals($result, $this->problemModel); self::assertEquals($exceptionMessage, $this->problemModel->getProblemErrorText()); self::assertEquals($exceptionCode, $this->problemModel->getProblemErrorCode()); } /** * @return void */ public function testGetSubscriberWithNoSubscriberId() { self::assertNull($this->problemModel->getSubscriber()); } /** * @return void */ public function testGetSubscriber() { $this->setSubscriber(); self::assertEquals($this->subscriberMock, $this->problemModel->getSubscriber()); } /** * @return void */ public function testUnsubscribeWithNoSubscriber() { $this->subscriberMock->expects($this->never()) ->method('__call') ->with($this->equalTo('setSubscriberStatus')); $result = $this->problemModel->unsubscribe(); self::assertEquals($this->problemModel, $result); } /** * @return void */ public function testUnsubscribe() { $this->setSubscriber(); $this->subscriberMock->expects($this->at(1)) ->method('__call') ->with($this->equalTo('setSubscriberStatus'), $this->equalTo([Subscriber::STATUS_UNSUBSCRIBED])) ->willReturnSelf(); $this->subscriberMock->expects($this->at(2)) ->method('__call') ->with($this->equalTo('setIsStatusChanged')) ->willReturnSelf(); $this->subscriberMock->expects($this->once()) ->method('save'); $result = $this->problemModel->unsubscribe(); self::assertEquals($this->problemModel, $result); } /** * Sets subscriber to the Problem model */ private function setSubscriber() { $subscriberId = 1; $this->problemModel->setSubscriberId($subscriberId); $this->subscriberFactoryMock->expects($this->once()) ->method('create') ->willReturn($this->subscriberMock); $this->subscriberMock->expects($this->once()) ->method('load') ->with($subscriberId) ->willReturnSelf(); } }