generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from ergebnis/feature/test-passed-subscriber
Enhancement: Implement TestPassedSubscriber
- Loading branch information
Showing
3 changed files
with
158 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2021 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/phpunit-slow-test-collector | ||
*/ | ||
|
||
namespace Ergebnis\PHPUnit\SlowTestCollector\Subscriber; | ||
|
||
use Ergebnis\PHPUnit\SlowTestCollector\SlowTestCollector; | ||
use PHPUnit\Event; | ||
|
||
final class TestPassedSubscriber implements Event\Test\PassedSubscriber | ||
{ | ||
private SlowTestCollector $slowTestCollector; | ||
|
||
public function __construct(SlowTestCollector $slowTestCollector) | ||
{ | ||
$this->slowTestCollector = $slowTestCollector; | ||
} | ||
|
||
public function notify(Event\Test\Passed $event): void | ||
{ | ||
$this->slowTestCollector->testPassed( | ||
$event->test(), | ||
$event->telemetryInfo()->time() | ||
); | ||
} | ||
} |
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,121 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2021 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/phpunit-slow-test-collector | ||
*/ | ||
|
||
namespace Ergebnis\PHPUnit\SlowTestCollector\Test\Unit\Subscriber; | ||
|
||
use Ergebnis\PHPUnit\SlowTestCollector\SlowTest; | ||
use Ergebnis\PHPUnit\SlowTestCollector\SlowTestCollector; | ||
use Ergebnis\PHPUnit\SlowTestCollector\Subscriber\TestPassedSubscriber; | ||
use Ergebnis\Test\Util; | ||
use PHPUnit\Event; | ||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \Ergebnis\PHPUnit\SlowTestCollector\Subscriber\TestPassedSubscriber | ||
* | ||
* @uses \Ergebnis\PHPUnit\SlowTestCollector\SlowTest | ||
* @uses \Ergebnis\PHPUnit\SlowTestCollector\SlowTestCollector | ||
*/ | ||
final class TestPassedSubscriberTest extends Framework\TestCase | ||
{ | ||
use Util\Helper; | ||
|
||
public function testNotifyCollectsPreparedTest(): void | ||
{ | ||
$faker = self::faker(); | ||
|
||
$maximumDuration = Event\Telemetry\Duration::fromSeconds($faker->numberBetween( | ||
5, | ||
10 | ||
)); | ||
|
||
$preparedTime = Event\Telemetry\HRTime::fromSecondsAndNanoseconds( | ||
$faker->numberBetween(), | ||
0 | ||
); | ||
|
||
$preparedTest = self::createTest('test'); | ||
|
||
$passedTime = Event\Telemetry\HRTime::fromSecondsAndNanoseconds( | ||
$preparedTime->seconds() + $maximumDuration->seconds() + 1, | ||
0 | ||
); | ||
|
||
$passedTest = clone $preparedTest; | ||
|
||
$passedTestEvent = new Event\Test\Passed( | ||
self::createTelemetryInfo($passedTime), | ||
$preparedTest | ||
); | ||
|
||
$slowTestCollector = new SlowTestCollector($maximumDuration); | ||
|
||
$slowTestCollector->testPrepared( | ||
$preparedTest, | ||
$preparedTime | ||
); | ||
|
||
$subscriber = new TestPassedSubscriber($slowTestCollector); | ||
|
||
$subscriber->notify($passedTestEvent); | ||
|
||
$expected = [ | ||
SlowTest::fromTestAndDuration( | ||
$passedTest, | ||
$passedTime->duration($preparedTime) | ||
), | ||
]; | ||
|
||
self::assertEquals($expected, $slowTestCollector->slowTests()); | ||
} | ||
|
||
private static function createTelemetryInfo(Event\Telemetry\HRTime $time): Event\Telemetry\Info | ||
{ | ||
$faker = self::faker(); | ||
|
||
return new Event\Telemetry\Info( | ||
new Event\Telemetry\Snapshot( | ||
$time, | ||
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween()), | ||
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween()) | ||
), | ||
Event\Telemetry\Duration::fromSeconds($faker->numberBetween()), | ||
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween()), | ||
Event\Telemetry\Duration::fromSeconds($faker->numberBetween()), | ||
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween()), | ||
); | ||
} | ||
|
||
private static function createTest(string $methodName): Event\Code\Test | ||
{ | ||
$faker = self::faker(); | ||
|
||
$methodNameWithDataSet = \sprintf( | ||
'%s with data set #%d', | ||
$methodName, | ||
$faker->numberBetween() | ||
); | ||
|
||
if ($faker->boolean) { | ||
$methodNameWithDataSet = $methodName; | ||
} | ||
|
||
return new Event\Code\Test( | ||
self::class, | ||
$methodName, | ||
$methodNameWithDataSet | ||
); | ||
} | ||
} |