diff --git a/.editorconfig b/.editorconfig index bc5ea30..d1ed229 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,44 +5,26 @@ root = true [*] charset = utf-8 +end_of_line = lf indent_size = 4 indent_style = space -end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true [*.js] indent_size = 2 -[*.json] -indent_size = 2 - -[*.json.dist] -indent_size = 2 - -[*.json5] -indent_size = 2 - -[*.json5.dist] +[*.{json,json.dist}] indent_size = 2 [*.md] trim_trailing_whitespace = false -[*.neon] -indent_size = 2 - -[*.neon.dist] -indent_size = 2 - -[*.xml] -indent_size = 2 - -[*.xml.dist] +[*.{neon,neon.dist}] indent_size = 2 -[*.yml] +[*.{xml,xml.dist}] indent_size = 2 -[*.yml.dist] +[*.{yml,yml.dist}] indent_size = 2 diff --git a/.gitattributes b/.gitattributes index c356234..a2e945a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,7 +1,7 @@ # Path-based git attributes # https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html -# Ignore all test and documentation with "export-ignore". +# Ignore tests and tooling with "export-ignore" /.github/ export-ignore /tests/ export-ignore /.editorconfig export-ignore diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 491957b..4a2db91 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -49,7 +49,7 @@ Before submitting a pull request: ## Requirements -- *[PSR-12 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-12-extended-coding-style-guide.md)* where possible - falling back to [PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) otherwise - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://github.com/PHPCSStandards/PHP_CodeSniffer). +- *[PSR-12 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-12-extended-coding-style-guide.md)* where possible - falling back to [PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) otherwise - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://github.com/squizlabs/PHP_CodeSniffer). - **Add tests!** - Your patch won't be accepted if it doesn't have tests. diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 71778b0..5acfea9 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -1,4 +1,4 @@ -name: run-tests +name: "run-tests" on: push: @@ -8,31 +8,33 @@ on: schedule: - cron: "0 0 * * 0" -permissions: - contents: read - jobs: all_tests: name: "PHP${{ matrix.php }} TB${{ matrix.testbench }} ${{ matrix.os-title }} ${{ matrix.dependency-prefer-title }}" runs-on: "${{ matrix.os }}" + permissions: + contents: "read" strategy: fail-fast: true matrix: os: [ "ubuntu-latest", "macos-latest", "windows-latest" ] - php: [ "8.3", "8.2", "8.1", "8.0" ] - testbench: [ "^8.0", "^7.0", "^6.26", "^6.0" ] + php: [ "8.4", "8.3", "8.2", "8.1", "8.0" ] + testbench: [ "^9.0", "^8.0", "^7.0", "^6.26", "^6.0" ] dependency-prefer: [ "prefer-stable", "prefer-lowest" ] include: + - php: "8.4" + phpunit: "^10.5.44 | ^11.0" + phpunit-config-file: "phpunit.github-actions.xml.dist" - php: "8.3" - phpunit: "^10.1.0" + phpunit: "^10.5.44 | ^11.0" phpunit-config-file: "phpunit.github-actions.xml.dist" - php: "8.2" - phpunit: "^10.1.0" + phpunit: "^10.5.44 | ^11.0" phpunit-config-file: "phpunit.github-actions.xml.dist" - php: "8.1" - phpunit: "^10.1.0" + phpunit: "^10.5.44" phpunit-config-file: "phpunit.github-actions.xml.dist" - php: "8.0" phpunit: "^9.3" @@ -62,11 +64,25 @@ jobs: dependency-prefer-title: "stable" - dependency-prefer: "prefer-lowest" dependency-prefer-title: "lowest" + exclude: + - testbench: "^9.0" + php: "8.1" + - testbench: "^9.0" + php: "8.0" + +# - testbench: "^8.0" +# php: "8.4" +# - testbench: "^8.0" +# php: "8.3" - testbench: "^8.0" php: "8.0" + - testbench: "^6.26" # Laravel 8 for higher versions of PHP php: "8.0" + + - testbench: "^6.0" # Laravel 8 for lower versions of PHP + php: "8.4" - testbench: "^6.0" # Laravel 8 for lower versions of PHP php: "8.3" - testbench: "^6.0" # Laravel 8 for lower versions of PHP @@ -78,45 +94,46 @@ jobs: - name: "Checkout code" uses: "actions/checkout@v4" - - name: "Validate composer.json and composer.lock" - run: "composer validate --strict" - - name: "Setup PHP" uses: "shivammathur/setup-php@v2" with: php-version: "${{ matrix.php }}" - extensions: fileinfo # required by league/flysystem on Windows + extensions: "fileinfo" # required by league/flysystem on Windows ini-values: "error_reporting=E_ALL" - coverage: none + coverage: "none" env: COMPOSER_TOKEN: "${{ secrets.GITHUB_TOKEN }}" + - name: "Validate composer.json and composer.lock" + run: "composer validate --strict" + # find composer's cache directory - so we know which directory to cache in the next step - name: "Find composer's cache directory" id: "composer-cache" - shell: bash # make sure this step works on Windows - see https://github.com/actions/runner/issues/2224#issuecomment-1289533957 + shell: "bash" # make sure this step works on Windows - see https://github.com/actions/runner/issues/2224#issuecomment-1289533957 run: | echo "composer_cache_dir=$(composer config cache-files-dir)">> "$GITHUB_OUTPUT" - name: "Cache composer's cache directory" - uses: "actions/cache@v3" + uses: "actions/cache@v4" with: path: "${{ steps.composer-cache.outputs.composer_cache_dir }}" key: "[${{ matrix.os }}][php-${{ matrix.php }}][testbench-${{ matrix.testbench }}][${{ matrix.dependency-prefer }}][composer.json-${{ hashFiles('composer.json') }}]" - name: "Install dependencies" - uses: "nick-fields/retry@v2" + uses: "nick-fields/retry@v3" with: timeout_minutes: 5 max_attempts: 5 - shell: bash # make sure "^" characters are interpreted properly on Windows (e.g. in "^5.0") + shell: "bash" # make sure "^" characters are interpreted properly on Windows (e.g. in "^5.0") command: | composer remove "infection/infection" --dev --no-interaction --no-update - composer remove "phpstan/phpstan" --dev --no-interaction --no-update - composer remove "squizlabs/php_codesniffer" --dev --no-interaction --no-update composer require "orchestra/testbench:${{ matrix.testbench }}" --dev --no-interaction --no-update + composer remove "phpstan/phpstan" --dev --no-interaction --no-update + composer remove "phpstan/phpstan-strict-rules" --dev --no-interaction --no-update composer require "phpunit/phpunit:${{ matrix.phpunit }}" --dev --no-interaction --no-update + composer remove "squizlabs/php_codesniffer" --dev --no-interaction --no-update composer update --${{ matrix.dependency-prefer }} --prefer-dist --no-interaction --optimize-autoloader --no-progress - name: "Execute tests" - run: vendor/bin/phpunit --configuration=${{ matrix.phpunit-config-file }} --no-coverage --stop-on-error --stop-on-failure + run: vendor/bin/phpunit "--configuration=${{ matrix.phpunit-config-file }}" --no-coverage --stop-on-error --stop-on-failure diff --git a/.gitignore b/.gitignore index a7ca0c0..e7b2fe6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .idea/ .phpunit/ .phpunit.cache/ +.vscode/ build/ infection/ phpunit/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 638674c..8350670 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,18 @@ All notable changes to `code-distortion/clarity-context` will be documented in this file. -The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [0.1.0] - 2023-12-31 +## [0.1.1] - 2024-02-05 + +### Added +- Added support for PHP 8.4 + + + +## [0.1.0] - 2023-12-31 ### Added - Initial release diff --git a/LICENSE.md b/LICENSE.md index 7d3f620..b600db2 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024 Tim Chandler +Copyright (c) 2025 Tim Chandler Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index a818c7d..89d07d1 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # Clarity Context - Understand Your Exceptions [![Latest Version on Packagist](https://img.shields.io/packagist/v/code-distortion/clarity-context.svg?style=flat-square)](https://packagist.org/packages/code-distortion/clarity-context) -![PHP Version](https://img.shields.io/badge/PHP-8.0%20to%208.3-blue?style=flat-square) -![Laravel](https://img.shields.io/badge/laravel-8%20to%2010-blue?style=flat-square) -[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/code-distortion/clarity-context/run-tests.yml?branch=master&style=flat-square)](https://github.com/code-distortion/clarity-context/actions) +![PHP Version](https://img.shields.io/badge/PHP-8.0%20to%208.4-blue?style=flat-square) +![Laravel](https://img.shields.io/badge/laravel-8%20to%2011-blue?style=flat-square) +[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/code-distortion/clarity-context/run-tests.yml?branch=main&style=flat-square)](https://github.com/code-distortion/clarity-context/actions) [![Buy The World a Tree](https://img.shields.io/badge/treeware-%F0%9F%8C%B3-lightgreen?style=flat-square)](https://plant.treeware.earth/code-distortion/clarity-context) [![Contributor Covenant](https://img.shields.io/badge/contributor%20covenant-v2.1%20adopted-ff69b4.svg?style=flat-square)](.github/CODE_OF_CONDUCT.md) @@ -302,7 +302,7 @@ You can retrieve the following details from the Meta objects: ``` php // all Meta classes -$meta->getFile(); // the relevant file +$meta->getFile(); // the relevant file $meta->getProjectFile(); // the same file, but relative to the project-root's dir $meta->getLine(); // the relevant line number $meta->getFunction(); // the function or method being run at the time @@ -346,7 +346,7 @@ $context->getCallStack()->getMetaGroups(); Each MetaGroup contains similar details to the `Frame` object. ``` php -$metaGroup->getFile(); // the path to the file containing the code being run +$metaGroup->getFile(); // the path to the file containing the code being run $metaGroup->getProjectFile(); // the same file, but relative to the project-root's dir $metaGroup->getLine(); // the relevant line number $metaGroup->getFunction(); // the function or method being run at the time diff --git a/composer.json b/composer.json index abae055..496405f 100644 --- a/composer.json +++ b/composer.json @@ -21,15 +21,15 @@ } ], "require": { - "php": "8.0.* | 8.1.* | 8.2.* | 8.3.*", - "code-distortion/staticall": "^0.1.0" + "php": "8.0.* | 8.1.* | 8.2.* | 8.3.* | 8.4.*" }, "require-dev": { - "infection/infection": "^0.10 | ^0.11 | ^0.12 | ^0.13 | ^0.14 | ^0.15 | ^0.16 | ^0.17 | ^0.18 | ^0.19 | ^0.20 | ^0.21 | ^0.22 | ^0.23 | ^0.24 | ^0.25 | ^0.26 | ^0.27", - "orchestra/testbench": "^6.12 | ^7.0 | ^8.0", - "phpstan/phpstan": "^0.9 | ^0.10 | ^0.11 | ^0.12 | ^1.0", - "phpunit/phpunit": "~4.8 | ^5.0 | ^6.0 | ^7.0 | ^8.4 | ^9.0 | ^10.0", - "squizlabs/php_codesniffer": "^3.8.0" + "infection/infection": "^0.26 | ^0.27 | ^0.28 | ^0.29", + "orchestra/testbench": "^6.12 | ^7.0 | ^8.0 | ^9.0", + "phpstan/phpstan": "^2.0", + "phpstan/phpstan-strict-rules": "^2.0", + "phpunit/phpunit": "^9.6.22 | ^10.0 | ^11.0", + "squizlabs/php_codesniffer": "^3.11.2" }, "autoload": { "psr-4": { diff --git a/infection.json.dist b/infection.json.dist index ab322be..d26bbc0 100644 --- a/infection.json.dist +++ b/infection.json.dist @@ -3,7 +3,7 @@ "source": { "directories": [ "src" - ], + ] "excludes": [ "src/ServiceProvider.php" ] @@ -14,7 +14,7 @@ "html": "infection/infection.html", "summary": "infection/summary.log", "json": "infection/infection-log.json", - "perMutator": "infection/per-mutator.md", + "perMutator": "infection/per-mutator.md" }, "mutators": { "@default": true diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 537f123..f5abb38 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,3 +1,5 @@ +includes: + - vendor/phpstan/phpstan-strict-rules/rules.neon parameters: paths: - src/ @@ -5,3 +7,5 @@ parameters: level: max parallel: processTimeout: 300.0 + excludePaths: + - /**/ManualTest.php diff --git a/phpunit.github-actions.up-to-9.xml.dist b/phpunit.github-actions.up-to-9.xml.dist index a7f0698..6370bc5 100644 --- a/phpunit.github-actions.up-to-9.xml.dist +++ b/phpunit.github-actions.up-to-9.xml.dist @@ -2,7 +2,7 @@ - ./tests/Unit + ./tests/Unit/ - ./tests/Integration + ./tests/Integration/ diff --git a/phpunit.github-actions.xml.dist b/phpunit.github-actions.xml.dist index c2947ea..0678f68 100644 --- a/phpunit.github-actions.xml.dist +++ b/phpunit.github-actions.xml.dist @@ -2,7 +2,7 @@ - ./tests/Unit + ./tests/Unit/ - ./tests/Integration + ./tests/Integration/ - ./src + ./src/ diff --git a/phpunit.xml.dist b/phpunit.xml.dist index bd6f0a5..4fddaec 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,7 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" backupGlobals="true" - backupStaticProperties="false" + backupStaticProperties="true" beStrictAboutChangesToGlobalState="false" beStrictAboutOutputDuringTests="true" beStrictAboutTestsThatDoNotTestAnything="true" @@ -21,12 +21,14 @@ displayDetailsOnSkippedTests="true" enforceTimeLimit="false" failOnDeprecation="true" - failOnIncomplete="false" + failOnIncomplete="true" failOnEmptyTestSuite="true" failOnNotice="true" failOnRisky="true" failOnSkipped="true" failOnWarning="true" + processIsolation="false" + requireCoverageMetadata="false" stopOnDefect="true" stopOnDeprecation="true" stopOnError="true" @@ -39,20 +41,20 @@ > - ./tests/Unit + ./tests/Unit/ - ./tests/Integration + ./tests/Integration/ - + - ./src + ./src/ diff --git a/src/Support/Framework/DepInjection/LaravelDepInjection.php b/src/Support/Framework/DepInjection/LaravelDepInjection.php index f7d735e..9b0cff7 100644 --- a/src/Support/Framework/DepInjection/LaravelDepInjection.php +++ b/src/Support/Framework/DepInjection/LaravelDepInjection.php @@ -10,6 +10,10 @@ */ class LaravelDepInjection implements FrameworkDepInjectionInterface { +// private static array $cache = []; + + + /** * Get a value (or class instance) using the dependency container. * @@ -19,6 +23,15 @@ class LaravelDepInjection implements FrameworkDepInjectionInterface */ public static function get(string $key, mixed $default = null): mixed { +// if (isset(self::$cache[$key])) { +// return self::$cache[$key]; +// } +// return is_callable($default) +// ? $default() +// : $default; + + + /** @var Application $app */ $app = app(); @@ -43,6 +56,15 @@ public static function get(string $key, mixed $default = null): mixed */ public static function getOrSet(string $key, mixed $default): mixed { +// if (!isset(self::$cache[$key])) { +// self::$cache[$key] = is_callable($default) +// ? $default() +// : $default; +// } +// return self::$cache[$key]; + + + /** @var Application $app */ $app = app(); @@ -75,6 +97,11 @@ public static function getOrSet(string $key, mixed $default): mixed */ public static function set(string $key, mixed $value): void { +// self::$cache[$key] = $value; +// return; + + + /** @var Application $app */ $app = app(); @@ -108,6 +135,10 @@ public static function make(string $abstract, array $parameters = []): mixed */ public static function call(callable $callable, array $parameters = []): mixed { +// return call_user_func_array($callable, $parameters); + + + /** @var Application $app */ $app = app(); return $app->call($callable, $parameters); diff --git a/tests/Integration/ClarityIntegrationTest.php b/tests/Integration/ClarityIntegrationTest.php index f3bf0a2..ad11311 100644 --- a/tests/Integration/ClarityIntegrationTest.php +++ b/tests/Integration/ClarityIntegrationTest.php @@ -6,6 +6,7 @@ use CodeDistortion\ClarityContext\Support\CallStack\MetaData\ContextMeta; use CodeDistortion\ClarityContext\Tests\LaravelTestCase; use Exception; +use PHPUnit\Framework\Attributes\Test; /** * Test the Clarity class. @@ -22,6 +23,7 @@ class ClarityIntegrationTest extends LaravelTestCase * @return void * @throws Exception Doesn't throw this, but phpcs expects this to be here. */ + #[Test] public static function test_that_meta_data_is_remembered_when_calling_call_user_func_array() { call_user_func_array([new Clarity(), 'context'], ['something']); diff --git a/tests/Integration/Support/API/ContextAPIIntegrationTest.php b/tests/Integration/Support/API/ContextAPIIntegrationTest.php index cb99f6e..d7d1af7 100644 --- a/tests/Integration/Support/API/ContextAPIIntegrationTest.php +++ b/tests/Integration/Support/API/ContextAPIIntegrationTest.php @@ -9,6 +9,7 @@ use CodeDistortion\ClarityContext\Tests\TestSupport\LaravelConfigHelper; use CodeDistortion\ClarityContext\Tests\TestSupport\PHPStackTraceHelper; use CodeDistortion\ClarityContext\Tests\TestSupport\SimulateControlPackage; +use PHPUnit\Framework\Attributes\Test; /** * Test the ContextAPI class. @@ -25,6 +26,7 @@ class ContextAPIIntegrationTest extends LaravelTestCase * * @return void */ + #[Test] public static function test_build_context_method_for_php_stack_trace_and_check_output_context_object(): void { // add some config settings to make sure they're picked up later @@ -73,6 +75,7 @@ public static function test_build_context_method_for_php_stack_trace_and_check_o * * @return void */ + #[Test] public static function test_build_context_method_for_an_exception_and_check_output_context_object(): void { // add some config settings to make sure they're picked up later diff --git a/tests/Integration/Support/Context/CallStack/CallStackAndFrameGenerationIntegrationTest.php b/tests/Integration/Support/Context/CallStack/CallStackAndFrameGenerationIntegrationTest.php index 33c937b..ecb8ad6 100644 --- a/tests/Integration/Support/Context/CallStack/CallStackAndFrameGenerationIntegrationTest.php +++ b/tests/Integration/Support/Context/CallStack/CallStackAndFrameGenerationIntegrationTest.php @@ -18,6 +18,7 @@ use CodeDistortion\ClarityContext\Tests\TestSupport\SimulateControlPackage; use Exception; use Illuminate\Contracts\Container\BindingResolutionException; +use PHPUnit\Framework\Attributes\Test; use Throwable; /** @@ -35,6 +36,7 @@ class CallStackAndFrameGenerationIntegrationTest extends LaravelTestCase * @return void * @throws Exception Doesn't throw this, but phpcs expects this to be here. */ + #[Test] public static function test_the_retrieval_of_meta_objects(): void { Clarity::context('ONE'); @@ -84,6 +86,7 @@ public static function test_the_retrieval_of_meta_objects(): void * * @return void */ + #[Test] public static function test_callstack_frames_and_meta_objects_when_built_for_a_thrown_exception(): void { Clarity::context('hello'); @@ -159,6 +162,7 @@ public static function test_callstack_frames_and_meta_objects_when_built_for_a_t * * @return void */ + #[Test] public static function test_callstack_frames_and_meta_objects_when_built_from_passed_exception(): void { Clarity::context('hello'); @@ -194,6 +198,7 @@ public static function test_callstack_frames_and_meta_objects_when_built_from_pa * * @return void */ + #[Test] public static function test_callstack_frames_and_meta_objects_when_not_built_from_an_exception(): void { Clarity::context('hello'); @@ -230,6 +235,7 @@ public static function test_callstack_frames_and_meta_objects_when_not_built_fro * * @return void */ + #[Test] public static function test_what_happens_when_the_project_root_cant_be_resolved(): void { $e = null; @@ -277,6 +283,7 @@ public static function test_what_happens_when_the_project_root_cant_be_resolved( * * @return void */ + #[Test] public static function test_that_meta_matches_the_frames(): void { // simulate Control running and catching an exception @@ -313,6 +320,7 @@ public static function test_that_meta_matches_the_frames(): void * @return void * @throws Exception Doesn't throw this, but phpcs expects this to be here. */ + #[Test] public static function test_exception_thrown_meta(): void { $callback = function (Context $context, Exception $e) { @@ -382,6 +390,7 @@ public static function test_exception_thrown_meta(): void * @return void * @throws Exception Doesn't throw this, but phpcs expects this to be here. */ + #[Test] public static function test_exception_caught_meta(): void { $callback = function (Context $context) { @@ -453,6 +462,7 @@ public static function test_exception_caught_meta(): void * @return void * @throws Exception Doesn't throw this, but phpcs expects this to be here. */ + #[Test] public static function test_last_application_frame_meta(): void { $callback = function (Context $context, Exception $e) { @@ -544,6 +554,7 @@ public static function test_last_application_frame_meta(): void * @return void * @throws Exception Doesn't throw this, but phpcs expects this to be here. */ + #[Test] public static function test_that_the_callstack_file_and_line_numbers_are_shifted_by_1(): void { $closure = function () { @@ -565,15 +576,21 @@ public static function test_that_the_callstack_file_and_line_numbers_are_shifted $frame->getFunction() ); self::assertSame(__FILE__, $frame->getFile()); - self::assertSame(__LINE__ + 11, $frame->getLine()); + self::assertSame(__LINE__ + 17, $frame->getLine()); + + // PHP 8.4+ includes extra information in the frame's "function" key + $compare = version_compare(PHP_VERSION, '8.4', '>=') + ? '{closure:CodeDistortion\ClarityContext\Tests\Integration\Support\Context\CallStack\CallStackAndFrame' + . 'GenerationIntegrationTest::test_that_the_callstack_file_and_line_numbers_are_shifted_by_1():560}' + : 'CodeDistortion\ClarityContext\Tests\Integration\Support\Context\CallStack\{closure}'; $frame = $frames[1]; self::assertSame( - 'CodeDistortion\ClarityContext\Tests\Integration\Support\Context\CallStack\{closure}', + $compare, $frame->getFunction() ); self::assertSame(__FILE__, $frame->getFile()); - self::assertSame(__LINE__ - 25, $frame->getLine()); + self::assertSame(__LINE__ - 31, $frame->getLine()); }; $closure(); @@ -588,6 +605,7 @@ public static function test_that_the_callstack_file_and_line_numbers_are_shifted * * @return void */ + #[Test] public static function test_that_the_last_frame_is_marked_so(): void { // simulate Control running and catching an exception diff --git a/tests/Integration/Support/MetaCallStackPruning2IntegrationTest.php b/tests/Integration/Support/MetaCallStackPruning2IntegrationTest.php index 09d7211..dbec38e 100644 --- a/tests/Integration/Support/MetaCallStackPruning2IntegrationTest.php +++ b/tests/Integration/Support/MetaCallStackPruning2IntegrationTest.php @@ -8,6 +8,7 @@ use CodeDistortion\ClarityContext\Support\CallStack\MetaData\ContextMeta; use CodeDistortion\ClarityContext\Tests\LaravelTestCase; use CodeDistortion\ClarityContext\Tests\TestSupport\SomeOtherClass; +use PHPUnit\Framework\Attributes\Test; /** * Test that the MetaCallStack class prunes frames properly. @@ -23,6 +24,7 @@ class MetaCallStackPruning2IntegrationTest extends LaravelTestCase * * @return void */ + #[Test] public static function test_frame_pruning_when_calling_an_object_method(): void { $objects = []; @@ -54,6 +56,7 @@ public static function test_frame_pruning_when_calling_an_object_method(): void * * @return void */ + #[Test] public static function test_frame_pruning_when_calling_a_class_static_method(): void { for ($count = 0; $count <= 1; $count++) { @@ -82,6 +85,7 @@ public static function test_frame_pruning_when_calling_a_class_static_method(): * * @return void */ + #[Test] public static function test_frame_pruning_when_calling_a_closure(): void { $callback = function (?string $context) { diff --git a/tests/TestSupport/MethodCall.php b/tests/TestSupport/MethodCall.php deleted file mode 100644 index dd18e91..0000000 --- a/tests/TestSupport/MethodCall.php +++ /dev/null @@ -1,67 +0,0 @@ -method = $method; - $this->args = $args; - } - - /** - * Retrieve the method. - * - * @return string - */ - public function getMethod(): string - { - return $this->method; - } - - /** - * Retrieve the arguments. - * - * @return mixed[] - */ - public function getArgs(): array - { - return $this->args; - } - - /** - * Retrieve the arguments, with arrays flattened out. - * - * @param callable|null $filterCallback The callback to filter the arguments by. - * @return mixed[] - */ - public function getArgsFlat(callable $filterCallback = null): array - { - $flatArgs = []; - foreach ($this->args as $arg) { - $arg = is_array($arg) - ? $arg - : [$arg]; - $flatArgs = array_merge($flatArgs, $arg); - } - - return !is_null($filterCallback) - ? array_filter($flatArgs, $filterCallback) - : $flatArgs; - } -} diff --git a/tests/TestSupport/MethodCalls.php b/tests/TestSupport/MethodCalls.php deleted file mode 100644 index a69bc90..0000000 --- a/tests/TestSupport/MethodCalls.php +++ /dev/null @@ -1,118 +0,0 @@ -methodCalls[] = new MethodCall($method, $args); - } - return $this; - } - - /** - * Retrieve the method calls, with an optional method name filter. - * - * @param string|string[]|null $method The method type to filter by. - * @return MethodCall[] - */ - public function getCalls(string|array $method = null): array - { - if (is_null($method)) { - return $this->methodCalls; - } - - $methods = is_array($method) - ? $method - : [$method]; - - $methodCalls = []; - foreach ($this->methodCalls as $methodCall) { - if (in_array($methodCall->getMethod(), $methods)) { - $methodCalls[] = $methodCall; - } - } - return $methodCalls; - } - - /** - * Check if any methods have been specified. - * - * @return boolean - */ - public function hasCalls(): bool - { - return count($this->methodCalls) > 0; - } - - /** - * Check if a particular method call exists. - * - * @param string $method The method type to filter by. - * @return boolean - */ - public function hasCall(string $method): bool - { - foreach ($this->methodCalls as $methodCall) { - if ($methodCall->getMethod() == $method) { - return true; - } - } - return false; - } - - /** - * Pick all the parameters of a particular method call. - * - * @param string $method The method call type to pick arguments from. - * @param callable|null $filterCallback The callback to filter the arguments by. - * @return mixed[] - */ - public function getAllCallArgsFlat(string $method, callable $filterCallback = null): array - { - return collect($this->methodCalls) - ->filter(fn(MethodCall $m) => $m->getMethod() == $method) - ->map(fn(MethodCall $m) => $m->getArgsFlat($filterCallback)) - ->flatten(1) - ->toArray(); - } -} diff --git a/tests/Unit/API/ContextAPIUnitTest.php b/tests/Unit/API/ContextAPIUnitTest.php index 438c367..1e8bdf9 100644 --- a/tests/Unit/API/ContextAPIUnitTest.php +++ b/tests/Unit/API/ContextAPIUnitTest.php @@ -11,6 +11,8 @@ use CodeDistortion\ClarityContext\Tests\TestSupport\PHPStackTraceHelper; use CodeDistortion\ClarityContext\Tests\TestSupport\SimulateControlPackage; use Exception; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; /** * Test the ContextAPI class. @@ -26,6 +28,7 @@ class ContextAPIUnitTest extends LaravelTestCase * * @return void */ + #[Test] public static function test_build_context_here_method_has_correct_values(): void { LaravelConfigHelper::updateChannelsWhenKnown(['channel-when-known']); @@ -55,6 +58,8 @@ public static function test_build_context_here_method_has_correct_values(): void * @param boolean $expectException Whether an exception is expected or not. * @return void */ + #[Test] + #[DataProvider('phpStackTraceDataProvider')] public static function test_build_context_here_method_builds_based_on_frames_back( int $framesBack, bool $expectException @@ -104,6 +109,8 @@ public static function phpStackTraceDataProvider(): array * @param boolean $catch Whether to pretend to catch the exception or not. * @return void */ + #[Test] + #[DataProvider(methodName: 'buildContextFromExceptionDataProvider')] public static function test_build_context_from_exception_method(?bool $report, bool $isKnown, bool $catch): void { LaravelConfigHelper::updateReportSetting($report); @@ -185,6 +192,7 @@ public static function buildContextFromExceptionDataProvider(): array * * @return void */ + #[Test] public static function test_the_context_remember_retrieve_and_forget_methods(): void { self::assertNull(ContextAPI::getLatestExceptionContext()); diff --git a/tests/Unit/API/DataAPIUnitTest.php b/tests/Unit/API/DataAPIUnitTest.php index b4b022d..603b893 100644 --- a/tests/Unit/API/DataAPIUnitTest.php +++ b/tests/Unit/API/DataAPIUnitTest.php @@ -5,6 +5,7 @@ use CodeDistortion\ClarityContext\API\DataAPI; use CodeDistortion\ClarityContext\Tests\LaravelTestCase; use CodeDistortion\ClarityContext\Tests\TestSupport\LaravelConfigHelper; +use PHPUnit\Framework\Attributes\Test; /** * Test the DataAPI class. @@ -22,6 +23,7 @@ class DataAPIUnitTest extends LaravelTestCase * * @return void */ + #[Test] public static function test_the_storage_and_retrieval_of_trace_identifiers_when_disabled(): void { LaravelConfigHelper::disableClarity(); @@ -38,6 +40,7 @@ public static function test_the_storage_and_retrieval_of_trace_identifiers_when_ * * @return void */ + #[Test] public static function test_the_storage_and_retrieval_of_trace_identifiers(): void { // no identifiers yet diff --git a/tests/Unit/API/MetaCallStackAPIUnitTest.php b/tests/Unit/API/MetaCallStackAPIUnitTest.php index ec0d45e..7090413 100644 --- a/tests/Unit/API/MetaCallStackAPIUnitTest.php +++ b/tests/Unit/API/MetaCallStackAPIUnitTest.php @@ -9,6 +9,7 @@ use CodeDistortion\ClarityContext\Support\Support; use CodeDistortion\ClarityContext\Tests\LaravelTestCase; use CodeDistortion\ClarityContext\Tests\TestSupport\LaravelConfigHelper; +use PHPUnit\Framework\Attributes\Test; /** * Test the MetaCallStackAPI class. @@ -24,6 +25,7 @@ class MetaCallStackAPIUnitTest extends LaravelTestCase * * @return void */ + #[Test] public static function test_push_meta_data_method_when_clarity_is_disabled(): void { LaravelConfigHelper::disableClarity(); @@ -42,6 +44,7 @@ public static function test_push_meta_data_method_when_clarity_is_disabled(): vo * * @return void */ + #[Test] public static function test_push_meta_data_method(): void { // add some meta-data @@ -91,6 +94,7 @@ public static function test_push_meta_data_method(): void * * @return void */ + #[Test] public static function test_push_meta_data_method_with_invalid_frames_back(): void { // test going back too many frames @@ -116,6 +120,7 @@ public static function test_push_meta_data_method_with_invalid_frames_back(): vo * * @return void */ + #[Test] public static function test_push_multiple_meta_data_method_when_clarity_is_disabled(): void { LaravelConfigHelper::disableClarity(); @@ -134,6 +139,7 @@ public static function test_push_multiple_meta_data_method_when_clarity_is_disab * * @return void */ + #[Test] public static function test_push_multiple_meta_data_method(): void { // add some meta-data @@ -182,6 +188,7 @@ public static function test_push_multiple_meta_data_method(): void * * @return void */ + #[Test] public static function test_push_multiple_meta_data_method_with_invalid_frames_back(): void { // test going back too many frames @@ -206,6 +213,7 @@ public static function test_push_multiple_meta_data_method_with_invalid_frames_b * * @return void */ + #[Test] public static function test_replace_meta_data_method_when_clarity_is_disabled(): void { // add some meta-data + try to replace, even though Clarity has been disabled @@ -228,6 +236,7 @@ public static function test_replace_meta_data_method_when_clarity_is_disabled(): * * @return void */ + #[Test] public static function test_replace_meta_data_method(): void { $summariseMetaData = function () { @@ -353,6 +362,7 @@ public static function test_replace_meta_data_method(): void * * @return void */ + #[Test] public static function test_that_control_run_meta_is_removed_from_top(): void { MetaCallStackAPI::pushMultipleMetaData(InternalSettings::META_DATA_TYPE__CONTROL_CALL, null, ['call-one']); diff --git a/tests/Unit/ClarityUnitTest.php b/tests/Unit/ClarityUnitTest.php index a3fadb7..bfcbc0e 100644 --- a/tests/Unit/ClarityUnitTest.php +++ b/tests/Unit/ClarityUnitTest.php @@ -12,6 +12,8 @@ use CodeDistortion\ClarityContext\Tests\LaravelTestCase; use CodeDistortion\ClarityContext\Tests\TestSupport\LaravelConfigHelper; use Exception; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; /** * Test the Clarity class. @@ -32,6 +34,8 @@ class ClarityUnitTest extends LaravelTestCase * @param array $expected The expected meta-data values. * @return void */ + #[Test] + #[DataProvider('metaDataCombinationDataProvider')] public static function test_the_addition_of_meta_data( string|array $arg1, string|array|null $arg2, @@ -68,15 +72,19 @@ public static function test_the_addition_of_meta_data( * @test * @dataProvider metaDataCombinationDataProvider * - * @param string|string[] $arg1 The first argument to pass. - * @param string|string[]|null $arg2 The second argument to pass (when not null). - * @param string|string[]|null $arg3 The third argument to pass (when not null). + * @param string|string[] $arg1 The first argument to pass. + * @param string|string[]|null $arg2 The second argument to pass (when not null). + * @param string|string[]|null $arg3 The third argument to pass (when not null). + * @param array $expected The expected meta-data values. * @return void */ + #[Test] + #[DataProvider('metaDataCombinationDataProvider')] public static function test_the_addition_of_meta_data_when_disabled( string|array $arg1, string|array|null $arg2, string|array|null $arg3, + array $expected, ): void { LaravelConfigHelper::disableClarity(); @@ -168,6 +176,7 @@ public static function metaDataCombinationDataProvider(): array * * @return void */ + #[Test] public static function test_build_context_here_method(): void { $frameCount = count(debug_backtrace()) + 1; @@ -236,6 +245,7 @@ public static function test_build_context_here_method(): void * * @return void */ + #[Test] public static function test_retrieval_of_exception_context_objects(): void { // no "latest" Context yet @@ -271,6 +281,7 @@ public static function test_retrieval_of_exception_context_objects(): void * * @return void */ + #[Test] public static function test_the_addition_of_trace_identifiers(): void { // no identifiers yet diff --git a/tests/Unit/ContextUnitTest.php b/tests/Unit/ContextUnitTest.php index 5976369..adee0b4 100644 --- a/tests/Unit/ContextUnitTest.php +++ b/tests/Unit/ContextUnitTest.php @@ -21,6 +21,8 @@ use CodeDistortion\ClarityContext\Tests\TestSupport\PHPStackTraceHelper; use CodeDistortion\ClarityContext\Tests\TestSupport\SimulateControlPackage; use Exception; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; use Throwable; /** @@ -37,6 +39,7 @@ class ContextUnitTest extends LaravelTestCase * * @return void */ + #[Test] public static function test_context_crud(): void { // CREATE a new Context object @@ -175,6 +178,7 @@ public static function test_context_crud(): void * @return void * @throws Exception Doesn't throw this, but phpcs expects this to be here. */ + #[Test] public static function test_retrieval_of_callstack_and_stack_trace(): void { $context = new Context( @@ -220,6 +224,7 @@ public static function test_retrieval_of_callstack_and_stack_trace(): void * @return void * @throws Exception Doesn't throw this, but phpcs expects this to be here. */ + #[Test] public static function test_context_fetching_methods_separately(): void { $fakeId = mt_rand(); @@ -281,6 +286,7 @@ public static function test_context_fetching_methods_separately(): void * * @return void */ + #[Test] public static function test_generation_of_callstack_and_stack_trace_based_on_an_exception(): void { $exception = new Exception(); @@ -364,6 +370,7 @@ public static function test_generation_of_callstack_and_stack_trace_based_on_an_ * * @return void */ + #[Test] public static function test_generation_of_callstack_and_stack_trace_based_on_a_php_stack_trace(): void { $phpStackTrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS); @@ -440,6 +447,7 @@ public static function test_generation_of_callstack_and_stack_trace_based_on_a_p * * @return void */ + #[Test] public static function test_the_context_class_can_identify_the_caught_here_frame_when_based_on_an_exception(): void { $catcherObjectId = 1; @@ -475,6 +483,7 @@ public static function test_the_context_class_can_identify_the_caught_here_frame * * @return void */ + #[Test] public static function test_the_context_class_wont_identify_the_caught_here_frame_when_based_on_stack_trace(): void { $catcherObjectId = 1; @@ -513,6 +522,7 @@ public static function test_the_context_class_wont_identify_the_caught_here_fram * * @return void */ + #[Test] public static function test_worth_reporting(): void { $buildContext = function (MetaCallStack $metaCallStack, ?int $catcherObjectId = null): Context { @@ -561,6 +571,8 @@ public static function test_worth_reporting(): void * @param boolean $enabled Whether Clarity is enabled or not. * @return void */ + #[Test] + #[DataProvider('clarityEnabledDataProvider')] public static function test_that_meta_objects_arent_created_when_clarity_is_disabled(bool $enabled): void { $enabled @@ -644,6 +656,7 @@ public static function clarityEnabledDataProvider(): array * * @return void */ + #[Test] public static function test_when_an_invalid_meta_data_type_is_encountered(): void { $metaCallStack = new MetaCallStack(); diff --git a/tests/Unit/Exceptions/ExceptionUnitTest.php b/tests/Unit/Exceptions/ExceptionUnitTest.php index b4de401..2d54911 100644 --- a/tests/Unit/Exceptions/ExceptionUnitTest.php +++ b/tests/Unit/Exceptions/ExceptionUnitTest.php @@ -5,6 +5,7 @@ use CodeDistortion\ClarityContext\Exceptions\ClarityContextInitialisationException; use CodeDistortion\ClarityContext\Exceptions\ClarityContextRuntimeException; use CodeDistortion\ClarityContext\Tests\PHPUnitTestCase; +use PHPUnit\Framework\Attributes\Test; /** * Test the Exception classes. @@ -20,6 +21,7 @@ class ExceptionUnitTest extends PHPUnitTestCase * * @return void */ + #[Test] public static function test_exception_messages(): void { // ClarityContextInitialisationException diff --git a/tests/Unit/Support/CallStack/CallStackFramesUnitTest.php b/tests/Unit/Support/CallStack/CallStackFramesUnitTest.php index 8510d1d..7f73ce0 100644 --- a/tests/Unit/Support/CallStack/CallStackFramesUnitTest.php +++ b/tests/Unit/Support/CallStack/CallStackFramesUnitTest.php @@ -6,6 +6,7 @@ use CodeDistortion\ClarityContext\Support\CallStack\Frame; use CodeDistortion\ClarityContext\Support\Support; use CodeDistortion\ClarityContext\Tests\PHPUnitTestCase; +use PHPUnit\Framework\Attributes\Test; /** * Test CallStack's methods that search for different types of frames. @@ -21,6 +22,7 @@ class CallStackFramesUnitTest extends PHPUnitTestCase * * @return void */ + #[Test] public static function test_accessing_the_last_application_frame(): void { $frame1 = self::buildCallStackFrame('/var/www/html/src/some-file1', '/var/www/html', false); @@ -68,6 +70,7 @@ public static function test_accessing_the_last_application_frame(): void * * @return void */ + #[Test] public static function test_accessing_the_thrown_here_frame(): void { $frame1 = self::buildCallStackFrame('/var/www/html/src/some-file1', '/var/www/html', false, false); @@ -115,6 +118,7 @@ public static function test_accessing_the_thrown_here_frame(): void * * @return void */ + #[Test] public static function test_accessing_the_caught_here_frame(): void { $frame1 = self::buildCallStackFrame('/var/www/html/src/some-file1', '/var/www/html', false, false, false); diff --git a/tests/Unit/Support/CallStack/CallStackMetaGroupUnitTest.php b/tests/Unit/Support/CallStack/CallStackMetaGroupUnitTest.php index b22600c..0fa8153 100644 --- a/tests/Unit/Support/CallStack/CallStackMetaGroupUnitTest.php +++ b/tests/Unit/Support/CallStack/CallStackMetaGroupUnitTest.php @@ -12,6 +12,8 @@ use CodeDistortion\ClarityContext\Tests\LaravelTestCase; use CodeDistortion\ClarityContext\Tests\TestSupport\SimulateControlPackage; use Exception; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; use Throwable; /** @@ -40,6 +42,8 @@ class CallStackMetaGroupUnitTest extends LaravelTestCase * @return void * @throws Exception Doesn't throw this, but phpcs expects this to be here. */ + #[Test] + #[DataProvider('buildMetaGroupsDataProvider')] public static function test_the_building_of_meta_groups( callable $callable, array $expected, diff --git a/tests/Unit/Support/CallStack/CallStackMetaUnitTest.php b/tests/Unit/Support/CallStack/CallStackMetaUnitTest.php index e89ecff..9a8b122 100644 --- a/tests/Unit/Support/CallStack/CallStackMetaUnitTest.php +++ b/tests/Unit/Support/CallStack/CallStackMetaUnitTest.php @@ -12,6 +12,7 @@ use CodeDistortion\ClarityContext\Support\CallStack\MetaData\Meta; use CodeDistortion\ClarityContext\Support\Support; use CodeDistortion\ClarityContext\Tests\PHPUnitTestCase; +use PHPUnit\Framework\Attributes\Test; /** * Test that CallStack returns Meta objects properly. @@ -27,6 +28,7 @@ class CallStackMetaUnitTest extends PHPUnitTestCase * * @return void */ + #[Test] public static function test_retrieval_of_meta_objects(): void { $callMeta1 = self::buildCallMeta(); diff --git a/tests/Unit/Support/CallStack/CallStackPHPInterfacesUnitTest.php b/tests/Unit/Support/CallStack/CallStackPHPInterfacesUnitTest.php index 9b522ad..21c6e86 100644 --- a/tests/Unit/Support/CallStack/CallStackPHPInterfacesUnitTest.php +++ b/tests/Unit/Support/CallStack/CallStackPHPInterfacesUnitTest.php @@ -8,6 +8,7 @@ use CodeDistortion\ClarityContext\Tests\PHPUnitTestCase; use InvalidArgumentException; use OutOfBoundsException; +use PHPUnit\Framework\Attributes\Test; /** * Test CallStack's implementation of PHP's ArrayAccess, Countable and SeekableIterator Interfaces. @@ -23,6 +24,7 @@ class CallStackPHPInterfacesUnitTest extends PHPUnitTestCase * * @return void */ + #[Test] public static function test_call_stack(): void { $implements = class_implements(CallStack::class); diff --git a/tests/Unit/Support/CallStack/FrameUnitTest.php b/tests/Unit/Support/CallStack/FrameUnitTest.php index be32638..a40ec6b 100644 --- a/tests/Unit/Support/CallStack/FrameUnitTest.php +++ b/tests/Unit/Support/CallStack/FrameUnitTest.php @@ -7,6 +7,7 @@ use CodeDistortion\ClarityContext\Support\CallStack\MetaData\Meta; use CodeDistortion\ClarityContext\Support\Support; use CodeDistortion\ClarityContext\Tests\PHPUnitTestCase; +use PHPUnit\Framework\Attributes\Test; use stdClass; /** @@ -23,6 +24,7 @@ class FrameUnitTest extends PHPUnitTestCase * * @return void */ + #[Test] public static function test_call_stack_frame(): void { $projectRootDir = (string) realpath(__DIR__ . '/../../../../'); diff --git a/tests/Unit/Support/CallStack/MetaData/CallMetaUnitTest.php b/tests/Unit/Support/CallStack/MetaData/CallMetaUnitTest.php index d257a0d..74ae4f9 100644 --- a/tests/Unit/Support/CallStack/MetaData/CallMetaUnitTest.php +++ b/tests/Unit/Support/CallStack/MetaData/CallMetaUnitTest.php @@ -4,6 +4,7 @@ use CodeDistortion\ClarityContext\Support\CallStack\MetaData\CallMeta; use CodeDistortion\ClarityContext\Tests\PHPUnitTestCase; +use PHPUnit\Framework\Attributes\Test; /** * Test the CallMeta class. @@ -19,6 +20,7 @@ class CallMetaUnitTest extends PHPUnitTestCase * * @return void */ + #[Test] public static function test_call_meta(): void { $rand = mt_rand(); diff --git a/tests/Unit/Support/CallStack/MetaData/ContextMetaUnitTest.php b/tests/Unit/Support/CallStack/MetaData/ContextMetaUnitTest.php index b73e6b4..71d4c6c 100644 --- a/tests/Unit/Support/CallStack/MetaData/ContextMetaUnitTest.php +++ b/tests/Unit/Support/CallStack/MetaData/ContextMetaUnitTest.php @@ -4,6 +4,7 @@ use CodeDistortion\ClarityContext\Support\CallStack\MetaData\ContextMeta; use CodeDistortion\ClarityContext\Tests\PHPUnitTestCase; +use PHPUnit\Framework\Attributes\Test; /** * Test the ContextMeta class. @@ -19,6 +20,7 @@ class ContextMetaUnitTest extends PHPUnitTestCase * * @return void */ + #[Test] public static function test_context_meta(): void { $rand = mt_rand(); diff --git a/tests/Unit/Support/CallStack/MetaData/ExceptionCaughtMetaUnitTest.php b/tests/Unit/Support/CallStack/MetaData/ExceptionCaughtMetaUnitTest.php index c019115..7b39e76 100644 --- a/tests/Unit/Support/CallStack/MetaData/ExceptionCaughtMetaUnitTest.php +++ b/tests/Unit/Support/CallStack/MetaData/ExceptionCaughtMetaUnitTest.php @@ -4,6 +4,7 @@ use CodeDistortion\ClarityContext\Support\CallStack\MetaData\ExceptionCaughtMeta; use CodeDistortion\ClarityContext\Tests\PHPUnitTestCase; +use PHPUnit\Framework\Attributes\Test; /** * Test the ExceptionCaughtMeta class. @@ -19,6 +20,7 @@ class ExceptionCaughtMetaUnitTest extends PHPUnitTestCase * * @return void */ + #[Test] public static function test_exception_caught_meta(): void { $rand = mt_rand(); diff --git a/tests/Unit/Support/CallStack/MetaData/ExceptionThrownMetaUnitTest.php b/tests/Unit/Support/CallStack/MetaData/ExceptionThrownMetaUnitTest.php index e5ecda8..fd79c35 100644 --- a/tests/Unit/Support/CallStack/MetaData/ExceptionThrownMetaUnitTest.php +++ b/tests/Unit/Support/CallStack/MetaData/ExceptionThrownMetaUnitTest.php @@ -4,6 +4,7 @@ use CodeDistortion\ClarityContext\Support\CallStack\MetaData\ExceptionThrownMeta; use CodeDistortion\ClarityContext\Tests\PHPUnitTestCase; +use PHPUnit\Framework\Attributes\Test; /** * Test the ExceptionThrownMeta class. @@ -19,6 +20,7 @@ class ExceptionThrownMetaUnitTest extends PHPUnitTestCase * * @return void */ + #[Test] public static function test_exception_thrown_meta(): void { $rand = mt_rand(); diff --git a/tests/Unit/Support/CallStack/MetaData/LastApplicationFrameMetaUnitTest.php b/tests/Unit/Support/CallStack/MetaData/LastApplicationFrameMetaUnitTest.php index e2c08e6..2124600 100644 --- a/tests/Unit/Support/CallStack/MetaData/LastApplicationFrameMetaUnitTest.php +++ b/tests/Unit/Support/CallStack/MetaData/LastApplicationFrameMetaUnitTest.php @@ -4,6 +4,7 @@ use CodeDistortion\ClarityContext\Support\CallStack\MetaData\LastApplicationFrameMeta; use CodeDistortion\ClarityContext\Tests\PHPUnitTestCase; +use PHPUnit\Framework\Attributes\Test; /** * Test the LastApplicationFrameMeta class. @@ -19,6 +20,7 @@ class LastApplicationFrameMetaUnitTest extends PHPUnitTestCase * * @return void */ + #[Test] public static function test_last_application_frame_meta(): void { $rand = mt_rand(); diff --git a/tests/Unit/Support/CallStack/MetaGroupUnitTest.php b/tests/Unit/Support/CallStack/MetaGroupUnitTest.php index 419d53a..dff0901 100644 --- a/tests/Unit/Support/CallStack/MetaGroupUnitTest.php +++ b/tests/Unit/Support/CallStack/MetaGroupUnitTest.php @@ -6,6 +6,8 @@ use CodeDistortion\ClarityContext\Support\CallStack\MetaData\ExceptionThrownMeta; use CodeDistortion\ClarityContext\Support\CallStack\MetaGroup; use CodeDistortion\ClarityContext\Tests\PHPUnitTestCase; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; use stdClass; /** @@ -28,6 +30,8 @@ class MetaGroupUnitTest extends PHPUnitTestCase * @param boolean $exceptionCaughtInThisFrame Whether $exceptionCaughtInThisFrame will be set to true or false. * @return void */ + #[Test] + #[DataProvider('metaGroupCrudDataProvider')] public static function test_meta_group_crud( bool $isInApplicationFrame, bool $isInLastApplicationFrame, diff --git a/tests/Unit/Support/Framework/LaravelFrameworkConfigUnitTest.php b/tests/Unit/Support/Framework/LaravelFrameworkConfigUnitTest.php index b9b390a..63fb9f5 100644 --- a/tests/Unit/Support/Framework/LaravelFrameworkConfigUnitTest.php +++ b/tests/Unit/Support/Framework/LaravelFrameworkConfigUnitTest.php @@ -7,6 +7,7 @@ use CodeDistortion\ClarityContext\Support\Framework\Framework; use CodeDistortion\ClarityContext\Support\InternalSettings; use CodeDistortion\ClarityContext\Tests\LaravelTestCase; +use PHPUnit\Framework\Attributes\Test; /** * Test the Laravel framework config integration. @@ -22,6 +23,7 @@ class LaravelFrameworkConfigUnitTest extends LaravelTestCase * * @return void */ + #[Test] public static function test_framework_config_caching(): void { self::assertSame(Framework::config(), Framework::config()); @@ -37,6 +39,7 @@ public static function test_framework_config_caching(): void * @return void * @throws ClarityContextInitialisationException Doesn't throw this, but phpcs expects this to be here. */ + #[Test] public static function test_project_root_dir_detection(): void { self::assertSame( @@ -54,6 +57,7 @@ public static function test_project_root_dir_detection(): void * * @return void */ + #[Test] public static function test_framework_config_crud(): void { $config = Framework::config(); @@ -199,6 +203,7 @@ public static function test_framework_config_crud(): void * * @return void */ + #[Test] public static function test_framework_config_settings(): void { $config = Framework::config(); diff --git a/tests/Unit/Support/Framework/LaravelFrameworkDepInjUnitTest.php b/tests/Unit/Support/Framework/LaravelFrameworkDepInjUnitTest.php index e8c3101..90f62ea 100644 --- a/tests/Unit/Support/Framework/LaravelFrameworkDepInjUnitTest.php +++ b/tests/Unit/Support/Framework/LaravelFrameworkDepInjUnitTest.php @@ -6,6 +6,7 @@ use CodeDistortion\ClarityContext\Support\Support; use CodeDistortion\ClarityContext\Tests\PHPUnitTestCase; use CodeDistortion\ClarityContext\Tests\TestSupport\ValueObject; +use PHPUnit\Framework\Attributes\Test; /** * Test the Laravel framework dependency injection integration. @@ -21,6 +22,7 @@ class LaravelFrameworkDepInjUnitTest extends PHPUnitTestCase * * @return void */ + #[Test] public static function test_framework_dep_inj_caching(): void { self::assertSame(Framework::depInj(), Framework::depInj()); @@ -35,6 +37,7 @@ public static function test_framework_dep_inj_caching(): void * * @return void */ + #[Test] public static function test_framework_dep_inj(): void { $depInjection = Framework::depInj(); diff --git a/tests/Unit/Support/MetaCallStackUnitTest.php b/tests/Unit/Support/MetaCallStackUnitTest.php index 506acc2..bb2d7a3 100644 --- a/tests/Unit/Support/MetaCallStackUnitTest.php +++ b/tests/Unit/Support/MetaCallStackUnitTest.php @@ -9,6 +9,7 @@ use CodeDistortion\ClarityContext\Tests\TestSupport\PHPStackTraceHelper; use CodeDistortion\ClarityContext\Tests\TestSupport\SomeOtherClass; use Exception; +use PHPUnit\Framework\Attributes\Test; /** * Test the MetaCallStack class. @@ -26,6 +27,7 @@ class MetaCallStackUnitTest extends PHPUnitTestCase * * @return void */ + #[Test] public static function test_storage_of_meta_data_x_steps_back(): void { // start empty @@ -67,6 +69,7 @@ public static function test_storage_of_meta_data_x_steps_back(): void * * @return void */ + #[Test] public static function test_storage_of_different_sorts_of_meta_data(): void { $metaCallStack = new MetaCallStack(); @@ -93,6 +96,7 @@ public static function test_storage_of_different_sorts_of_meta_data(): void * * @return void */ + #[Test] public static function test_replacement_of_a_meta_datas_value(): void { $metaCallStack = new MetaCallStack(); @@ -168,6 +172,7 @@ public static function test_replacement_of_a_meta_datas_value(): void * * @return void */ + #[Test] public static function test_pruning_when_adding_meta_data(): void { $metaCallStack = new MetaCallStack(); @@ -244,6 +249,7 @@ public static function test_pruning_when_adding_meta_data(): void * * @return void */ + #[Test] public static function test_pruning_based_on_a_stack_trace(): void { // add some meta-data in this frame @@ -288,6 +294,7 @@ public static function test_pruning_based_on_a_stack_trace(): void * * @return void */ + #[Test] public static function test_pruning_based_on_an_exception(): void { // build a new MetaCallStack instance - also add some initial meta-data in the previous frame @@ -445,6 +452,7 @@ private static function pruneWithExceptionAndCheck( * * @return void */ + #[Test] public static function test_meta_data_pruning_when_calling_the_same_closure_on_the_same_line(): void { $metaCallStack = new MetaCallStack(); @@ -498,6 +506,7 @@ public static function test_meta_data_pruning_when_calling_the_same_closure_on_t * * @return void */ + #[Test] public static function test_meta_data_pruning_when_calling_different_closures_on_the_same_line(): void { $metaCallStack = new MetaCallStack(); @@ -522,11 +531,39 @@ public static function test_meta_data_pruning_when_calling_different_closures_on $metaData = $metaCallStack->getStackMetaData(); $lastIndex = max(array_keys($metaData)); - self::assertSame(2, count($metaData[$lastIndex])); - self::assertSame('typeA', $metaData[$lastIndex][0]['type']); - self::assertSame('valueA', $metaData[$lastIndex][0]['value']); - self::assertSame('typeB', $metaData[$lastIndex][1]['type']); - self::assertSame('valueB', $metaData[$lastIndex][1]['value']); + // PHP 8.4+ + // From PHP 8.4, the closure's frame includes extra information in the "function" key which lets Clarity Context + // tell the difference between the two closures! + // + // 12 => array:5 [ + // "file" => "/var/www/html/code-distortion/clarity-context/tests/Unit/Support/MetaCallStackUnitTest.php" + // "line" => 518 + // "function" => "{closure:CodeDistortion\ClarityContext\Tests\Unit\Support\MetaCallStackUnitTest::test_meta_data_pruning_when_calling_different_closures_on_the_same_line():517}" + // "class" => "CodeDistortion\ClarityContext\Tests\Unit\Support\MetaCallStackUnitTest" + // "type" => "::" + // ] + if (version_compare(PHP_VERSION, '8.4', '>=')) { + self::assertSame(1, count($metaData[$lastIndex])); + self::assertSame('typeB', $metaData[$lastIndex][0]['type']); + self::assertSame('valueB', $metaData[$lastIndex][0]['value']); + // < PHP 8.4 + // Before PHP 8.4, the closure's frame contained generic inforrmation in the "function" key making it impossible + // for Clarity Context to tell the difference between the two closures + // + // 12 => array:5 [ + // "file" => "/var/www/html/code-distortion/clarity-context/tests/Unit/Support/MetaCallStackUnitTest.php" + // "line" => 523 + // "function" => "CodeDistortion\ClarityContext\Tests\Unit\Support\{closure}" + // "class" => "CodeDistortion\ClarityContext\Tests\Unit\Support\MetaCallStackUnitTest" + // "type" => "::" + // ] + } else { + self::assertSame(2, count($metaData[$lastIndex])); + self::assertSame('typeA', $metaData[$lastIndex][0]['type']); + self::assertSame('valueA', $metaData[$lastIndex][0]['value']); + self::assertSame('typeB', $metaData[$lastIndex][1]['type']); + self::assertSame('valueB', $metaData[$lastIndex][1]['value']); + } @@ -557,6 +594,7 @@ public static function test_meta_data_pruning_when_calling_different_closures_on * * @return void */ + #[Test] public static function test_meta_data_pruning_when_calling_the_same_static_method_on_the_same_line(): void { $metaCallStack = new MetaCallStack(); @@ -600,6 +638,7 @@ public static function test_meta_data_pruning_when_calling_the_same_static_metho * * @return void */ + #[Test] public static function test_meta_data_pruning_when_calling_different_static_methods_on_the_same_line(): void { $metaCallStack = new MetaCallStack(); @@ -670,6 +709,7 @@ private static function addMetaDataB(MetaCallStack $metaCallStack, string $type, * * @return void */ + #[Test] public static function test_the_addition_of_multiple_meta_data_from_the_same_call(): void { // add some meta-data to start with diff --git a/tests/Unit/Support/SupportUnitTest.php b/tests/Unit/Support/SupportUnitTest.php index 8ca6bb2..ef7bce9 100644 --- a/tests/Unit/Support/SupportUnitTest.php +++ b/tests/Unit/Support/SupportUnitTest.php @@ -9,6 +9,8 @@ use CodeDistortion\ClarityContext\Support\MetaCallStack; use CodeDistortion\ClarityContext\Support\Support; use CodeDistortion\ClarityContext\Tests\PHPUnitTestCase; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; /** * Test the Support class. @@ -28,6 +30,8 @@ class SupportUnitTest extends PHPUnitTestCase * @param mixed[] $expected The expected output. * @return void */ + #[Test] + #[DataProvider('argumentDataProvider')] public static function test_normalise_args_method(array $previous, array $args, array $expected): void { $normalised = Support::normaliseArgs($previous, $args); @@ -128,6 +132,8 @@ private static function buildArgs(array $previous, array $args): array * @param string $projectRootDir The project root dir. * @return void */ + #[Test] + #[DataProvider('resolveProjectFileDataProvider')] public static function test_resolve_project_file_method( string $expected, string $file, @@ -229,6 +235,8 @@ public static function resolveProjectFileDataProvider(): array * @param string $projectRootDir The project root dir. * @return void */ + #[Test] + #[DataProvider('resolveIsApplicationFileCheckDataProvider')] public static function test_is_application_file_check_method( bool $expected, string $projectFile, @@ -299,6 +307,8 @@ public static function resolveIsApplicationFileCheckDataProvider(): array * @param boolean $expected The expected outcome. * @return void */ + #[Test] + #[DataProvider('metaTypeCountDataProvider')] public static function test_decide_if_meta_counts_are_worth_listing_method( array $metaTypeCounts, bool $expected @@ -338,6 +348,7 @@ public static function metaTypeCountDataProvider(): array * * @return void */ + #[Test] public static function test_the_get_global_meta_call_stack_method(): void { $metaCallStack1 = Support::getGlobalMetaCallStack(); @@ -356,6 +367,7 @@ public static function test_the_get_global_meta_call_stack_method(): void * * @return void */ + #[Test] public static function test_step_back_stack_trace_method(): void { $phpStackTrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS); @@ -407,6 +419,7 @@ public static function test_step_back_stack_trace_method(): void * * @return void */ + #[Test] public static function test_prepare_stack_trace_method(): void { $phpStackTrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS); @@ -492,6 +505,8 @@ public static function test_prepare_stack_trace_method(): void * @param integer $addFrames The number of frames to add. * @return void */ + #[Test] + #[DataProvider('exceptionHandlerFramesDataProvider')] public static function test_that_laravel_exception_handler_frames_are_pruned(int $startFrameCount, int $addFrames) { $phpStackTrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS);