function ExecutableFinderTest::testComposerInstalledInProject

Tests that the executable finder tries to use a local copy of Composer.

Attributes

#[TestWith([ TRUE, ])] #[TestWith([ FALSE, ])]

Parameters

bool $chmod_result: Whether the Composer binary will be successfully made read-only.

File

core/modules/package_manager/tests/src/Unit/ExecutableFinderTest.php, line 54

Class

ExecutableFinderTest
@covers \Drupal\package_manager\ExecutableFinder[[api-linebreak]] @group package_manager @internal

Namespace

Drupal\Tests\package_manager\Unit

Code

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(), $this->getConfigFactoryStub([
    'package_manager.settings' => [
      'executables' => [],
    ],
  ]), $file_system->reveal());
  $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, or Composer isn't locally installed, the
  // decorated executable finder should be called.
  unlink($composer_bin);
  $this->assertSame('the real Composer', $finder->find('composer'));
  $reflector->setValue($finder, FALSE);
  $this->assertSame('the real Composer', $finder->find('composer'));
}

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