NotificationServiceTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test class for \Magento\AdminNotification\Model\NotificationService
  8. */
  9. namespace Magento\AdminNotification\Test\Unit\Model;
  10. class NotificationServiceTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * Retrieve instance of notification service model
  14. *
  15. * @param $notificationId
  16. * @return \Magento\AdminNotification\Model\NotificationService
  17. */
  18. protected function _getServiceInstanceForMarkAsReadTest($notificationId)
  19. {
  20. /**
  21. * @var
  22. * $notificationFactory \PHPUnit_Framework_MockObject_MockObject|\Magento\AdminNotification\Model\InboxFactory
  23. */
  24. $notificationFactory = $this->createPartialMock(
  25. \Magento\AdminNotification\Model\InboxFactory::class,
  26. ['create']
  27. );
  28. $notification = $this->createPartialMock(
  29. \Magento\AdminNotification\Model\Inbox::class,
  30. ['load', 'getId', 'save', 'setIsRead', '__sleep', '__wakeup']
  31. );
  32. $notification->expects($this->once())->method('load')->with($notificationId)->will($this->returnSelf());
  33. $notification->expects($this->once())->method('getId')->will($this->returnValue($notificationId));
  34. // when notification Id is valid, add additional expectations
  35. if ($notificationId) {
  36. $notification->expects($this->once())->method('save')->will($this->returnSelf());
  37. $notification->expects($this->once())->method('setIsRead')->with(1)->will($this->returnSelf());
  38. }
  39. $notificationFactory->expects($this->once())->method('create')->will($this->returnValue($notification));
  40. return new \Magento\AdminNotification\Model\NotificationService($notificationFactory);
  41. }
  42. public function testMarkAsRead()
  43. {
  44. $notificationId = 1;
  45. $service = $this->_getServiceInstanceForMarkAsReadTest($notificationId);
  46. $service->markAsRead($notificationId);
  47. }
  48. /**
  49. * @expectedException \Magento\Framework\Exception\LocalizedException
  50. * @expectedExceptionMessage Wrong notification ID specified.
  51. */
  52. public function testMarkAsReadThrowsExceptionWhenNotificationIdIsInvalid()
  53. {
  54. $notificationId = null;
  55. $service = $this->_getServiceInstanceForMarkAsReadTest($notificationId);
  56. $service->markAsRead($notificationId);
  57. }
  58. }