From f6a07b90afaee5b03713b1531c4c9b75946116b4 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Thu, 12 Dec 2024 23:07:17 +0400 Subject: [PATCH 1/4] Update metafiles --- .editorconfig | 13 +++- .gitattributes | 17 ++--- .gitignore | 20 +++--- .styleci.yml | 73 -------------------- composer.json | 180 +++++++++++++++++++++++++------------------------ phpunit.xml | 26 ++++--- 6 files changed, 133 insertions(+), 196 deletions(-) delete mode 100644 .styleci.yml diff --git a/.editorconfig b/.editorconfig index 814b011..55de6f8 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,5 +1,3 @@ -# editorconfig.org - root = true [*] @@ -10,5 +8,14 @@ indent_style = space indent_size = 4 trim_trailing_whitespace = true -[*.json] +[*.{yml, yaml, sh, conf, neon*}] indent_size = 2 + +[*.http] +end_of_line = crlf + +[*.md] +trim_trailing_whitespace = false + +[Makefile] +indent_style = tab diff --git a/.gitattributes b/.gitattributes index b7404a3..3b1d7aa 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,10 +1,7 @@ -/.github export-ignore -/.gitattributes export-ignore -/.gitignore export-ignore -/phpunit.xml.dist export-ignore -/tests export-ignore -/.editorconfig export-ignore -/.php_cs.dist.php export-ignore -/psalm.xml export-ignore -/psalm.xml.dist export-ignore -/UPGRADING.md export-ignore +/.* export-ignore +/tests export-ignore +/*.xml export-ignore +/*.yml export-ignore +/*.lock export-ignore +/*.dist export-ignore +Makefile export-ignore diff --git a/.gitignore b/.gitignore index a0e397f..a37b2b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,8 @@ -.idea -.php_cs -.php_cs.cache -.phpunit.result.cache -build -composer.lock -coverage -docs -vendor -node_modules -.php-cs-fixer.cache -.phpunit.cache/ +/.*/ +!/.github/ +/runtime/ +/vendor/ +/.env +/composer.lock +*.log +*.exe diff --git a/.styleci.yml b/.styleci.yml deleted file mode 100644 index 6fde134..0000000 --- a/.styleci.yml +++ /dev/null @@ -1,73 +0,0 @@ -preset: psr12 -risky: true - -version: 8 - -enabled: - - alpha_ordered_traits - - array_indentation - - array_push - - combine_consecutive_issets - - combine_consecutive_unsets - - combine_nested_dirname - - declare_strict_types - - dir_constant - - fully_qualified_strict_types - - function_to_constant - - is_null - - magic_constant_casing - - magic_method_casing - - method_separation - - modernize_types_casting - - native_function_casing - - native_function_type_declaration_casing - - no_alias_functions - - no_empty_comment - - no_empty_phpdoc - - no_empty_statement - - no_extra_block_blank_lines - - no_short_bool_cast - - no_superfluous_elseif - - no_unneeded_control_parentheses - - no_unneeded_curly_braces - - no_unneeded_final_method - - no_unset_cast - - no_unused_imports - - no_unused_lambda_imports - - no_useless_else - - no_useless_return - - normalize_index_brace - - php_unit_dedicate_assert - - php_unit_dedicate_assert_internal_type - - php_unit_expectation - - php_unit_mock - - php_unit_mock_short_will_return - - php_unit_namespaced - - php_unit_no_expectation_annotation - - phpdoc_no_empty_return - - phpdoc_no_useless_inheritdoc - - phpdoc_order - - phpdoc_property - - phpdoc_scalar - - phpdoc_separation - - phpdoc_singular_inheritdoc - - phpdoc_trim - - phpdoc_trim_consecutive_blank_line_separation - - phpdoc_type_to_var - - phpdoc_types - - phpdoc_types_order - - print_to_echo - - regular_callable_call - - return_assignment - - self_accessor - - self_static_accessor - - set_type_to_cast - - short_array_syntax - - short_list_syntax - - simplified_if_return - - single_quote - - standardize_not_equals - - ternary_to_null_coalescing - - trailing_comma_in_multiline_array - - unalign_double_arrow - - unalign_equals diff --git a/composer.json b/composer.json index d63242d..218e663 100644 --- a/composer.json +++ b/composer.json @@ -1,95 +1,99 @@ { - "name": "spiral/testing", - "description": "Spiral Framework testing SDK", - "keywords": ["spiral-packages", "spiral", "testing"], - "license": "MIT", - "authors": [ - { - "name": "Anton Titov (wolfy-j)", - "email": "wolfy-j@spiralscout.com" + "name": "spiral/testing", + "description": "Spiral Framework testing SDK", + "keywords": [ + "spiral-packages", + "spiral", + "testing" + ], + "license": "MIT", + "authors": [ + { + "name": "Anton Titov (wolfy-j)", + "email": "wolfy-j@spiralscout.com" + }, + { + "name": "Pavel Buchnev (butschster)", + "email": "pavel.buchnev@spiralscout.com" + }, + { + "name": "Aleksei Gagarin (roxblnfk)", + "email": "alexey.gagarin@spiralscout.com" + }, + { + "name": "Maksim Smakouz (msmakouz)", + "email": "maksim.smakouz@spiralscout.com" + }, + { + "name": "RoadRunner Community", + "homepage": "https://github.com/spiral/roadrunner/graphs/contributors" + } + ], + "homepage": "https://spiral.dev/", + "support": { + "docs": "https://spiral.dev/docs/testing-start", + "issues": "https://github.com/spiral/testing/issues", + "chat": "https://discord.gg/TFeEmCs" }, - { - "name": "Pavel Buchnev (butschster)", - "email": "pavel.buchnev@spiralscout.com" + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/spiral" + } + ], + "require": { + "ext-json": "*", + "php": ">=8.1", + "nyholm/psr7": "^1.5", + "mockery/mockery": "^1.5", + "phpunit/phpunit": "^9.6 || ^10.0", + "spiral/auth": "^3.14.8", + "spiral/auth-http": "^3.14.8", + "spiral/boot": "^3.14.8", + "spiral/events": "^3.14.8", + "spiral/console": "^3.14.8", + "spiral/core": "^3.14.8", + "spiral/http": "^3.14.8", + "spiral/mailer": "^3.14.8", + "spiral/queue": "^3.14.8", + "spiral/session": "^3.14.8", + "spiral/security": "^3.14.8", + "spiral/tokenizer": "^3.14.8", + "spiral/storage": "^3.14.8", + "spiral/views": "^3.14.8", + "spiral/translator": "^3.14.8", + "spiral/scaffolder": "^3.14.8", + "symfony/mime": "^6.0 || ^7.0" }, - { - "name": "Aleksei Gagarin (roxblnfk)", - "email": "alexey.gagarin@spiralscout.com" + "suggest": { + "brianium/paratest": "Required to run tests in parallel (^6.0).", + "ext-gd": "Required to use generate fake image files" }, - { - "name": "Maksim Smakouz (msmakouz)", - "email": "maksim.smakouz@spiralscout.com" + "require-dev": { + "spiral/dumper": "^3.3", + "spiral/framework": "^3.14.8", + "spiral/roadrunner-bridge": "^2.2 || ^3.7 || ^4.0", + "spiral-packages/league-event": "^1.0.1", + "spiral/nyholm-bridge": "^1.3", + "vimeo/psalm": "^5.9" }, - { - "name": "RoadRunner Community", - "homepage": "https://github.com/spiral/roadrunner/graphs/contributors" - } - ], - "homepage": "https://spiral.dev/", - "support": { - "docs": "https://spiral.dev/docs/testing-start", - "issues": "https://github.com/spiral/testing/issues", - "forum": "https://forum.roadrunner.dev/", - "chat": "https://discord.gg/TFeEmCs" - }, - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/roadrunner-server" - } - ], - "require": { - "ext-json": "*", - "php": ">=8.1", - "nyholm/psr7": "^1.5", - "mockery/mockery": "^1.5", - "phpunit/phpunit": "^9.6 || ^10.0", - "spiral/auth": "^3.14.3", - "spiral/auth-http": "^3.14.3", - "spiral/boot": "^3.14.3", - "spiral/events": "^3.14.3", - "spiral/console": "^3.14.3", - "spiral/core": "^3.14.3", - "spiral/http": "^3.14.3", - "spiral/mailer": "^3.14.3", - "spiral/queue": "^3.14.3", - "spiral/session": "^3.14.3", - "spiral/security": "^3.14.3", - "spiral/tokenizer": "^3.14.3", - "spiral/storage": "^3.14.3", - "spiral/views": "^3.14.3", - "spiral/translator": "^3.14.3", - "spiral/scaffolder": "^3.14.3", - "symfony/mime": "^6.0 || ^7.0" - }, - "suggest": { - "brianium/paratest": "Required to run tests in parallel (^6.0).", - "ext-gd": "Required to use generate fake image files" - }, - "require-dev": { - "spiral/dumper": "^3.3", - "spiral/framework": "^3.14.3", - "spiral/roadrunner-bridge": "^2.2 || ^3.7 || ^4.0", - "spiral-packages/league-event": "^1.0.1", - "spiral/nyholm-bridge": "^1.3", - "vimeo/psalm": "^5.9" - }, - "autoload": { - "psr-4": { - "Spiral\\Testing\\": "src" - } - }, - "autoload-dev": { - "psr-4": { - "Spiral\\Testing\\Tests\\App\\": "tests/app", - "Spiral\\Testing\\Tests\\": "tests/src" - } - }, - "minimum-stability": "dev", - "prefer-stable": true, - "config": { - "allow-plugins": { - "spiral/composer-publish-plugin": false + "autoload": { + "psr-4": { + "Spiral\\Testing\\": "src" + } + }, + "autoload-dev": { + "sort-packages": true, + "psr-4": { + "Spiral\\Testing\\Tests\\App\\": "tests/app", + "Spiral\\Testing\\Tests\\": "tests/src" + } + }, + "minimum-stability": "dev", + "prefer-stable": true, + "config": { + "allow-plugins": { + "spiral/composer-publish-plugin": false + } } - } } diff --git a/phpunit.xml b/phpunit.xml index 335b7ba..27d113c 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,6 +1,7 @@ + backupStaticProperties="false" +> tests - - ./src - - - - + + + - + + + + src + + + tests + + From f1f40a1c4ec5ed5a82462d9993128ba0aea8c888 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Thu, 12 Dec 2024 23:10:08 +0400 Subject: [PATCH 2/4] Install Spiral Code Style --- .github/workflows/cs-fix.yml | 16 ++++++++++++++++ .php-cs-fixer.dist.php | 13 +++++++++++++ composer.json | 14 +++++++++++--- 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/cs-fix.yml create mode 100644 .php-cs-fixer.dist.php diff --git a/.github/workflows/cs-fix.yml b/.github/workflows/cs-fix.yml new file mode 100644 index 0000000..db5860c --- /dev/null +++ b/.github/workflows/cs-fix.yml @@ -0,0 +1,16 @@ +--- + +on: # yamllint disable-line rule:truthy + push: + branches: + - '*' + +name: Fix Code Style + +jobs: + cs-fix: + permissions: + contents: write + uses: spiral/gh-actions/.github/workflows/cs-fix.yml@master + +... diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..45745a1 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,13 @@ +include(__DIR__ . '/src') + ->include(__DIR__ . '/tests') + ->include(__FILE__) + ->cache('./runtime/php-cs-fixer.cache') + ->allowRisky(false) + ->build(); diff --git a/composer.json b/composer.json index 218e663..e0d028d 100644 --- a/composer.json +++ b/composer.json @@ -70,11 +70,12 @@ "ext-gd": "Required to use generate fake image files" }, "require-dev": { + "spiral-packages/league-event": "^1.0.1", + "spiral/code-style": "^2.2", "spiral/dumper": "^3.3", "spiral/framework": "^3.14.8", - "spiral/roadrunner-bridge": "^2.2 || ^3.7 || ^4.0", - "spiral-packages/league-event": "^1.0.1", "spiral/nyholm-bridge": "^1.3", + "spiral/roadrunner-bridge": "^2.2 || ^3.7 || ^4.0", "vimeo/psalm": "^5.9" }, "autoload": { @@ -83,7 +84,6 @@ } }, "autoload-dev": { - "sort-packages": true, "psr-4": { "Spiral\\Testing\\Tests\\App\\": "tests/app", "Spiral\\Testing\\Tests\\": "tests/src" @@ -92,8 +92,16 @@ "minimum-stability": "dev", "prefer-stable": true, "config": { + "sort-packages": true, "allow-plugins": { "spiral/composer-publish-plugin": false } + }, + "scripts": { + "cs:diff": "php-cs-fixer fix --dry-run -v --diff", + "cs:fix": "php-cs-fixer fix -v", + "psalm": "psalm", + "psalm:baseline": "psalm --set-baseline=psalm-baseline.xml", + "test": "phpunit --color=always" } } From 14e85fe4cf3d067185f038ecc7c19708e2350ef4 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Thu, 12 Dec 2024 23:12:31 +0400 Subject: [PATCH 3/4] Apply Code Style --- src/Attribute/Config.php | 9 +- src/Attribute/Env.php | 7 +- src/Attribute/TestScope.php | 7 +- src/Attribute/WithoutExceptionHandling.php | 8 +- src/Auth/FakeActorProvider.php | 5 +- src/Events/FakeEventDispatcher.php | 17 ++- src/Http/FakeHttp.php | 3 +- src/Http/FakeTokenStorage.php | 6 +- src/Http/File.php | 5 +- src/Http/FileFactory.php | 4 +- src/Http/TestResponse.php | 64 ++++----- src/Http/Token.php | 5 +- src/Mailer/FakeMailer.php | 47 ++++--- src/Queue/FakeQueue.php | 58 ++++---- src/Queue/FakeQueueManager.php | 15 +-- src/Session/FakeSession.php | 11 +- src/Session/FakeSessionSection.php | 45 ++++--- src/Storage/FakeBucket.php | 66 +++++----- src/Storage/FakeBucketFactory.php | 13 +- src/TestApp.php | 16 +-- src/TestCase.php | 124 ++++++++++-------- src/Traits/InteractsWithConsole.php | 14 +- src/Traits/InteractsWithCore.php | 31 +++-- src/Traits/InteractsWithDispatcher.php | 8 +- src/Traits/InteractsWithEvents.php | 4 +- src/Traits/InteractsWithExceptions.php | 14 +- src/Traits/InteractsWithFileSystem.php | 8 +- src/Traits/InteractsWithHttp.php | 2 +- src/Traits/InteractsWithMailer.php | 2 +- src/Traits/InteractsWithQueue.php | 2 +- src/Traits/InteractsWithScaffolder.php | 6 +- src/Traits/InteractsWithStorage.php | 4 +- src/Traits/InteractsWithViews.php | 6 +- src/Traits/TestableKernel.php | 14 +- tests/app/Bootloader/BlogBootloader.php | 17 +-- .../Controller/EventDispatcherController.php | 5 +- tests/app/Controller/PostController.php | 1 - tests/app/Controller/UploadController.php | 2 +- tests/app/Event/AnotherEvent.php | 5 +- tests/app/Event/SomeEvent.php | 5 +- .../Repositories/PostRepositoryInterface.php | 1 + tests/app/Services/BlogService.php | 5 +- tests/app/Services/BlogServiceInterface.php | 5 +- tests/src/Attribute/EnvTest.php | 2 +- .../WithoutExceptionHandlingTest.php | 6 +- tests/src/ConsoleTest.php | 1 - tests/src/Core/BootloaderTest.php | 12 +- tests/src/Event/EventDispatcherTest.php | 17 ++- tests/src/Http/FakeHttpTest.php | 4 +- tests/src/Scaffolder/ScaffolderTest.php | 4 +- tests/src/Storage/StorageBucketFakerTest.php | 14 +- tests/src/TestCase.php | 2 +- .../src/TestCase/Fixture/OtherParentClass.php | 4 +- .../Fixture/WithMethodsInNestedParent.php | 4 +- .../TestCase/Fixture/WithMethodsInParent.php | 4 +- tests/src/TestCase/Fixture/WithoutTraits.php | 4 +- tests/src/Traits/InteractsWithMailerTest.php | 9 +- tests/src/Traits/InteractsWithQueueTest.php | 9 +- 58 files changed, 376 insertions(+), 416 deletions(-) diff --git a/src/Attribute/Config.php b/src/Attribute/Config.php index 4af0da8..24fa0d8 100644 --- a/src/Attribute/Config.php +++ b/src/Attribute/Config.php @@ -4,18 +4,15 @@ namespace Spiral\Testing\Attribute; -use Attribute; -use Closure; - -#[Attribute(flags: Attribute::TARGET_METHOD|Attribute::IS_REPEATABLE)] +#[\Attribute(flags: \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] final class Config { - public ?Closure $closure; + public ?\Closure $closure; public function __construct( public string $path, public mixed $value = null, - callable $closure = null, + ?callable $closure = null, ) { $this->closure = $closure !== null ? $closure(...) : null; } diff --git a/src/Attribute/Env.php b/src/Attribute/Env.php index ef14006..ea8d66b 100644 --- a/src/Attribute/Env.php +++ b/src/Attribute/Env.php @@ -4,14 +4,11 @@ namespace Spiral\Testing\Attribute; -use Attribute; - -#[Attribute(flags: Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] +#[\Attribute(flags: \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] final class Env { public function __construct( public readonly string $key, public readonly int|string|null|bool $value = null, - ) { - } + ) {} } diff --git a/src/Attribute/TestScope.php b/src/Attribute/TestScope.php index 1c63a50..be9ca3b 100644 --- a/src/Attribute/TestScope.php +++ b/src/Attribute/TestScope.php @@ -4,14 +4,11 @@ namespace Spiral\Testing\Attribute; -use Attribute; - -#[Attribute(flags: Attribute::TARGET_METHOD|Attribute::TARGET_CLASS)] +#[\Attribute(flags: \Attribute::TARGET_METHOD | \Attribute::TARGET_CLASS)] final class TestScope { public function __construct( public readonly string|\BackedEnum|array $scope, public readonly array $bindings = [], - ) { - } + ) {} } diff --git a/src/Attribute/WithoutExceptionHandling.php b/src/Attribute/WithoutExceptionHandling.php index 6cbcb6c..19ca1c1 100644 --- a/src/Attribute/WithoutExceptionHandling.php +++ b/src/Attribute/WithoutExceptionHandling.php @@ -4,9 +4,5 @@ namespace Spiral\Testing\Attribute; -use Attribute; - -#[Attribute(flags: Attribute::TARGET_METHOD)] -final class WithoutExceptionHandling -{ -} +#[\Attribute(flags: \Attribute::TARGET_METHOD)] +final class WithoutExceptionHandling {} diff --git a/src/Auth/FakeActorProvider.php b/src/Auth/FakeActorProvider.php index 7a382b2..d3057ed 100644 --- a/src/Auth/FakeActorProvider.php +++ b/src/Auth/FakeActorProvider.php @@ -10,9 +10,8 @@ final class FakeActorProvider implements ActorProviderInterface { public function __construct( - private readonly object $actor - ) { - } + private readonly object $actor, + ) {} public function getActor(TokenInterface $token): ?object { diff --git a/src/Events/FakeEventDispatcher.php b/src/Events/FakeEventDispatcher.php index 4f4588e..639a553 100644 --- a/src/Events/FakeEventDispatcher.php +++ b/src/Events/FakeEventDispatcher.php @@ -22,8 +22,7 @@ public function __construct( private readonly array $eventsToFake = [], private readonly ?EventDispatcherInterface $eventDispatcher = null, private readonly ?ListenerProviderInterface $listenerProvider = null, - ) { - } + ) {} public function dispatch(object $event): ?object { @@ -72,8 +71,8 @@ public function assertListening(string $expectedEvent, string $expectedListener) \sprintf( 'Event [%s] does not have the [%s] listener attached to it.', $expectedEvent::class, - $expectedListener - ) + $expectedListener, + ), ); } @@ -85,7 +84,7 @@ public function assertDispatched(string $event, ?\Closure $callback = null): voi { PHPUnit::assertTrue( \count($this->dispatched($event, $callback)) > 0, - "The expected [{$event}] event was not dispatched." + "The expected [{$event}] event was not dispatched.", ); } @@ -102,7 +101,7 @@ public function assertDispatchedTimes(string $event, int $times = 1): void PHPUnit::assertSame( $times, $count, - "The expected [{$event}] event was dispatched {$count} times instead of {$times} times." + "The expected [{$event}] event was dispatched {$count} times instead of {$times} times.", ); } @@ -116,7 +115,7 @@ public function assertNotDispatched(string $event, ?\Closure $callback = null): PHPUnit::assertCount( 0, $this->dispatched($event, $callback), - "The unexpected [{$event}] event was dispatched." + "The unexpected [{$event}] event was dispatched.", ); } @@ -130,7 +129,7 @@ public function assertNothingDispatched(): void PHPUnit::assertSame( 0, $count, - "{$count} unexpected events were dispatched." + "{$count} unexpected events were dispatched.", ); } @@ -150,7 +149,7 @@ public function dispatched(string $event, ?\Closure $callback = null): array return \array_filter( $this->dispatchedEvents[$event], - static fn(object $event): bool => $callback($event) + static fn(object $event): bool => $callback($event), ); } diff --git a/src/Http/FakeHttp.php b/src/Http/FakeHttp.php index e0ea6d3..de17348 100644 --- a/src/Http/FakeHttp.php +++ b/src/Http/FakeHttp.php @@ -32,7 +32,6 @@ class FakeHttp private array $defaultServerVariables = []; private array $defaultHeaders = []; private array $defaultCookies = []; - private ?object $actor = null; private ?SessionInterface $session = null; private BinderInterface $binder; @@ -47,7 +46,7 @@ public function __construct( ) { $this->binder = $container ->get(InvokerInterface::class) - ->invoke(static fn (#[Proxy] BinderInterface $binder): BinderInterface => $binder); + ->invoke(static fn(#[Proxy] BinderInterface $binder): BinderInterface => $binder); } public function withActor(object $actor): self diff --git a/src/Http/FakeTokenStorage.php b/src/Http/FakeTokenStorage.php index b099111..50b87c7 100644 --- a/src/Http/FakeTokenStorage.php +++ b/src/Http/FakeTokenStorage.php @@ -14,12 +14,10 @@ public function load(string $id): ?TokenInterface return new Token($id, []); } - public function create(array $payload, \DateTimeInterface $expiresAt = null): TokenInterface + public function create(array $payload, ?\DateTimeInterface $expiresAt = null): TokenInterface { return new Token(uniqid(), $payload, $expiresAt); } - public function delete(TokenInterface $token): void - { - } + public function delete(TokenInterface $token): void {} } diff --git a/src/Http/File.php b/src/Http/File.php index 072496f..60dd552 100644 --- a/src/Http/File.php +++ b/src/Http/File.php @@ -20,12 +20,11 @@ class File extends UploadedFile public ?string $fakeMimeType = null; /** - * @param string $filename * @param resource $tempFile */ public function __construct( private string $filename, - private $tempFile + private $tempFile, ) { $extension = pathinfo($filename, PATHINFO_EXTENSION); $mimeType = (new MimeTypes())->getMimeTypes($extension)[0] ?? 'application/octet-stream'; @@ -35,7 +34,7 @@ public function __construct( fstat($tempFile)['size'], UPLOAD_ERR_OK, $filename, - $mimeType + $mimeType, ); } diff --git a/src/Http/FileFactory.php b/src/Http/FileFactory.php index 4b40516..ad0f01a 100644 --- a/src/Http/FileFactory.php +++ b/src/Http/FileFactory.php @@ -9,7 +9,7 @@ final class FileFactory /** * Create a new fake file. */ - public function createFile(string $filename, ?int $kilobytes = null, string $mimeType = null): File + public function createFile(string $filename, ?int $kilobytes = null, ?string $mimeType = null): File { $file = new File($filename, tmpfile()); @@ -27,7 +27,7 @@ public function createFile(string $filename, ?int $kilobytes = null, string $mim /** * Create a new fake file with given content. */ - public function createFileWithContent(string $filename, string $content, string $mimeType = null): File + public function createFileWithContent(string $filename, string $content, ?string $mimeType = null): File { $tmpFile = tmpfile(); fwrite($tmpFile, $content); diff --git a/src/Http/TestResponse.php b/src/Http/TestResponse.php index 01bff34..6dfc9d6 100644 --- a/src/Http/TestResponse.php +++ b/src/Http/TestResponse.php @@ -12,7 +12,7 @@ final class TestResponse implements \Stringable private array $cookies; public function __construct( - private readonly ResponseInterface $response + private readonly ResponseInterface $response, ) { $this->cookies = $this->fetchCookies($this->response->getHeader('Set-Cookie')); } @@ -29,7 +29,7 @@ public function assertHasHeader(string $name, ?string $value = null): self { TestCase::assertTrue( $this->response->hasHeader($name), - \sprintf('Response does not contain header with name [%s].', $name) + \sprintf('Response does not contain header with name [%s].', $name), ); $headerValue = $this->response->getHeaderLine($name); @@ -38,7 +38,7 @@ public function assertHasHeader(string $name, ?string $value = null): self TestCase::assertSame( $value, $headerValue, - \sprintf("Header [%s] was found, but value [%s] does not match [%s].", $name, $headerValue, $value) + \sprintf("Header [%s] was found, but value [%s] does not match [%s].", $name, $headerValue, $value), ); } @@ -49,7 +49,7 @@ public function assertHeaderMissing(string $name): self { TestCase::assertFalse( $this->response->hasHeader($name), - \sprintf('Response contains header with name [%s].', $name) + \sprintf('Response contains header with name [%s].', $name), ); return $this; @@ -66,7 +66,7 @@ public function assertStatus(int $status): self $this->response->getReasonPhrase(), $status, (string) $this->response->getBody(), - ) + ), ); return $this; @@ -93,7 +93,7 @@ public function assertNoContent(int $status = 204): self TestCase::assertEmpty( $this->response->getBody()->getContents(), - 'Response content should be empty.' + 'Response content should be empty.', ); return $this; @@ -123,8 +123,8 @@ public function assertBodySame(string $needle): self { TestCase::assertSame( $needle, - (string)$this->response->getBody(), - \sprintf('Response is not same with [%s]', $needle) + (string) $this->response->getBody(), + \sprintf('Response is not same with [%s]', $needle), ); return $this; @@ -134,8 +134,8 @@ public function assertBodyNotSame(string $needle): self { TestCase::assertNotSame( $needle, - (string)$this->response->getBody(), - \sprintf('Response is same with [%s]', $needle) + (string) $this->response->getBody(), + \sprintf('Response is same with [%s]', $needle), ); return $this; @@ -145,8 +145,8 @@ public function assertBodyContains(string $needle): self { TestCase::assertStringContainsString( $needle, - (string)$this->response->getBody(), - \sprintf('Response doesn\'t contain [%s]', $needle) + (string) $this->response->getBody(), + \sprintf('Response doesn\'t contain [%s]', $needle), ); return $this; @@ -157,7 +157,7 @@ public function assertCookieExists(string $key): self TestCase::assertArrayHasKey( $key, $this->getCookies(), - \sprintf('Response doesn\'t have cookie with name [%s]', $key) + \sprintf('Response doesn\'t have cookie with name [%s]', $key), ); return $this; @@ -168,7 +168,7 @@ public function assertCookieMissed(string $key): self TestCase::assertArrayNotHasKey( $key, $this->getCookies(), - \sprintf('Response has cookie with name [%s]', $key) + \sprintf('Response has cookie with name [%s]', $key), ); return $this; @@ -181,7 +181,7 @@ public function assertCookieSame(string $key, mixed $value): self TestCase::assertSame( $value, $this->cookies[$key], - \sprintf('Response cookie with name [%s] is not equal.', $key) + \sprintf('Response cookie with name [%s] is not equal.', $key), ); return $this; @@ -197,9 +197,25 @@ public function getOriginalResponse(): ResponseInterface return $this->response; } + /** + * @return array + */ + public function getCookies(): array + { + return $this->cookies; + } + + public function getJsonParsedBody(): array + { + return \json_decode( + (string) $this->response->getBody(), + true, + ); + } + public function __toString(): string { - return (string)$this->getOriginalResponse()->getBody(); + return (string) $this->getOriginalResponse()->getBody(); } private function fetchCookies(array $header): array @@ -212,20 +228,4 @@ private function fetchCookies(array $header): array return $result; } - - /** - * @return array - */ - public function getCookies(): array - { - return $this->cookies; - } - - public function getJsonParsedBody(): array - { - return \json_decode( - (string)$this->response->getBody(), - true - ); - } } diff --git a/src/Http/Token.php b/src/Http/Token.php index 26547d5..a3117ae 100644 --- a/src/Http/Token.php +++ b/src/Http/Token.php @@ -11,9 +11,8 @@ class Token implements TokenInterface public function __construct( private readonly string $id, private readonly array $payload, - private readonly ?\DateTimeInterface $expiresAt = null - ) { - } + private readonly ?\DateTimeInterface $expiresAt = null, + ) {} public function getID(): string { diff --git a/src/Mailer/FakeMailer.php b/src/Mailer/FakeMailer.php index bd26712..bc808f9 100644 --- a/src/Mailer/FakeMailer.php +++ b/src/Mailer/FakeMailer.php @@ -4,7 +4,6 @@ namespace Spiral\Testing\Mailer; -use Closure; use PHPUnit\Framework\TestCase; use Spiral\Mailer\MailerInterface; use Spiral\Mailer\MessageInterface; @@ -13,44 +12,29 @@ class FakeMailer implements MailerInterface { private array $messages = []; - private function filterMessages(string $type, Closure $callback = null): array - { - $messages = \array_filter($this->messages, static function (MessageInterface $msg) use ($type): bool { - return $msg instanceof $type; - }); - - $callback = $callback ?: static function (MessageInterface $msg): bool { - return true; - }; - - return \array_filter($messages, static function (MessageInterface $msg) use ($callback) { - return $callback($msg); - }); - } - /** * @return MessageInterface[] */ - public function assertSent(string $message, Closure $callback = null): array + public function assertSent(string $message, ?\Closure $callback = null): array { $messages = $this->filterMessages($message, $callback); TestCase::assertTrue( \count($messages) > 0, - \sprintf('The expected [%s] message was not sent.', $message) + \sprintf('The expected [%s] message was not sent.', $message), ); return $messages; } - public function assertNotSent(string $message, Closure $callback = null): void + public function assertNotSent(string $message, ?\Closure $callback = null): void { $messages = $this->filterMessages($message, $callback); TestCase::assertCount( 0, $messages, - \sprintf('The unexpected [%s] message was sent.', $message) + \sprintf('The unexpected [%s] message was sent.', $message), ); } @@ -68,8 +52,8 @@ public function assertSentTimes(string $message, int $times = 1): array 'The expected [%s] message was sent {%d} times instead of {%d} times.', $message, \count($messages), - $times - ) + $times, + ), ); return $messages; @@ -88,8 +72,8 @@ public function assertNothingSent(): void $this->messages, \sprintf( 'The following messages were sent unexpectedly: %s.', - $messages - ) + $messages, + ), ); } @@ -104,4 +88,19 @@ public function clear(): void { $this->messages = []; } + + private function filterMessages(string $type, ?\Closure $callback = null): array + { + $messages = \array_filter($this->messages, static function (MessageInterface $msg) use ($type): bool { + return $msg instanceof $type; + }); + + $callback = $callback ?: static function (MessageInterface $msg): bool { + return true; + }; + + return \array_filter($messages, static function (MessageInterface $msg) use ($callback) { + return $callback($msg); + }); + } } diff --git a/src/Queue/FakeQueue.php b/src/Queue/FakeQueue.php index dc76ffa..e092688 100644 --- a/src/Queue/FakeQueue.php +++ b/src/Queue/FakeQueue.php @@ -35,50 +35,32 @@ class FakeQueue implements QueueInterface public function __construct( private readonly HandlerRegistryInterface $registry, private readonly string $name, - private readonly string $driver - ) - { - } - - /** - * @return list - */ - private function filterJobs(string $name, \Closure $callback = null): array - { - $jobs = $this->jobs[$name] ?? []; - - $callback = $callback ?: static function (array $data): bool { - return true; - }; - - return \array_filter($jobs, static function (array $data) use ($callback) { - return $callback($data); - }); - } + private readonly string $driver, + ) {} /** * @return list */ - public function assertPushed(string $name, \Closure $callback = null): array + public function assertPushed(string $name, ?\Closure $callback = null): array { $jobs = $this->filterJobs($name, $callback); TestCase::assertTrue( \count($jobs) > 0, - \sprintf('The expected job [%s] was not pushed.', $name) + \sprintf('The expected job [%s] was not pushed.', $name), ); return $jobs; } - public function assertNotPushed(string $name, \Closure $callback = null): void + public function assertNotPushed(string $name, ?\Closure $callback = null): void { $jobs = $this->filterJobs($name, $callback); TestCase::assertCount( 0, $jobs, - \sprintf('The unexpected job [%s] was pushed.', $name) + \sprintf('The unexpected job [%s] was pushed.', $name), ); } @@ -89,7 +71,7 @@ public function assertNothingPushed(): void TestCase::assertCount( 0, $this->jobs, - \sprintf('The following jobs were pushed unexpectedly: %s', $jobs) + \sprintf('The following jobs were pushed unexpectedly: %s', $jobs), ); } @@ -107,8 +89,8 @@ public function assertPushedTimes(string $name, int $times = 1): array 'The expected job [%s] was sent {%d} times instead of {%d} times.', $name, \count($jobs), - $times - ) + $times, + ), ); return $jobs; @@ -117,7 +99,7 @@ public function assertPushedTimes(string $name, int $times = 1): array /** * @return list */ - public function assertPushedOnQueue(string $queue, string $name, \Closure $callback = null): array + public function assertPushedOnQueue(string $queue, string $name, ?\Closure $callback = null): array { return $this->assertPushed($name, static function (array $data) use ($queue, $callback) { if ($data['options']->getQueue() !== $queue) { @@ -128,11 +110,11 @@ public function assertPushedOnQueue(string $queue, string $name, \Closure $callb }); } - public function push(string $name, mixed $payload = [], OptionsInterface $options = null): string + public function push(string $name, mixed $payload = [], ?OptionsInterface $options = null): string { $this->jobs[$name][] = [ 'name' => $name, - 'handler' => \class_exists($name) ? $this->registry->getHandler($name): null, + 'handler' => \class_exists($name) ? $this->registry->getHandler($name) : null, 'payload' => $payload, 'options' => $options ?? new Options(), ]; @@ -144,4 +126,20 @@ public function clear(): void { $this->jobs = []; } + + /** + * @return list + */ + private function filterJobs(string $name, ?\Closure $callback = null): array + { + $jobs = $this->jobs[$name] ?? []; + + $callback = $callback ?: static function (array $data): bool { + return true; + }; + + return \array_filter($jobs, static function (array $data) use ($callback) { + return $callback($data); + }); + } } diff --git a/src/Queue/FakeQueueManager.php b/src/Queue/FakeQueueManager.php index 5196c98..a8cc693 100644 --- a/src/Queue/FakeQueueManager.php +++ b/src/Queue/FakeQueueManager.php @@ -17,9 +17,8 @@ class FakeQueueManager implements QueueConnectionProviderInterface public function __construct( private readonly Container $container, - private readonly QueueConfig $config - ) { - } + private readonly QueueConfig $config, + ) {} public function getConnection(?string $name = null): FakeQueue { @@ -36,15 +35,15 @@ public function getConnection(?string $name = null): FakeQueue return $this->connections[$name] = $this->container->make(FakeQueue::class, $config); } - private function getDefaultDriver(): string - { - return $this->config->getDefaultDriver(); - } - public function clearAll(): void { foreach ($this->connections as $connection) { $connection->clear(); } } + + private function getDefaultDriver(): string + { + return $this->config->getDefaultDriver(); + } } diff --git a/src/Session/FakeSession.php b/src/Session/FakeSession.php index 92152b4..aba5954 100644 --- a/src/Session/FakeSession.php +++ b/src/Session/FakeSession.php @@ -16,7 +16,7 @@ public function __construct( private array $data, private readonly string $clientSignature, private readonly int $lifetime = 3600, - ?string $id = null + ?string $id = null, ) { if (! empty($id) && $this->validID($id)) { $this->id = $id; @@ -40,7 +40,7 @@ public function resume(): void $this->data['_CREATED'] = time(); } - $this->id = 'session-'.time(); + $this->id = 'session-' . time(); $this->started = true; } @@ -55,7 +55,7 @@ public function regenerateID(): SessionInterface $this->resume(); $this->data['_CREATED'] = time(); - $this->id = 'session-'.time(); + $this->id = 'session-' . time(); $this->resume(); @@ -98,10 +98,11 @@ public function destroy(): bool return $this->commit(); } - public function getSection(string $name = null): SessionSectionInterface + public function getSection(?string $name = null): SessionSectionInterface { return new FakeSessionSection( - $name ?? '_DEFAULT', $this->data[$name] ?? [] + $name ?? '_DEFAULT', + $this->data[$name] ?? [], ); } diff --git a/src/Session/FakeSessionSection.php b/src/Session/FakeSessionSection.php index 8b00afd..f014a84 100644 --- a/src/Session/FakeSessionSection.php +++ b/src/Session/FakeSessionSection.php @@ -10,29 +10,8 @@ class FakeSessionSection implements SessionSectionInterface { public function __construct( private readonly string $name, - private array $data - ) { - } - - public function __get(string $name) - { - return $this->get($name); - } - - public function __set(string $name, $value): void - { - $this->set($name, $value); - } - - public function __isset(string $name) - { - return $this->has($name); - } - - public function __unset(string $name): void - { - $this->delete($name); - } + private array $data, + ) {} public function getIterator(): \ArrayIterator { @@ -105,4 +84,24 @@ public function clear(): void { $this->data = []; } + + public function __get(string $name) + { + return $this->get($name); + } + + public function __set(string $name, $value): void + { + $this->set($name, $value); + } + + public function __isset(string $name) + { + return $this->has($name); + } + + public function __unset(string $name): void + { + $this->delete($name); + } } diff --git a/src/Storage/FakeBucket.php b/src/Storage/FakeBucket.php index a749c17..1779c95 100644 --- a/src/Storage/FakeBucket.php +++ b/src/Storage/FakeBucket.php @@ -17,18 +17,11 @@ class FakeBucket extends Bucket private array $moved = []; private array $visibility = []; - private function filterFiles(array $files, \Closure $callback): array - { - return \array_filter($files, static function (array $data) use ($callback) { - return $callback($data); - }); - } - public function assertExists(string $pathname): void { TestCase::assertTrue( $this->file($pathname)->exists(), - \sprintf('The expected [%s] files is not exist.', $pathname) + \sprintf('The expected [%s] files is not exist.', $pathname), ); } @@ -36,31 +29,31 @@ public function assertNotExist(string $pathname): void { TestCase::assertFalse( $this->file($pathname)->exists(), - \sprintf('The unexpected [%s] files is exist.', $pathname) + \sprintf('The unexpected [%s] files is exist.', $pathname), ); } public function assertCreated(string $pathname): void { - $files = $this->filterFiles($this->created, function (array $data) use($pathname) { + $files = $this->filterFiles($this->created, function (array $data) use ($pathname) { return $data['pathname'] === $pathname; }); TestCase::assertTrue( \count($files) > 0, - \sprintf('The expected [%s] files was not created.', $pathname) + \sprintf('The expected [%s] files was not created.', $pathname), ); } public function assertNotCreated(string $pathname): void { - $files = $this->filterFiles($this->created, function (array $data) use($pathname) { + $files = $this->filterFiles($this->created, function (array $data) use ($pathname) { return $data['pathname'] === $pathname; }); TestCase::assertTrue( \count($files) === 0, - \sprintf('The expected [%s] files was created.', $pathname) + \sprintf('The expected [%s] files was created.', $pathname), ); } @@ -84,25 +77,25 @@ public function write(string $pathname, $content, array $config = []): FileInter public function assertVisibilityChanged(string $pathname): void { - $files = $this->filterFiles($this->visibility, function (array $data) use($pathname) { + $files = $this->filterFiles($this->visibility, function (array $data) use ($pathname) { return $data['pathname'] === $pathname; }); TestCase::assertTrue( \count($files) > 0, - \sprintf('The expected [%s] files visibility was not changed.', $pathname) + \sprintf('The expected [%s] files visibility was not changed.', $pathname), ); } public function assertVisibilityNotChanged(string $pathname): void { - $files = $this->filterFiles($this->visibility, function (array $data) use($pathname) { + $files = $this->filterFiles($this->visibility, function (array $data) use ($pathname) { return $data['pathname'] === $pathname; }); TestCase::assertTrue( \count($files) === 0, - \sprintf('The expected [%s] files visibility was changed.', $pathname) + \sprintf('The expected [%s] files visibility was changed.', $pathname), ); } @@ -117,33 +110,33 @@ public function setVisibility(string $pathname, string $visibility): FileInterfa public function assertCopied(string $pathname, string $destination): void { - $files = $this->filterFiles($this->copied, function (array $data) use($pathname, $destination) { + $files = $this->filterFiles($this->copied, function (array $data) use ($pathname, $destination) { return $data['pathname'] === $pathname && $data['destination'] === $destination; }); TestCase::assertTrue( \count($files) > 0, - \sprintf('The expected [%s] files was not copied.', $pathname) + \sprintf('The expected [%s] files was not copied.', $pathname), ); } public function assertNotCopied(string $pathname, string $destination): void { - $files = $this->filterFiles($this->copied, function (array $data) use($pathname, $destination) { + $files = $this->filterFiles($this->copied, function (array $data) use ($pathname, $destination) { return $data['pathname'] === $pathname && $data['destination'] === $destination; }); TestCase::assertTrue( \count($files) === 0, - \sprintf('The expected [%s] files was copied.', $pathname) + \sprintf('The expected [%s] files was copied.', $pathname), ); } public function copy( string $pathname, string $destination, - BucketInterface $storage = null, - array $config = [] + ?BucketInterface $storage = null, + array $config = [], ): FileInterface { $file = parent::copy($pathname, $destination, $storage, $config); @@ -154,33 +147,33 @@ public function copy( public function assertMoved(string $pathname, string $destination): void { - $files = $this->filterFiles($this->moved, function (array $data) use($pathname, $destination) { + $files = $this->filterFiles($this->moved, function (array $data) use ($pathname, $destination) { return $data['pathname'] === $pathname && $data['destination'] === $destination; }); TestCase::assertTrue( \count($files) > 0, - \sprintf('The expected [%s] files was not moved.', $pathname) + \sprintf('The expected [%s] files was not moved.', $pathname), ); } public function assertNotMoved(string $pathname, string $destination): void { - $files = $this->filterFiles($this->moved, function (array $data) use($pathname, $destination) { + $files = $this->filterFiles($this->moved, function (array $data) use ($pathname, $destination) { return $data['pathname'] === $pathname && $data['destination'] === $destination; }); TestCase::assertTrue( \count($files) === 0, - \sprintf('The expected [%s] files was moved.', $pathname) + \sprintf('The expected [%s] files was moved.', $pathname), ); } public function move( string $pathname, string $destination, - BucketInterface $storage = null, - array $config = [] + ?BucketInterface $storage = null, + array $config = [], ): FileInterface { $file = parent::move($pathname, $destination, $storage, $config); @@ -191,25 +184,25 @@ public function move( public function assertDeleted(string $pathname): void { - $files = $this->filterFiles($this->deleted, function (array $data) use($pathname) { + $files = $this->filterFiles($this->deleted, function (array $data) use ($pathname) { return $data['pathname'] === $pathname; }); TestCase::assertTrue( \count($files) > 0, - \sprintf('The expected [%s] files was not deleted.', $pathname) + \sprintf('The expected [%s] files was not deleted.', $pathname), ); } public function assertNotDeleted(string $pathname): void { - $files = $this->filterFiles($this->deleted, function (array $data) use($pathname) { + $files = $this->filterFiles($this->deleted, function (array $data) use ($pathname) { return $data['pathname'] === $pathname; }); TestCase::assertTrue( \count($files) === 0, - \sprintf('The expected [%s] files was deleted.', $pathname) + \sprintf('The expected [%s] files was deleted.', $pathname), ); } @@ -219,4 +212,11 @@ public function delete(string $pathname, bool $clean = false): void $this->deleted[] = \compact('pathname', 'clean'); } + + private function filterFiles(array $files, \Closure $callback): array + { + return \array_filter($files, static function (array $data) use ($callback) { + return $callback($data); + }); + } } diff --git a/src/Storage/FakeBucketFactory.php b/src/Storage/FakeBucketFactory.php index 634818f..8127e19 100644 --- a/src/Storage/FakeBucketFactory.php +++ b/src/Storage/FakeBucketFactory.php @@ -14,21 +14,20 @@ final class FakeBucketFactory implements BucketFactoryInterface { public function __construct( - private readonly string $path - ) { - } + private readonly string $path, + ) {} public function createFromAdapter( FilesystemAdapter $adapter, - string $name = null, - UriResolverInterface $resolver = null + ?string $name = null, + ?UriResolverInterface $resolver = null, ): BucketInterface { return new FakeBucket( new Filesystem( - new LocalFilesystemAdapter($this->path . '/' . $name) + new LocalFilesystemAdapter($this->path . '/' . $name), ), $name, - $resolver + $resolver, ); } } diff --git a/src/TestApp.php b/src/TestApp.php index 6bd47ff..a508d6a 100644 --- a/src/TestApp.php +++ b/src/TestApp.php @@ -71,26 +71,26 @@ protected function mapDirectories(array $directories): array } if (! isset($directories['app'])) { - $directories['app'] = $directories['root'].'/app/'; + $directories['app'] = $directories['root'] . '/app/'; } return array_merge( [ // public root - 'public' => $directories['root'].'/public/', + 'public' => $directories['root'] . '/public/', // vendor libraries - 'vendor' => $directories['root'].'/vendor/', + 'vendor' => $directories['root'] . '/vendor/', // data directories - 'runtime' => $directories['root'].'/runtime/', - 'cache' => $directories['root'].'/runtime/cache/', + 'runtime' => $directories['root'] . '/runtime/', + 'cache' => $directories['root'] . '/runtime/cache/', // application directories - 'config' => $directories['app'].'/config/', - 'resources' => $directories['app'].'/resources/', + 'config' => $directories['app'] . '/config/', + 'resources' => $directories['app'] . '/resources/', ], - $directories + $directories, ); } } diff --git a/src/TestCase.php b/src/TestCase.php index 570c860..3b62139 100644 --- a/src/TestCase.php +++ b/src/TestCase.php @@ -4,7 +4,6 @@ namespace Spiral\Testing; -use Closure; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use PHPUnit\Framework\TestCase as BaseTestCase; use Spiral\Boot\AbstractKernel; @@ -20,30 +19,33 @@ abstract class TestCase extends BaseTestCase { - use Traits\InteractsWithConsole, - Traits\InteractsWithHttp, - Traits\InteractsWithCore, - Traits\InteractsWithFileSystem, - Traits\InteractsWithConfig, - Traits\InteractsWithDispatcher, - Traits\InteractsWithMailer, - Traits\InteractsWithQueue, - Traits\InteractsWithEvents, - Traits\InteractsWithStorage, - Traits\InteractsWithExceptions, - Traits\InteractsWithViews, - Traits\InteractsWithTranslator, - Traits\InteractsWithScaffolder, - MockeryPHPUnitIntegration; + use Traits\InteractsWithConsole; + use Traits\InteractsWithHttp; + use Traits\InteractsWithCore; + use Traits\InteractsWithFileSystem; + use Traits\InteractsWithConfig; + use Traits\InteractsWithDispatcher; + use Traits\InteractsWithMailer; + use Traits\InteractsWithQueue; + use Traits\InteractsWithEvents; + use Traits\InteractsWithStorage; + use Traits\InteractsWithExceptions; + use Traits\InteractsWithViews; + use Traits\InteractsWithTranslator; + use Traits\InteractsWithScaffolder; + use MockeryPHPUnitIntegration; public const ENV = []; public const MAKE_APP_ON_STARTUP = true; private ?TestableKernelInterface $app = null; - /** @var array */ + + /** @var array<\Closure> */ private array $beforeBooting = []; - /** @var array */ + + /** @var array<\Closure> */ private array $beforeInit = []; + private ?EnvironmentInterface $environment = null; /** @@ -80,24 +82,12 @@ public function rootDirectory(): string return dirname(__DIR__); } - protected function setUp(): void - { - parent::setUp(); - - if (static::MAKE_APP_ON_STARTUP) { - $variables = [...static::ENV, ...$this->getEnvVariablesFromConfig()]; - $this->initApp($variables); - } - - $this->setUpTraits(); - } - - public function beforeBooting(Closure $callback): void + public function beforeBooting(\Closure $callback): void { $this->beforeBooting[] = $callback; } - public function beforeInit(Closure $callback): void + public function beforeInit(\Closure $callback): void { $this->beforeInit[] = $callback; } @@ -150,7 +140,7 @@ public function makeApp(array $env = [], Container $container = new Container()) $configManager->modify( $config, - new Set($key, $attribute->closure?->__invoke() ?? $attribute->value) + new Set($key, $attribute->closure?->__invoke() ?? $attribute->value), ); } }); @@ -174,7 +164,7 @@ public function initApp(array $env = [], Container $container = new Container()) * @param array $bindings * @throws \Throwable */ - public function runScoped(Closure $callback, array $bindings = [], ?string $name = null): mixed + public function runScoped(\Closure $callback, array $bindings = [], ?string $name = null): mixed { if ($this->environment) { $bindings[EnvironmentInterface::class] = $this->environment; @@ -183,6 +173,18 @@ public function runScoped(Closure $callback, array $bindings = [], ?string $name return $this->getContainer()->runScope($bindings, $callback); } + protected function setUp(): void + { + parent::setUp(); + + if (static::MAKE_APP_ON_STARTUP) { + $variables = [...static::ENV, ...$this->getEnvVariablesFromConfig()]; + $this->initApp($variables); + } + + $this->setUpTraits(); + } + protected function tearDown(): void { parent::tearDown(); @@ -193,7 +195,6 @@ protected function tearDown(): void ->setStaticPropertyValue('container', null); } - /** * @template TClass * @@ -202,7 +203,7 @@ protected function tearDown(): void * * @return array */ - protected function getTestAttributes(string $attribute, string $method = null): array + protected function getTestAttributes(string $attribute, ?string $method = null): array { try { $methodName = $method ?? (\method_exists($this, 'name') ? $this->name() : $this->getName(false)); @@ -242,6 +243,34 @@ protected function runTest(): mixed return $result; } + private static function runScopes(array $scopes, \Closure $callback, Container $container, array $bindings): mixed + { + begin: + if ($scopes === []) { + foreach ($bindings as $key => $value) { + $container->removeBinding($key); + $container->bind($key, $value); + } + + return $container->invoke($callback); + } + + $scope = \array_shift($scopes); + if ($scope !== null && \in_array($scope, Introspector::scopeNames($container), true)) { + goto begin; + } + + $isLast = $scopes === []; + return $container->runScope( + new Scope($scope, $isLast ? $bindings : []), + $isLast + ? $callback + : static function (Container $container) use ($scopes, $callback, $bindings): mixed { + return self::runScopes($scopes, $callback, $container, $bindings); + }, + ); + } + private function runTraitSetUpOrTearDown(string $method): void { $ref = new \ReflectionClass(static::class); @@ -252,7 +281,7 @@ private function runTraitSetUpOrTearDown(string $method): void } } - while($parent = $ref->getParentClass()) { + while ($parent = $ref->getParentClass()) { foreach ($parent->getTraits() as $trait) { if (\method_exists($this, $name = $method . $trait->getShortName())) { $this->{$name}(); @@ -280,25 +309,4 @@ private function getTestScope(): ?TestScope return null; } - - private static function runScopes(array $scopes, Closure $callback, Container $container, array $bindings): mixed - { - begin: - if ($scopes === []) { - return $container->runScope($bindings, $callback); - } - - - $scope = \array_shift($scopes); - if ($scopes !== null && \in_array($scope, Introspector::scopeNames(), true)) { - goto begin; - } - - return $container->runScope( - new Scope($scope, []), - function (Container $container) use ($scopes, $callback, $bindings): mixed { - return self::runScopes($scopes, $callback, $container, $bindings); - }, - ); - } } diff --git a/src/Traits/InteractsWithConsole.php b/src/Traits/InteractsWithConsole.php index 573d496..bd5cad7 100644 --- a/src/Traits/InteractsWithConsole.php +++ b/src/Traits/InteractsWithConsole.php @@ -20,11 +20,11 @@ public function assertConsoleCommandOutputContainsStrings( string $command, array $args = [], array|string $strings = [], - ?int $verbosityLevel = null + ?int $verbosityLevel = null, ): void { $output = $this->runCommand($command, $args); - foreach ((array)$strings as $string) { + foreach ((array) $strings as $string) { $this->assertStringContainsString( $string, $output, @@ -32,8 +32,8 @@ public function assertConsoleCommandOutputContainsStrings( 'Console command [%s] with args [%s] does not contain string [%s]', $command, json_encode($args), - $string - ) + $string, + ), ); } } @@ -42,15 +42,15 @@ public function assertCommandRegistered(string $name): void { $this->assertTrue( $this->getConsole()->getApplication()->has($name), - \sprintf('Command [%s] is not registered.', $name) + \sprintf('Command [%s] is not registered.', $name), ); } final public function runCommand( string $command, array $args = [], - OutputInterface $output = null, - ?int $verbosityLevel = null + ?OutputInterface $output = null, + ?int $verbosityLevel = null, ): string { $input = new ArrayInput($args); $input->setInteractive(false); diff --git a/src/Traits/InteractsWithCore.php b/src/Traits/InteractsWithCore.php index 68268a2..e3220bc 100644 --- a/src/Traits/InteractsWithCore.php +++ b/src/Traits/InteractsWithCore.php @@ -19,7 +19,7 @@ public function assertBootloaderRegistered(string $class): void $this->assertContains( $class, $this->getRegisteredBootloaders(), - \sprintf('Bootloader [%s] was not boot.', $class) + \sprintf('Bootloader [%s] was not boot.', $class), ); } @@ -31,7 +31,7 @@ public function assertBootloaderMissed(string $class): void $this->assertNotContains( $class, $this->getRegisteredBootloaders(), - \sprintf('Bootloader [%s] was boot.', $class) + \sprintf('Bootloader [%s] was boot.', $class), ); } @@ -39,7 +39,7 @@ public function assertContainerMissed(string $alias): void { $this->assertFalse( $this->getContainer()->has($alias), - \sprintf('Container contains entry with name [%s].', $alias) + \sprintf('Container contains entry with name [%s].', $alias), ); } @@ -47,7 +47,7 @@ public function assertContainerInstantiable( string $alias, ?string $class = null, array $params = [], - ?\Closure $callback = null + ?\Closure $callback = null, ): void { $class ??= $alias; @@ -64,8 +64,8 @@ public function assertContainerInstantiable( "Container [%s] was found, but binding [%s] does not match [%s].", $alias, $class, - get_class($realObject) - ) + get_class($realObject), + ), ); if ($callback) { @@ -77,11 +77,11 @@ public function assertContainerBound( string $alias, ?string $class = null, array $params = [], - ?\Closure $callback = null + ?\Closure $callback = null, ): void { $this->assertTrue( $this->getContainer()->has($alias), - \sprintf('Container does not contain entry with name [%s].', $alias) + \sprintf('Container does not contain entry with name [%s].', $alias), ); $this->assertContainerInstantiable($alias, $class, $params, $callback); @@ -90,14 +90,14 @@ public function assertContainerBound( public function assertContainerBoundNotAsSingleton( string $alias, string $class, - array $params = [] + array $params = [], ): void { $this->assertContainerBound($alias, $class, $params); $this->assertNotSame( $this->getContainer()->make($alias, $params), $this->getContainer()->make($alias, $params), - \sprintf("Container [%s] is bound, but it contains a singleton.", $alias) + \sprintf("Container [%s] is bound, but it contains a singleton.", $alias), ); } @@ -107,7 +107,7 @@ public function assertContainerBoundAsSingleton(string $alias, string $class, ?\ $this->assertSame( $realObject, $this->getContainer()->get($alias), - \sprintf("Container [%s] is bound, but it contains not a singleton.", $alias) + \sprintf("Container [%s] is bound, but it contains not a singleton.", $alias), ); if ($callback) { @@ -127,7 +127,6 @@ public function getRegisteredBootloaders(): array /** * @param class-string $alias * @param class-string|null $interface - * @return \Mockery\MockInterface */ public function mockContainer(string $alias, ?string $interface = null): \Mockery\MockInterface { @@ -135,7 +134,7 @@ public function mockContainer(string $alias, ?string $interface = null): \Mocker $this->getContainer()->bindSingleton( $alias, - $mock = \Mockery::mock($interface ?? $alias) + $mock = \Mockery::mock($interface ?? $alias), ); return $mock; @@ -151,7 +150,7 @@ public function withEnvironment(array $env): self $current = $this->getContainer()->get(EnvironmentInterface::class)->getAll(); $this->environment = new Environment( - array_merge($current, $env) + array_merge($current, $env), ); $this->getContainer()->removeBinding(EnvironmentInterface::class); @@ -167,7 +166,7 @@ public function assertEnvironmentValueSame(string $key, mixed $value): void $this->assertSame( $currentValue, $value, - \sprintf('Current environment value for key [%s] is [%s], expected [%s].', $key, $currentValue, $value) + \sprintf('Current environment value for key [%s] is [%s], expected [%s].', $key, $currentValue, $value), ); } @@ -176,7 +175,7 @@ public function assertEnvironmentHasKey(string $key): void $this->assertArrayHasKey( $key, $this->getContainer()->get(EnvironmentInterface::class)->getAll(), - \sprintf('Environment does not have key with name [%s].', $key) + \sprintf('Environment does not have key with name [%s].', $key), ); } diff --git a/src/Traits/InteractsWithDispatcher.php b/src/Traits/InteractsWithDispatcher.php index 8250616..52d22cd 100644 --- a/src/Traits/InteractsWithDispatcher.php +++ b/src/Traits/InteractsWithDispatcher.php @@ -16,7 +16,7 @@ public function assertDispatcherCanBeServed(string $dispatcher): void { $this->assertTrue( $this->getContainer()->invoke([$dispatcher, 'canServe']), - \sprintf('Dispatcher [%s] can not be served.', $dispatcher) + \sprintf('Dispatcher [%s] can not be served.', $dispatcher), ); } @@ -27,7 +27,7 @@ public function assertDispatcherCannotBeServed(string $dispatcher): void { $this->assertFalse( $this->getContainer()->invoke([$dispatcher, 'canServe']), - \sprintf('Dispatcher [%s] can be served.', $dispatcher) + \sprintf('Dispatcher [%s] can be served.', $dispatcher), ); } @@ -56,7 +56,7 @@ public function assertDispatcherRegistered(string $dispatcher): void $this->assertContains( $dispatcher, $this->getRegisteredDispatchers(), - \sprintf('Dispatcher [%s] was not loaded.', $dispatcher) + \sprintf('Dispatcher [%s] was not loaded.', $dispatcher), ); } @@ -68,7 +68,7 @@ public function assertDispatcherMissed(string $dispatcher): void $this->assertNotContains( $dispatcher, $this->getRegisteredDispatchers(), - \sprintf('Dispatcher [%s] was loaded.', $dispatcher) + \sprintf('Dispatcher [%s] was loaded.', $dispatcher), ); } diff --git a/src/Traits/InteractsWithEvents.php b/src/Traits/InteractsWithEvents.php index 2b6d33d..5e985a3 100644 --- a/src/Traits/InteractsWithEvents.php +++ b/src/Traits/InteractsWithEvents.php @@ -16,8 +16,8 @@ public function fakeEventDispatcher(array $eventsToFake = []): FakeEventDispatch EventDispatcherInterface::class, $dispatcher = $this->getContainer()->make( FakeEventDispatcher::class, - ['eventsToFake' => $eventsToFake] - ) + ['eventsToFake' => $eventsToFake], + ), ); return $dispatcher; diff --git a/src/Traits/InteractsWithExceptions.php b/src/Traits/InteractsWithExceptions.php index 0ef17ea..01c0f55 100644 --- a/src/Traits/InteractsWithExceptions.php +++ b/src/Traits/InteractsWithExceptions.php @@ -17,13 +17,9 @@ protected function withoutExceptionHandling(): void $this->getContainer()->bind( ExceptionHandlerInterface::class, new class implements ExceptionHandlerInterface { - public function register(): void - { - } + public function register(): void {} - public function handleGlobalException(\Throwable $e): void - { - } + public function handleGlobalException(\Throwable $e): void {} public function getRenderer(?string $format = null): ?ExceptionRendererInterface { @@ -33,7 +29,7 @@ public function getRenderer(?string $format = null): ?ExceptionRendererInterface public function render( \Throwable $exception, ?Verbosity $verbosity = Verbosity::BASIC, - string $format = null, + ?string $format = null, ): string { throw $exception; } @@ -43,11 +39,11 @@ public function canRender(string $format): bool return true; } - public function report(\Throwable $exception,): void + public function report(\Throwable $exception): void { throw $exception; } - } + }, ); } diff --git a/src/Traits/InteractsWithFileSystem.php b/src/Traits/InteractsWithFileSystem.php index 82dd0b3..0e44978 100644 --- a/src/Traits/InteractsWithFileSystem.php +++ b/src/Traits/InteractsWithFileSystem.php @@ -13,7 +13,7 @@ public function assertDirectoryAliasDefined(string $name): void { $this->assertTrue( $this->getDirectories()->has($name), - \sprintf('Application directory with alias [%s] is not defined.', $name) + \sprintf('Application directory with alias [%s] is not defined.', $name), ); } @@ -30,8 +30,8 @@ public function assertDirectoryAliasMatches(string $name, string $path): void 'Application directory with alias [%s] does not match [%s]. Current path is [%s]', $name, $path, - $currentPath - ) + $currentPath, + ), ); } @@ -45,7 +45,7 @@ public function getDirectoryByAlias(string $name, ?string $path = null): string $dir = $this->getDirectories()->get($name); if ($path) { - return $dir.ltrim($path, '/'); + return $dir . ltrim($path, '/'); } return $dir; diff --git a/src/Traits/InteractsWithHttp.php b/src/Traits/InteractsWithHttp.php index beea671..318596a 100644 --- a/src/Traits/InteractsWithHttp.php +++ b/src/Traits/InteractsWithHttp.php @@ -21,7 +21,7 @@ final public function fakeHttp(): FakeHttp 'fileFactory' => $this->getFileFactory(), 'scope' => function (\Closure $closure, array $bindings = []) { return self::runScopes(['http'], $closure, $this->getContainer(), $bindings); - } + }, ]); } } diff --git a/src/Traits/InteractsWithMailer.php b/src/Traits/InteractsWithMailer.php index 6a870d1..4bfba45 100644 --- a/src/Traits/InteractsWithMailer.php +++ b/src/Traits/InteractsWithMailer.php @@ -22,7 +22,7 @@ public function fakeMailer(): FakeMailer $container->removeBinding(MailerInterface::class); $container->bindSingleton( MailerInterface::class, - $mailer = new FakeMailer() + $mailer = new FakeMailer(), ); return $mailer; diff --git a/src/Traits/InteractsWithQueue.php b/src/Traits/InteractsWithQueue.php index c998461..5c6b59a 100644 --- a/src/Traits/InteractsWithQueue.php +++ b/src/Traits/InteractsWithQueue.php @@ -22,7 +22,7 @@ public function fakeQueue(): FakeQueueManager $container->removeBinding(QueueConnectionProviderInterface::class); $container->bindSingleton( QueueConnectionProviderInterface::class, - $manager = $container->get(FakeQueueManager::class) + $manager = $container->get(FakeQueueManager::class), ); return $manager; diff --git a/src/Traits/InteractsWithScaffolder.php b/src/Traits/InteractsWithScaffolder.php index 65a74ca..2d3607d 100644 --- a/src/Traits/InteractsWithScaffolder.php +++ b/src/Traits/InteractsWithScaffolder.php @@ -27,7 +27,8 @@ public function assertScaffolderCommandSame( $root = $this->getDirectoryByAlias('root'); $this->assertSame( \str_replace($root, '', $expectedFilename), - \str_replace($root, '', $filename), 'Generated filename is not the same with expected.' + \str_replace($root, '', $filename), + 'Generated filename is not the same with expected.', ); } @@ -58,7 +59,8 @@ public function assertScaffolderCommandContains( $root = $this->getDirectoryByAlias('root'); $this->assertSame( \str_replace($root, '', $expectedFilename), - \str_replace($root, '', $filename), 'Generated filename is not the same with expected.' + \str_replace($root, '', $filename), + 'Generated filename is not the same with expected.', ); } diff --git a/src/Traits/InteractsWithStorage.php b/src/Traits/InteractsWithStorage.php index 4568903..e82da23 100644 --- a/src/Traits/InteractsWithStorage.php +++ b/src/Traits/InteractsWithStorage.php @@ -15,7 +15,7 @@ trait InteractsWithStorage public function fakeStorage(): StorageInterface { $factory = new FakeBucketFactory( - $root = $this->getDirectoryByAlias('runtime', 'testing/disks') + $root = $this->getDirectoryByAlias('runtime', 'testing/disks'), ); $this->cleanupDirectories($root); @@ -27,7 +27,7 @@ public function fakeStorage(): StorageInterface $storage->add( $name, $factory->createFromAdapter($adapter, $name, new StaticResolver(new Uri('http://127.0.0.1/public'))), - true + true, ); } diff --git a/src/Traits/InteractsWithViews.php b/src/Traits/InteractsWithViews.php index 08e9eb6..303b619 100644 --- a/src/Traits/InteractsWithViews.php +++ b/src/Traits/InteractsWithViews.php @@ -17,7 +17,7 @@ public function assertViewSame(string $path, array $data = [], string $expected { $this->assertSame( $expected, - $this->getViews()->render($path, $data) + $this->getViews()->render($path, $data), ); } @@ -25,7 +25,7 @@ public function assertViewContains(string $path, array $data = [], array|string { $result = $this->getViews()->render($path, $data); - foreach ((array)$strings as $string) { + foreach ((array) $strings as $string) { $this->assertStringContainsString($string, $result); } } @@ -34,7 +34,7 @@ public function assertViewNotContains(string $path, array $data = [], array|stri { $result = $this->getViews()->render($path, $data); - foreach ((array)$strings as $string) { + foreach ((array) $strings as $string) { $this->assertStringNotContainsString($string, $result); } } diff --git a/src/Traits/TestableKernel.php b/src/Traits/TestableKernel.php index aadacc0..6b8ec1b 100644 --- a/src/Traits/TestableKernel.php +++ b/src/Traits/TestableKernel.php @@ -11,7 +11,6 @@ trait TestableKernel { - /** @inheritDoc */ public function getContainer(): Container { $scopedContainer = ContainerScope::getContainer(); @@ -25,17 +24,22 @@ public function getContainer(): Container return $this->container; } - /** @return array> */ + /** + * @return array> + */ public function getRegisteredDispatchers(): array { - return \array_map(static fn (string|DispatcherInterface $dispatcher): string => \is_object($dispatcher) + return \array_map( + static fn(string|DispatcherInterface $dispatcher): string => \is_object($dispatcher) ? $dispatcher::class : $dispatcher, - $this->dispatchers + $this->dispatchers, ); } - /** @return array */ + /** + * @return array + */ public function getRegisteredBootloaders(): array { return $this->bootloader->getClasses(); diff --git a/tests/app/Bootloader/BlogBootloader.php b/tests/app/Bootloader/BlogBootloader.php index 8f992fb..c1c04f4 100644 --- a/tests/app/Bootloader/BlogBootloader.php +++ b/tests/app/Bootloader/BlogBootloader.php @@ -14,35 +14,32 @@ final class BlogBootloader extends Bootloader { protected const BINDINGS = [ - BlogServiceInterface::class => BlogService::class + BlogServiceInterface::class => BlogService::class, ]; - protected const SINGLETONS = [ - PostRepositoryInterface::class => [self::class, 'initPostRepository'] + PostRepositoryInterface::class => [self::class, 'initPostRepository'], ]; /** * The configuration file should be modified by an attribute BEFORE it's used in the boot method */ - public function boot(StorageConfig $config): void - { - } + public function boot(StorageConfig $config): void {} protected function initPostRepository(): PostRepositoryInterface { return new ArrayPostRepository([ [ 'title' => 'foo', - 'text' => 'bar' + 'text' => 'bar', ], [ 'title' => 'foo1', - 'text' => 'bar1' + 'text' => 'bar1', ], [ 'title' => 'foo2', - 'text' => 'bar2' - ] + 'text' => 'bar2', + ], ]); } } diff --git a/tests/app/Controller/EventDispatcherController.php b/tests/app/Controller/EventDispatcherController.php index 8e31e44..31d7057 100644 --- a/tests/app/Controller/EventDispatcherController.php +++ b/tests/app/Controller/EventDispatcherController.php @@ -12,9 +12,8 @@ class EventDispatcherController { public function __construct( - private readonly EventDispatcherInterface $eventDispatcher - ) { - } + private readonly EventDispatcherInterface $eventDispatcher, + ) {} #[Route('/dispatch/some', 'dispatch.some')] public function dispatchSome(): string diff --git a/tests/app/Controller/PostController.php b/tests/app/Controller/PostController.php index 6faa8fc..4832b87 100644 --- a/tests/app/Controller/PostController.php +++ b/tests/app/Controller/PostController.php @@ -4,7 +4,6 @@ namespace Spiral\Testing\Tests\App\Controller; -use Spiral\Http\Request\InputManager; use Spiral\Testing\Tests\App\Repositories\PostRepositoryInterface; final class PostController diff --git a/tests/app/Controller/UploadController.php b/tests/app/Controller/UploadController.php index 8bb3bf1..75d51fb 100644 --- a/tests/app/Controller/UploadController.php +++ b/tests/app/Controller/UploadController.php @@ -17,7 +17,7 @@ public function upload(StorageInterface $storage, InputManager $input): string $storage->bucket('uploads')->write( $image->getClientFilename(), - $image->getStream() + $image->getStream(), ); return $image->getClientFilename(); diff --git a/tests/app/Event/AnotherEvent.php b/tests/app/Event/AnotherEvent.php index 15dbc56..6020b91 100644 --- a/tests/app/Event/AnotherEvent.php +++ b/tests/app/Event/AnotherEvent.php @@ -7,7 +7,6 @@ class AnotherEvent { public function __construct( - public readonly string $anotherParam - ) { - } + public readonly string $anotherParam, + ) {} } diff --git a/tests/app/Event/SomeEvent.php b/tests/app/Event/SomeEvent.php index af88f3b..cbd25e9 100644 --- a/tests/app/Event/SomeEvent.php +++ b/tests/app/Event/SomeEvent.php @@ -7,7 +7,6 @@ class SomeEvent { public function __construct( - public readonly int $someParam - ) { - } + public readonly int $someParam, + ) {} } diff --git a/tests/app/Repositories/PostRepositoryInterface.php b/tests/app/Repositories/PostRepositoryInterface.php index 482e83a..16ebd8f 100644 --- a/tests/app/Repositories/PostRepositoryInterface.php +++ b/tests/app/Repositories/PostRepositoryInterface.php @@ -7,5 +7,6 @@ interface PostRepositoryInterface { public function all(): array; + public function findById(int $id): ?array; } diff --git a/tests/app/Services/BlogService.php b/tests/app/Services/BlogService.php index 1b4924b..d964243 100644 --- a/tests/app/Services/BlogService.php +++ b/tests/app/Services/BlogService.php @@ -4,7 +4,4 @@ namespace Spiral\Testing\Tests\App\Services; -final class BlogService implements BlogServiceInterface -{ - -} +final class BlogService implements BlogServiceInterface {} diff --git a/tests/app/Services/BlogServiceInterface.php b/tests/app/Services/BlogServiceInterface.php index 242ee27..c797821 100644 --- a/tests/app/Services/BlogServiceInterface.php +++ b/tests/app/Services/BlogServiceInterface.php @@ -4,7 +4,4 @@ namespace Spiral\Testing\Tests\App\Services; -interface BlogServiceInterface -{ - -} +interface BlogServiceInterface {} diff --git a/tests/src/Attribute/EnvTest.php b/tests/src/Attribute/EnvTest.php index d8184b5..3cddc76 100644 --- a/tests/src/Attribute/EnvTest.php +++ b/tests/src/Attribute/EnvTest.php @@ -13,7 +13,7 @@ final class EnvTest extends TestCase { public const ENV = [ 'FOO' => 'BAR', - 'BAZ' => 'QUX' + 'BAZ' => 'QUX', ]; public function testDefaultEnv(): void diff --git a/tests/src/Attribute/WithoutExceptionHandlingTest.php b/tests/src/Attribute/WithoutExceptionHandlingTest.php index 1b1d73f..bb17575 100644 --- a/tests/src/Attribute/WithoutExceptionHandlingTest.php +++ b/tests/src/Attribute/WithoutExceptionHandlingTest.php @@ -15,7 +15,7 @@ public function testDefaultHandler(): void { $this->assertInstanceOf( ExceptionHandler::class, - $this->getContainer()->get(ExceptionHandlerInterface::class) + $this->getContainer()->get(ExceptionHandlerInterface::class), ); } @@ -25,7 +25,7 @@ public function testSuppressWithMethod(): void $this->assertNotInstanceOf( ExceptionHandler::class, - $this->getContainer()->get(ExceptionHandlerInterface::class) + $this->getContainer()->get(ExceptionHandlerInterface::class), ); } @@ -34,7 +34,7 @@ public function testSuppressWithAttribute(): void { $this->assertNotInstanceOf( ExceptionHandler::class, - $this->getContainer()->get(ExceptionHandlerInterface::class) + $this->getContainer()->get(ExceptionHandlerInterface::class), ); } } diff --git a/tests/src/ConsoleTest.php b/tests/src/ConsoleTest.php index d5e99e1..2c79ac9 100644 --- a/tests/src/ConsoleTest.php +++ b/tests/src/ConsoleTest.php @@ -13,7 +13,6 @@ public function testRegisteredCommand(): void $this->assertCommandRegistered('foo'); } - public function testNotRegisteredCommandShouldThrowAnException(): void { $this->expectException(ExpectationFailedException::class); diff --git a/tests/src/Core/BootloaderTest.php b/tests/src/Core/BootloaderTest.php index 72b4b82..73bfb99 100644 --- a/tests/src/Core/BootloaderTest.php +++ b/tests/src/Core/BootloaderTest.php @@ -17,7 +17,7 @@ public function testPostRepositoryInterfaceBinding(): void { $this->assertContainerBoundAsSingleton( PostRepositoryInterface::class, - ArrayPostRepository::class + ArrayPostRepository::class, ); } @@ -25,7 +25,7 @@ public function testBlogServiceInterfaceBinding(): void { $this->assertContainerBound( BlogServiceInterface::class, - BlogService::class + BlogService::class, ); } @@ -33,7 +33,7 @@ public function testBlogServiceInterfaceIsNotSingleton(): void { $this->assertContainerBoundNotAsSingleton( BlogServiceInterface::class, - BlogService::class + BlogService::class, ); } @@ -43,13 +43,13 @@ public function testAssertContainerBoundAsSingletonShouldThrowAnException(): voi $this->expectExceptionMessage( \sprintf( 'Container [%s] is bound, but it contains not a singleton.', - BlogServiceInterface::class - ) + BlogServiceInterface::class, + ), ); $this->assertContainerBoundAsSingleton( BlogServiceInterface::class, - BlogService::class + BlogService::class, ); } } diff --git a/tests/src/Event/EventDispatcherTest.php b/tests/src/Event/EventDispatcherTest.php index 9df6060..c2dfe59 100644 --- a/tests/src/Event/EventDispatcherTest.php +++ b/tests/src/Event/EventDispatcherTest.php @@ -4,7 +4,6 @@ namespace Spiral\Testing\Tests\Event; -use PHPUnit\Framework\Attributes\RunInSeparateProcess; use PHPUnit\Framework\ExpectationFailedException; use Spiral\Testing\Events\FakeEventDispatcher; use Spiral\Testing\Tests\App\Event\AnotherEvent; @@ -19,14 +18,6 @@ final class EventDispatcherTest extends TestCase private \Spiral\Testing\Http\FakeHttp $http; private \Spiral\Testing\Events\FakeEventDispatcher $eventDispatcher; - protected function setUp(): void - { - parent::setUp(); - - $this->eventDispatcher = $this->fakeEventDispatcher(); - $this->http = $this->fakeHttp(); - } - public function testWithoutDispatcher(): void { $dispatcher = new FakeEventDispatcher(); @@ -111,4 +102,12 @@ public function testAssertListeningShouldThrowAnException(): void $this->eventDispatcher->assertListening(SomeEvent::class, AnotherListener::class); } + + protected function setUp(): void + { + parent::setUp(); + + $this->eventDispatcher = $this->fakeEventDispatcher(); + $this->http = $this->fakeHttp(); + } } diff --git a/tests/src/Http/FakeHttpTest.php b/tests/src/Http/FakeHttpTest.php index cded893..2f50c24 100644 --- a/tests/src/Http/FakeHttpTest.php +++ b/tests/src/Http/FakeHttpTest.php @@ -66,12 +66,12 @@ public function testGetJsonParsedBody(): void $http = $this->fakeHttp(); $arr = [ 'foo' => 'bar', - 'list' => [1, 2, 3, 4] + 'list' => [1, 2, 3, 4], ]; $response = $http->get('/get/query-params', $arr); self::assertSame( $arr, - $response->getJsonParsedBody() + $response->getJsonParsedBody(), ); } diff --git a/tests/src/Scaffolder/ScaffolderTest.php b/tests/src/Scaffolder/ScaffolderTest.php index db65638..1acbb4f 100644 --- a/tests/src/Scaffolder/ScaffolderTest.php +++ b/tests/src/Scaffolder/ScaffolderTest.php @@ -87,7 +87,7 @@ public function testCreateCommandWithAdditionalOptions(): void ], expectedStrings: [ "#[Option(description: 'Argument description')]", - 'private bool $foo;' + 'private bool $foo;', ], ); } @@ -99,7 +99,7 @@ public function testAfterTestFilesShoulBeRestored(): void $this->assertScaffolderCommandContains( 'create:command', [ - 'name' => 'TestCommand' + 'name' => 'TestCommand', ], expectedStrings: ['final class TestCommand extends Command'], ); diff --git a/tests/src/Storage/StorageBucketFakerTest.php b/tests/src/Storage/StorageBucketFakerTest.php index 64a0c19..687086f 100644 --- a/tests/src/Storage/StorageBucketFakerTest.php +++ b/tests/src/Storage/StorageBucketFakerTest.php @@ -10,13 +10,6 @@ final class StorageBucketFakerTest extends TestCase { private \Spiral\Storage\StorageInterface $storage; - protected function setUp(): void - { - parent::setUp(); - - $this->storage = $this->fakeStorage(); - } - public function testWrite(): void { if (! function_exists('imagecreatetruecolor')) { @@ -41,4 +34,11 @@ public function testWrite(): void $public->assertExists('file.txt'); $public->assertNotExist('image.jpg'); } + + protected function setUp(): void + { + parent::setUp(); + + $this->storage = $this->fakeStorage(); + } } diff --git a/tests/src/TestCase.php b/tests/src/TestCase.php index 7ed25ad..c2c7f7b 100644 --- a/tests/src/TestCase.php +++ b/tests/src/TestCase.php @@ -6,7 +6,7 @@ abstract class TestCase extends \Spiral\Testing\TestCase { public function rootDirectory(): string { - return __DIR__.'/../'; + return __DIR__ . '/../'; } public function defineBootloaders(): array diff --git a/tests/src/TestCase/Fixture/OtherParentClass.php b/tests/src/TestCase/Fixture/OtherParentClass.php index 51f5bb6..24adbdc 100644 --- a/tests/src/TestCase/Fixture/OtherParentClass.php +++ b/tests/src/TestCase/Fixture/OtherParentClass.php @@ -4,6 +4,4 @@ namespace Spiral\Testing\Tests\TestCase\Fixture; -class OtherParentClass extends ParentClass -{ -} +class OtherParentClass extends ParentClass {} diff --git a/tests/src/TestCase/Fixture/WithMethodsInNestedParent.php b/tests/src/TestCase/Fixture/WithMethodsInNestedParent.php index ed730bf..e380d37 100644 --- a/tests/src/TestCase/Fixture/WithMethodsInNestedParent.php +++ b/tests/src/TestCase/Fixture/WithMethodsInNestedParent.php @@ -4,6 +4,4 @@ namespace Spiral\Testing\Tests\TestCase\Fixture; -final class WithMethodsInNestedParent extends OtherParentClass -{ -} +final class WithMethodsInNestedParent extends OtherParentClass {} diff --git a/tests/src/TestCase/Fixture/WithMethodsInParent.php b/tests/src/TestCase/Fixture/WithMethodsInParent.php index f37c96b..b2504d5 100644 --- a/tests/src/TestCase/Fixture/WithMethodsInParent.php +++ b/tests/src/TestCase/Fixture/WithMethodsInParent.php @@ -4,6 +4,4 @@ namespace Spiral\Testing\Tests\TestCase\Fixture; -final class WithMethodsInParent extends ParentClass -{ -} +final class WithMethodsInParent extends ParentClass {} diff --git a/tests/src/TestCase/Fixture/WithoutTraits.php b/tests/src/TestCase/Fixture/WithoutTraits.php index f906f07..9234595 100644 --- a/tests/src/TestCase/Fixture/WithoutTraits.php +++ b/tests/src/TestCase/Fixture/WithoutTraits.php @@ -6,6 +6,4 @@ use Spiral\Testing\TestCase; -final class WithoutTraits extends TestCase -{ -} +final class WithoutTraits extends TestCase {} diff --git a/tests/src/Traits/InteractsWithMailerTest.php b/tests/src/Traits/InteractsWithMailerTest.php index 0d15d64..46ecc4d 100644 --- a/tests/src/Traits/InteractsWithMailerTest.php +++ b/tests/src/Traits/InteractsWithMailerTest.php @@ -28,15 +28,14 @@ public function test(): void self::assertSame($mailer, $mailer2); } - private function getSomeService(Container $container):object + private function getSomeService(Container $container): object { - return new class ($container) { + return new class($container) { use InteractsWithMailer; public function __construct( - private readonly Container $container - ) { - } + private readonly Container $container, + ) {} public function getContainer(): Container { diff --git a/tests/src/Traits/InteractsWithQueueTest.php b/tests/src/Traits/InteractsWithQueueTest.php index 60b4a12..1221773 100644 --- a/tests/src/Traits/InteractsWithQueueTest.php +++ b/tests/src/Traits/InteractsWithQueueTest.php @@ -31,15 +31,14 @@ public function test(): void self::assertSame($queue, $queue2); } - private function getSomeService(Container $container):object + private function getSomeService(Container $container): object { - return new class ($container) { + return new class($container) { use InteractsWithQueue; public function __construct( - private readonly Container $container - ) { - } + private readonly Container $container, + ) {} public function getContainer(): Container { From 8b6e472d392519684490b2c5761da6ddff0df451 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Thu, 12 Dec 2024 23:13:28 +0400 Subject: [PATCH 4/4] Integrate psalm --- .github/workflows/psalm.yml | 15 + psalm-baseline.xml | 477 ++++++++++++++++++++++++++++ psalm.xml | 22 ++ src/Traits/InteractsWithConsole.php | 1 + 4 files changed, 515 insertions(+) create mode 100644 .github/workflows/psalm.yml create mode 100644 psalm-baseline.xml create mode 100644 psalm.xml diff --git a/.github/workflows/psalm.yml b/.github/workflows/psalm.yml new file mode 100644 index 0000000..4c1f9dd --- /dev/null +++ b/.github/workflows/psalm.yml @@ -0,0 +1,15 @@ +on: + pull_request: null + push: + branches: + - master + - '*.*' + +name: static analysis + +jobs: + psalm: + uses: spiral/gh-actions/.github/workflows/psalm.yml@master + with: + os: >- + ['ubuntu-latest'] diff --git a/psalm-baseline.xml b/psalm-baseline.xml new file mode 100644 index 0000000..ba8fe60 --- /dev/null +++ b/psalm-baseline.xml @@ -0,0 +1,477 @@ + + + + + + + + + + + + + + + + + + + + + + eventDispatcher?->dispatch($event)]]> + + + + + + + + + + + + + + + + + + + + scope)($handler, $bindings)]]> + + + + + + + + + + + + binder]]> + + + + + + + + + + container->get(Http::class)]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + response->getBody(), + true, + )]]> + + + cookies]]> + ]]> + + + + + + + + + + + + + + + + + + + + + + + + + ]]> + + + jobs]]> + + + + + + + + connections[$name]]]> + + + connections[$name]]]> + + + + + + + + + connections]]> + + + connections[$name] = $this->container->make(FakeQueue::class, $config)]]> + + + + + + + + data[$name] ?? []]]> + + + + id)]]> + + + + + + + + + + + + + + + + + + + + + + + + data[$this->name][$name]]]> + + + + + + get($offset)]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + bootloaders !== null + ? $this->bootloaders + : parent::defineBootloaders()]]> + + + + + + + + + + + + + + defineBootloaders()]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + app]]> + + + makeApp($env, $container)]]> + + + + + + + + + + + + + + + + + + + + + + defaultVerbosityLevel]]> + + + + + + + + + + + + + + getRegisteredBootloaders()]]> + getRegisteredBootloaders()]]> + + + ]]> + + + + + + + + + + + + + + ]]> + + + ]]> + + + + + + + + + + + getContainer()->get(KernelInterface::class)->getRegisteredDispatchers()]]> + + + + + + + + + + + + + []]]> + + + + + + + + getContainer()->make( + FakeEventDispatcher::class, + ['eventsToFake' => $eventsToFake], + )]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + >]]> + + + + + + + + + + >]]> + + + diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..93b44db --- /dev/null +++ b/psalm.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + diff --git a/src/Traits/InteractsWithConsole.php b/src/Traits/InteractsWithConsole.php index bd5cad7..35f3049 100644 --- a/src/Traits/InteractsWithConsole.php +++ b/src/Traits/InteractsWithConsole.php @@ -55,6 +55,7 @@ final public function runCommand( $input = new ArrayInput($args); $input->setInteractive(false); $output = $output ?? new BufferedOutput(); + /** @psalm-suppress ArgumentTypeCoercion */ $output->setVerbosity($verbosityLevel ?? $this->defaultVerbosityLevel); $this->getConsole()->run($command, $input, $output);