Verified Commit 4007ca7b authored by Elias Häußler's avatar Elias Häußler 🐛
Browse files

[FEATURE] Introduce API zu register and unregister custom services

parent 79e800b2
<?php
declare(strict_types=1);
namespace EliasHaeussler\ComposerUpdateReporter\Exception;
/*
* This file is part of the Composer package "eliashaeussler/composer-update-reporter".
*
* Copyright (C) 2021 Elias Häußler <elias@haeussler.dev>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use EliasHaeussler\ComposerUpdateReporter\Service\ServiceInterface;
/**
* InvalidServiceException.
*
* @author Elias Häußler <elias@haeussler.dev>
* @license GPL-3.0-or-later
*/
class InvalidServiceException extends \Exception
{
public static function create(string $className): self
{
return new self(
sprintf('Service "%s" does not implement "%s".', $className, ServiceInterface::class),
1600814017
);
}
}
...@@ -30,6 +30,7 @@ use EliasHaeussler\ComposerUpdateCheck\IO\Style; ...@@ -30,6 +30,7 @@ use EliasHaeussler\ComposerUpdateCheck\IO\Style;
use EliasHaeussler\ComposerUpdateCheck\IO\Verbosity; use EliasHaeussler\ComposerUpdateCheck\IO\Verbosity;
use EliasHaeussler\ComposerUpdateCheck\Options; use EliasHaeussler\ComposerUpdateCheck\Options;
use EliasHaeussler\ComposerUpdateCheck\Package\UpdateCheckResult; use EliasHaeussler\ComposerUpdateCheck\Package\UpdateCheckResult;
use EliasHaeussler\ComposerUpdateReporter\Exception\InvalidServiceException;
use EliasHaeussler\ComposerUpdateReporter\Service\Email; use EliasHaeussler\ComposerUpdateReporter\Service\Email;
use EliasHaeussler\ComposerUpdateReporter\Service\GitLab; use EliasHaeussler\ComposerUpdateReporter\Service\GitLab;
use EliasHaeussler\ComposerUpdateReporter\Service\Mattermost; use EliasHaeussler\ComposerUpdateReporter\Service\Mattermost;
...@@ -95,10 +96,6 @@ class Reporter ...@@ -95,10 +96,6 @@ class Reporter
$services = []; $services = [];
/** @var ServiceInterface $registeredService */ /** @var ServiceInterface $registeredService */
foreach ($this->registeredServices as $registeredService) { foreach ($this->registeredServices as $registeredService) {
if (!in_array(ServiceInterface::class, class_implements($registeredService), true)) {
/* @phpstan-ignore-next-line */
throw new \InvalidArgumentException(sprintf('Service "%s" must implement "%s".', $registeredService, ServiceInterface::class), 1600814017);
}
if ($registeredService::isEnabled($this->configuration)) { if ($registeredService::isEnabled($this->configuration)) {
$service = $registeredService::fromConfiguration($this->configuration); $service = $registeredService::fromConfiguration($this->configuration);
$service->setBehavior($this->behavior); $service->setBehavior($this->behavior);
...@@ -121,11 +118,26 @@ class Reporter ...@@ -121,11 +118,26 @@ class Reporter
} }
/** /**
* @param string[] $registeredServices * @throws InvalidServiceException
*/ */
public function setRegisteredServices(array $registeredServices): self public function registerService(string $service): self
{
if (!in_array(ServiceInterface::class, class_implements($service), true)) {
throw InvalidServiceException::create($service);
}
if (!in_array($service, $this->registeredServices, true)) {
$this->registeredServices[] = $service;
}
return $this;
}
public function unregisterService(string $service): self
{ {
$this->registeredServices = $registeredServices; if (($key = array_search($service, $this->registeredServices, true)) !== false) {
unset($this->registeredServices[$key]);
}
return $this; return $this;
} }
......
...@@ -30,8 +30,15 @@ use EliasHaeussler\ComposerUpdateCheck\IO\OutputBehavior; ...@@ -30,8 +30,15 @@ use EliasHaeussler\ComposerUpdateCheck\IO\OutputBehavior;
use EliasHaeussler\ComposerUpdateCheck\IO\Style; use EliasHaeussler\ComposerUpdateCheck\IO\Style;
use EliasHaeussler\ComposerUpdateCheck\IO\Verbosity; use EliasHaeussler\ComposerUpdateCheck\IO\Verbosity;
use EliasHaeussler\ComposerUpdateCheck\Options; use EliasHaeussler\ComposerUpdateCheck\Options;
use EliasHaeussler\ComposerUpdateCheck\Package\OutdatedPackage;
use EliasHaeussler\ComposerUpdateCheck\Package\UpdateCheckResult; use EliasHaeussler\ComposerUpdateCheck\Package\UpdateCheckResult;
use EliasHaeussler\ComposerUpdateReporter\Exception\InvalidServiceException;
use EliasHaeussler\ComposerUpdateReporter\Reporter; use EliasHaeussler\ComposerUpdateReporter\Reporter;
use EliasHaeussler\ComposerUpdateReporter\Service\Email;
use EliasHaeussler\ComposerUpdateReporter\Service\GitLab;
use EliasHaeussler\ComposerUpdateReporter\Service\Mattermost;
use EliasHaeussler\ComposerUpdateReporter\Service\Slack;
use EliasHaeussler\ComposerUpdateReporter\Service\Teams;
use EliasHaeussler\ComposerUpdateReporter\Tests\Unit\Fixtures\Service\DummyService; use EliasHaeussler\ComposerUpdateReporter\Tests\Unit\Fixtures\Service\DummyService;
/** /**
...@@ -54,6 +61,9 @@ class ReporterTest extends AbstractTestCase ...@@ -54,6 +61,9 @@ class ReporterTest extends AbstractTestCase
*/ */
protected $composer; protected $composer;
/**
* @throws InvalidServiceException
*/
protected function setUp(): void protected function setUp(): void
{ {
$this->goToTestDirectory(); $this->goToTestDirectory();
...@@ -61,20 +71,13 @@ class ReporterTest extends AbstractTestCase ...@@ -61,20 +71,13 @@ class ReporterTest extends AbstractTestCase
DummyService::$enabled = true; DummyService::$enabled = true;
$this->composer = Factory::create(new NullIO()); $this->composer = Factory::create(new NullIO());
$this->subject = new Reporter($this->composer); $this->subject = (new Reporter($this->composer))
} ->unregisterService(Email::class)
->unregisterService(GitLab::class)
/** ->unregisterService(Mattermost::class)
* @test ->unregisterService(Slack::class)
*/ ->unregisterService(Teams::class)
public function reportThrowsExceptionIfInvalidServiceIsRegistered(): void ->registerService(DummyService::class);
{
$this->subject->setRegisteredServices([\Exception::class]);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1600814017);
$this->subject->report(new UpdateCheckResult([]));
} }
/** /**
...@@ -84,7 +87,6 @@ class ReporterTest extends AbstractTestCase ...@@ -84,7 +87,6 @@ class ReporterTest extends AbstractTestCase
{ {
DummyService::$enabled = false; DummyService::$enabled = false;
$this->subject->setRegisteredServices([DummyService::class]);
$this->subject->report(new UpdateCheckResult([])); $this->subject->report(new UpdateCheckResult([]));
static::assertFalse(DummyService::$reportWasExecuted); static::assertFalse(DummyService::$reportWasExecuted);
...@@ -95,7 +97,6 @@ class ReporterTest extends AbstractTestCase ...@@ -95,7 +97,6 @@ class ReporterTest extends AbstractTestCase
*/ */
public function reportIncludesEnabledServicesForReporting(): void public function reportIncludesEnabledServicesForReporting(): void
{ {
$this->subject->setRegisteredServices([DummyService::class]);
$this->subject->report(new UpdateCheckResult([])); $this->subject->report(new UpdateCheckResult([]));
static::assertTrue(DummyService::$reportWasExecuted); static::assertTrue(DummyService::$reportWasExecuted);
...@@ -109,7 +110,6 @@ class ReporterTest extends AbstractTestCase ...@@ -109,7 +110,6 @@ class ReporterTest extends AbstractTestCase
$behavior = new OutputBehavior(new Style(), new Verbosity(), new NullIO()); $behavior = new OutputBehavior(new Style(), new Verbosity(), new NullIO());
$this->subject->setBehavior($behavior); $this->subject->setBehavior($behavior);
$this->subject->setRegisteredServices([DummyService::class]);
$this->subject->report(new UpdateCheckResult([])); $this->subject->report(new UpdateCheckResult([]));
static::assertSame($behavior, DummyService::$customBehavior); static::assertSame($behavior, DummyService::$customBehavior);
...@@ -125,12 +125,36 @@ class ReporterTest extends AbstractTestCase ...@@ -125,12 +125,36 @@ class ReporterTest extends AbstractTestCase
$options = new Options(); $options = new Options();
$this->subject->setOptions($options); $this->subject->setOptions($options);
$this->subject->setRegisteredServices([DummyService::class]);
$this->subject->report(new UpdateCheckResult([])); $this->subject->report(new UpdateCheckResult([]));
static::assertSame($options, DummyService::$customOptions); static::assertSame($options, DummyService::$customOptions);
} }
/**
* @test
*/
public function registerServiceThrowsExceptionIfInvalidServiceIsGiven(): void
{
$this->expectException(InvalidServiceException::class);
$this->expectExceptionCode(1600814017);
$this->subject->registerService(\Exception::class);
}
/**
* @test
*/
public function unregisterServiceRemovesServiceFromListOfRegisteredServices(): void
{
$this->subject->unregisterService(DummyService::class);
$this->subject->report(new UpdateCheckResult([
new OutdatedPackage('foo/baz', '1.0.0', '1.0.5'),
]));
static::assertFalse(DummyService::$reportWasExecuted);
}
protected function tearDown(): void protected function tearDown(): void
{ {
$this->goBackToInitialDirectory(); $this->goBackToInitialDirectory();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment