Skip to content
Draft
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
6 changes: 1 addition & 5 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ jobs:
strategy:
matrix:
php:
- version: '8.2'
composer_flags: --prefer-lowest
- version: '8.3'
composer_flags: --prefer-lowest
- version: '8.4'
composer_flags: --prefer-lowest
- version: '8.2'
composer_flags: --prefer-stable
- version: '8.3'
composer_flags: --prefer-stable
- version: '8.4'
Expand Down Expand Up @@ -62,7 +58,7 @@ jobs:
run: ./bin/phpunit --coverage-text --coverage-clover clover.xml

- name: Upload Scrutinizer coverage
if: matrix.php.version == '8.2' && matrix.php.composer_flags == '--prefer-stable'
if: matrix.php.version == '8.3' && matrix.php.composer_flags == '--prefer-stable'
uses: sudo-bot/action-scrutinizer@latest
with:
cli-args: "--format=php-clover clover.xml --revision=${{ github.event.pull_request.head.sha || github.sha }}"
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A library to manage patch requests",
"keywords": ["rest", "patch", "api", "symfony", "bundle"],
"require": {
"php": "^8.2",
"php": "^8.3",
"ext-json": "*",
"symfony/property-access": "^6.0 || ^7.0",
"symfony/http-foundation": "^6.0 || ^7.0",
Expand Down
14 changes: 5 additions & 9 deletions src/PatchManager/Event/PatchManagerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@
use Cypress\PatchManager\Patchable;
use Symfony\Contracts\EventDispatcher\Event;

class PatchManagerEvent extends Event
abstract class PatchManagerEvent extends Event
{
private MatchedPatchOperation $matchedPatchOperation;

private Patchable $subject;

public function __construct(MatchedPatchOperation $matchedPatchOperation, Patchable $subject)
{
$this->matchedPatchOperation = $matchedPatchOperation;
$this->subject = $subject;
public function __construct(
private readonly MatchedPatchOperation $matchedPatchOperation,
private readonly Patchable $subject
) {
}

public function getMatchedPatchOperation(): MatchedPatchOperation
Expand Down
8 changes: 4 additions & 4 deletions src/PatchManager/Event/PatchManagerEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
final class PatchManagerEvents
{
/**
* this event gets fired before calling an handler
* this event gets fired before calling a handler
*/
public const PATCH_MANAGER_PRE = 'patch_manager.pre';
public const string PATCH_MANAGER_PRE = 'patch_manager.pre';

/**
* this event gets fired after calling an handler
* this event gets fired after calling a handler
*/
public const PATCH_MANAGER_POST = 'patch_manager.post';
public const string PATCH_MANAGER_POST = 'patch_manager.post';
}
27 changes: 27 additions & 0 deletions src/PatchManager/Event/PostHandlerInvocationEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Cypress\PatchManager\Event;

use Cypress\PatchManager\MatchedPatchOperation;
use Cypress\PatchManager\Patchable;

class PostHandlerInvocationEvent extends PatchManagerEvent
{
public function __construct(
MatchedPatchOperation $matchedPatchOperation,
Patchable $subject,
private readonly ?\Throwable $exception = null
) {
parent::__construct(
matchedPatchOperation: $matchedPatchOperation,
subject: $subject
);
}

public function getException(): ?\Throwable
{
return $this->exception;
}
}
9 changes: 9 additions & 0 deletions src/PatchManager/Event/PreHandlerInvocationEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Cypress\PatchManager\Event;

class PreHandlerInvocationEvent extends PatchManagerEvent
{
}
30 changes: 24 additions & 6 deletions src/PatchManager/PatchManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Cypress\PatchManager\Event\PatchManagerEvent;
use Cypress\PatchManager\Event\PatchManagerEvents;
use Cypress\PatchManager\Event\PostHandlerInvocationEvent;
use Cypress\PatchManager\Event\PreHandlerInvocationEvent;
use Cypress\PatchManager\Exception\HandlerNotFoundException;
use PhpCollection\Sequence;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -36,25 +38,41 @@ public function setEventDispatcherInterface(EventDispatcherInterface $eventDispa
}

/**
* @param array<Patchable>|Patchable|\Traversable $subject a Patchable instance or a collection of instances
* @param iterable|Patchable $subject a Patchable instance or a collection of instances
* @throws Exception\InvalidJsonRequestContent
* @throws Exception\MissingOperationNameRequest
* @throws Exception\MissingOperationRequest
* @throws HandlerNotFoundException
*/
public function handle($subject): void
public function handle(Patchable|iterable $subject): void
{
$matchedOperations = $this->getMatchedOperations($subject);
$this->handleSubject($subject, $matchedOperations);
}

protected function doHandle(MatchedPatchOperation $matchedPatchOperation, Patchable $subject): void
{
$event = new PatchManagerEvent($matchedPatchOperation, $subject);
$this->dispatchEvents($event, $matchedPatchOperation->getOpName(), PatchManagerEvents::PATCH_MANAGER_PRE);
$this->dispatchEvents(
event: new PreHandlerInvocationEvent($matchedPatchOperation, $subject),
opName: $matchedPatchOperation->getOpName(),
type: PatchManagerEvents::PATCH_MANAGER_PRE
);

$exception = null;

$matchedPatchOperation->process($subject);
$this->dispatchEvents($event, $matchedPatchOperation->getOpName(), PatchManagerEvents::PATCH_MANAGER_POST);
try {
$matchedPatchOperation->process($subject);
} catch (\Throwable $e) {
$exception = $e;

throw $e;
} finally {
$this->dispatchEvents(
event: new PostHandlerInvocationEvent($matchedPatchOperation, $subject, $exception),
opName: $matchedPatchOperation->getOpName(),
type: PatchManagerEvents::PATCH_MANAGER_POST
);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

declare(strict_types=1);

namespace Cypress\PatchManager\Tests\Bundle\DependencyInjection;

use Cypress\PatchManager\Bundle\DependencyInjection\PatchManagerCompilerPass;
use Cypress\PatchManager\Tests\PatchManagerTestCase;
use PHPUnit\Framework\Attributes\Test;
use Prophecy\Prophecy\ObjectProphecy;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand All @@ -24,7 +27,8 @@ public function setUp(): void
$this->compilerPass = new PatchManagerCompilerPass();
}

public function testProcessWithoutDefinition(): void
#[Test]
public function processWithoutDefinition(): void
{
try {
$this->cb->hasDefinition("patch_manager.operation_matcher")->willReturn(false);
Expand All @@ -36,7 +40,8 @@ public function testProcessWithoutDefinition(): void
$this->assertTrue(true);
}

public function testProcess(): void
#[Test]
public function shouldProcessSucceed(): void
{
$this->cb->findTaggedServiceIds()->willReturn(['test.service' => 'test', 'test.service2' => 'test2']);
$definition = $this->prophesize(Definition::class);
Expand Down
6 changes: 5 additions & 1 deletion tests/PatchManager/Bundle/PatchManagerBundleTest.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<?php

declare(strict_types=1);

namespace Cypress\PatchManager\Tests\Bundle;

use Cypress\PatchManager\Bundle\DependencyInjection\PatchManagerCompilerPass;
use Cypress\PatchManager\Bundle\PatchManagerBundle;
use Cypress\PatchManager\PatchOperationHandler;
use Cypress\PatchManager\Tests\PatchManagerTestCase;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class PatchManagerBundleTest extends PatchManagerTestCase
{
public function testBuild(): void
#[Test]
public function buildShouldSucceed(): void
{
$cb = $this->prophesize(ContainerBuilder::class);
$cb->addCompilerPass(new PatchManagerCompilerPass())->shouldBeCalled()->willReturn($cb->reveal());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
<?php

declare(strict_types=1);

namespace Cypress\PatchManager\Tests\Bundle\RequestAdapter;

use Cypress\PatchManager\Bundle\RequestAdapter\RequestStackAdapter;
use Cypress\PatchManager\Request\Operations;
use Cypress\PatchManager\Tests\PatchManagerTestCase;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class RequestStackAdapterTest extends PatchManagerTestCase
{
public function testCall(): void
#[Test]
public function callShouldBeResolved(): void
{
$currentRequest = $this->prophesize(Request::class);
$currentRequest->getContent()->willReturn('{"op":"data"}');
Expand Down
2 changes: 2 additions & 0 deletions tests/PatchManager/FakeObjects/SubjectA.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Cypress\PatchManager\Tests\FakeObjects;

use Cypress\PatchManager\Patchable as IPatchable;
Expand Down
2 changes: 2 additions & 0 deletions tests/PatchManager/FakeObjects/SubjectB.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Cypress\PatchManager\Tests\FakeObjects;

use Cypress\PatchManager\Patchable as IPatchable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Cypress\PatchManager\Tests\Handler\FakeObjects;

use Cypress\PatchManager\Patchable;
Expand Down
2 changes: 2 additions & 0 deletions tests/PatchManager/Handler/FakeObjects/DataSubject.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Cypress\PatchManager\Tests\Handler\FakeObjects;

use Cypress\PatchManager\Patchable;
Expand Down
2 changes: 2 additions & 0 deletions tests/PatchManager/Handler/FakeObjects/FiniteSubject.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Cypress\PatchManager\Tests\Handler\FakeObjects;

use Cypress\PatchManager\Patchable;
Expand Down
18 changes: 13 additions & 5 deletions tests/PatchManager/Handler/FiniteHandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Cypress\PatchManager\Tests\Handler;

use Cypress\PatchManager\Handler\FiniteHandler;
Expand All @@ -11,6 +13,7 @@
use Finite\State\StateInterface;
use Finite\StateMachine\StateMachine;
use Mockery as m;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Component\OptionsResolver\OptionsResolver;

class FiniteHandlerTest extends PatchManagerTestCase
Expand All @@ -35,12 +38,14 @@ public function setUp(): void
$this->handler = new FiniteHandler($finiteFactory);
}

public function testGetName(): void
#[Test]
public function getNameShouldReturnTheHandlerIdentifier(): void
{
$this->assertEquals('sm', $this->handler->getName());
}

public function testHandleOk(): void
#[Test]
public function handleShouldSucceed(): void
{
$patchable = new FiniteSubject();
$this->stateMachine->setObject($patchable);
Expand All @@ -59,7 +64,8 @@ public function testHandleOk(): void
$this->assertEquals('s2', $patchable->getFiniteState());
}

public function testHandleWithException(): void
#[Test]
public function handleShouldReturnAnException(): void
{
$this->expectException(StateException::class);
$patchable = new FiniteSubject();
Expand All @@ -78,7 +84,8 @@ public function testHandleWithException(): void
);
}

public function testHandleWrongWithCheck(): void
#[Test]
public function handleWithWrongCheck(): void
{
$patchable = new FiniteSubject();
$this->stateMachine->setObject($patchable);
Expand All @@ -97,7 +104,8 @@ public function testHandleWrongWithCheck(): void
$this->assertEquals('s1', $patchable->getFiniteState());
}

public function testConfigureOptions(): void
#[Test]
public function shouldConfigureOptions(): void
{
$or = new OptionsResolver();
$this->handler->configureOptions($or);
Expand Down
9 changes: 7 additions & 2 deletions tests/PatchManager/MatchedPatchOperationTest.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
<?php

declare(strict_types=1);

namespace Cypress\PatchManager\Tests;

use Cypress\PatchManager\MatchedPatchOperation;
use Cypress\PatchManager\Tests\FakeObjects\SubjectA;
use PHPUnit\Framework\Attributes\Test;
use Prophecy\Argument;

class MatchedPatchOperationTest extends PatchManagerTestCase
{
public function testMatchFor(): void
#[Test]
public function matchFor(): void
{
$mpo = MatchedPatchOperation::create([], $this->mockHandler('data')->reveal());
$this->assertTrue($mpo->matchFor('data'));
$this->assertFalse($mpo->matchFor('method'));
}

public function testProcess(): void
#[Test]
public function process(): void
{
$handler = $this->mockHandler('data');
$handler->handle(Argument::any(), Argument::any())->shouldBeCalled();
Expand Down
Loading