|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of PHPUnit resources helpers library |
| 4 | + * |
| 5 | + * @copyright Copyright (c) Erwane BRETON |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace ResourceHelper\Test\TestCase\Extension; |
| 12 | + |
| 13 | +use Composer\InstalledVersions; |
| 14 | +use PHPUnit\Event\Code; |
| 15 | +use PHPUnit\Event\Telemetry; |
| 16 | +use PHPUnit\Event\Test\AfterTestMethodFinished; |
| 17 | +use PHPUnit\Event\TestData\TestDataCollection; |
| 18 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use PHPUnit\Metadata\MetadataCollection; |
| 21 | +use ResourceHelper\Extension\CleanupSubscriber; |
| 22 | +use ResourceHelper\ResourceHelper; |
| 23 | +use function hrtime; |
| 24 | + |
| 25 | +#[CoversClass(CleanupSubscriber::class)] |
| 26 | +class CleanupSubscriberTest extends TestCase |
| 27 | +{ |
| 28 | + final protected function telemetryInfo(): Telemetry\Info |
| 29 | + { |
| 30 | + return new Telemetry\Info( |
| 31 | + new Telemetry\Snapshot( |
| 32 | + Telemetry\HRTime::fromSecondsAndNanoseconds(...hrtime()), |
| 33 | + Telemetry\MemoryUsage::fromBytes(1000), |
| 34 | + Telemetry\MemoryUsage::fromBytes(2000), |
| 35 | + new Telemetry\GarbageCollectorStatus(0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, false, false, false, 0), |
| 36 | + ), |
| 37 | + Telemetry\Duration::fromSecondsAndNanoseconds(123, 456), |
| 38 | + Telemetry\MemoryUsage::fromBytes(2000), |
| 39 | + Telemetry\Duration::fromSecondsAndNanoseconds(234, 567), |
| 40 | + Telemetry\MemoryUsage::fromBytes(3000), |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + final protected function testValueObject(): Code\TestMethod |
| 45 | + { |
| 46 | + return new Code\TestMethod( |
| 47 | + 'FooTest', |
| 48 | + 'testBar', |
| 49 | + 'FooTest.php', |
| 50 | + 1, |
| 51 | + Code\TestDoxBuilder::fromClassNameAndMethodName('Foo', 'bar'), |
| 52 | + MetadataCollection::fromArray([]), |
| 53 | + TestDataCollection::fromArray([]), |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + public function testNotify(): void |
| 58 | + { |
| 59 | + ResourceHelper::createTmpTestDir($this); |
| 60 | + |
| 61 | + $sub = new CleanupSubscriber(); |
| 62 | + |
| 63 | + $telemetryInfo = $this->telemetryInfo(); |
| 64 | + $phpunitVersion = InstalledVersions::getVersion('phpunit/phpunit'); |
| 65 | + if (version_compare($phpunitVersion, '12.0.0', '>=')) { |
| 66 | + $test = $this->testValueObject(); |
| 67 | + } else { |
| 68 | + $test = 'testNotify'; |
| 69 | + } |
| 70 | + $calledMethods = [ |
| 71 | + new Code\ClassMethod(static::class, 'testNotify'), |
| 72 | + ]; |
| 73 | + |
| 74 | + $event = new AfterTestMethodFinished($telemetryInfo, $test, ...$calledMethods); |
| 75 | + |
| 76 | + $sub->notify($event); |
| 77 | + |
| 78 | + $this->assertDirectoryDoesNotExist(ResourceHelper::getTmpTestPath($this)); |
| 79 | + } |
| 80 | +} |
0 commit comments