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;
use EliasHaeussler\ComposerUpdateCheck\IO\Verbosity;
use EliasHaeussler\ComposerUpdateCheck\Options;
use EliasHaeussler\ComposerUpdateCheck\Package\UpdateCheckResult;
use EliasHaeussler\ComposerUpdateReporter\Exception\InvalidServiceException;
use EliasHaeussler\ComposerUpdateReporter\Service\Email;
use EliasHaeussler\ComposerUpdateReporter\Service\GitLab;
use EliasHaeussler\ComposerUpdateReporter\Service\Mattermost;
......@@ -95,10 +96,6 @@ class Reporter
$services = [];
/** @var ServiceInterface $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)) {
$service = $registeredService::fromConfiguration($this->configuration);
$service->setBehavior($this->behavior);
......@@ -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;
}
......
......@@ -30,8 +30,15 @@ use EliasHaeussler\ComposerUpdateCheck\IO\OutputBehavior;
use EliasHaeussler\ComposerUpdateCheck\IO\Style;
use EliasHaeussler\ComposerUpdateCheck\IO\Verbosity;
use EliasHaeussler\ComposerUpdateCheck\Options;
use EliasHaeussler\ComposerUpdateCheck\Package\OutdatedPackage;
use EliasHaeussler\ComposerUpdateCheck\Package\UpdateCheckResult;
use EliasHaeussler\ComposerUpdateReporter\Exception\InvalidServiceException;
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;
/**
......@@ -54,6 +61,9 @@ class ReporterTest extends AbstractTestCase
*/
protected $composer;
/**
* @throws InvalidServiceException
*/
protected function setUp(): void
{
$this->goToTestDirectory();
......@@ -61,20 +71,13 @@ class ReporterTest extends AbstractTestCase
DummyService::$enabled = true;
$this->composer = Factory::create(new NullIO());
$this->subject = new Reporter($this->composer);
}
/**
* @test
*/
public function reportThrowsExceptionIfInvalidServiceIsRegistered(): void
{
$this->subject->setRegisteredServices([\Exception::class]);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1600814017);
$this->subject->report(new UpdateCheckResult([]));
$this->subject = (new Reporter($this->composer))
->unregisterService(Email::class)
->unregisterService(GitLab::class)
->unregisterService(Mattermost::class)
->unregisterService(Slack::class)
->unregisterService(Teams::class)
->registerService(DummyService::class);
}
/**
......@@ -84,7 +87,6 @@ class ReporterTest extends AbstractTestCase
{
DummyService::$enabled = false;
$this->subject->setRegisteredServices([DummyService::class]);
$this->subject->report(new UpdateCheckResult([]));
static::assertFalse(DummyService::$reportWasExecuted);
......@@ -95,7 +97,6 @@ class ReporterTest extends AbstractTestCase
*/
public function reportIncludesEnabledServicesForReporting(): void
{
$this->subject->setRegisteredServices([DummyService::class]);
$this->subject->report(new UpdateCheckResult([]));
static::assertTrue(DummyService::$reportWasExecuted);
......@@ -109,7 +110,6 @@ class ReporterTest extends AbstractTestCase
$behavior = new OutputBehavior(new Style(), new Verbosity(), new NullIO());
$this->subject->setBehavior($behavior);
$this->subject->setRegisteredServices([DummyService::class]);
$this->subject->report(new UpdateCheckResult([]));
static::assertSame($behavior, DummyService::$customBehavior);
......@@ -125,12 +125,36 @@ class ReporterTest extends AbstractTestCase
$options = new Options();
$this->subject->setOptions($options);
$this->subject->setRegisteredServices([DummyService::class]);
$this->subject->report(new UpdateCheckResult([]));
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
{
$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