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
37 changes: 37 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Tests

on:
pull_request:
push:
branches:
- master

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/master' }}

jobs:

tests:
runs-on: ubuntu-24.04
strategy:
matrix:
php-versions: [ '8.2', '8.3', '8.4' ]
fail-fast: false
steps:
- uses: actions/checkout@v4

- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
coverage: none

- name: Composer install
run: composer install --no-scripts --no-interaction --prefer-dist -oa

- name: Run Phpunit
run: ./vendor/bin/phpunit

- name: Run PHPStan
run: ./vendor/bin/phpstan analyse src
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
composer.lock
composer.phar
vendor/
.phpunit.result.cache
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@
},
"require": {
"php": ">=8.2",
"doctrine/annotations": "~2.0",
"openclassrooms/cache": "^v1.1.0"
"doctrine/annotations": "^1.0|~2.0",
"psr/log": "^2.0|^3.0",
"psr/cache": "^2.0|^3.0"
},
"require-dev": {
"phpunit/phpunit": "^9.6.20"
"phpunit/phpunit": "^11.0",
"phpstan/phpstan": "^2.1",
"symfony/cache": "^5.4|^6.4|^7.0"
},
"extra": {
"branch-alias": {
Expand Down
34 changes: 13 additions & 21 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false"
bootstrap = "./tests/bootstrap.php">

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd"
bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite name="Use Case Test Suite">
<directory>./tests/</directory>
<testsuite name="unit">
<directory>tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>

<source>
<include>
<directory>src/</directory>
</include>
</source>

<coverage pathCoverage="true" />
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace OpenClassrooms\UseCase\Application\Services\Event\Exceptions;

use Exception;

/**
* @author Romain Kuzniak <[email protected]>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,30 @@

namespace OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Impl\Cache;

use OpenClassrooms\Cache\Cache\Cache;
use OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Impl\DTO\ProxyStrategyResponseDTO;
use OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Requestors\Cache\CacheProxyStrategyRequest;
use OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Requestors\PostExecuteProxyStrategy;
use OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Requestors\PreExecuteProxyStrategy;
use OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Requestors\ProxyStrategy;
use OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Requestors\ProxyStrategyRequest;
use OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Responders\ProxyStrategyResponse;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;

/**
* @author Romain Kuzniak <[email protected]>
*/
class CacheProxyStrategy implements PreExecuteProxyStrategy, PostExecuteProxyStrategy
{

/**
* @var mixed
*/
private $data;

/**
* @var Cache
* @var CacheItemPoolInterface
*/
private $cache;

/**
* @var bool
*/
private $postExecute = true;
private $postExecute;

/**
* @return string
Expand All @@ -46,28 +41,14 @@
public function preExecute(ProxyStrategyRequest $proxyStrategyRequest)
{
/** @var CacheProxyStrategyRequest $proxyStrategyRequest */
$this->data = $this->cache->fetchWithNamespace(
$item = $this->fetchWithNamespace(
$proxyStrategyRequest->getId(),
$proxyStrategyRequest->getNamespaceId()
);

if ($this->responseIsInCache()) {
$stopExecution = true;
$this->postExecute = false;
} else {
$stopExecution = false;
}
$response = new ProxyStrategyResponseDTO($this->data, $stopExecution);

return $response;
}
$this->postExecute = !$item->isHit();

/**
* @return bool
*/
private function responseIsInCache()
{
return $this->data;
return new ProxyStrategyResponseDTO($item->get(), $item->isHit());
}

/**
Expand All @@ -76,17 +57,51 @@
public function postExecute(ProxyStrategyRequest $proxyStrategyRequest)
{
/** @var CacheProxyStrategyRequest $proxyStrategyRequest */
$saved = $this->cache->saveWithNamespace(
$item = $this->saveWithNamespace(
$proxyStrategyRequest->getId(),
$proxyStrategyRequest->getData(),
$proxyStrategyRequest->getNamespaceId(),
$proxyStrategyRequest->getLifeTime()
);
$response = new ProxyStrategyResponseDTO($saved, false);

return $response;
return new ProxyStrategyResponseDTO($item->get(), false);
}

private function fetchWithNamespace(string $id, string $namespace = null): CacheItemInterface

Check failure on line 70 in src/OpenClassrooms/UseCase/Application/Services/Proxy/Strategies/Impl/Cache/CacheProxyStrategy.php

View workflow job for this annotation

GitHub Actions / tests (8.4)

Deprecated in PHP 8.4: Parameter #2 $namespace (string) is implicitly nullable via default value null.
{
if ($namespace !== null) {
$namespaceId = $this->cache->getItem($namespace);

$id = ((string) $namespaceId->get()) . $id;
}

return $this->cache->getItem($id);
}

private function saveWithNamespace(string $id, mixed $data, string $namespace = null, int $lifetime = null): CacheItemInterface

Check failure on line 81 in src/OpenClassrooms/UseCase/Application/Services/Proxy/Strategies/Impl/Cache/CacheProxyStrategy.php

View workflow job for this annotation

GitHub Actions / tests (8.4)

Deprecated in PHP 8.4: Parameter #3 $namespace (string) is implicitly nullable via default value null.

Check failure on line 81 in src/OpenClassrooms/UseCase/Application/Services/Proxy/Strategies/Impl/Cache/CacheProxyStrategy.php

View workflow job for this annotation

GitHub Actions / tests (8.4)

Deprecated in PHP 8.4: Parameter #4 $lifetime (int) is implicitly nullable via default value null.
{
if ($namespace !== null) {
$namespaceId = $this->cache->getItem($namespace);

if (!$namespaceId->isHit()) {
$namespaceId->set($namespace . '_' . random_int(0, 10000));
$namespaceId->expiresAfter(604800); // 7 days

$this->cache->save($namespaceId);
}

$id = ((string) $namespaceId->get()) . $id;
}

$item = $this->cache->getItem($id);
$item->set($data)->expiresAfter($lifetime);

$this->cache->save($item);

return $item;
}


/**
* @return boolean
*/
Expand All @@ -95,7 +110,7 @@
return $this->postExecute;
}

public function setCache(Cache $cache)
public function setCache(CacheItemPoolInterface $cache)
{
$this->cache = $cache;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class CacheProxyStrategyBagImpl extends ProxyStrategyBag

public function isPostExecute()
{
/** @var $this ->proxyStrategy */

return $this->proxyStrategy->isPostExecute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public function preExecute(ProxyStrategyRequest $proxyStrategyRequest)
*/
public function postExecute(ProxyStrategyRequest $proxyStrategyRequest)
{
return $this->transaction->commit();
$this->transaction->commit();

return new ProxyStrategyResponseDTO();
}

/**
Expand All @@ -54,8 +56,10 @@ public function postExecute(ProxyStrategyRequest $proxyStrategyRequest)
public function onException(ProxyStrategyRequest $proxyStrategyRequest)
{
if ($this->transaction->isTransactionActive()) {
return $this->transaction->rollBack();
$this->transaction->rollBack();
}

return new ProxyStrategyResponseDTO();
}

public function setTransaction(Transaction $transaction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
interface OnExceptionProxyStrategy extends ProxyStrategy
{
/**
* @return ProxyStrategyResponse
* @return ?bool
*/
public function onException(ProxyStrategyRequest $proxyStrategyRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace OpenClassrooms\UseCase\Application\Services\Proxy\UseCases;

use Doctrine\Common\Annotations\Reader;
use OpenClassrooms\Cache\Cache\Cache;
use OpenClassrooms\UseCase\Application\Services\Event\EventFactory;
use OpenClassrooms\UseCase\Application\Services\Event\EventSender;
use OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Impl\Cache\CacheProxyStrategy;
Expand All @@ -28,6 +27,7 @@
use OpenClassrooms\UseCase\Application\Services\Security\Security;
use OpenClassrooms\UseCase\Application\Services\Transaction\Transaction;
use OpenClassrooms\UseCase\BusinessRules\Requestors\UseCase;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -42,7 +42,7 @@
protected $useCaseProxy;

/**
* @var Cache
* @var CacheItemPoolInterface
*/
private $cache;

Expand Down Expand Up @@ -85,7 +85,7 @@
/**
* @return UseCaseProxyBuilder
*/
public function withCache(Cache $cache = null)
public function withCache(CacheItemPoolInterface $cache = null)

Check failure on line 88 in src/OpenClassrooms/UseCase/Application/Services/Proxy/UseCases/UseCaseProxyBuilder.php

View workflow job for this annotation

GitHub Actions / tests (8.4)

Deprecated in PHP 8.4: Parameter #1 $cache (Psr\Cache\CacheItemPoolInterface) is implicitly nullable via default value null.
{
$this->cache = $cache;

Expand All @@ -95,7 +95,7 @@
/**
* @return UseCaseProxyBuilder
*/
public function withEventSender(EventSender $event = null)

Check failure on line 98 in src/OpenClassrooms/UseCase/Application/Services/Proxy/UseCases/UseCaseProxyBuilder.php

View workflow job for this annotation

GitHub Actions / tests (8.4)

Deprecated in PHP 8.4: Parameter #1 $event (OpenClassrooms\UseCase\Application\Services\Event\EventSender) is implicitly nullable via default value null.
{
$this->event = $event;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function getTotalPages()
/**
* @inheritDoc
*/
public function getIterator()
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->items);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function setItemsPerPage($itemsPerPage)
$this->itemsPerPage = $itemsPerPage;
}

public function getIterator()
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->items);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace OpenClassrooms\UseCase\BusinessRules\Responders\Exceptions;

use Exception;

/**
* @author Romain Kuzniak <[email protected]>
* @deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function getItems();
*/
public function getItemsPerPage();

public function getIterator();
public function getIterator(): \Traversable;

/**
* @return int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace OpenClassrooms\Tests\UseCase\Application\Annotations\Event;

use Doctrine\Common\Annotations\AnnotationException;
use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;

/**
* @author Romain Kuzniak <[email protected]>
*/
class EventTest extends \PHPUnit_Framework_TestCase
class EventTest extends TestCase
{

/**
Expand All @@ -17,16 +19,18 @@ class EventTest extends \PHPUnit_Framework_TestCase

/**
* @test
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Method "invalid method" is not allowed. Allowed: pre, post and onException
*/
public function InvalidMethod_ThrowException()
{
$class = new \ReflectionClass(new EventClassDummy());

$this->expectException(AnnotationException::class);
$this->expectExceptionMessage('Method "invalid method" is not allowed. Allowed: pre, post and onException');

$this->reader->getMethodAnnotation($class->getMethod('invalidMethod'), 'event');
}

protected function setUp()
protected function setUp(): void
{
$this->reader = new AnnotationReader();
}
Expand Down
Loading
Loading