NotificationStorageTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model\Customer;
  7. use Magento\Customer\Model\Customer\NotificationStorage;
  8. class NotificationStorageTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var NotificationStorage
  12. */
  13. private $notificationStorage;
  14. /**
  15. * @var \Magento\Framework\Cache\FrontendInterface|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $cacheMock;
  18. /**
  19. * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $serializerMock;
  22. protected function setUp()
  23. {
  24. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  25. $this->cacheMock = $this->createMock(\Magento\Framework\Cache\FrontendInterface::class);
  26. $this->notificationStorage = $objectManager->getObject(
  27. NotificationStorage::class,
  28. ['cache' => $this->cacheMock]
  29. );
  30. $this->serializerMock = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
  31. $objectManager->setBackwardCompatibleProperty($this->notificationStorage, 'serializer', $this->serializerMock);
  32. }
  33. public function testAdd()
  34. {
  35. $customerId = 1;
  36. $notificationType = 'some_type';
  37. $data = [
  38. 'customer_id' => $customerId,
  39. 'notification_type' => $notificationType
  40. ];
  41. $serializedData = 'serialized data';
  42. $this->serializerMock->expects($this->once())
  43. ->method('serialize')
  44. ->with($data)
  45. ->willReturn($serializedData);
  46. $this->cacheMock->expects($this->once())
  47. ->method('save')
  48. ->with(
  49. $serializedData,
  50. $this->getCacheKey($notificationType, $customerId)
  51. );
  52. $this->notificationStorage->add($notificationType, $customerId);
  53. }
  54. public function testIsExists()
  55. {
  56. $customerId = 1;
  57. $notificationType = 'some_type';
  58. $this->cacheMock->expects($this->once())
  59. ->method('test')
  60. ->with($this->getCacheKey($notificationType, $customerId))
  61. ->willReturn(true);
  62. $this->assertTrue($this->notificationStorage->isExists($notificationType, $customerId));
  63. }
  64. public function testRemove()
  65. {
  66. $customerId = 1;
  67. $notificationType = 'some_type';
  68. $this->cacheMock->expects($this->once())
  69. ->method('remove')
  70. ->with($this->getCacheKey($notificationType, $customerId));
  71. $this->notificationStorage->remove($notificationType, $customerId);
  72. }
  73. /**
  74. * Get cache key
  75. *
  76. * @param string $notificationType
  77. * @param string $customerId
  78. * @return string
  79. */
  80. private function getCacheKey($notificationType, $customerId)
  81. {
  82. return 'notification_' . $notificationType . '_' . $customerId;
  83. }
  84. }