Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -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 }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/composer.lock
/.idea
/clover.xml
.phpunit.result.cache
23 changes: 0 additions & 23 deletions .travis.yml

This file was deleted.

3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
38 changes: 16 additions & 22 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false"
bootstrap = "vendor/autoload.php">

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<listeners>
<listener class="JClaveau\PHPUnit\Listener\StopwatchListener">
</listener>
</listeners>

<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>

<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
6 changes: 4 additions & 2 deletions src/Framework/Constraint/ExecutionTimeBelow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() );

Expand All @@ -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)';
}
Expand Down
6 changes: 4 additions & 2 deletions src/Framework/Constraint/MemoryUsageBelow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Framework/Constraint/TestCaseRelatedConstraint.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace JClaveau\PHPUnit\Framework\Constraint;
use PHPUnit_Framework_Constraint as Constraint;
use PHPUnit_Framework_TestCase as TestCase;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\TestCase;

abstract class TestCaseRelatedConstraint extends Constraint
{
Expand All @@ -12,7 +12,6 @@ abstract class TestCaseRelatedConstraint extends Constraint

public function __construct(TestCase $test_case)
{
parent::__construct();
$this->testCase = $test_case;
}
}
7 changes: 5 additions & 2 deletions src/Listener/StopwatchListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
16 changes: 10 additions & 6 deletions tests/AssertsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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()
);
Expand All @@ -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()
);
Expand All @@ -53,16 +53,20 @@ 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?)
);
}

/**
*/
public function test_getExecutionTime()
{
$this->sleep(1.2);
$this->assertEquals(1.2, $this->getExecutionTime(), '', 0.01);
$this->assertEqualsWithDelta(1.2, $this->getExecutionTime(), 0.1);
}

/**
Expand Down