Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IoTest.php
Go to the documentation of this file.
1 <?php
7 
10 
11 class IoTest extends \PHPUnit\Framework\TestCase
12 {
16  const GENERATION_DIRECTORY = 'generation_directory';
17 
18  const CLASS_NAME = 'class_name';
19 
20  const CLASS_FILE_NAME = 'class/file/name';
21 
22  const FILE_CONTENT = "content";
23 
31  protected $_generationDirectory;
32 
34  protected $_object;
35 
37  protected $_filesystemDriverMock;
38 
40  protected $existingFile = '/Magento/Class/Exists.php';
41 
43  protected $nonExistingFile = '/Magento/Class/Does/Not/Exists.php';
44 
45  protected function setUp()
46  {
47  $this->_generationDirectory = rtrim(self::GENERATION_DIRECTORY, '/') . '/';
48 
49  $this->_filesystemDriverMock = $this->createMock(\Magento\Framework\Filesystem\Driver\File::class);
50 
51  $this->_object = new \Magento\Framework\Code\Generator\Io(
52  $this->_filesystemDriverMock,
53  self::GENERATION_DIRECTORY
54  );
55  }
56 
57  protected function tearDown()
58  {
59  unset($this->_generationDirectory);
60  unset($this->_filesystemMock);
61  unset($this->_object);
62  unset($this->_filesystemDriverMock);
63  }
64 
65  public function testGetResultFileDirectory()
66  {
67  $expectedDirectory = self::GENERATION_DIRECTORY . '/' . 'class/';
68  $this->assertEquals($expectedDirectory, $this->_object->getResultFileDirectory(self::CLASS_NAME));
69  }
70 
71  public function testGetResultFileName()
72  {
73  $expectedFileName = self::GENERATION_DIRECTORY . '/class/name.php';
74  $this->assertEquals($expectedFileName, $this->_object->generateResultFileName(self::CLASS_NAME));
75  }
76 
80  public function testWriteResultFileAlreadyExists($resultFileName, $fileExists, $exceptionDuringRename, $success)
81  {
82  $this->_filesystemDriverMock->expects($this->once())
83  ->method('filePutContents')
84  ->with(
85  $this->stringContains($resultFileName),
86  "<?php\n" . self::FILE_CONTENT
87  )->willReturn(true);
88  $isExistsInvocationCount = $exceptionDuringRename ? 1 : 0;
89  $this->_filesystemDriverMock->expects($this->exactly($isExistsInvocationCount))
90  ->method('isExists')
91  ->willReturn($fileExists);
92 
93  if (!$exceptionDuringRename) {
94  $renameMockEvent = $this->returnValue(true);
95  } elseif ($fileExists) {
96  $renameMockEvent = $this->throwException(new FileSystemException(new Phrase('File already exists')));
97  } else {
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);
102  }
103 
104  $this->_filesystemDriverMock->expects($this->once())
105  ->method('rename')
106  ->with(
107  $this->stringContains($resultFileName),
108  $resultFileName
109  )->will($renameMockEvent); //Throw exception or return true
110 
111  $this->assertSame($success, $this->_object->writeResultFile($resultFileName, self::FILE_CONTENT));
112  }
113 
117  public function testWriteResultFileAlreadyExistsDataProvider()
118  {
119  return [
120  'Writing file succeeds: writeResultFile succeeds' => [
121  'resultFileName' => $this->nonExistingFile,
122  'fileExists' => false,
123  'exceptionDuringRename' => false,
124  'success' => true
125 
126  ],
127  'Writing file fails because class already exists on disc: writeResultFile succeeds' => [
128  'resultFileName' => $this->existingFile,
129  'fileExists' => true,
130  'exceptionDuringRename' => true,
131  'success' => true
132  ],
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,
137  'success' => false
138  ]
139  ];
140  }
141 
142  public function testMakeGenerationDirectoryWritable()
143  {
144  $this->_filesystemDriverMock->expects(
145  $this->once()
146  )->method(
147  'isWritable'
148  )->with(
149  $this->equalTo($this->_generationDirectory)
150  )->will(
151  $this->returnValue(true)
152  );
153 
154  $this->assertTrue($this->_object->makeGenerationDirectory());
155  }
156 
157  public function testMakeGenerationDirectoryReadOnly()
158  {
159  $this->_filesystemDriverMock->expects(
160  $this->once()
161  )->method(
162  'isWritable'
163  )->with(
164  $this->equalTo($this->_generationDirectory)
165  )->will(
166  $this->returnValue(false)
167  );
168 
169  $this->_filesystemDriverMock->expects(
170  $this->once()
171  )->method(
172  'createDirectory'
173  )->with(
174  $this->equalTo($this->_generationDirectory),
175  $this->anything()
176  )->will(
177  $this->returnValue(true)
178  );
179 
180  $this->assertTrue($this->_object->makeGenerationDirectory());
181  }
182 
183  public function testGetGenerationDirectory()
184  {
185  $this->assertEquals($this->_generationDirectory, $this->_object->getGenerationDirectory());
186  }
187 
193  public function testFileExists($fileName, $exists)
194  {
195  $this->_filesystemDriverMock->expects(
196  $this->once()
197  )->method(
198  'isExists'
199  )->with(
200  $this->equalTo($fileName)
201  )->will(
202  $this->returnValue($exists)
203  );
204 
205  $this->assertSame($exists, $this->_object->fileExists($fileName));
206  }
207 
211  public function fileExistsDataProvider()
212  {
213  return [
214  ['fileName' => $this->existingFile, 'exists' => true],
215  ['fileName' => $this->nonExistingFile, 'exists' => false]
216  ];
217  }
218 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
return false
Definition: gallery.phtml:36
$fileName
Definition: translate.phtml:15