ExecutableFinderTest.php

Same filename and directory in other branches
  1. 11.x core/modules/package_manager/tests/src/Unit/ExecutableFinderTest.php

Namespace

Drupal\Tests\package_manager\Unit

File

core/modules/package_manager/tests/src/Unit/ExecutableFinderTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\package_manager\Unit;

use ColinODell\PsrTestLogger\TestLogger;
use Drupal\Component\Serialization\Json;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Site\Settings;
use Drupal\package_manager\ExecutableFinder;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;
use PhpTuf\ComposerStager\API\Finder\Service\ExecutableFinderInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\TestWith;

/**
 * Tests Package Manager's executable finder service.
 *
 * @internal
 */
class ExecutableFinderTest extends UnitTestCase {
  
  /**
   * Tests that the executable finder tries to use a local copy of Composer.
   */
  public function testComposerInstalledInProject(bool $chmod_result) : void {
    vfsStream::setup('root', NULL, [
      'composer-path' => [
        'bin' => [
          'composer' => 'A fake Composer executable.',
        ],
        'composer.json' => Json::encode([
          'bin' => [
            'bin/composer',
          ],
        ]),
      ],
    ]);
    $composer_bin = 'vfs://root/composer-path/bin/composer';
    $this->assertTrue(chmod($composer_bin, 0755));
    $decorated = $this->prophesize(ExecutableFinderInterface::class);
    $decorated->find('composer')
      ->willReturn('the real Composer');
    // The Composer binary is executable and should be made read-only.
    $file_system = $this->prophesize(FileSystemInterface::class);
    $file_system->chmod($composer_bin, 0644)
      ->willReturn($chmod_result)
      ->shouldBeCalled();
    $finder = new ExecutableFinder($decorated->reveal(), $file_system->reveal(), $this->getConfigFactoryStub([
      'package_manager.settings' => [],
    ]));
    $logger = new TestLogger();
    $finder->setLogger($logger);
    $reflector = new \ReflectionProperty($finder, 'composerPackagePath');
    $reflector->setValue($finder, dirname($composer_bin, 2));
    $this->assertSame($composer_bin, $finder->find('composer'));
    // If the permissions change will fail, a warning should be logged.
    $predicate = function (array $record) use ($composer_bin) : bool {
      return $record['message'] === 'Composer was found at %path, but could not be made read-only.' && $record['context']['%path'] === $composer_bin;
    };
    $this->assertSame(!$chmod_result, $logger->hasRecordThatPasses($predicate));
    // If the executable disappears and Composer isn't locally installed, fall
    // back to a setting.
    unlink($composer_bin);
    $reflector->setValue($finder, FALSE);
    new Settings([
      'package_manager_composer_path' => 'Composer from settings',
    ]);
    $this->assertSame('Composer from settings', $finder->find('composer'));
    // If all else fails, the decorated executable finder should be called.
    new Settings([]);
    $this->assertSame('the real Composer', $finder->find('composer'));
  }

}

Classes

Title Deprecated Summary
ExecutableFinderTest Tests Package Manager's executable finder service.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.