Skip to content
Snippets Groups Projects
Commit b7ff1c56 authored by Adam G-H's avatar Adam G-H
Browse files

Issue #3230049 by phenaproxima, tedbow: Add validation subscriber to check...

Issue #3230049 by phenaproxima, tedbow: Add validation subscriber to check that Composer can be found
parent 5e32805d
No related branches found
No related tags found
No related merge requests found
...@@ -76,3 +76,8 @@ services: ...@@ -76,3 +76,8 @@ services:
arguments: ['%app.root%', '%site.path%', '@file_system', '@stream_wrapper_manager'] arguments: ['%app.root%', '%site.path%', '@file_system', '@stream_wrapper_manager']
tags: tags:
- { name: event_subscriber } - { name: event_subscriber }
automatic_updates.composer_executable_validator:
class: Drupal\automatic_updates\Validation\ComposerExecutableValidator
arguments: ['@automatic_updates.exec_finder']
tags:
- { name: event_subscriber }
<?php
namespace Drupal\automatic_updates\Validation;
use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\automatic_updates\Event\UpdateEvent;
use PhpTuf\ComposerStager\Exception\IOException;
use PhpTuf\ComposerStager\Infrastructure\Process\ExecutableFinderInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Validates that the Composer executable can be found.
*/
class ComposerExecutableValidator implements EventSubscriberInterface {
/**
* The executable finder service.
*
* @var \PhpTuf\ComposerStager\Infrastructure\Process\ExecutableFinderInterface
*/
protected $executableFinder;
/**
* Constructs a ComposerExecutableValidator object.
*
* @param \PhpTuf\ComposerStager\Infrastructure\Process\ExecutableFinderInterface $executable_finder
* The executable finder service.
*/
public function __construct(ExecutableFinderInterface $executable_finder) {
$this->executableFinder = $executable_finder;
}
/**
* Validates that the Composer executable can be found.
*
* @param \Drupal\automatic_updates\Event\UpdateEvent $event
* The event object.
*/
public function checkForComposerExecutable(UpdateEvent $event): void {
try {
$this->executableFinder->find('composer');
}
catch (IOException $e) {
$error = ValidationResult::createError([
$e->getMessage(),
]);
$event->addValidationResult($error);
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
AutomaticUpdatesEvents::READINESS_CHECK => 'checkForComposerExecutable',
];
}
}
<?php
namespace Drupal\Tests\automatic_updates\Kernel\ReadinessValidation;
use Drupal\automatic_updates\Validation\ValidationResult;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\automatic_updates\Traits\ValidationTestTrait;
use PhpTuf\ComposerStager\Exception\IOException;
use PhpTuf\ComposerStager\Infrastructure\Process\ExecutableFinderInterface;
/**
* @covers \Drupal\automatic_updates\Validation\ComposerExecutableValidator
*
* @group automatic_updates
*/
class ComposerExecutableValidatorTest extends KernelTestBase {
use ValidationTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['automatic_updates'];
/**
* Tests that an error is raised if the Composer executable isn't found.
*/
public function testErrorIfComposerNotFound(): void {
$exception = new IOException("This is your regularly scheduled error.");
// The executable finder throws an exception if it can't find the requested
// executable.
$exec_finder = $this->prophesize(ExecutableFinderInterface::class);
$exec_finder->find('composer')
->willThrow($exception)
->shouldBeCalled();
$this->container->set('automatic_updates.exec_finder', $exec_finder->reveal());
// The validator should translate that exception into an error.
$error = ValidationResult::createError([
$exception->getMessage(),
]);
$results = $this->container->get('automatic_updates.readiness_validation_manager')
->run()
->getResults();
$this->assertValidationResultsEqual([$error], $results);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment