CanViewNotificationTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ReleaseNotification\Test\Unit\Model\Condition;
  7. use Magento\ReleaseNotification\Model\Condition\CanViewNotification;
  8. use Magento\ReleaseNotification\Model\ResourceModel\Viewer\Logger;
  9. use Magento\ReleaseNotification\Model\Viewer\Log;
  10. use Magento\Framework\App\ProductMetadataInterface;
  11. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  12. use Magento\Backend\Model\Auth\Session;
  13. use Magento\Framework\App\CacheInterface;
  14. class CanViewNotificationTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /** @var CanViewNotification */
  17. private $canViewNotification;
  18. /** @var Logger|\PHPUnit_Framework_MockObject_MockObject */
  19. private $viewerLoggerMock;
  20. /** @var ProductMetadataInterface|\PHPUnit_Framework_MockObject_MockObject */
  21. private $productMetadataMock;
  22. /** @var Session|\PHPUnit_Framework_MockObject_MockObject */
  23. private $sessionMock;
  24. /** @var Log|\PHPUnit_Framework_MockObject_MockObject */
  25. private $logMock;
  26. /** @var $cacheStorageMock \PHPUnit_Framework_MockObject_MockObject|CacheInterface */
  27. private $cacheStorageMock;
  28. public function setUp()
  29. {
  30. $this->cacheStorageMock = $this->getMockBuilder(CacheInterface::class)
  31. ->getMockForAbstractClass();
  32. $this->logMock = $this->getMockBuilder(Log::class)
  33. ->getMock();
  34. $this->sessionMock = $this->getMockBuilder(Session::class)
  35. ->disableOriginalConstructor()
  36. ->setMethods(['getUser', 'getId'])
  37. ->getMock();
  38. $this->viewerLoggerMock = $this->getMockBuilder(Logger::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->productMetadataMock = $this->getMockBuilder(ProductMetadataInterface::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $objectManager = new ObjectManager($this);
  45. $this->canViewNotification = $objectManager->getObject(
  46. CanViewNotification::class,
  47. [
  48. 'viewerLogger' => $this->viewerLoggerMock,
  49. 'session' => $this->sessionMock,
  50. 'productMetadata' => $this->productMetadataMock,
  51. 'cacheStorage' => $this->cacheStorageMock,
  52. ]
  53. );
  54. }
  55. public function testIsVisibleLoadDataFromCache()
  56. {
  57. $this->sessionMock->expects($this->once())
  58. ->method('getUser')
  59. ->willReturn($this->sessionMock);
  60. $this->sessionMock->expects($this->once())
  61. ->method('getId')
  62. ->willReturn(1);
  63. $this->cacheStorageMock->expects($this->once())
  64. ->method('load')
  65. ->with('release-notification-popup-1')
  66. ->willReturn("0");
  67. $this->assertEquals(false, $this->canViewNotification->isVisible([]));
  68. }
  69. /**
  70. * @param bool $expected
  71. * @param string $version
  72. * @param string|null $lastViewVersion
  73. * @dataProvider isVisibleProvider
  74. */
  75. public function testIsVisible($expected, $version, $lastViewVersion)
  76. {
  77. $this->cacheStorageMock->expects($this->once())
  78. ->method('load')
  79. ->with('release-notification-popup-1')
  80. ->willReturn(false);
  81. $this->sessionMock->expects($this->once())
  82. ->method('getUser')
  83. ->willReturn($this->sessionMock);
  84. $this->sessionMock->expects($this->once())
  85. ->method('getId')
  86. ->willReturn(1);
  87. $this->productMetadataMock->expects($this->once())
  88. ->method('getVersion')
  89. ->willReturn($version);
  90. $this->logMock->expects($this->once())
  91. ->method('getLastViewVersion')
  92. ->willReturn($lastViewVersion);
  93. $this->viewerLoggerMock->expects($this->once())
  94. ->method('get')
  95. ->with(1)
  96. ->willReturn($this->logMock);
  97. $this->cacheStorageMock->expects($this->once())
  98. ->method('save')
  99. ->with(false, 'release-notification-popup-1');
  100. $this->assertEquals($expected, $this->canViewNotification->isVisible([]));
  101. }
  102. /**
  103. * @return array
  104. */
  105. public function isVisibleProvider()
  106. {
  107. return [
  108. [false, '2.2.1-dev', '999.999.999-alpha'],
  109. [true, '2.2.1-dev', '2.0.0'],
  110. [true, '2.2.1-dev', null],
  111. [false, '2.2.1-dev', '2.2.1'],
  112. [true, '2.2.1-dev', '2.2.0'],
  113. [true, '2.3.0', '2.2.0'],
  114. [false, '2.2.2', '2.2.2'],
  115. ];
  116. }
  117. }