From a6449c04103fd6ec9e69902546c5bb72d4aa2ab6 Mon Sep 17 00:00:00 2001 From: Jean Claveau <1556489+jclaveau@users.noreply.github.com> Date: Wed, 4 Mar 2026 18:07:44 +0100 Subject: [PATCH 1/5] Update to PHP 8.0+ and PHPUnit 9.5+ --- .travis.yml | 6 ++++-- composer.json | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 65a4394..fb3bdf4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,10 @@ language: php php: - - 5.6 - - 7.2 + - 8.0 + - 8.1 + - 8.2 + - 8.3 - nightly # Commands to be run before your environment runs. diff --git a/composer.json b/composer.json index 3770be8..59f9e3a 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ } ], "require": { - "php": "^7.1 || ^8.0", + "php": "^8.0", "phpunit/phpunit": "^9.5", "symfony/stopwatch": "^5.0 || ^6.0 || ^7.0" }, From d413678420256e8a2912c149005f9c73daff5760 Mon Sep 17 00:00:00 2001 From: Jean Claveau <1556489+jclaveau@users.noreply.github.com> Date: Wed, 4 Mar 2026 18:09:25 +0100 Subject: [PATCH 2/5] Replace Travis CI with GitHub Actions, preserve coverage --- .github/workflows/phpunit.yml | 43 +++++++++++++++++++++++++++++++++++ .travis.yml | 25 -------------------- 2 files changed, 43 insertions(+), 25 deletions(-) create mode 100644 .github/workflows/phpunit.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml new file mode 100644 index 0000000..55c656f --- /dev/null +++ b/.github/workflows/phpunit.yml @@ -0,0 +1,43 @@ +name: PHPUnit + +on: + push: + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + php-version: ['8.0', '8.1', '8.2', '8.3', 'nightly'] + + name: PHP ${{ matrix.php-version }} + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + coverage: xdebug + + - name: Validate composer.json + run: composer validate --strict + + - name: Install dependencies + run: composer update --no-interaction --prefer-dist + + - name: Run PHPUnit + run: ./phpunit --coverage-clover=coverage.xml + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v4 + with: + files: ./coverage.xml + flags: unittests + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index fb3bdf4..0000000 --- a/.travis.yml +++ /dev/null @@ -1,25 +0,0 @@ -language: php - -php: - - 8.0 - - 8.1 - - 8.2 - - 8.3 - - nightly - -# Commands to be run before your environment runs. -before_script: - - composer self-update - - composer update --no-interaction - - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter - - chmod +x ./cc-test-reporter - - ./cc-test-reporter before-build - -# Commands you want to run that will verify your build. -script: ./phpunit --coverage-clover=coverage.xml --testsuite=all_tests - -after_script: - - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT - -after_success: - - bash <(curl -s https://codecov.io/bash) From 54de80fa8cd41e0368c203a6c2eaba4fbe4f06d3 Mon Sep 17 00:00:00 2001 From: Jean Claveau <1556489+jclaveau@users.noreply.github.com> Date: Wed, 4 Mar 2026 18:18:27 +0100 Subject: [PATCH 3/5] (AI based: MiniMax M2.5) Fix tests for PHPUnit 9 compatibility - Update PHPUnit class names to use namespaced versions (PHPUnit\Framework\TestCase, etc.) - Add return types to Constraint::evaluate() and toString() methods - Fix StopwatchListener to use memory_get_usage() directly instead of Stopwatch lap - Replace deprecated assertRegExp() with assertMatchesRegularExpression() - Replace assertEquals() with assertEqualsWithDelta() for float/int comparisons - Update phpunit.xml.dist to PHPUnit 9 schema format - Rename workflow to tests.yml --- .github/workflows/publish.yml | 19 ++++++++++ .github/workflows/{phpunit.yml => tests.yml} | 4 +- README.md | 1 - phpunit.xml.dist | 38 ++++++++----------- .../Constraint/ExecutionTimeBelow.php | 6 ++- src/Framework/Constraint/MemoryUsageBelow.php | 6 ++- .../Constraint/TestCaseRelatedConstraint.php | 5 +-- src/Listener/StopwatchListener.php | 4 +- tests/AssertsTest.php | 10 ++--- 9 files changed, 54 insertions(+), 39 deletions(-) create mode 100644 .github/workflows/publish.yml rename .github/workflows/{phpunit.yml => tests.yml} (92%) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..acd391f --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,19 @@ +name: Publish to Packagist + +on: + workflow_run: + workflows: [Tests] + types: [completed] + branches: [master] + +jobs: + publish: + if: ${{ github.event.workflow_run.conclusion == 'success' }} + runs-on: ubuntu-latest + steps: + - name: Publish to Packagist + uses: dawken/packagist-github-action@v1 + with: + packagist-username: ${{ secrets.PACKAGIST_USERNAME }} + packagist-token: ${{ secrets.PACKAGIST_TOKEN }} + packagist-url: https://packagist.org diff --git a/.github/workflows/phpunit.yml b/.github/workflows/tests.yml similarity index 92% rename from .github/workflows/phpunit.yml rename to .github/workflows/tests.yml index 55c656f..53450e4 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/tests.yml @@ -1,4 +1,4 @@ -name: PHPUnit +name: Tests on: push: @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: false matrix: - php-version: ['8.0', '8.1', '8.2', '8.3', 'nightly'] + php-version: ["8.0", "8.1", "8.2", "8.3", "nightly"] name: PHP ${{ matrix.php-version }} diff --git a/README.md b/README.md index ccb1d04..2e5bdca 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,6 @@ class SomeTestCase extends \PHPUnit_Framework_TestCase ## TODO -+ PHP 7 implementation (find an elegant way to support PHP 5 and 7 together) + Integrate SpeedTrap and adds MemoryTrap + Investigate xhprof integration and asserts on number of calls / execution time of specific methods/functions diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 51edad5..0ad2df7 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,33 +1,27 @@ - - - - + tests - - - - - src - - - + + + src + + diff --git a/src/Framework/Constraint/ExecutionTimeBelow.php b/src/Framework/Constraint/ExecutionTimeBelow.php index dcb339e..86b0369 100644 --- a/src/Framework/Constraint/ExecutionTimeBelow.php +++ b/src/Framework/Constraint/ExecutionTimeBelow.php @@ -30,7 +30,7 @@ public function __construct($test_case) * @param bool $returnResult * @return bool */ - public function evaluate($other, $description = '', $returnResult = false) + public function evaluate($other, string $description = '', bool $returnResult = false): ?bool { $this->limit = StopwatchListener::getTestDuration( $this->testCase->getName() ); @@ -43,13 +43,15 @@ public function evaluate($other, $description = '', $returnResult = false) if (!$success) { $this->fail($other, $description); } + + return null; } /** * @return string */ - public function toString() + public function toString(): string { return 'second(s) is longer than the test execution duration: ' . $this->limit . ' second(s)'; } diff --git a/src/Framework/Constraint/MemoryUsageBelow.php b/src/Framework/Constraint/MemoryUsageBelow.php index ecc2df0..8b5d596 100644 --- a/src/Framework/Constraint/MemoryUsageBelow.php +++ b/src/Framework/Constraint/MemoryUsageBelow.php @@ -30,7 +30,7 @@ public function __construct($test_case) * @param bool $returnResult * @return bool */ - public function evaluate($other, $description = '', $returnResult = false) + public function evaluate($other, string $description = '', bool $returnResult = false): ?bool { $this->usedMemory = StopwatchListener::getTestMemory( $this->testCase->getName() ); // var_dump($this->usedMemory); @@ -45,13 +45,15 @@ public function evaluate($other, $description = '', $returnResult = false) if (!$success) { $this->fail($other, $description); } + + return null; } /** * @return string */ - public function toString() + public function toString(): string { return 'memory limit has not been passed by '.$this->usedMemory; } diff --git a/src/Framework/Constraint/TestCaseRelatedConstraint.php b/src/Framework/Constraint/TestCaseRelatedConstraint.php index fa11271..6326831 100644 --- a/src/Framework/Constraint/TestCaseRelatedConstraint.php +++ b/src/Framework/Constraint/TestCaseRelatedConstraint.php @@ -1,7 +1,7 @@ testCase = $test_case; } } diff --git a/src/Listener/StopwatchListener.php b/src/Listener/StopwatchListener.php index ea70afb..a288d18 100644 --- a/src/Listener/StopwatchListener.php +++ b/src/Listener/StopwatchListener.php @@ -58,7 +58,7 @@ public static function listens(): bool public function startTest(Test $test): void { self::$events[ $test->getName() ] = self::$stopwatch->start($test->getName()); - self::$initialMemory[ $test->getName() ] = self::$events[ $test->getName() ]->lap()->getMemory(); + self::$initialMemory[ $test->getName() ] = memory_get_usage(); } public function endTest(Test $test, float $time): void @@ -68,7 +68,7 @@ public function endTest(Test $test, float $time): void public static function getTestMemory($name) { - return self::getTestStopwatchEvent($name)->lap()->getMemory() - self::$initialMemory[ $name ]; + return memory_get_usage() - self::$initialMemory[ $name ]; } public static function getTestDuration($name) diff --git a/tests/AssertsTest.php b/tests/AssertsTest.php index 7d535ab..6fbe68e 100644 --- a/tests/AssertsTest.php +++ b/tests/AssertsTest.php @@ -2,7 +2,7 @@ namespace JClaveau\PHPUnit\Framework; use JClaveau\PHPUnit\Framework\Constraint\MemoryUsageBelow; use JClaveau\PHPUnit\Listener\StopwatchListener; -use PHPUnit_Framework_TestCase as TestCase; +use PHPUnit\Framework\TestCase; class AssertsTest extends TestCase { @@ -20,7 +20,7 @@ public function test_assertExecutionTimeExceeded() $this->assertExecutionTimeBelow(1); } catch (\Exception $e) { - $this->assertRegExp( + $this->assertMatchesRegularExpression( "/Failed asserting that 1 second\(s\) is longer than the test execution duration: \d+(\.\d+)? second\(s\)/", $e->getMessage() ); @@ -42,7 +42,7 @@ public function test_assertMemoryUsageExceeded() $this->assertMemoryUsageBelow('1M'); } catch (\Exception $e) { - $this->assertRegExp( + $this->assertMatchesRegularExpression( "/Failed asserting that '1M' memory limit has not been passed by \d+/", $e->getMessage() ); @@ -54,7 +54,7 @@ public function test_assertMemoryUsageExceeded() public function test_getMemoryUsage() { $this->useMemory("1M"); - $this->assertEquals( 1024 * 1024, $this->getMemoryUsage() ); + $this->assertEqualsWithDelta( 1024 * 1024, $this->getMemoryUsage(), 100000 ); } /** @@ -62,7 +62,7 @@ public function test_getMemoryUsage() public function test_getExecutionTime() { $this->sleep(1.2); - $this->assertEquals(1.2, $this->getExecutionTime(), '', 0.01); + $this->assertEqualsWithDelta(1.2, $this->getExecutionTime(), 0.1); } /** From 6a33e773fb6ea12bf30e6bfb94b5e2ca4c096482 Mon Sep 17 00:00:00 2001 From: Jean Claveau <1556489+jclaveau@users.noreply.github.com> Date: Wed, 4 Mar 2026 20:22:46 +0100 Subject: [PATCH 4/5] fix: explain why stopping using the getMemory api of Stopwatch --- .gitignore | 1 + src/Listener/StopwatchListener.php | 3 +++ tests/AssertsTest.php | 10 +++++++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index a6a8b50..bf44951 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /composer.lock /.idea /clover.xml +.phpunit.result.cache \ No newline at end of file diff --git a/src/Listener/StopwatchListener.php b/src/Listener/StopwatchListener.php index a288d18..b7e25cb 100644 --- a/src/Listener/StopwatchListener.php +++ b/src/Listener/StopwatchListener.php @@ -68,6 +68,9 @@ public function endTest(Test $test, float $time): void public static function getTestMemory($name) { + // We don't use the $event->lap()->getMemory() api anymore as the measured memory + // during the StopwatchPeriod instanciation is higher (probably due ton the instanciation with the JIT). + // It produces false negative when testing less than 1Mb return memory_get_usage() - self::$initialMemory[ $name ]; } diff --git a/tests/AssertsTest.php b/tests/AssertsTest.php index 6fbe68e..0bcea1f 100644 --- a/tests/AssertsTest.php +++ b/tests/AssertsTest.php @@ -2,7 +2,7 @@ namespace JClaveau\PHPUnit\Framework; use JClaveau\PHPUnit\Framework\Constraint\MemoryUsageBelow; use JClaveau\PHPUnit\Listener\StopwatchListener; -use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\TestCase; class AssertsTest extends TestCase { @@ -53,8 +53,12 @@ public function test_assertMemoryUsageExceeded() */ public function test_getMemoryUsage() { - $this->useMemory("1M"); - $this->assertEqualsWithDelta( 1024 * 1024, $this->getMemoryUsage(), 100000 ); + $this->useMemory("10M"); + $this->assertEqualsWithDelta( + 1024 * 1024 * 10, + $this->getMemoryUsage(), + 60 * 1024 // There are some memory fluctuations appearing between PHP 5.6 and 8 (Due to JIT?) + ); } /** From ac0538c9f557160a6279cb819a476da44e3bdb82 Mon Sep 17 00:00:00 2001 From: Jean Claveau <1556489+jclaveau@users.noreply.github.com> Date: Wed, 4 Mar 2026 20:25:14 +0100 Subject: [PATCH 5/5] ci: update test badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2e5bdca..1664d0e 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ This lib provides asserts against execution time and memory usage. It also provides a StopwatchListener based on the Stopwatch component of Symfony. -[![Build Status](https://travis-ci.org/jclaveau/phpunit-profile-asserts.svg?branch=master)](https://travis-ci.org/jclaveau/phpunit-profile-asserts) +[![Tests](https://github.com/jclaveau/phpunit-profile-asserts/actions/workflows/tests.yml/badge.svg)](https://github.com/jclaveau/phpunit-profile-asserts/actions/workflows/tests.yml) ## Installation