Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PhpReadinessCheckTest.php
Go to the documentation of this file.
1 <?php
7 
11 
12 class PhpReadinessCheckTest extends \PHPUnit\Framework\TestCase
13 {
17  private $composerInfo;
18 
22  private $phpInfo;
23 
27  private $versionParser;
28 
34  protected $dataSize;
35 
39  private $phpReadinessCheck;
40 
41  public function setUp()
42  {
43  $this->composerInfo = $this->createMock(\Magento\Framework\Composer\ComposerInformation::class);
44  $this->phpInfo = $this->createMock(\Magento\Setup\Model\PhpInformation::class);
45  $this->versionParser = $this->createMock(\Composer\Package\Version\VersionParser::class);
46  $this->dataSize = $this->createMock(\Magento\Framework\Convert\DataSize::class);
47  $this->phpReadinessCheck = new PhpReadinessCheck(
48  $this->composerInfo,
49  $this->phpInfo,
50  $this->versionParser,
51  $this->dataSize
52  );
53  }
54 
56  {
57  $this->composerInfo->expects($this->once())
58  ->method('getRequiredPhpVersion')
59  ->willThrowException(new \Exception());
60  $expected = [
62  'data' => [
63  'error' => 'phpVersionError',
64  'message' => 'Cannot determine required PHP version: '
65  ]
66  ];
67  $this->assertEquals($expected, $this->phpReadinessCheck->checkPhpVersion());
68  }
69 
71  {
72  $this->composerInfo->expects($this->once())->method('getRequiredPhpVersion')->willReturn('1.0');
73  $multipleConstraints = $this->getMockForAbstractClass(
74  \Composer\Semver\Constraint\ConstraintInterface::class,
75  [],
76  '',
77  false
78  );
79  $this->versionParser->expects($this->at(0))->method('parseConstraints')->willReturn($multipleConstraints);
80  $this->versionParser->expects($this->at(1))
81  ->method('normalize')
82  ->willThrowException(new \UnexpectedValueException());
83  $this->versionParser->expects($this->at(2))->method('normalize')->willReturn('1.0');
84  $currentPhpVersion = $this->getMockForAbstractClass(
85  \Composer\Semver\Constraint\ConstraintInterface::class,
86  [],
87  '',
88  false
89  );
90  $this->versionParser->expects($this->at(3))->method('parseConstraints')->willReturn($currentPhpVersion);
91  $multipleConstraints->expects($this->once())->method('matches')->willReturn(true);
92  $expected = [
94  'data' => [
95  'required' => 1.0,
96  'current' => PHP_VERSION,
97  ],
98  ];
99  $this->assertEquals($expected, $this->phpReadinessCheck->checkPhpVersion());
100  }
101 
103  {
104  $this->composerInfo->expects($this->once())->method('getRequiredPhpVersion')->willReturn('1.0');
105  $multipleConstraints = $this->getMockForAbstractClass(
106  \Composer\Semver\Constraint\ConstraintInterface::class,
107  [],
108  '',
109  false
110  );
111  $this->versionParser->expects($this->at(0))->method('parseConstraints')->willReturn($multipleConstraints);
112  $this->versionParser->expects($this->at(1))
113  ->method('normalize')
114  ->willThrowException(new \UnexpectedValueException());
115  $this->versionParser->expects($this->at(2))->method('normalize')->willReturn('1.0');
116  $currentPhpVersion = $this->getMockForAbstractClass(
117  \Composer\Semver\Constraint\ConstraintInterface::class,
118  [],
119  '',
120  false
121  );
122  $this->versionParser->expects($this->at(3))->method('parseConstraints')->willReturn($currentPhpVersion);
123  $multipleConstraints->expects($this->once())->method('matches')->willReturn(false);
124  $expected = [
126  'data' => [
127  'required' => 1.0,
128  'current' => PHP_VERSION,
129  ],
130  ];
131  $this->assertEquals($expected, $this->phpReadinessCheck->checkPhpVersion());
132  }
133 
134  private function setUpNoPrettyVersionParser()
135  {
136  $multipleConstraints = $this->getMockForAbstractClass(
137  \Composer\Semver\Constraint\ConstraintInterface::class,
138  [],
139  '',
140  false
141  );
142  $this->versionParser->expects($this->at(0))->method('parseConstraints')->willReturn($multipleConstraints);
143  $this->versionParser->expects($this->at(1))->method('normalize')->willReturn('1.0');
144  $currentPhpVersion = $this->getMockForAbstractClass(
145  \Composer\Semver\Constraint\ConstraintInterface::class,
146  [],
147  '',
148  false
149  );
150  $this->versionParser->expects($this->at(2))->method('parseConstraints')->willReturn($currentPhpVersion);
151  $multipleConstraints->expects($this->once())->method('matches')->willReturn(true);
152  }
153 
154  public function testCheckPhpVersion()
155  {
156  $this->composerInfo->expects($this->once())->method('getRequiredPhpVersion')->willReturn('1.0');
157 
158  $this->setUpNoPrettyVersionParser();
159  $expected = [
161  'data' => [
162  'required' => 1.0,
163  'current' => PHP_VERSION,
164  ],
165  ];
166  $this->assertEquals($expected, $this->phpReadinessCheck->checkPhpVersion());
167  }
168 
169  public function testCheckPhpVersionFailed()
170  {
171  $this->composerInfo->expects($this->once())->method('getRequiredPhpVersion')->willReturn('1.0');
172  $multipleConstraints = $this->getMockForAbstractClass(
173  \Composer\Semver\Constraint\ConstraintInterface::class,
174  [],
175  '',
176  false
177  );
178  $this->versionParser->expects($this->at(0))->method('parseConstraints')->willReturn($multipleConstraints);
179  $this->versionParser->expects($this->at(1))->method('normalize')->willReturn('1.0');
180  $currentPhpVersion = $this->getMockForAbstractClass(
181  \Composer\Semver\Constraint\ConstraintInterface::class,
182  [],
183  '',
184  false
185  );
186  $this->versionParser->expects($this->at(2))->method('parseConstraints')->willReturn($currentPhpVersion);
187  $multipleConstraints->expects($this->once())->method('matches')->willReturn(false);
188  $expected = [
190  'data' => [
191  'required' => 1.0,
192  'current' => PHP_VERSION,
193  ],
194  ];
195  $this->assertEquals($expected, $this->phpReadinessCheck->checkPhpVersion());
196  }
197 
198  public function testCheckPhpSettings()
199  {
200  $this->phpInfo->expects($this->once())->method('getCurrent')->willReturn(['xdebug']);
201  $this->phpInfo->expects($this->once())->method('getRequiredMinimumXDebugNestedLevel')->willReturn(50);
202  $xdebugMessage = sprintf(
203  'Your current setting of xdebug.max_nesting_level=%d.
204  Magento 2 requires it to be set to %d or more.
205  Edit your config, restart web server, and try again.',
206  100,
207  50
208  );
209 
210  $rawPostMessage = sprintf(
211  'Your PHP Version is %s, but always_populate_raw_post_data = -1.
212  $HTTP_RAW_POST_DATA is deprecated from PHP 5.6 onwards and will be removed in PHP 7.0.
213  This will stop the installer from running.
214  Please open your php.ini file and set always_populate_raw_post_data to -1.
215  If you need more help please call your hosting provider.',
216  PHP_VERSION
217  );
218  $expected = [
220  'data' => [
221  'xdebug_max_nesting_level' => [
222  'message' => $xdebugMessage,
223  'error' => false,
224  ],
225  'missed_function_imagecreatefromjpeg' => [
226  'message' => 'You must have installed GD library with --with-jpeg-dir=DIR option.',
227  'helpUrl' => 'http://php.net/manual/en/image.installation.php',
228  'error' => false,
229  ],
230  ],
231  ];
232  if (!$this->isPhp7OrHhvm()) {
233  $this->setUpNoPrettyVersionParser();
234  $expected['data']['always_populate_raw_post_data'] = [
235  'message' => $rawPostMessage,
236  'helpUrl' => 'http://php.net/manual/en/ini.core.php#ini.always-populate-settings-data',
237  'error' => false
238  ];
239  }
240  $this->assertEquals($expected, $this->phpReadinessCheck->checkPhpSettings());
241  }
242 
243  public function testCheckPhpSettingsFailed()
244  {
245  $this->phpInfo->expects($this->once())->method('getCurrent')->willReturn(['xdebug']);
246  $this->phpInfo->expects($this->once())->method('getRequiredMinimumXDebugNestedLevel')->willReturn(200);
247  $xdebugMessage = sprintf(
248  'Your current setting of xdebug.max_nesting_level=%d.
249  Magento 2 requires it to be set to %d or more.
250  Edit your config, restart web server, and try again.',
251  100,
252  200
253  );
254 
255  $rawPostMessage = sprintf(
256  'Your PHP Version is %s, but always_populate_raw_post_data = -1.
257  $HTTP_RAW_POST_DATA is deprecated from PHP 5.6 onwards and will be removed in PHP 7.0.
258  This will stop the installer from running.
259  Please open your php.ini file and set always_populate_raw_post_data to -1.
260  If you need more help please call your hosting provider.',
261  PHP_VERSION
262  );
263  $expected = [
265  'data' => [
266  'xdebug_max_nesting_level' => [
267  'message' => $xdebugMessage,
268  'error' => true,
269  ],
270  'missed_function_imagecreatefromjpeg' => [
271  'message' => 'You must have installed GD library with --with-jpeg-dir=DIR option.',
272  'helpUrl' => 'http://php.net/manual/en/image.installation.php',
273  'error' => false,
274  ],
275  ],
276  ];
277  if (!$this->isPhp7OrHhvm()) {
278  $this->setUpNoPrettyVersionParser();
279  $expected['data']['always_populate_raw_post_data'] = [
280  'message' => $rawPostMessage,
281  'helpUrl' => 'http://php.net/manual/en/ini.core.php#ini.always-populate-settings-data',
282  'error' => false
283  ];
284  }
285  $this->assertEquals($expected, $this->phpReadinessCheck->checkPhpSettings());
286  }
287 
289  {
290  $this->phpInfo->expects($this->once())->method('getCurrent')->willReturn([]);
291 
292  $rawPostMessage = sprintf(
293  'Your PHP Version is %s, but always_populate_raw_post_data = -1.
294  $HTTP_RAW_POST_DATA is deprecated from PHP 5.6 onwards and will be removed in PHP 7.0.
295  This will stop the installer from running.
296  Please open your php.ini file and set always_populate_raw_post_data to -1.
297  If you need more help please call your hosting provider.',
298  PHP_VERSION
299  );
300  $expected = [
302  'data' => []
303  ];
304  if (!$this->isPhp7OrHhvm()) {
305  $this->setUpNoPrettyVersionParser();
306  $expected['data'] = [
307  'always_populate_raw_post_data' => [
308  'message' => $rawPostMessage,
309  'helpUrl' => 'http://php.net/manual/en/ini.core.php#ini.always-populate-settings-data',
310  'error' => false
311  ]
312  ];
313  }
314 
315  $expected['data']['missed_function_imagecreatefromjpeg'] = [
316  'message' => 'You must have installed GD library with --with-jpeg-dir=DIR option.',
317  'helpUrl' => 'http://php.net/manual/en/image.installation.php',
318  'error' => false,
319  ];
320 
321  $this->assertEquals($expected, $this->phpReadinessCheck->checkPhpSettings());
322  }
323 
325  {
326 
327  $this->dataSize->expects($this->any())->method('convertSizeToBytes')->willReturnMap(
328  [
329  ['512M', 512],
330  ['756M', 756],
331  ['2G', 2048],
332 
333  ]
334  );
335 
336  $rawPostMessage =
337  'Your current PHP memory limit is 512M.
338  Magento 2 requires it to be set to 756M or more.
339  As a user with root privileges, edit your php.ini file to increase memory_limit.
340  (The command php --ini tells you where it is located.)
341  After that, restart your web server and try again.';
342 
343  $expected['memory_limit'] = [
344  'message' => $rawPostMessage,
345  'error' => true,
346  'warning' => false,
347  ];
348 
349  $this->assertEquals($expected, $this->phpReadinessCheck->checkMemoryLimit());
350  }
351 
353  {
354  $this->composerInfo->expects($this->once())
355  ->method('getRequiredExtensions')
356  ->willThrowException(new \Exception());
357  $expected = [
359  'data' => [
360  'error' => 'phpExtensionError',
361  'message' => 'Cannot determine required PHP extensions: '
362  ],
363  ];
364  $this->assertEquals($expected, $this->phpReadinessCheck->checkPhpExtensions());
365  }
366 
367  public function testCheckPhpExtensions()
368  {
369  $this->composerInfo->expects($this->once())
370  ->method('getRequiredExtensions')
371  ->willReturn(['a', 'b', 'c']);
372  $this->phpInfo->expects($this->once())
373  ->method('getCurrent')
374  ->willReturn(['a', 'b', 'c', 'd']);
375  $expected = [
377  'data' => [
378  'required' => ['a', 'b', 'c'],
379  'missing' => [],
380  ]
381  ];
382  $this->assertEquals($expected, $this->phpReadinessCheck->checkPhpExtensions());
383  }
384 
386  {
387  $this->composerInfo->expects($this->once())
388  ->method('getRequiredExtensions')
389  ->willReturn(['a', 'b', 'c']);
390  $this->phpInfo->expects($this->once())
391  ->method('getCurrent')
392  ->willReturn(['a', 'b']);
393  $expected = [
395  'data' => [
396  'required' => ['a', 'b', 'c'],
397  'missing' => ['c'],
398  ]
399  ];
400  $this->assertEquals($expected, $this->phpReadinessCheck->checkPhpExtensions());
401  }
402 
406  protected function isPhp7OrHhvm()
407  {
408  return version_compare(PHP_VERSION, '7.0.0-beta') >= 0 || defined('HHVM_VERSION');
409  }
410 }
411 
412 namespace Magento\Setup\Model;
413 
418 function ini_get($param)
419 {
420  if ($param === 'xdebug.max_nesting_level') {
421  return 100;
422  } elseif ($param === 'always_populate_raw_post_data') {
423  return -1;
424  } elseif ($param === 'memory_limit') {
425  return '512M';
426  }
427 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
return false
Definition: gallery.phtml:36