Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create PhpUnitHook for bypassing final classes
- Loading branch information
1 parent
5d2ac90
commit 6245070
Showing
16 changed files
with
323 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Bruha\CodingStandard\CustomHooks; | ||
|
||
use DG\BypassFinals; | ||
use PHPUnit\Runner\AfterLastTestHook; | ||
use PHPUnit\Runner\AfterTestHook; | ||
use PHPUnit\Runner\BeforeTestHook; | ||
|
||
/** | ||
* Class PhpUnitHook | ||
* | ||
* @package Bruha\CodingStandard\CustomHooks | ||
*/ | ||
final class PhpUnitHook implements BeforeTestHook, AfterTestHook, AfterLastTestHook | ||
{ | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $tests = []; | ||
|
||
/** | ||
* @var float | ||
*/ | ||
private $time = 0.0; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $count = 0; | ||
|
||
/** | ||
* @param string $test | ||
*/ | ||
public function executeBeforeTest(string $test): void | ||
{ | ||
$test; | ||
|
||
BypassFinals::enable(); | ||
} | ||
|
||
/** | ||
* @param string $test | ||
* @param float $time | ||
*/ | ||
public function executeAfterTest(string $test, float $time): void | ||
{ | ||
$this->tests[(string) $time] = $test; | ||
$this->time += $time; | ||
$this->count++; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function executeAfterLastTest(): void | ||
{ | ||
krsort($this->tests, SORT_NUMERIC); | ||
|
||
echo sprintf( | ||
'%s%s%07.3fs (100.000%%): Time analysis of %s tests:%s', | ||
PHP_EOL, | ||
PHP_EOL, | ||
$this->time, | ||
$this->count, | ||
PHP_EOL | ||
); | ||
|
||
foreach ($this->tests as $time => $name) { | ||
echo sprintf('%07.3fs (%07.3f%%): %s%s', $time, $time / $this->time * 100, $name, PHP_EOL); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Tests\Integration\CustomHooks; | ||
|
||
use Bruha\CodingStandard\CustomHooks\PhpUnitHook; | ||
use DG\BypassFinals; | ||
use Tests\AbstractTestCase; | ||
use Tests\PrivateTrait; | ||
|
||
/** | ||
* Class PhpUnitHookTest | ||
* | ||
* @package Tests\Integration\CustomHooks | ||
*/ | ||
final class PhpUnitHookTest extends AbstractTestCase | ||
{ | ||
|
||
use PrivateTrait; | ||
|
||
/** | ||
* @var PhpUnitHook | ||
*/ | ||
private $hook; | ||
|
||
/** | ||
* | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->hook = new PhpUnitHook(); | ||
} | ||
|
||
/** | ||
* @covers PhpUnitHook::executeBeforeTest | ||
*/ | ||
public function testExecuteBeforeTest(): void | ||
{ | ||
$this->hook->executeBeforeTest('Test'); | ||
|
||
self::assertTrue(in_array(BypassFinals::PROTOCOL, stream_get_wrappers(), TRUE)); | ||
} | ||
|
||
/** | ||
* @covers PhpUnitHook::executeAfterTest | ||
*/ | ||
public function testExecuteAfterTest(): void | ||
{ | ||
$this->hook->executeAfterTest('Test::test', 1); | ||
|
||
self::assertEquals(['1' => 'Test::test'], $this->getProperty($this->hook, 'tests')); | ||
self::assertEquals(1, $this->getProperty($this->hook, 'time')); | ||
self::assertEquals(1, $this->getProperty($this->hook, 'count')); | ||
} | ||
|
||
/** | ||
* @covers PhpUnitHook::executeAfterLastTest | ||
*/ | ||
public function testExecuteAfterLastTest(): void | ||
{ | ||
ob_start(); | ||
$this->hook->executeAfterTest('Test::test', 1); | ||
$this->hook->executeAfterLastTest(); | ||
$output = explode(PHP_EOL, (string) ob_get_contents()); | ||
ob_end_clean(); | ||
|
||
self::assertGreaterThan(0, count($output)); | ||
self::assertEquals('', array_pop($output)); | ||
self::assertEquals('', array_shift($output)); | ||
self::assertEquals('', array_shift($output)); | ||
self::assertRegExp( | ||
'/\d+\.\d+s \(100\.000%\): Time analysis of \d+ tests:/', | ||
(string) array_shift($output) | ||
); | ||
self::assertRegExp( | ||
'/\d+\.\d+s \(\d+\.\d+%\): \w+::\w+/', | ||
(string) array_shift($output) | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.