FaviconTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Test\Unit\Model\Favicon;
  7. use \Magento\Theme\Model\Favicon\Favicon;
  8. use Magento\Config\Model\Config\Backend\Image\Favicon as ImageFavicon;
  9. use Magento\Framework\App\Filesystem\DirectoryList;
  10. use Magento\Framework\UrlInterface;
  11. use Magento\Store\Model\ScopeInterface;
  12. /**
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class FaviconTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var Favicon
  19. */
  20. protected $object;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\Store
  23. */
  24. protected $store;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Config\ScopeConfigInterface
  27. */
  28. protected $scopeManager;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\MediaStorage\Helper\File\Storage\Database
  31. */
  32. protected $fileStorageDatabase;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Filesystem\Directory\ReadInterface
  35. */
  36. protected $mediaDir;
  37. /**
  38. * Initialize testable object
  39. */
  40. protected function setUp()
  41. {
  42. $storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)->getMock();
  43. $this->store = $this->getMockBuilder(
  44. \Magento\Store\Model\Store::class
  45. )->disableOriginalConstructor()->getMock();
  46. $storeManager->expects($this->any())
  47. ->method('getStore')
  48. ->willReturn($this->store);
  49. /** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
  50. $this->scopeManager = $this->getMockBuilder(
  51. \Magento\Framework\App\Config\ScopeConfigInterface::class
  52. )->getMock();
  53. $this->fileStorageDatabase = $this->getMockBuilder(\Magento\MediaStorage\Helper\File\Storage\Database::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $this->mediaDir = $this->getMockBuilder(
  60. \Magento\Framework\Filesystem\Directory\ReadInterface::class
  61. )->getMock();
  62. $filesystem->expects($this->once())
  63. ->method('getDirectoryRead')
  64. ->with(DirectoryList::MEDIA)
  65. ->willReturn($this->mediaDir);
  66. /** @var \Magento\Framework\Filesystem $filesystem */
  67. $this->object = new Favicon(
  68. $storeManager,
  69. $this->scopeManager,
  70. $this->fileStorageDatabase,
  71. $filesystem
  72. );
  73. }
  74. /**
  75. * cover negative case for getFaviconFile
  76. */
  77. public function testGetFaviconFileNegative()
  78. {
  79. $this->assertFalse($this->object->getFaviconFile());
  80. }
  81. /**
  82. * cover positive case for getFaviconFile and checkIsFile
  83. */
  84. public function testGetFaviconFile()
  85. {
  86. $scopeConfigValue = 'path';
  87. $urlToMediaDir = 'http://magento.url/pub/media/';
  88. $expectedFile = ImageFavicon::UPLOAD_DIR . '/' . $scopeConfigValue;
  89. $expectedUrl = $urlToMediaDir . $expectedFile;
  90. $this->scopeManager->expects($this->once())
  91. ->method('getValue')
  92. ->with('design/head/shortcut_icon', ScopeInterface::SCOPE_STORE)
  93. ->willReturn($scopeConfigValue);
  94. $this->store->expects($this->once())
  95. ->method('getBaseUrl')
  96. ->with(UrlInterface::URL_TYPE_MEDIA)
  97. ->willReturn($urlToMediaDir);
  98. $this->fileStorageDatabase->expects($this->once())
  99. ->method('checkDbUsage')
  100. ->willReturn(true);
  101. $this->fileStorageDatabase->expects($this->once())
  102. ->method('saveFileToFilesystem')
  103. ->willReturn(true);
  104. $this->mediaDir->expects($this->at(0))
  105. ->method('isFile')
  106. ->with($expectedFile)
  107. ->willReturn(false);
  108. $this->mediaDir->expects($this->at(1))
  109. ->method('isFile')
  110. ->with($expectedFile)
  111. ->willReturn(true);
  112. $results = $this->object->getFaviconFile();
  113. $this->assertEquals(
  114. $expectedUrl,
  115. $results
  116. );
  117. $this->assertNotFalse($results);
  118. }
  119. /**
  120. * cover getDefaultFavicon
  121. */
  122. public function testGetDefaultFavicon()
  123. {
  124. $this->assertEquals('Magento_Theme::favicon.ico', $this->object->getDefaultFavicon());
  125. }
  126. }