Skip to content
Open
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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2014-2017 MyBuilder Limited
Copyright (c) 2014-2018 MyBuilder Limited

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

[![Build Status](https://secure.travis-ci.org/mybuilder/phpunit-accelerator.svg?branch=master)](http://travis-ci.org/mybuilder/phpunit-accelerator)

Inspired by [Kris Wallsmith faster PHPUnit article](http://kriswallsmith.net/post/18029585104/faster-phpunit), we've created a [PHPUnit](http://phpunit.de) test listener that speeds up PHPUnit tests about 20% by freeing memory.
*Depreciation *
Our latest benchmarking shows that there is now little improvement with PHPUnit Accelerator.
PHPUnit itself has made changes which do a lot of this now. Our plan is not to update this going forward unless we can find other areas we can speed up.

## Installation

To install this library, run the command below and you will get the latest version

``` bash
```bash
composer require mybuilder/phpunit-accelerator --dev
```

Expand Down Expand Up @@ -36,8 +38,8 @@ As an example, if we hypothetically wanted to ignore all tests which include "Le
use MyBuilder\PhpunitAccelerator\IgnoreTestPolicy;

class IgnoreLegacyTestPolicy implements IgnoreTestPolicy {
public function shouldIgnore(\ReflectionObject $testReflection) {
return strpos($testReflection->getFilename(), 'Legacy') !== false;
public function shouldIgnore(\ReflectionObject $testReflection): bool {
return strpos($testReflection->getFileName(), 'Legacy') !== false;
}
}
```
Expand All @@ -56,6 +58,9 @@ And pass it to the constructor of our test listener in `phpunit.xml` configurati
</phpunit>
```

Inspired by [Kris Wallsmith faster PHPUnit article](http://kriswallsmith.net/post/18029585104/faster-phpunit), we've created a [PHPUnit](http://phpunit.de) test listener that speeds up PHPUnit tests about 20% by freeing memory.

---

Created by [MyBuilder](http://www.mybuilder.com/) - Check out our [blog](http://tech.mybuilder.com/) for more insight into this and other open-source projects we release.
Created by [MyBuilder](https://www.mybuilder.com/) - Check out our [blog](https://tech.mybuilder.com/) for more insight into this and other open-source projects we release.
We are always looking to hire good people for our London office.
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "true"
syntaxCheck = "false"
bootstrap = "./vendor/autoload.php" >

<testsuites>
Expand Down
5 changes: 1 addition & 4 deletions src/IgnoreTestPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@

interface IgnoreTestPolicy
{
/**
* @return boolean
*/
public function shouldIgnore(\ReflectionObject $testReflection);
public function shouldIgnore(\ReflectionObject $testReflection):bool;
}
13 changes: 7 additions & 6 deletions src/TestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@

use PHPUnit\Framework\TestListener as BaseTestListener;
use PHPUnit\Framework\TestListenerDefaultImplementation;
use PHPUnit\Framework\Test;

class TestListener implements BaseTestListener
{
use TestListenerDefaultImplementation;

private $ignorePolicy;

const PHPUNIT_PROPERTY_PREFIX = 'PHPUnit_';
private const PHPUNIT_PROPERTY_PREFIX = 'PHPUnit_';

public function __construct(IgnoreTestPolicy $ignorePolicy = null)
{
$this->ignorePolicy = ($ignorePolicy) ?: new NeverIgnoreTestPolicy();
$this->ignorePolicy = $ignorePolicy ?: new NeverIgnoreTestPolicy();
}

public function endTest(\PHPUnit\Framework\Test $test, float $time): void
public function endTest(Test $test, float $time): void
{
$testReflection = new \ReflectionObject($test);

Expand All @@ -29,7 +30,7 @@ public function endTest(\PHPUnit\Framework\Test $test, float $time): void
$this->safelyFreeProperties($test, $testReflection->getProperties());
}

private function safelyFreeProperties(\PHPUnit\Framework\Test $test, array $properties)
private function safelyFreeProperties(Test $test, array $properties)
{
foreach ($properties as $property) {
if ($this->isSafeToFreeProperty($property)) {
Expand All @@ -48,7 +49,7 @@ private function isNotPhpUnitProperty(\ReflectionProperty $property)
return 0 !== strpos($property->getDeclaringClass()->getName(), self::PHPUNIT_PROPERTY_PREFIX);
}

private function freeProperty(\PHPUnit\Framework\Test $test, \ReflectionProperty $property)
private function freeProperty(Test $test, \ReflectionProperty $property)
{
$property->setAccessible(true);
$property->setValue($test, null);
Expand All @@ -57,7 +58,7 @@ private function freeProperty(\PHPUnit\Framework\Test $test, \ReflectionProperty

class NeverIgnoreTestPolicy implements IgnoreTestPolicy
{
public function shouldIgnore(\ReflectionObject $testReflection)
public function shouldIgnore(\ReflectionObject $testReflection): bool
{
return false;
}
Expand Down
26 changes: 9 additions & 17 deletions tests/TestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,51 @@

class TestListenerTest extends \PHPUnit\Framework\TestCase
{
/** @var DummyTest */
private $dummyTest;

protected function setUp()
{
$this->dummyTest = new DummyTest();
}

/**
* @test
*/
public function shouldFreeTestProperty()
public function testShouldFreeTestProperty():void
{
$this->endTest(new TestListener());

$this->assertFreesTestProperty();
}

private function endTest(TestListener $listener)
private function endTest(TestListener $listener):void
{
$listener->endTest($this->dummyTest, 0);
}

private function assertFreesTestProperty()
private function assertFreesTestProperty():void
{
$this->assertNull($this->dummyTest->property);
}

/**
* @test
*/
public function shouldNotFreePhpUnitProperty()
public function testShouldNotFreePhpUnitProperty():void
{
$this->endTest(new TestListener());

$this->assertDoesNotFreePHPUnitProperty();
}

private function assertDoesNotFreePHPUnitProperty()
private function assertDoesNotFreePHPUnitProperty():void
{
$this->assertNotNull($this->dummyTest->phpUnitProperty);
}

/**
* @test
*/
public function shouldNotFreeTestPropertyWithIgnoreAlwaysPolicy()
public function testShouldNotFreeTestPropertyWithIgnoreAlwaysPolicy(): void
{
$this->endTest(new TestListener(new AlwaysIgnoreTestPolicy()));

$this->assertDoesNotFreeTestProperty();
}

private function assertDoesNotFreeTestProperty()
private function assertDoesNotFreeTestProperty():void
{
$this->assertNotNull($this->dummyTest->property);
}
Expand All @@ -75,7 +67,7 @@ class DummyTest extends \PHPUnit_Fake

class AlwaysIgnoreTestPolicy implements IgnoreTestPolicy
{
public function shouldIgnore(\ReflectionObject $testReflection)
public function shouldIgnore(\ReflectionObject $testReflection): bool
{
return true;
}
Expand Down