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