Skip to content

Commit 827aa77

Browse files
authored
Init container also for tests without dataprovider
1 parent b50ac58 commit 827aa77

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Testing\PHPUnit;
4+
5+
use PHPStan\DependencyInjection\InvalidIgnoredErrorExceptionTest;
6+
7+
final class ContainerInitializer
8+
{
9+
10+
public static function initialize(string $testClassName): void
11+
{
12+
// This test expects an exception during container initialization
13+
if ($testClassName === InvalidIgnoredErrorExceptionTest::class) {
14+
return;
15+
}
16+
17+
$testClassName::getContainer();
18+
}
19+
20+
}

src/Testing/PHPUnit/InitContainerBeforeDataProviderSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function notify(DataProviderMethodCalled $event): void
2020
return;
2121
}
2222

23-
$testClassName::getContainer();
23+
ContainerInitializer::initialize($testClassName);
2424
}
2525

2626
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Testing\PHPUnit;
4+
5+
use Override;
6+
use PHPStan\Testing\PHPStanTestCase;
7+
use PHPUnit\Event\Test\PreparationStarted;
8+
use PHPUnit\Event\Test\PreparationStartedSubscriber;
9+
use function is_a;
10+
11+
final class InitContainerBeforeTestSubscriber implements PreparationStartedSubscriber
12+
{
13+
14+
#[Override]
15+
public function notify(PreparationStarted $event): void
16+
{
17+
$test = $event->test();
18+
if (!$test->isTestMethod()) {
19+
// skip PHPT tests
20+
return;
21+
}
22+
23+
$testClassName = $test->className();
24+
25+
if (!is_a($testClassName, PhpStanTestCase::class, true)) {
26+
return;
27+
}
28+
29+
ContainerInitializer::initialize($testClassName);
30+
}
31+
32+
}

src/Testing/PHPUnit/PHPStanPHPUnitExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public function bootstrap(
2121
$facade->registerSubscriber(
2222
new InitContainerBeforeDataProviderSubscriber(),
2323
);
24+
$facade->registerSubscriber(
25+
new InitContainerBeforeTestSubscriber(),
26+
);
2427
}
2528

2629
}

0 commit comments

Comments
 (0)