Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NotificationStorageTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class NotificationStorageTest extends \PHPUnit\Framework\TestCase
11 {
12 
16  private $notificationStorage;
17 
21  private $cacheMock;
22 
26  private $serializerMock;
27 
28  protected function setUp()
29  {
30  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
31  $this->cacheMock = $this->createMock(\Magento\Framework\Cache\FrontendInterface::class);
32  $this->notificationStorage = $objectManager->getObject(
33  NotificationStorage::class,
34  ['cache' => $this->cacheMock]
35  );
36  $this->serializerMock = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
37  $objectManager->setBackwardCompatibleProperty($this->notificationStorage, 'serializer', $this->serializerMock);
38  }
39 
40  public function testAdd()
41  {
42  $customerId = 1;
43  $notificationType = 'some_type';
44  $data = [
45  'customer_id' => $customerId,
46  'notification_type' => $notificationType
47  ];
48  $serializedData = 'serialized data';
49  $this->serializerMock->expects($this->once())
50  ->method('serialize')
51  ->with($data)
52  ->willReturn($serializedData);
53  $this->cacheMock->expects($this->once())
54  ->method('save')
55  ->with(
56  $serializedData,
57  $this->getCacheKey($notificationType, $customerId)
58  );
59  $this->notificationStorage->add($notificationType, $customerId);
60  }
61 
62  public function testIsExists()
63  {
64  $customerId = 1;
65  $notificationType = 'some_type';
66  $this->cacheMock->expects($this->once())
67  ->method('test')
68  ->with($this->getCacheKey($notificationType, $customerId))
69  ->willReturn(true);
70  $this->assertTrue($this->notificationStorage->isExists($notificationType, $customerId));
71  }
72 
73  public function testRemove()
74  {
75  $customerId = 1;
76  $notificationType = 'some_type';
77  $this->cacheMock->expects($this->once())
78  ->method('remove')
79  ->with($this->getCacheKey($notificationType, $customerId));
80  $this->notificationStorage->remove($notificationType, $customerId);
81  }
82 
90  private function getCacheKey($notificationType, $customerId)
91  {
92  return 'notification_' . $notificationType . '_' . $customerId;
93  }
94 }
$objectManager
Definition: bootstrap.php:17