11 class IoTest
extends \PHPUnit\Framework\TestCase
16 const GENERATION_DIRECTORY =
'generation_directory';
18 const CLASS_NAME =
'class_name';
20 const CLASS_FILE_NAME =
'class/file/name';
22 const FILE_CONTENT =
"content";
31 protected $_generationDirectory;
37 protected $_filesystemDriverMock;
40 protected $existingFile =
'/Magento/Class/Exists.php';
43 protected $nonExistingFile =
'/Magento/Class/Does/Not/Exists.php';
45 protected function setUp()
47 $this->_generationDirectory = rtrim(self::GENERATION_DIRECTORY,
'/') .
'/';
49 $this->_filesystemDriverMock = $this->createMock(\
Magento\Framework\Filesystem\Driver\File::class);
51 $this->_object = new \Magento\Framework\Code\Generator\Io(
52 $this->_filesystemDriverMock,
53 self::GENERATION_DIRECTORY
57 protected function tearDown()
59 unset($this->_generationDirectory);
60 unset($this->_filesystemMock);
61 unset($this->_object);
62 unset($this->_filesystemDriverMock);
65 public function testGetResultFileDirectory()
67 $expectedDirectory = self::GENERATION_DIRECTORY .
'/' .
'class/';
68 $this->assertEquals($expectedDirectory, $this->_object->getResultFileDirectory(self::CLASS_NAME));
71 public function testGetResultFileName()
73 $expectedFileName = self::GENERATION_DIRECTORY .
'/class/name.php';
74 $this->assertEquals($expectedFileName, $this->_object->generateResultFileName(self::CLASS_NAME));
80 public function testWriteResultFileAlreadyExists($resultFileName, $fileExists, $exceptionDuringRename, $success)
82 $this->_filesystemDriverMock->expects($this->once())
83 ->method(
'filePutContents')
85 $this->stringContains($resultFileName),
86 "<?php\n" . self::FILE_CONTENT
88 $isExistsInvocationCount = $exceptionDuringRename ? 1 : 0;
89 $this->_filesystemDriverMock->expects($this->exactly($isExistsInvocationCount))
91 ->willReturn($fileExists);
93 if (!$exceptionDuringRename) {
94 $renameMockEvent = $this->returnValue(
true);
96 $renameMockEvent = $this->throwException(
new FileSystemException(
new Phrase(
'File already exists')));
98 $exceptionMessage =
'Some error renaming file';
99 $renameMockEvent = $this->throwException(
new FileSystemException(
new Phrase($exceptionMessage)));
100 $this->expectException(\
Magento\Framework\Exception\FileSystemException::class);
101 $this->expectExceptionMessage($exceptionMessage);
104 $this->_filesystemDriverMock->expects($this->once())
107 $this->stringContains($resultFileName),
109 )->will($renameMockEvent);
111 $this->assertSame($success, $this->_object->writeResultFile($resultFileName, self::FILE_CONTENT));
117 public function testWriteResultFileAlreadyExistsDataProvider()
120 'Writing file succeeds: writeResultFile succeeds' => [
121 'resultFileName' => $this->nonExistingFile,
122 'fileExists' =>
false,
123 'exceptionDuringRename' =>
false,
127 'Writing file fails because class already exists on disc: writeResultFile succeeds' => [
128 'resultFileName' => $this->existingFile,
129 'fileExists' =>
true,
130 'exceptionDuringRename' =>
true,
133 'Error renaming file, btu class does not exist on disc: writeResultFile throws exception and fails' => [
134 'resultFileName' => $this->nonExistingFile,
135 'fileExists' =>
false,
136 'exceptionDuringRename' =>
true,
142 public function testMakeGenerationDirectoryWritable()
144 $this->_filesystemDriverMock->expects(
149 $this->equalTo($this->_generationDirectory)
151 $this->returnValue(
true)
154 $this->assertTrue($this->_object->makeGenerationDirectory());
157 public function testMakeGenerationDirectoryReadOnly()
159 $this->_filesystemDriverMock->expects(
164 $this->equalTo($this->_generationDirectory)
166 $this->returnValue(
false)
169 $this->_filesystemDriverMock->expects(
174 $this->equalTo($this->_generationDirectory),
177 $this->returnValue(
true)
180 $this->assertTrue($this->_object->makeGenerationDirectory());
183 public function testGetGenerationDirectory()
185 $this->assertEquals($this->_generationDirectory, $this->_object->getGenerationDirectory());
193 public function testFileExists(
$fileName, $exists)
195 $this->_filesystemDriverMock->expects(
202 $this->returnValue($exists)
205 $this->assertSame($exists, $this->_object->fileExists(
$fileName));
211 public function fileExistsDataProvider()
214 [
'fileName' => $this->existingFile,
'exists' =>
true],
215 [
'fileName' => $this->nonExistingFile,
'exists' =>
false]
elseif(isset( $params[ 'redirect_parent']))