Repository: symfony/symfony
File: src/Symfony/Component/Process/Tests/ExecutableFinderTest.php (PHP)

Code before:
        $result = $finder->find($name, null, [$fixturesDir]);

        $this->assertSamePath($fixturesDir.\DIRECTORY_SEPARATOR.$name.$suffix, $result);
    }

    #[RunInSeparateProcess]
    public function testFindWithOpenBaseDir()
    {
        if ('\\' === \DIRECTORY_SEPARATOR) {
            $this->markTestSkipped('Cannot run test on windows');
        }

        if (\ini_get('open_basedir')) {
            $this->markTestSkipped('Cannot test when open_basedir is set');
        }

        putenv('PATH='.\dirname(\PHP_BINARY));
        $initialOpenBaseDir = ini_set('open_basedir', \dirname(\PHP_BINARY).\PATH_SEPARATOR.'/');

        try {
            $finder = new ExecutableFinder();
            $result = $finder->find($this->getPhpBinaryName());

            $this->assertSamePath(\PHP_BINARY, $result);
        } finally {
            ini_set('open_basedir', $initialOpenBaseDir);
        }
    }

    #[RunInSeparateProcess]
    public function testFindBatchExecutableOnWindows()
    {
        if (\ini_get('open_basedir')) {
            $this->markTestSkipped('Cannot test when open_basedir is set');
        }
        if ('\\' !== \DIRECTORY_SEPARATOR) {
            $this->markTestSkipped('Can be only tested on windows');
        }

        $tempDir = realpath(sys_get_temp_dir());
        $target = str_replace('.tmp', '_tmp', tempnam($tempDir, 'example-windows-executable'));

        try {
            touch($target);
            touch($target.'.BAT');

            $this->assertFalse(is_executable($target));

            putenv('PATH='.$tempDir);

            $finder = new ExecutableFinder();

Review comment (bug):
This never used to work as expected as you cannot widen the open basedir restrictions after tightening it before. Later on when PHPUnit cleaned up the test environment this led to a PHP warning which in turn [lets the test fail](https://github.com/xabbuh/symfony/actions/runs/16723312255/job/47332548280#step:9:1232):

```
src/Symfony/Component/Process                                                19s
+ /home/runner/work/symfony/symfony/phpunit --exclude-group tty --exclude-group benchmark --exclude-group intl-data --exclude-group integration --exclude-group transient src/Symfony/Component/Process
PHPUnit 12.1.6 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.23
Configuration: /home/runner/work/symfony/symfony/phpunit.xml.dist

........WS.S...................................................  63 / 186 ( 33%)
...................SSS......................................... 126 / 186 ( 67%)
...........................................................S    186 / 186 (100%)

Time: 00:19.259, Memory: 16.00 MB

1 test triggered 1 PHP warning:

1) /home/runner/work/symfony/symfony/.phpunit/phpunit-12.1-0/src/Framework/TestCase.php:1290
ini_set(): open_basedir restriction in effect. File() is not within the allowed path(s): (/usr/bin:/)

Triggered by:

* Symfony\Component\Process\Tests\ExecutableFinderTest::testFindWithOpenBaseDir
  /home/runner/work/symfony/symfony/src/Symfony/Component/Process/Tests/ExecutableFinderTest.php:115
```

Code after:

        $result = $finder->find($name, null, [$fixturesDir]);

        $this->assertSamePath($fixturesDir.\DIRECTORY_SEPARATOR.$name.$suffix, $result);
    }

    #[RunInSeparateProcess]
    public function testFindWithOpenBaseDir()
    {
        if ('\\' === \DIRECTORY_SEPARATOR) {
            $this->markTestSkipped('Cannot run test on windows');
        }

        if (\ini_get('open_basedir')) {
            $this->markTestSkipped('Cannot test when open_basedir is set');
        }

        $process = new Process([\PHP_BINARY, '-d', 'open_basedir='.\dirname(\PHP_BINARY).\PATH_SEPARATOR.'/', __DIR__.'/Fixtures/open_basedir.php']);
        $process->run();
        $result = $process->getOutput();

        $this->assertSamePath(\PHP_BINARY, $result);
    }

    #[RunInSeparateProcess]
    public function testFindBatchExecutableOnWindows()
    {
        if (\ini_get('open_basedir')) {
            $this->markTestSkipped('Cannot test when open_basedir is set');
        }
        if ('\\' !== \DIRECTORY_SEPARATOR) {
            $this->markTestSkipped('Can be only tested on windows');
        }

        $tempDir = realpath(sys_get_temp_dir());
        $target = str_replace('.tmp', '_tmp', tempnam($tempDir, 'example-windows-executable'));

        try {
            touch($target);
            touch($target.'.BAT');

            $this->assertFalse(is_executable($target));

            putenv('PATH='.$tempDir);

            $finder = new ExecutableFinder();
            $result = $finder->find(basename($target), false);
        } finally {
            unlink($target);
            unlink($target.'.BAT');
        }