|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Setup\Test\Unit\Module\I18n\Pack\Writer\File; |
| 7 | + |
| 8 | +use Magento\Setup\Module\I18n\Context; |
| 9 | +use Magento\Setup\Module\I18n\Locale; |
| 10 | +use Magento\Setup\Module\I18n\Dictionary; |
| 11 | +use Magento\Setup\Module\I18n\Dictionary\Phrase; |
| 12 | +use Magento\Setup\Module\I18n\Pack\Writer\File\AbstractFile; |
| 13 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 14 | + |
| 15 | +/** |
| 16 | + * Tests for Magento\Setup\Module\I18n\Pack\Writer\File\AbstractFile |
| 17 | + */ |
| 18 | +class AbstractFileTest extends \PHPUnit_Framework_TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var Context|\PHPUnit_Framework_MockObject_MockObject |
| 22 | + */ |
| 23 | + private $contextMock; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var Locale|\PHPUnit_Framework_MockObject_MockObject |
| 27 | + */ |
| 28 | + private $localeMock; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var Dictionary|\PHPUnit_Framework_MockObject_MockObject |
| 32 | + */ |
| 33 | + private $dictionaryMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var Phrase|\PHPUnit_Framework_MockObject_MockObject |
| 37 | + */ |
| 38 | + private $phraseMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var AbstractFile|\PHPUnit_Framework_MockObject_MockObject |
| 42 | + */ |
| 43 | + private $object; |
| 44 | + |
| 45 | + /** |
| 46 | + * @inheritdoc |
| 47 | + */ |
| 48 | + protected function setUp() |
| 49 | + { |
| 50 | + $objectManagerHelper = new ObjectManagerHelper($this); |
| 51 | + |
| 52 | + $this->contextMock = $this->getMock(Context::class, [], [], '', false, false); |
| 53 | + $this->localeMock = $this->getMock(Locale::class, [], [], '', false, false); |
| 54 | + $this->dictionaryMock = $this->getMock(Dictionary::class, [], [], '', false, false); |
| 55 | + $this->phraseMock = $this->getMock(Phrase::class, [], [], '', false, false); |
| 56 | + |
| 57 | + $constructorArguments = $objectManagerHelper->getConstructArguments( |
| 58 | + AbstractFile::class, |
| 59 | + ['context' => $this->contextMock] |
| 60 | + ); |
| 61 | + |
| 62 | + $this->object = $this->getMockBuilder(AbstractFile::class) |
| 63 | + ->setMethods(['_createDirectoryIfNotExist', '_writeFile']) |
| 64 | + ->setConstructorArgs($constructorArguments) |
| 65 | + ->getMockForAbstractClass(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param string $contextType |
| 70 | + * @param array $contextValue |
| 71 | + * @dataProvider writeDictionaryWithRuntimeExceptionDataProvider |
| 72 | + * @expectedException \RuntimeException |
| 73 | + * @return void |
| 74 | + */ |
| 75 | + public function testWriteDictionaryWithRuntimeException( |
| 76 | + $contextType, |
| 77 | + array $contextValue |
| 78 | + ) { |
| 79 | + $this->configureGeneralPhrasesMock($contextType, $contextValue); |
| 80 | + |
| 81 | + $this->object->expects($this->never()) |
| 82 | + ->method('_createDirectoryIfNotExist'); |
| 83 | + $this->object->expects($this->never()) |
| 84 | + ->method('_writeFile'); |
| 85 | + $this->object->writeDictionary($this->dictionaryMock, $this->localeMock); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @return array |
| 90 | + */ |
| 91 | + public function writeDictionaryWithRuntimeExceptionDataProvider() |
| 92 | + { |
| 93 | + return [ |
| 94 | + ['', []], |
| 95 | + ['module', []], |
| 96 | + ['', ['Magento_Module']], |
| 97 | + ]; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @expectedException \InvalidArgumentException |
| 102 | + * @expectedExceptionMessage Some error. Row #1. |
| 103 | + * @return void |
| 104 | + */ |
| 105 | + public function testWriteDictionaryWithInvalidArgumentException() |
| 106 | + { |
| 107 | + $contextType = 'module'; |
| 108 | + $contextValue = 'Magento_Module'; |
| 109 | + |
| 110 | + $this->configureGeneralPhrasesMock($contextType, [$contextValue]); |
| 111 | + |
| 112 | + $this->object->expects($this->never()) |
| 113 | + ->method('_createDirectoryIfNotExist'); |
| 114 | + $this->object->expects($this->never()) |
| 115 | + ->method('_writeFile'); |
| 116 | + |
| 117 | + $this->contextMock->expects($this->once()) |
| 118 | + ->method('buildPathToLocaleDirectoryByContext') |
| 119 | + ->with($contextType, $contextValue) |
| 120 | + ->willThrowException(new \InvalidArgumentException('Some error.')); |
| 121 | + |
| 122 | + $this->object->writeDictionary($this->dictionaryMock, $this->localeMock); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @return void |
| 127 | + */ |
| 128 | + public function testWriteDictionaryWherePathIsNull() |
| 129 | + { |
| 130 | + $contextType = 'module'; |
| 131 | + $contextValue = 'Magento_Module'; |
| 132 | + |
| 133 | + $this->configureGeneralPhrasesMock($contextType, [$contextValue]); |
| 134 | + |
| 135 | + $this->object->expects($this->never()) |
| 136 | + ->method('_createDirectoryIfNotExist'); |
| 137 | + $this->object->expects($this->never()) |
| 138 | + ->method('_writeFile'); |
| 139 | + |
| 140 | + $this->contextMock->expects($this->once()) |
| 141 | + ->method('buildPathToLocaleDirectoryByContext') |
| 142 | + ->with($contextType, $contextValue) |
| 143 | + ->willReturn(null); |
| 144 | + |
| 145 | + $this->object->writeDictionary($this->dictionaryMock, $this->localeMock); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @return void |
| 150 | + */ |
| 151 | + public function testWriteDictionary() |
| 152 | + { |
| 153 | + $contextType = 'module'; |
| 154 | + $contextValue = 'Magento_Module'; |
| 155 | + $path = '/some/path/'; |
| 156 | + $phrase = 'Phrase'; |
| 157 | + $locale = 'en_EN'; |
| 158 | + $fileExtension = 'csv'; |
| 159 | + $file = $path . $locale . '.' . $fileExtension; |
| 160 | + |
| 161 | + $this->configureGeneralPhrasesMock($contextType, [$contextValue]); |
| 162 | + |
| 163 | + $this->phraseMock->expects($this->once()) |
| 164 | + ->method('getPhrase') |
| 165 | + ->willReturn($phrase); |
| 166 | + |
| 167 | + $this->localeMock->expects($this->once()) |
| 168 | + ->method('__toString') |
| 169 | + ->willReturn($locale); |
| 170 | + |
| 171 | + $this->object->expects($this->once()) |
| 172 | + ->method('_getFileExtension') |
| 173 | + ->willReturn($fileExtension); |
| 174 | + $this->object->expects($this->once()) |
| 175 | + ->method('_createDirectoryIfNotExist') |
| 176 | + ->with(dirname($file)); |
| 177 | + $this->object->expects($this->once()) |
| 178 | + ->method('_writeFile') |
| 179 | + ->with($file, [$phrase => $this->phraseMock]); |
| 180 | + |
| 181 | + $this->contextMock->expects($this->once()) |
| 182 | + ->method('buildPathToLocaleDirectoryByContext') |
| 183 | + ->with($contextType, $contextValue) |
| 184 | + ->willReturn($path); |
| 185 | + |
| 186 | + $this->object->writeDictionary($this->dictionaryMock, $this->localeMock); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * @param string $contextType |
| 191 | + * @param array $contextValue |
| 192 | + * @return void |
| 193 | + */ |
| 194 | + private function configureGeneralPhrasesMock($contextType, array $contextValue) |
| 195 | + { |
| 196 | + $this->phraseMock->expects($this->any()) |
| 197 | + ->method('getContextType') |
| 198 | + ->willReturn($contextType); |
| 199 | + |
| 200 | + $this->phraseMock->expects($this->any()) |
| 201 | + ->method('getContextValue') |
| 202 | + ->willReturn($contextValue); |
| 203 | + |
| 204 | + $this->dictionaryMock->expects($this->once()) |
| 205 | + ->method('getPhrases') |
| 206 | + ->willReturn([$this->phraseMock]); |
| 207 | + } |
| 208 | +} |
0 commit comments