Skip to content

Commit 57cd5fa

Browse files
authored
Merge pull request #1 from Roave/feature/detect-memory-leaks
Detect memory leaks in test runs
2 parents 0cb0763 + 187eba6 commit 57cd5fa

25 files changed

+1243
-0
lines changed

.gitattributes

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/test export-ignore
2+
.gitattributes export-ignore
3+
.gitignore export-ignore
4+
.travis.install.sh export-ignore
5+
.travis.yml export-ignore
6+
infection.json.dist export-ignore
7+
phpcs.xml.dist export-ignore
8+
phpstan.neon.dist export-ignore
9+
phpunit.xml.dist export-ignore
10+
psalm.xml.dist export-ignore

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
vendor
22
.phpunit.result.cache
3+
phpunit.xml
4+
composer.lock
5+
phpstan.neon
6+
psalm.xml
7+
phpcs.xml
8+
infection.json
9+
infection.log

.travis.install.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
set -x
2+
3+
IGNORE_PLATFORM_REQUIREMENTS=""
4+
5+
if [ "$TRAVIS_PHP_VERSION" = 'nightly' ] ; then
6+
IGNORE_PLATFORM_REQUIREMENTS="--ignore-platform-reqs"
7+
fi
8+
9+
composer update $IGNORE_PLATFORM_REQUIREMENTS
10+
11+
if [ "$DEPENDENCIES" = 'low' ] ; then
12+
composer update --prefer-lowest --prefer-stable $IGNORE_PLATFORM_REQUIREMENTS
13+
fi

.travis.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
language: php
2+
3+
sudo: false
4+
5+
php:
6+
- 7.3
7+
- 7.4snapshot
8+
- nightly
9+
10+
env:
11+
matrix:
12+
- DEPENDENCIES="high"
13+
- DEPENDENCIES="low"
14+
15+
before_script:
16+
- sh .travis.install.sh
17+
18+
script:
19+
- ./vendor/bin/phpstan analyse
20+
- ./vendor/bin/psalm
21+
- ./vendor/bin/phpunit
22+
- ./vendor/bin/phpcs
23+
- phpdbg -qrr ./vendor/bin/infection -vvv --test-framework-options='--testsuite=unit' --min-msi=98 --min-covered-msi=98
24+
25+
matrix:
26+
allow_failures:
27+
- php: nightly

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Roave, LLC
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# roave/no-leaks
2+
3+
This library is a [PHPUnit](https://github.com/sebastianbergmann/phpunit) plugin
4+
that detects memory leaks in tested code or tests.
5+
6+
## Installation
7+
8+
```sh
9+
composer require --dev roave/no-leaks
10+
```
11+
12+
## Usage
13+
14+
In your `phpunit.xml` configuration, add following section:
15+
16+
```xml
17+
<listeners>
18+
<listener class="Roave\NoLeaks\PHPUnit\CollectTestExecutionMemoryFootprints"/>
19+
</listeners>
20+
21+
<extensions>
22+
<extension class="Roave\NoLeaks\PHPUnit\CollectTestExecutionMemoryFootprints"/>
23+
</extensions>
24+
```
25+
26+
Then run:s
27+
28+
```sh
29+
vendor/bin/roave-no-leaks
30+
```
31+
32+
If any memory leaks are detected, you should see an output like
33+
following:
34+
35+
```
36+
Exception: The following test produced memory leaks:
37+
* My\Leaky\Test::testSomething
38+
* My\Leaky\Test::testSomethingElse
39+
```
40+
41+
## Configuration and parameters
42+
43+
[`vendor/bin/roave-no-leaks`](./bin/roave-no-leaks) supports all
44+
configuration parameters and console parameters of PHPUnit.
45+
46+
## Known issues
47+
48+
Please be aware that this is not a full substitute for PHPUnit:
49+
50+
* the output format is to be improved
51+
* memory leak detection for scalar types and arrays is not reliable
52+
* can fail depending on xdebug/phpdbg/php-sapi changes
53+
54+
## Professional Support
55+
56+
If you need help with setting up this library in your project,
57+
you can contact us at team@roave.com for consulting/support.

bin/roave-no-leaks

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
declare(strict_types=1);
5+
6+
namespace Roave\NoLeaks\CLI;
7+
8+
(function () {
9+
require __DIR__ . '/roave-no-leaks.php';
10+
})();

bin/roave-no-leaks.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Roave\NoLeaks\CLI;
6+
7+
use PHPUnit\TextUI\Command;
8+
9+
(function () {
10+
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
11+
require_once __DIR__ . '/../vendor/autoload.php';
12+
} else {
13+
require_once __DIR__ . '/../../../vendor/autoload.php';
14+
}
15+
16+
$_SERVER['argv'][] = '--repeat=3';
17+
18+
Command::main();
19+
})();

composer.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "roave/no-leaks",
3+
"license": "MIT",
4+
"autoload": {
5+
"psr-4": {
6+
"Roave\\NoLeaks\\PHPUnit\\": "src"
7+
}
8+
},
9+
"autoload-dev": {
10+
"psr-4": {
11+
"RoaveE2ETest\\NoLeaks\\PHPUnit\\": "test/e2e",
12+
"RoaveUnitTest\\NoLeaks\\PHPUnit\\": "test/unit"
13+
}
14+
},
15+
"require": {
16+
"php": "^7.3",
17+
"ext-json": "*",
18+
"phpunit/phpunit": "^8.0.4"
19+
},
20+
"config": {
21+
"sort-packages": true
22+
},
23+
"bin": [
24+
"bin/roave-no-leaks"
25+
],
26+
"require-dev": {
27+
"doctrine/coding-standard": "^5.0",
28+
"infection/infection": "^0.12.2",
29+
"phpstan/phpstan": "^0.11.4",
30+
"phpstan/phpstan-phpunit": "^0.11.0",
31+
"phpstan/phpstan-strict-rules": "^0.11.0",
32+
"psalm/plugin-phpunit": "^0.5.3",
33+
"squizlabs/php_codesniffer": "^3.4",
34+
"vimeo/psalm": "^3.2"
35+
}
36+
}

infection.json.dist

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"timeout": 10,
3+
"source": {
4+
"directories": [
5+
"src"
6+
]
7+
},
8+
"logs": {
9+
"text": "infection.log"
10+
},
11+
"mutators": {
12+
"@default": true
13+
}
14+
}

0 commit comments

Comments
 (0)