Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Latest
------

### Changes
* [#63](https://github.com/cleverage/cache-process-bundle/issues/63) The bundle throws exceptions implementing `ProcessExceptionInterface` of the process bundle instead of raw SPL exceptions (as [cleverage/process-bundle#317](https://github.com/cleverage/process-bundle/issues/317)): new `Exception\UnexpectedValueException`, extending the SPL exceptions thrown before (same messages, the existing `catch` keep working). `MissingAdapterException` implements `ProcessExceptionInterface`. Add tests.
* [#61](https://github.com/cleverage/cache-process-bundle/issues/61) Composer: require PHP `^8.2` instead of `>=8.2` (PHP 9 is no longer accepted before being tested), remove the authorization of the `symfony/flex` plugin, which is not required (the CI installs it globally), as [cleverage/process-bundle#319](https://github.com/cleverage/process-bundle/issues/319).
* [#56](https://github.com/cleverage/cache-process-bundle/issues/56) Define the public API (Symfony BC promise, as [cleverage/process-bundle#279](https://github.com/cleverage/process-bundle/issues/279)): `GetTask`, `SetTask`, `AdapterRegistry` and `CleverAgeCacheProcessBundle` are marked `@final since 2.3`, they will be `final` in 3.0: extending them triggers a deprecation with the Symfony `DebugClassLoader`. `AbstractCacheTask`, `Adapter` (extended by the applications) and `MissingAdapterException` stay open. `CleverAgeCacheProcessExtension` and `RegisterAdaptersPass` are marked `@internal`.
* [#55](https://github.com/cleverage/cache-process-bundle/issues/55) `CleverAgeCacheProcessExtension::findServices()` loads the service definition files with a glob pattern instead of a `Finder` (same files, as [cleverage/process-bundle#271](https://github.com/cleverage/process-bundle/issues/271)), `symfony/finder` is no longer required. Add tests.
Expand Down
4 changes: 3 additions & 1 deletion src/Exception/MissingAdapterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

namespace CleverAge\CacheProcessBundle\Exception;

use CleverAge\ProcessBundle\Exception\ProcessExceptionInterface;

/**
* Exception thrown when trying to fetch a missing Cache Adapter.
*/
class MissingAdapterException extends \Exception
class MissingAdapterException extends \Exception implements ProcessExceptionInterface
{
}
23 changes: 23 additions & 0 deletions src/Exception/UnexpectedValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

/*
* This file is part of the CleverAge/CacheProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CleverAge\CacheProcessBundle\Exception;

use CleverAge\ProcessBundle\Exception\ProcessExceptionInterface;

/**
* Thrown by the bundle, can be caught with the ProcessExceptionInterface of the process bundle.
*/
class UnexpectedValueException extends \UnexpectedValueException implements ProcessExceptionInterface
{
}
7 changes: 4 additions & 3 deletions src/Registry/AdapterRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use CleverAge\CacheProcessBundle\Adapter\AdapterInterface;
use CleverAge\CacheProcessBundle\Exception\MissingAdapterException;
use CleverAge\CacheProcessBundle\Exception\UnexpectedValueException;
use Symfony\Contracts\Service\ServiceProviderInterface;

/**
Expand Down Expand Up @@ -70,7 +71,7 @@ public function addAdapter(AdapterInterface $adapter, ?string $serviceId = null)
$message .= " by service \"{$definedBy}\", cannot register service \"{$serviceId}\"";
}

throw new \UnexpectedValueException($message);
throw new UnexpectedValueException($message);
}
$this->adapters[$code] = $adapter;
$this->serviceIds[$code] = $serviceId;
Expand Down Expand Up @@ -104,10 +105,10 @@ public function getAdapter(string $code): AdapterInterface

$adapter = $this->locator->get($code);
if (!$adapter instanceof AdapterInterface) {
throw new \UnexpectedValueException(\sprintf('The service of the cache adapter "%s" must implement "%s".', $code, AdapterInterface::class));
throw new UnexpectedValueException(\sprintf('The service of the cache adapter "%s" must implement "%s".', $code, AdapterInterface::class));
}
if ($code !== $adapter->getCode()) {
throw new \UnexpectedValueException(\sprintf('The cache adapter "%s" is registered with the code "%s" but its getCode() method returns "%s": both must be the same.', $adapter::class, $code, $adapter->getCode()));
throw new UnexpectedValueException(\sprintf('The cache adapter "%s" is registered with the code "%s" but its getCode() method returns "%s": both must be the same.', $adapter::class, $code, $adapter->getCode()));
}

return $this->adapters[$code] = $adapter;
Expand Down
3 changes: 2 additions & 1 deletion src/Task/AbstractCacheTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CleverAge\CacheProcessBundle\Task;

use CleverAge\CacheProcessBundle\Exception\UnexpectedValueException;
use CleverAge\CacheProcessBundle\Registry\AdapterRegistry;
use CleverAge\ProcessBundle\Model\AbstractConfigurableTask;
use CleverAge\ProcessBundle\Model\ProcessState;
Expand Down Expand Up @@ -67,7 +68,7 @@ protected function getMergedOptions(ProcessState $state): array

$input = $state->getInput() ?: [];
if (!\is_array($input)) {
throw new \UnexpectedValueException(\sprintf('%s expects an array or null input, %s given', (new \ReflectionClass($this))->getShortName(), get_debug_type($input)));
throw new UnexpectedValueException(\sprintf('%s expects an array or null input, %s given', (new \ReflectionClass($this))->getShortName(), get_debug_type($input)));
}

$resolver = new OptionsResolver();
Expand Down
3 changes: 2 additions & 1 deletion src/Task/GetTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CleverAge\CacheProcessBundle\Task;

use CleverAge\CacheProcessBundle\Exception\UnexpectedValueException;
use CleverAge\ProcessBundle\Model\ProcessState;
use Symfony\Component\OptionsResolver\Exception\AccessException;
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
Expand Down Expand Up @@ -57,7 +58,7 @@ public function execute(ProcessState $state): void
return;
}
if (self::ON_MISS_FAIL === $mergedOptions['on_miss']) {
throw new \UnexpectedValueException("Cache item {$mergedOptions['key']} is missing from adapter {$mergedOptions['adapter']}");
throw new UnexpectedValueException("Cache item {$mergedOptions['key']} is missing from adapter {$mergedOptions['adapter']}");
}
}

Expand Down
57 changes: 57 additions & 0 deletions tests/Exception/SplExceptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

/*
* This file is part of the CleverAge/CacheProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CleverAge\CacheProcessBundle\Tests\Exception;

use CleverAge\CacheProcessBundle\Exception\MissingAdapterException;
use CleverAge\CacheProcessBundle\Exception\UnexpectedValueException;
use CleverAge\ProcessBundle\Exception\ProcessExceptionInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

/**
* The exceptions thrown by the bundle can be caught as ProcessExceptionInterface, and still as the SPL exceptions they
* were before.
*/
#[CoversClass(UnexpectedValueException::class)]
#[CoversClass(MissingAdapterException::class)]
class SplExceptionsTest extends TestCase
{
/**
* @return iterable<string, array{class-string<\Throwable>, class-string<\Throwable>}>
*/
public static function provideExceptions(): iterable
{
yield 'UnexpectedValueException' => [UnexpectedValueException::class, \UnexpectedValueException::class];
}

public function testExceptionsOfTheBundleImplementTheInterface(): void
{
self::assertInstanceOf(ProcessExceptionInterface::class, new MissingAdapterException('message'));
}

/**
* @param class-string<\Throwable> $class
* @param class-string<\Throwable> $splClass
*/
#[DataProvider('provideExceptions')]
public function testExtendsTheSplExceptionAndImplementsTheInterface(string $class, string $splClass): void
{
$exception = new $class('message', 12);

self::assertInstanceOf($splClass, $exception);
self::assertInstanceOf(ProcessExceptionInterface::class, $exception);
self::assertSame(['message', 12], [$exception->getMessage(), $exception->getCode()]);
}
}
Loading