Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CanViewNotificationTest.php
Go to the documentation of this file.
1 <?php
7 
15 
16 class CanViewNotificationTest extends \PHPUnit\Framework\TestCase
17 {
19  private $canViewNotification;
20 
22  private $viewerLoggerMock;
23 
25  private $productMetadataMock;
26 
28  private $sessionMock;
29 
31  private $logMock;
32 
34  private $cacheStorageMock;
35 
36  public function setUp()
37  {
38  $this->cacheStorageMock = $this->getMockBuilder(CacheInterface::class)
39  ->getMockForAbstractClass();
40  $this->logMock = $this->getMockBuilder(Log::class)
41  ->getMock();
42  $this->sessionMock = $this->getMockBuilder(Session::class)
43  ->disableOriginalConstructor()
44  ->setMethods(['getUser', 'getId'])
45  ->getMock();
46  $this->viewerLoggerMock = $this->getMockBuilder(Logger::class)
47  ->disableOriginalConstructor()
48  ->getMock();
49  $this->productMetadataMock = $this->getMockBuilder(ProductMetadataInterface::class)
50  ->disableOriginalConstructor()
51  ->getMock();
52  $objectManager = new ObjectManager($this);
53  $this->canViewNotification = $objectManager->getObject(
54  CanViewNotification::class,
55  [
56  'viewerLogger' => $this->viewerLoggerMock,
57  'session' => $this->sessionMock,
58  'productMetadata' => $this->productMetadataMock,
59  'cacheStorage' => $this->cacheStorageMock,
60  ]
61  );
62  }
63 
65  {
66  $this->sessionMock->expects($this->once())
67  ->method('getUser')
68  ->willReturn($this->sessionMock);
69  $this->sessionMock->expects($this->once())
70  ->method('getId')
71  ->willReturn(1);
72  $this->cacheStorageMock->expects($this->once())
73  ->method('load')
74  ->with('release-notification-popup-1')
75  ->willReturn("0");
76  $this->assertEquals(false, $this->canViewNotification->isVisible([]));
77  }
78 
85  public function testIsVisible($expected, $version, $lastViewVersion)
86  {
87  $this->cacheStorageMock->expects($this->once())
88  ->method('load')
89  ->with('release-notification-popup-1')
90  ->willReturn(false);
91  $this->sessionMock->expects($this->once())
92  ->method('getUser')
93  ->willReturn($this->sessionMock);
94  $this->sessionMock->expects($this->once())
95  ->method('getId')
96  ->willReturn(1);
97  $this->productMetadataMock->expects($this->once())
98  ->method('getVersion')
99  ->willReturn($version);
100  $this->logMock->expects($this->once())
101  ->method('getLastViewVersion')
102  ->willReturn($lastViewVersion);
103  $this->viewerLoggerMock->expects($this->once())
104  ->method('get')
105  ->with(1)
106  ->willReturn($this->logMock);
107  $this->cacheStorageMock->expects($this->once())
108  ->method('save')
109  ->with(false, 'release-notification-popup-1');
110  $this->assertEquals($expected, $this->canViewNotification->isVisible([]));
111  }
112 
116  public function isVisibleProvider()
117  {
118  return [
119  [false, '2.2.1-dev', '999.999.999-alpha'],
120  [true, '2.2.1-dev', '2.0.0'],
121  [true, '2.2.1-dev', null],
122  [false, '2.2.1-dev', '2.2.1'],
123  [true, '2.2.1-dev', '2.2.0'],
124  [true, '2.3.0', '2.2.0'],
125  [false, '2.2.2', '2.2.2'],
126  ];
127  }
128 }
$objectManager
Definition: bootstrap.php:17