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/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..53450e4 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,43 @@ +name: Tests + +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/.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/.travis.yml b/.travis.yml deleted file mode 100644 index 65a4394..0000000 --- a/.travis.yml +++ /dev/null @@ -1,23 +0,0 @@ -language: php - -php: - - 5.6 - - 7.2 - - 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) diff --git a/README.md b/README.md index ccb1d04..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 @@ -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/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" }, 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..b7e25cb 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,10 @@ public function endTest(Test $test, float $time): void public static function getTestMemory($name) { - return self::getTestStopwatchEvent($name)->lap()->getMemory() - self::$initialMemory[ $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 ]; } public static function getTestDuration($name) diff --git a/tests/AssertsTest.php b/tests/AssertsTest.php index 7d535ab..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 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() ); @@ -53,8 +53,12 @@ public function test_assertMemoryUsageExceeded() */ public function test_getMemoryUsage() { - $this->useMemory("1M"); - $this->assertEquals( 1024 * 1024, $this->getMemoryUsage() ); + $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?) + ); } /** @@ -62,7 +66,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); } /**