PhpExecutableFinderTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 constant PHP_BINARY.
  20. */
  21. public function testFind()
  22. {
  23. $f = new PhpExecutableFinder();
  24. $current = \PHP_BINARY;
  25. $args = 'phpdbg' === \PHP_SAPI ? ' -qrr' : '';
  26. $this->assertEquals($current.$args, $f->find(), '::find() returns the executable PHP');
  27. $this->assertEquals($current, $f->find(false), '::find() returns the executable PHP');
  28. }
  29. /**
  30. * tests find() with the env var PHP_PATH.
  31. */
  32. public function testFindArguments()
  33. {
  34. $f = new PhpExecutableFinder();
  35. if ('phpdbg' === \PHP_SAPI) {
  36. $this->assertEquals(['-qrr'], $f->findArguments(), '::findArguments() returns phpdbg arguments');
  37. } else {
  38. $this->assertEquals([], $f->findArguments(), '::findArguments() returns no arguments');
  39. }
  40. }
  41. public function testNotExitsBinaryFile()
  42. {
  43. $f = new PhpExecutableFinder();
  44. $originalPhpBinary = getenv('PHP_BINARY');
  45. try {
  46. putenv('PHP_BINARY=/usr/local/php/bin/php-invalid');
  47. $this->assertFalse($f->find(), '::find() returns false because of not exist file');
  48. $this->assertFalse($f->find(false), '::find(false) returns false because of not exist file');
  49. } finally {
  50. putenv('PHP_BINARY='.$originalPhpBinary);
  51. }
  52. }
  53. public function testFindWithExecutableDirectory()
  54. {
  55. if ('\\' === \DIRECTORY_SEPARATOR) {
  56. $this->markTestSkipped('Directories are not executable on Windows');
  57. }
  58. $originalPhpBinary = getenv('PHP_BINARY');
  59. try {
  60. $executableDirectoryPath = sys_get_temp_dir().'/PhpExecutableFinderTest_testFindWithExecutableDirectory';
  61. @mkdir($executableDirectoryPath);
  62. $this->assertTrue(is_executable($executableDirectoryPath));
  63. putenv('PHP_BINARY='.$executableDirectoryPath);
  64. $this->assertFalse((new PhpExecutableFinder())->find());
  65. } finally {
  66. putenv('PHP_BINARY='.$originalPhpBinary);
  67. }
  68. }
  69. }