From 7661f96ed40d3139604bc51ea192d2246bdf4a71 Mon Sep 17 00:00:00 2001 From: Yohan Giarelli Date: Wed, 15 Jan 2025 22:57:44 +0100 Subject: [PATCH] ci: Added PHP CS Fixer --- .github/workflows/ci.yml | 18 ++++++ .gitignore | 1 + .php-cs-fixer.dist.php | 35 +++++++++++ composer.json | 3 +- examples/basic-graph.php | 24 ++++---- src/Dumper/Dumper.php | 2 + src/Dumper/MermaidDumper.php | 8 ++- src/Event/CanTransitionEvent.php | 2 + src/Event/Event.php | 5 +- src/Event/EventDispatcher.php | 6 +- src/Event/PostTransitionEvent.php | 2 + src/Event/PreTransitionEvent.php | 2 + src/Event/TransitionEvent.php | 5 +- src/Exception/BadStateClassException.php | 3 +- src/Exception/FiniteException.php | 2 + src/Exception/NoStateFoundException.php | 2 + src/Exception/NonUniqueStateException.php | 2 + src/Exception/PropertyNotFoundException.php | 2 + .../TransitionNotReachableException.php | 2 + .../DependencyInjection/FiniteExtension.php | 1 + src/Extension/Symfony/Bundle/FiniteBundle.php | 3 +- .../Command/DumpStateMachineCommand.php | 12 ++-- src/Extension/Twig/FiniteExtension.php | 5 +- src/State.php | 3 +- src/StateMachine.php | 46 +++++++------- src/Transition/Transition.php | 9 +-- src/Transition/TransitionInterface.php | 2 + tests/Dumper/MermaidDumperTest.php | 6 +- tests/E2E/AlternativeGraphTest.php | 16 ++--- tests/E2E/BasicGraphTest.php | 22 +++---- tests/Event/EventDispatcherTest.php | 14 +++-- .../FiniteExtensionTest.php | 5 +- .../Command/DumpStateMachineCommandTest.php | 8 ++- .../Fixtures/Controller/FiniteController.php | 6 +- .../Symfony/Fixtures/Model/Document.php | 1 + .../Symfony/Fixtures/State/DocumentState.php | 1 + .../Symfony/Fixtures/app/AppKernel.php | 5 +- .../Symfony/Fixtures/app/bootstrap.php | 3 +- .../Symfony/Functional/FiniteTest.php | 3 +- tests/Extension/Symfony/ServiceTest.php | 9 ++- tests/Extension/Twig/FiniteExtensionTest.php | 5 +- tests/Fixtures/AlternativeArticle.php | 8 ++- tests/Fixtures/AlternativeArticleState.php | 14 +++-- tests/Fixtures/Article.php | 10 +-- tests/Fixtures/SimpleArticleState.php | 20 +++--- tests/StateMachineTest.php | 61 ++++++++++--------- tests/Transition/TransitionTest.php | 9 +-- 47 files changed, 274 insertions(+), 159 deletions(-) create mode 100644 .php-cs-fixer.dist.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7a708f8..e7f7c1a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,6 +5,24 @@ on: pull_request: jobs: + cs: + runs-on: ubuntu-latest + name: Code Style + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.3' + + - name: Install dependencies + uses: ramsey/composer-install@v3 + + - name: PHP CS Fixer + run: ./vendor/bin/php-cs-fixer fix --dry-run --diff --ansi + psalm: runs-on: ubuntu-latest permissions: diff --git a/.gitignore b/.gitignore index 176e243..baf5363 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ tests/Extension/Symfony/Fixtures/app/var/ vendor/ .phpunit.result.cache +.php-cs-fixer.cache composer.lock diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..34ac90f --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,35 @@ +in(__DIR__) + ->exclude('tests/Extension/Symfony/Fixtures/app/var') + ->exclude('vendor'); + +return (new PhpCsFixer\Config()) + ->setRiskyAllowed(true) + ->setRules([ + '@Symfony' => true, + '@Symfony:risky' => true, + 'array_syntax' => [ + 'syntax' => 'short', + ], + 'braces' => [ + 'allow_single_line_closure' => true, + ], + 'declare_strict_types' => true, + 'modernize_types_casting' => true, + 'native_function_invocation' => ['include' => ['@compiler_optimized'], 'scope' => 'namespaced'], + 'no_useless_else' => true, + 'no_useless_return' => true, + 'ordered_imports' => true, + 'phpdoc_add_missing_param_annotation' => [ + 'only_untyped' => true, + ], + 'phpdoc_order' => true, + 'phpdoc_to_comment' => false, + 'semicolon_after_instruction' => true, + 'strict_comparison' => true, + 'strict_param' => true, + 'ternary_to_null_coalescing' => true, + ]) + ->setFinder($finder); diff --git a/composer.json b/composer.json index 801e219..8a4f22a 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,8 @@ "symfony/twig-bundle": ">=5.4,<8", "symfony/browser-kit": ">=5.4,<8", "symfony/css-selector": ">=5.4,<8", - "symfony/console": ">=5.4,<8" + "symfony/console": ">=5.4,<8", + "friendsofphp/php-cs-fixer": "^3.68" }, "autoload": { "psr-4": { diff --git a/examples/basic-graph.php b/examples/basic-graph.php index 9f45c0e..49e83f9 100755 --- a/examples/basic-graph.php +++ b/examples/basic-graph.php @@ -1,18 +1,20 @@ #!/usr/bin/env php getState()); - // Available transitions var_dump($stateMachine->getReachablesTransitions($document)); var_dump($stateMachine->can($document, DocumentState::PUBLISH)); @@ -77,7 +77,7 @@ public function setState(DocumentState $state): void // Apply transitions try { $stateMachine->apply($document, DocumentState::REPORT); -} catch (\Exception $e) { +} catch (Exception $e) { echo $e->getMessage(), "\n"; } diff --git a/src/Dumper/Dumper.php b/src/Dumper/Dumper.php index a025ccd..e1bbae3 100644 --- a/src/Dumper/Dumper.php +++ b/src/Dumper/Dumper.php @@ -1,5 +1,7 @@ getSourceStates() as $state) { - $output[] = sprintf( + $output[] = \sprintf( ' %s --> %s: %s', $state->value, $transition->getTargetState()->value, @@ -29,6 +31,6 @@ public function dump(string $stateEnum): string } } - return implode(PHP_EOL, $output); + return implode(\PHP_EOL, $output); } } diff --git a/src/Event/CanTransitionEvent.php b/src/Event/CanTransitionEvent.php index f1afc0e..7d7bdae 100644 --- a/src/Event/CanTransitionEvent.php +++ b/src/Event/CanTransitionEvent.php @@ -1,5 +1,7 @@ listeners[get_class($event)])) { + if (!isset($this->listeners[$event::class])) { return; } - foreach ($this->listeners[get_class($event)] as $listener) { + foreach ($this->listeners[$event::class] as $listener) { $listener($event); if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) { diff --git a/src/Event/PostTransitionEvent.php b/src/Event/PostTransitionEvent.php index fcbc934..08e65b2 100644 --- a/src/Event/PostTransitionEvent.php +++ b/src/Event/PostTransitionEvent.php @@ -1,5 +1,7 @@ add(new DumpStateMachineCommand); + $application->add(new DumpStateMachineCommand()); } } diff --git a/src/Extension/Symfony/Command/DumpStateMachineCommand.php b/src/Extension/Symfony/Command/DumpStateMachineCommand.php index ec05021..f342b77 100644 --- a/src/Extension/Symfony/Command/DumpStateMachineCommand.php +++ b/src/Extension/Symfony/Command/DumpStateMachineCommand.php @@ -1,5 +1,7 @@ getArgument('state_enum'); + $stateEnum = (string) $input->getArgument('state_enum'); if (!(enum_exists($stateEnum) && is_subclass_of($stateEnum, State::class))) { - $io->error('The state enum "' . $stateEnum . '" does not exist.'); + $io->error('The state enum "'.$stateEnum.'" does not exist.'); return self::FAILURE; } - $format = (string)$input->getArgument('format'); + $format = (string) $input->getArgument('format'); switch ($format) { case self::FORMAT_MERMAID: /** @psalm-suppress ArgumentTypeCoercion Type is enforced upper but not detected by psalm */ - $output->writeln((new MermaidDumper)->dump($stateEnum)); + $output->writeln((new MermaidDumper())->dump($stateEnum)); break; default: - $output->writeln('Unknown format "' . $format . '". Supported formats are: ' . implode(', ', self::FORMATS)); + $output->writeln('Unknown format "'.$format.'". Supported formats are: '.implode(', ', self::FORMATS)); return self::FAILURE; } diff --git a/src/Extension/Twig/FiniteExtension.php b/src/Extension/Twig/FiniteExtension.php index e0abd34..c8281ab 100644 --- a/src/Extension/Twig/FiniteExtension.php +++ b/src/Extension/Twig/FiniteExtension.php @@ -1,5 +1,7 @@ can($object, $transitionName, $stateClass)) { - throw new TransitionNotReachableException('Unable to apply transition ' . $transitionName); + throw new TransitionNotReachableException('Unable to apply transition '.$transitionName); } - $property = $this->extractStateProperty($object, $stateClass); + $property = $this->extractStateProperty($object, $stateClass); $transition = array_values( - array_filter( - $this->extractState($object, $stateClass)::getTransitions(), - fn(TransitionInterface $transition) => $transition->getName() === $transitionName, - ) - )[0]; + array_filter( + $this->extractState($object, $stateClass)::getTransitions(), + fn (TransitionInterface $transition) => $transition->getName() === $transitionName, + ) + )[0]; $this->dispatcher->dispatch(new PreTransitionEvent($object, $transitionName, $stateClass)); @@ -63,7 +64,7 @@ public function can(object $object, string $transitionName, ?string $stateClass continue; } - if (!in_array($state, $transition->getSourceStates())) { + if (!\in_array($state, $transition->getSourceStates(), true)) { return false; } @@ -73,7 +74,7 @@ public function can(object $object, string $transitionName, ?string $stateClass return !$event->isTransitionBlocked(); } - throw new TransitionNotReachableException(sprintf('No transition "%s" found', $transitionName)); + throw new TransitionNotReachableException(\sprintf('No transition "%s" found', $transitionName)); } /** @@ -85,20 +86,21 @@ public function getReachablesTransitions(object $object, ?string $stateClass = n return array_filter( $state->getTransitions(), - fn(TransitionInterface $transition) => $this->can($object, $transition->getName(), $stateClass), + fn (TransitionInterface $transition) => $this->can($object, $transition->getName(), $stateClass), ); } /** * @psalm-suppress MoreSpecificReturnType * @psalm-suppress LessSpecificReturnStatement + * * @return array> */ public function getStateClasses(object $object): array { return array_filter( array_map( - fn(\ReflectionProperty $property): string => (string)$property->getType(), + fn (\ReflectionProperty $property): string => (string) $property->getType(), $this->extractStateProperties($object), ), fn (?string $name): bool => enum_exists($name), @@ -107,7 +109,7 @@ public function getStateClasses(object $object): array public function hasState(object $object): bool { - return count($this->extractStateProperties($object)) > 0; + return \count($this->extractStateProperties($object)) > 0; } public function getDispatcher(): EventDispatcherInterface @@ -132,29 +134,29 @@ private function extractState(object $object, ?string $stateClass = null): State private function extractStateProperty(object $object, ?string $stateClass = null): \ReflectionProperty { if ($stateClass && !enum_exists($stateClass)) { - throw new NoStateFoundException(sprintf('Enum "%s" does not exists', $stateClass)); + throw new NoStateFoundException(\sprintf('Enum "%s" does not exists', $stateClass)); } $properties = $this->extractStateProperties($object); if (null !== $stateClass) { foreach ($properties as $property) { - if ((string)($property->getType()) === $stateClass) { + if ((string) $property->getType() === $stateClass) { return $property; } } - throw new BadStateClassException(sprintf('Found no state on object "%s" with class "%s"', get_class($object), $stateClass)); + throw new BadStateClassException(\sprintf('Found no state on object "%s" with class "%s"', $object::class, $stateClass)); } - if (1 === count($properties)) { + if (1 === \count($properties)) { return $properties[0]; } - if (count($properties) > 1) { - throw new NonUniqueStateException('Found multiple states on object ' . get_class($object)); + if (\count($properties) > 1) { + throw new NonUniqueStateException('Found multiple states on object '.$object::class); } - throw new NoStateFoundException('Found no state on object ' . get_class($object)); + throw new NoStateFoundException('Found no state on object '.$object::class); } /** diff --git a/src/Transition/Transition.php b/src/Transition/Transition.php index c8aef70..3eb6f10 100644 --- a/src/Transition/Transition.php +++ b/src/Transition/Transition.php @@ -1,5 +1,7 @@ */ - public readonly array $properties = [] - ) - { + public readonly array $properties = [], + ) { } public function getSourceStates(): array @@ -51,7 +52,7 @@ public function hasProperty(string $name): bool public function getPropertyValue(string $name): mixed { if (!$this->hasProperty($name)) { - throw new PropertyNotFoundException(sprintf('Property "%s" does not exist', $name)); + throw new PropertyNotFoundException(\sprintf('Property "%s" does not exist', $name)); } return $this->properties[$name]; diff --git a/src/Transition/TransitionInterface.php b/src/Transition/TransitionInterface.php index eea9fd3..c4fd924 100644 --- a/src/Transition/TransitionInterface.php +++ b/src/Transition/TransitionInterface.php @@ -1,5 +1,7 @@ assertSame( << disabled: disable published --> disabled: disable MERMAID, - (new MermaidDumper)->dump(SimpleArticleState::class) + (new MermaidDumper())->dump(SimpleArticleState::class) ); } } diff --git a/tests/E2E/AlternativeGraphTest.php b/tests/E2E/AlternativeGraphTest.php index b009ec8..fbf61d7 100644 --- a/tests/E2E/AlternativeGraphTest.php +++ b/tests/E2E/AlternativeGraphTest.php @@ -1,5 +1,7 @@ article = new AlternativeArticle('Hi ! I\'m an article.'); - $this->stateMachine = new StateMachine; + $this->article = new AlternativeArticle('Hi ! I\'m an article.'); + $this->stateMachine = new StateMachine(); } - public function test_it_has_transitions(): void + public function testItHasTransitions(): void { $this->assertCount(2, $this->article->getAlternativeState()::getTransitions()); $this->assertCount(1, $this->stateMachine->getReachablesTransitions($this->article, AlternativeArticleState::class)); @@ -29,20 +31,20 @@ public function test_it_has_transitions(): void ); } - public function test_it_allows_to_transition(): void + public function testItAllowsToTransition(): void { $this->assertTrue($this->stateMachine->can($this->article, AlternativeArticleState::MARK_READ, AlternativeArticleState::class)); $this->assertFalse($this->stateMachine->can($this->article, AlternativeArticleState::MARK_OLD, AlternativeArticleState::class)); } - public function test_it_applies_transition(): void + public function testItAppliesTransition(): void { $this->stateMachine->apply($this->article, AlternativeArticleState::MARK_READ, AlternativeArticleState::class); $this->assertSame(AlternativeArticleState::READ, $this->article->getAlternativeState()); } - public function test_it_reject_bad_transition(): void + public function testItRejectBadTransition(): void { $this->expectException(\InvalidArgumentException::class); $this->stateMachine->apply($this->article, AlternativeArticleState::MARK_OLD, AlternativeArticleState::class); diff --git a/tests/E2E/BasicGraphTest.php b/tests/E2E/BasicGraphTest.php index f5eb41c..8aeb13a 100644 --- a/tests/E2E/BasicGraphTest.php +++ b/tests/E2E/BasicGraphTest.php @@ -1,5 +1,7 @@ article = new Article('Hi ! I\'m an article.'); - $this->stateMachine = new StateMachine; + $this->article = new Article('Hi ! I\'m an article.'); + $this->stateMachine = new StateMachine(); } - - public function test_instantiate_and_have_state(): void + public function testInstantiateAndHaveState(): void { - $this->assertSame('draft', $this->article->getState()->value); } - public function test_it_has_transitions(): void + public function testItHasTransitions(): void { $this->assertCount(4, $this->article->getState()::getTransitions()); $this->assertCount(1, $this->stateMachine->getReachablesTransitions($this->article)); @@ -35,7 +35,7 @@ public function test_it_has_transitions(): void $this->assertSame(SimpleArticleState::PUBLISHED, $this->stateMachine->getReachablesTransitions($this->article)[0]->getTargetState()); } - public function test_it_reject_bad_transitions(): void + public function testItRejectBadTransitions(): void { $this->expectException(\InvalidArgumentException::class); $this->expectException(FiniteException::class); @@ -44,20 +44,20 @@ public function test_it_reject_bad_transitions(): void $this->stateMachine->can($this->article, 'touch this'); } - public function test_it_allows_to_transition(): void + public function testItAllowsToTransition(): void { $this->assertTrue($this->stateMachine->can($this->article, SimpleArticleState::PUBLISH)); $this->assertFalse($this->stateMachine->can($this->article, SimpleArticleState::REPORT)); } - public function test_it_applies_transition(): void + public function testItAppliesTransition(): void { $this->stateMachine->apply($this->article, SimpleArticleState::PUBLISH); $this->assertSame(SimpleArticleState::PUBLISHED, $this->article->getState()); } - public function test_it_reject_bad_transition(): void + public function testItRejectBadTransition(): void { $this->expectException(\InvalidArgumentException::class); diff --git a/tests/Event/EventDispatcherTest.php b/tests/Event/EventDispatcherTest.php index 1b2bc26..630ec83 100644 --- a/tests/Event/EventDispatcherTest.php +++ b/tests/Event/EventDispatcherTest.php @@ -1,5 +1,7 @@ getMockBuilder(Event::class)->disableOriginalConstructor()->getMock(); @@ -18,14 +20,14 @@ public function test_it_dispatch_event(): void $badListenerMock = $this->getMockBuilder(\stdClass::class)->addMethods(['__invoke'])->getMock(); $badListenerMock->expects($this->never())->method('__invoke')->with($objectMock); - $dispatcher = new EventDispatcher; + $dispatcher = new EventDispatcher(); $dispatcher->addEventListener($objectMock::class, $listenerMock); $dispatcher->addEventListener(Event::class, $badListenerMock); $dispatcher->dispatch($objectMock); } - public function test_it_dispatch_multiple_event(): void + public function testItDispatchMultipleEvent(): void { $objectMock = $this->getMockBuilder(Event::class)->disableOriginalConstructor()->getMock(); @@ -35,14 +37,14 @@ public function test_it_dispatch_multiple_event(): void $otherListenerMock = $this->getMockBuilder(\stdClass::class)->addMethods(['__invoke'])->getMock(); $otherListenerMock->expects($this->once())->method('__invoke')->with($objectMock); - $dispatcher = new EventDispatcher; + $dispatcher = new EventDispatcher(); $dispatcher->addEventListener($objectMock::class, $listenerMock); $dispatcher->addEventListener($objectMock::class, $otherListenerMock); $dispatcher->dispatch($objectMock); } - public function test_it_stops_propagation(): void + public function testItStopsPropagation(): void { $objectMock = $this->getMockBuilder(Event::class)->disableOriginalConstructor()->getMock(); $objectMock->expects($this->once())->method('stopPropagation'); @@ -58,7 +60,7 @@ public function test_it_stops_propagation(): void $otherListenerMock = $this->getMockBuilder(\stdClass::class)->addMethods(['__invoke'])->getMock(); $otherListenerMock->expects($this->never())->method('__invoke')->with($objectMock); - $dispatcher = new EventDispatcher; + $dispatcher = new EventDispatcher(); $dispatcher->addEventListener($objectMock::class, $listenerMock); $dispatcher->addEventListener($objectMock::class, $otherListenerMock); diff --git a/tests/Extension/Symfony/Bundle/DependencyInjection/FiniteExtensionTest.php b/tests/Extension/Symfony/Bundle/DependencyInjection/FiniteExtensionTest.php index 143833e..a455bed 100644 --- a/tests/Extension/Symfony/Bundle/DependencyInjection/FiniteExtensionTest.php +++ b/tests/Extension/Symfony/Bundle/DependencyInjection/FiniteExtensionTest.php @@ -1,4 +1,5 @@ getMockBuilder(ContainerBuilder::class) ->disableOriginalConstructor() @@ -25,7 +26,7 @@ public function test_it_loads_services(): void ), ); - $extension = new FiniteExtension; + $extension = new FiniteExtension(); $extension->load([], $container); } } diff --git a/tests/Extension/Symfony/Command/DumpStateMachineCommandTest.php b/tests/Extension/Symfony/Command/DumpStateMachineCommandTest.php index 06ce63a..9266553 100644 --- a/tests/Extension/Symfony/Command/DumpStateMachineCommandTest.php +++ b/tests/Extension/Symfony/Command/DumpStateMachineCommandTest.php @@ -1,5 +1,7 @@ commandTester = new CommandTester($command); } - public function test_it_returns_mermaid_dump(): void + public function testItReturnsMermaidDump(): void { $this->commandTester->execute([ 'state_enum' => DocumentState::class, @@ -32,7 +34,7 @@ public function test_it_returns_mermaid_dump(): void $this->commandTester->assertCommandIsSuccessful(); } - public function test_it_fails_with_unknown_state_enum(): void + public function testItFailsWithUnknownStateEnum(): void { $this->commandTester->execute([ 'state_enum' => 'UnknownStateEnum', @@ -42,7 +44,7 @@ public function test_it_fails_with_unknown_state_enum(): void $this->assertSame(1, $this->commandTester->getStatusCode()); } - public function test_it_fails_with_unknown_format(): void + public function testItFailsWithUnknownFormat(): void { $this->commandTester->execute([ 'state_enum' => DocumentState::class, diff --git a/tests/Extension/Symfony/Fixtures/Controller/FiniteController.php b/tests/Extension/Symfony/Fixtures/Controller/FiniteController.php index 7012fb7..10c9f7a 100644 --- a/tests/Extension/Symfony/Fixtures/Controller/FiniteController.php +++ b/tests/Extension/Symfony/Fixtures/Controller/FiniteController.php @@ -1,4 +1,5 @@ twig->render( diff --git a/tests/Extension/Symfony/Fixtures/Model/Document.php b/tests/Extension/Symfony/Fixtures/Model/Document.php index 58a927f..4666cdc 100644 --- a/tests/Extension/Symfony/Fixtures/Model/Document.php +++ b/tests/Extension/Symfony/Fixtures/Model/Document.php @@ -1,4 +1,5 @@ (new Definition(FiniteController::class)) ->setAutowired(true) ->addTag('controller.service_arguments'), - 'logger' => (new Definition(NullLogger::class)) + 'logger' => (new Definition(NullLogger::class)), ] ); } @@ -48,7 +49,7 @@ public function getProjectDir(): string public function loadRoutes(LoaderInterface $loader): RouteCollection { - $collection = new RouteCollection; + $collection = new RouteCollection(); $collection->add( 'finite', new Route('/finite', ['_controller' => FiniteController::class]), diff --git a/tests/Extension/Symfony/Fixtures/app/bootstrap.php b/tests/Extension/Symfony/Fixtures/app/bootstrap.php index b58eb5b..3e7798e 100644 --- a/tests/Extension/Symfony/Fixtures/app/bootstrap.php +++ b/tests/Extension/Symfony/Fixtures/app/bootstrap.php @@ -1,7 +1,8 @@ createClient(); $client->request('GET', '/finite'); diff --git a/tests/Extension/Symfony/ServiceTest.php b/tests/Extension/Symfony/ServiceTest.php index 3e18d95..c306c43 100644 --- a/tests/Extension/Symfony/ServiceTest.php +++ b/tests/Extension/Symfony/ServiceTest.php @@ -1,4 +1,5 @@ assertArrayHasKey(FiniteExtension::class, $container->get('twig')->getExtensions()); } - public function test_it_uses_the_symfony_dispatcher(): void + public function testItUsesTheSymfonyDispatcher(): void { $container = static::getContainer(); /** @var StateMachine $stateMachine */ $stateMachine = $container->get(StateMachine::class); - $stateMachine->can(new Document, 'publish'); + $stateMachine->can(new Document(), 'publish'); /** @var TraceableEventDispatcher $debugDispatcher */ $debugDispatcher = $container->get('debug.event_dispatcher'); diff --git a/tests/Extension/Twig/FiniteExtensionTest.php b/tests/Extension/Twig/FiniteExtensionTest.php index d0d8923..a3a675b 100644 --- a/tests/Extension/Twig/FiniteExtensionTest.php +++ b/tests/Extension/Twig/FiniteExtensionTest.php @@ -1,4 +1,5 @@ createMock(\stdClass::class); $stateMachine = $this->createMock(StateMachine::class); @@ -17,7 +18,7 @@ public function test_it_declare_twig_functions(): void $functions = (new FiniteExtension($stateMachine))->getFunctions(); $functions = array_combine( - array_map(fn($function) => $function->getName(), $functions), + array_map(fn ($function) => $function->getName(), $functions), $functions, ); $this->assertArrayHasKey('finite_can', $functions); diff --git a/tests/Fixtures/AlternativeArticle.php b/tests/Fixtures/AlternativeArticle.php index 0324834..89bcb61 100644 --- a/tests/Fixtures/AlternativeArticle.php +++ b/tests/Fixtures/AlternativeArticle.php @@ -1,16 +1,18 @@ createdAt = new \DateTimeImmutable; + $this->createdAt = new \DateTimeImmutable(); } public function getTitle(): string diff --git a/tests/Fixtures/AlternativeArticleState.php b/tests/Fixtures/AlternativeArticleState.php index db3f539..1b41138 100644 --- a/tests/Fixtures/AlternativeArticleState.php +++ b/tests/Fixtures/AlternativeArticleState.php @@ -1,5 +1,7 @@ createdAt = new \DateTimeImmutable; + $this->createdAt = new \DateTimeImmutable(); } public function getTitle(): string diff --git a/tests/Fixtures/SimpleArticleState.php b/tests/Fixtures/SimpleArticleState.php index 3c88cab..cfa34ac 100644 --- a/tests/Fixtures/SimpleArticleState.php +++ b/tests/Fixtures/SimpleArticleState.php @@ -1,5 +1,7 @@ assertInstanceOf(EventDispatcher::class, (new StateMachine)->getDispatcher()); - $this->assertInstanceOf(EventDispatcherInterface::class, (new StateMachine)->getDispatcher()); + $this->assertInstanceOf(EventDispatcher::class, (new StateMachine())->getDispatcher()); + $this->assertInstanceOf(EventDispatcherInterface::class, (new StateMachine())->getDispatcher()); } - public function test_it_use_constructor_event_dispatcher(): void + public function testItUseConstructorEventDispatcher(): void { $eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock(); $this->assertSame($eventDispatcher, (new StateMachine($eventDispatcher))->getDispatcher()); } - public function test_it_can_transition(): void + public function testItCanTransition(): void { $object = new Article('Hi !'); @@ -55,7 +56,7 @@ public function test_it_can_transition(): void $this->assertTrue($stateMachine->can($object, SimpleArticleState::PUBLISH)); } - public function test_it_blocks_transition(): void + public function testItBlocksTransition(): void { $object = new Article('Hi !'); @@ -64,15 +65,14 @@ public function test_it_blocks_transition(): void ->expects($this->once()) ->method('dispatch') ->with($this->isInstanceOf(CanTransitionEvent::class)) - ->willReturnCallback(fn(CanTransitionEvent $event) => $event->blockTransition()); - + ->willReturnCallback(fn (CanTransitionEvent $event) => $event->blockTransition()); $stateMachine = new StateMachine($eventDispatcher); $this->assertFalse($stateMachine->can($object, SimpleArticleState::PUBLISH)); } - public function test_it_applies_transition(): void + public function testItAppliesTransition(): void { $object = new Article('Hi !'); @@ -83,7 +83,7 @@ public function test_it_applies_transition(): void ->expects($matcher) ->method('dispatch') ->willReturnCallback( - fn(TransitionEvent $e) => match ($matcher->numberOfInvocations()) { + fn (TransitionEvent $e) => match ($matcher->numberOfInvocations()) { 1, 2, 3 => SimpleArticleState::PUBLISH === $e->getTransitionName(), 4, 5, 6 => SimpleArticleState::REPORT === $e->getTransitionName(), } @@ -95,13 +95,13 @@ public function test_it_applies_transition(): void $stateMachine->apply($object, SimpleArticleState::REPORT); } - public function test_it_rejects_non_stateful_object() + public function testItRejectsNonStatefulObject() { $this->expectException(\InvalidArgumentException::class); $this->expectException(FiniteException::class); $this->expectException(NoStateFoundException::class); - (new StateMachine)->can( + (new StateMachine())->can( new class { public string $title = 'Foobar'; }, @@ -109,65 +109,66 @@ public function test_it_rejects_non_stateful_object() ); } - public function test_it_rejects_unexistant_state_class() + public function testItRejectsUnexistantStateClass() { $this->expectException(\InvalidArgumentException::class); $this->expectException(FiniteException::class); $this->expectException(NoStateFoundException::class); - (new StateMachine)->can(new \stdClass, 'transition', 'Unexistant enum'); + (new StateMachine())->can(new \stdClass(), 'transition', 'Unexistant enum'); } - public function test_it_throws_if_no_state(): void + public function testItThrowsIfNoState(): void { $this->expectException(\InvalidArgumentException::class); $this->expectException(FiniteException::class); $this->expectException(NoStateFoundException::class); - $stateMachine = new StateMachine; - $stateMachine->can(new class extends \stdClass {}, 'transition'); + $stateMachine = new StateMachine(); + $stateMachine->can(new class extends \stdClass { + }, 'transition'); } - public function test_it_throws_if_bad_state(): void + public function testItThrowsIfBadState(): void { $this->expectException(\InvalidArgumentException::class); $this->expectException(FiniteException::class); $this->expectException(BadStateClassException::class); - $stateMachine = new StateMachine; + $stateMachine = new StateMachine(); $stateMachine->can(new Article('test'), 'publish', AlternativeArticleState::class); } - public function test_it_throws_if_many_state_and_none_given(): void + public function testItThrowsIfManyStateAndNoneGiven(): void { $this->expectException(\InvalidArgumentException::class); $this->expectException(FiniteException::class); $this->expectException(NonUniqueStateException::class); - $stateMachine = new StateMachine; + $stateMachine = new StateMachine(); $stateMachine->can(new AlternativeArticle('test'), 'publish'); } - public function test_it_returns_class_state_classes(): void + public function testItReturnsClassStateClasses(): void { $this->assertSame( [SimpleArticleState::class], - (new StateMachine)->getStateClasses(new Article('Hi !')), + (new StateMachine())->getStateClasses(new Article('Hi !')), ); $this->assertSame( [SimpleArticleState::class, AlternativeArticleState::class], - (new StateMachine)->getStateClasses(new AlternativeArticle('Hi !')), + (new StateMachine())->getStateClasses(new AlternativeArticle('Hi !')), ); $this->assertSame( [], - (new StateMachine)->getStateClasses(new \stdClass), + (new StateMachine())->getStateClasses(new \stdClass()), ); } - public function test_it_returns_if_object_has_state(): void + public function testItReturnsIfObjectHasState(): void { - $this->assertTrue((new StateMachine)->hasState(new Article('Hi !'))); - $this->assertTrue((new StateMachine)->hasState(new AlternativeArticle('Hi !'))); - $this->assertFalse((new StateMachine)->hasState(new \stdClass)); + $this->assertTrue((new StateMachine())->hasState(new Article('Hi !'))); + $this->assertTrue((new StateMachine())->hasState(new AlternativeArticle('Hi !'))); + $this->assertFalse((new StateMachine())->hasState(new \stdClass())); } } diff --git a/tests/Transition/TransitionTest.php b/tests/Transition/TransitionTest.php index 18fe64e..6d8be2e 100644 --- a/tests/Transition/TransitionTest.php +++ b/tests/Transition/TransitionTest.php @@ -1,4 +1,5 @@ assertTrue($this->object->hasProperty('property')); $this->assertTrue($this->object->hasProperty('property2')); $this->assertFalse($this->object->hasProperty('property3')); } - public function test_it_returns_property_value(): void + public function testItReturnsPropertyValue(): void { $this->assertSame('value', $this->object->getPropertyValue('property')); $this->assertSame('value2', $this->object->getPropertyValue('property2')); } - public function test_it_throws_exception_when_property_does_not_exist(): void + public function testItThrowsExceptionWhenPropertyDoesNotExist(): void { $this->expectException(\OutOfBoundsException::class); $this->expectException(FiniteException::class); @@ -47,7 +48,7 @@ public function test_it_throws_exception_when_property_does_not_exist(): void $this->object->getPropertyValue('property3'); } - public function test_it_returns_property_names(): void + public function testItReturnsPropertyNames(): void { $this->assertSame(['property', 'property2'], $this->object->getPropertyNames()); }