Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CompilerPreparationTest.php
Go to the documentation of this file.
1 <?php
7 
13 use Symfony\Component\Console\Input\ArgvInput;
14 use Zend\ServiceManager\ServiceManager;
16 use PHPUnit_Framework_MockObject_MockObject as Mock;
17 
18 class CompilerPreparationTest extends \PHPUnit\Framework\TestCase
19 {
23  private $model;
24 
28  private $serviceManagerMock;
29 
33  private $inputMock;
34 
38  private $filesystemDriverMock;
39 
43  private $generationDirectoryAccessMock;
44 
48  public function setUp()
49  {
50  $this->serviceManagerMock = $this->getMockBuilder(ServiceManager::class)
51  ->disableOriginalConstructor()
52  ->getMock();
53  $this->inputMock = $this->getMockBuilder(ArgvInput::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56  $this->filesystemDriverMock = $this->getMockBuilder(File::class)
57  ->disableOriginalConstructor()
58  ->getMock();
59  $this->generationDirectoryAccessMock = $this->getMockBuilder(GenerationDirectoryAccess::class)
60  ->disableOriginalConstructor()
61  ->getMock();
62 
63  $this->model = (new ObjectManager($this))->getObject(
64  CompilerPreparation::class,
65  [
66  'serviceManager' => $this->serviceManagerMock,
67  'input' => $this->inputMock,
68  'filesystemDriver' => $this->filesystemDriverMock,
69  'generationDirectoryAccess' => $this->generationDirectoryAccessMock,
70  ]
71  );
72  }
73 
81  public function testClearGenerationDirWhenNeeded($commandName, $isCompileCommand, $isHelpOption, $dirExists = false)
82  {
83  $this->inputMock->expects($this->once())
84  ->method('getFirstArgument')
85  ->willReturn($commandName);
86  $this->inputMock->expects($this->atLeastOnce())
87  ->method('hasParameterOption')
88  ->with($this->logicalOr('--help', '-h'))
89  ->willReturn($isHelpOption);
90 
91  if ($isCompileCommand && !$isHelpOption) {
92  $this->filesystemDriverMock->expects($this->exactly(2))
93  ->method('isExists')
94  ->willReturn($dirExists);
95  $this->filesystemDriverMock->expects($this->exactly(((int)$dirExists) * 2))
96  ->method('deleteDirectory');
97  } else {
98  $this->filesystemDriverMock->expects($this->never())
99  ->method('isExists');
100  $this->filesystemDriverMock->expects($this->never())
101  ->method('deleteDirectory');
102  }
103 
104  $this->generationDirectoryAccessMock->expects($this->any())
105  ->method('check')
106  ->willReturn(true);
107 
108  $this->model->handleCompilerEnvironment();
109  }
110 
114  public function commandNameDataProvider()
115  {
116  return [
117  'ST compiler, directory exists' => [
118  'commandName' => DiCompileCommand::NAME,
119  'isCompileCommand' => true,
120  'isHelpOption' => false,
121  'dirExists' => true
122  ],
123  'ST compiler, directory does not exist' => [
124  'commandName' => DiCompileCommand::NAME,
125  'isCompileCommand' => true,
126  'isHelpOption' => false,
127  'dirExists' => false
128  ],
129  'ST compiler, help option' => [
130  'commandName' => DiCompileCommand::NAME,
131  'isCompileCommand' => true,
132  'isHelpOption' => true,
133  'dirExists' => false
134  ],
135  'Other command' => [
136  'commandName' => 'not:a:compiler',
137  'isCompileCommand' => false,
138  'isHelpOption' => false,
139  ]
140  ];
141  }
142 
144  {
145  $customGenerationDirectory = '/custom/generated/code/directory';
146  $defaultDiDirectory = '/custom/di/directory';
147  $mageInitParams = ['MAGE_DIRS' => ['generation' => ['path' => $customGenerationDirectory]]];
148  $dirValueMap = [
149  [
150  $customGenerationDirectory,
151  $defaultDiDirectory
152  ],
153  [
154  true,
155  true
156  ]
157  ];
158 
159  $this->inputMock->expects($this->once())
160  ->method('getFirstArgument')
161  ->willReturn(DiCompileCommand::NAME);
162  $this->filesystemDriverMock->expects($this->exactly(2))
163  ->method('isExists')
164  ->willReturn(true);
165  $this->filesystemDriverMock->expects($this->exactly(2))
166  ->method('deleteDirectory')
167  ->willReturnMap($dirValueMap);
168  $this->serviceManagerMock->expects($this->once())
169  ->method('get')
171  ->willReturn($mageInitParams);
172  $this->generationDirectoryAccessMock->expects($this->once())
173  ->method('check')
174  ->willReturn(true);
175 
176  $this->model->handleCompilerEnvironment();
177  }
178 
180  {
181  $customGenerationDirectory = '/custom/generated/code/directory';
182  $customDiDirectory = '/custom/di/directory';
183  $dirResultMap = [
184  [
185  $this->logicalNot($this->equalTo($customGenerationDirectory)),
186  $this->logicalNot($this->equalTo($customDiDirectory))
187  ],
188  [
189  true,
190  true
191  ]
192  ];
193 
194  $this->inputMock->expects($this->once())
195  ->method('getFirstArgument')
196  ->willReturn(DiCompileCommand::NAME);
197  $this->filesystemDriverMock->expects($this->exactly(2))
198  ->method('isExists')
199  ->willReturn(true);
200  $this->filesystemDriverMock->expects($this->exactly(2))
201  ->method('deleteDirectory')
202  ->willReturnMap($dirResultMap);
203  $this->generationDirectoryAccessMock->expects($this->once())
204  ->method('check')
205  ->willReturn(true);
206 
207  $this->model->handleCompilerEnvironment();
208  }
209 }
return false
Definition: gallery.phtml:36
testClearGenerationDirWhenNeeded($commandName, $isCompileCommand, $isHelpOption, $dirExists=false)