PhpExecutableFinderTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Process\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Process\PhpExecutableFinder;
  13. /**
  14. * @author Robert Schönthal <seroscho@googlemail.com>
  15. */
  16. class PhpExecutableFinderTest extends TestCase
  17. {
  18. /**
  19. * tests find() with the env var PHP_PATH.
  20. */
  21. public function testFindWithPhpPath()
  22. {
  23. if (\defined('PHP_BINARY')) {
  24. $this->markTestSkipped('The PHP binary is easily available as of PHP 5.4');
  25. }
  26. $f = new PhpExecutableFinder();
  27. $current = $f->find();
  28. //not executable PHP_PATH
  29. putenv('PHP_PATH=/not/executable/php');
  30. $this->assertFalse($f->find(), '::find() returns false for not executable PHP');
  31. $this->assertFalse($f->find(false), '::find() returns false for not executable PHP');
  32. //executable PHP_PATH
  33. putenv('PHP_PATH='.$current);
  34. $this->assertEquals($f->find(), $current, '::find() returns the executable PHP');
  35. $this->assertEquals($f->find(false), $current, '::find() returns the executable PHP');
  36. }
  37. /**
  38. * tests find() with the constant PHP_BINARY.
  39. *
  40. * @requires PHP 5.4
  41. */
  42. public function testFind()
  43. {
  44. if (\defined('HHVM_VERSION')) {
  45. $this->markTestSkipped('Should not be executed in HHVM context.');
  46. }
  47. $f = new PhpExecutableFinder();
  48. $current = PHP_BINARY;
  49. $args = 'phpdbg' === \PHP_SAPI ? ' -qrr' : '';
  50. $this->assertEquals($current.$args, $f->find(), '::find() returns the executable PHP');
  51. $this->assertEquals($current, $f->find(false), '::find() returns the executable PHP');
  52. }
  53. /**
  54. * tests find() with the env var / constant PHP_BINARY with HHVM.
  55. */
  56. public function testFindWithHHVM()
  57. {
  58. if (!\defined('HHVM_VERSION')) {
  59. $this->markTestSkipped('Should be executed in HHVM context.');
  60. }
  61. $f = new PhpExecutableFinder();
  62. $current = getenv('PHP_BINARY') ?: PHP_BINARY;
  63. $this->assertEquals($current.' --php', $f->find(), '::find() returns the executable PHP');
  64. $this->assertEquals($current, $f->find(false), '::find() returns the executable PHP');
  65. }
  66. /**
  67. * tests find() with the env var PHP_PATH.
  68. */
  69. public function testFindArguments()
  70. {
  71. $f = new PhpExecutableFinder();
  72. if (\defined('HHVM_VERSION')) {
  73. $this->assertEquals($f->findArguments(), array('--php'), '::findArguments() returns HHVM arguments');
  74. } elseif ('phpdbg' === \PHP_SAPI) {
  75. $this->assertEquals($f->findArguments(), array('-qrr'), '::findArguments() returns phpdbg arguments');
  76. } else {
  77. $this->assertEquals($f->findArguments(), array(), '::findArguments() returns no arguments');
  78. }
  79. }
  80. /**
  81. * tests find() with default executable.
  82. */
  83. public function testFindWithSuffix()
  84. {
  85. if (\defined('PHP_BINARY')) {
  86. $this->markTestSkipped('The PHP binary is easily available as of PHP 5.4');
  87. }
  88. putenv('PHP_PATH=');
  89. putenv('PHP_PEAR_PHP_BIN=');
  90. $f = new PhpExecutableFinder();
  91. $current = $f->find();
  92. //TODO maybe php executable is custom or even Windows
  93. if ('\\' === \DIRECTORY_SEPARATOR) {
  94. $this->assertTrue(is_executable($current));
  95. $this->assertRegExp('/\\\\php\.(exe|bat|cmd|com)$/i', $current, '::find() returns the executable PHP with suffixes');
  96. }
  97. }
  98. }