Skip to content

Commit 1dcacae

Browse files
authored
feat(discovery): allow exceptions on DoNotDiscover classes (#521)
1 parent 1d3da1a commit 1dcacae

File tree

4 files changed

+73
-8
lines changed

4 files changed

+73
-8
lines changed

src/Tempest/Core/src/DoNotDiscover.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,12 @@
99
#[Attribute(Attribute::TARGET_CLASS)]
1010
final class DoNotDiscover
1111
{
12+
public function __construct(
13+
/**
14+
* Allows the specified `Discovery` classes to still discover this class.
15+
* @var array<class-string<\Tempest\Core\Discovery>>
16+
*/
17+
public readonly array $except = [],
18+
) {
19+
}
1220
}

src/Tempest/Core/src/Kernel/LoadDiscoveryClasses.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,10 @@ public function __invoke(): void
9595
}
9696
}
9797

98-
if ($input instanceof ClassReflector && $input->hasAttribute(DoNotDiscover::class)) {
99-
continue;
100-
}
101-
10298
if ($input instanceof ClassReflector) {
103-
$discovery->discover($input);
99+
if ($this->shouldDiscover($discovery, $input)) {
100+
$discovery->discover($input);
101+
}
104102
} elseif ($discovery instanceof DiscoversPath) {
105103
$discovery->discoverPath($input);
106104
}
@@ -114,4 +112,13 @@ public function __invoke(): void
114112
}
115113
}
116114
}
115+
116+
private function shouldDiscover(Discovery $discovery, ClassReflector $input): bool
117+
{
118+
if (is_null($attribute = $input->getAttribute(DoNotDiscover::class))) {
119+
return true;
120+
}
121+
122+
return in_array($discovery::class, $attribute->except, strict: true);
123+
}
117124
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Tempest\Fixtures\Discovery;
6+
7+
use Tempest\Core\DoNotDiscover;
8+
use Tempest\Database\Migration;
9+
use Tempest\Database\MigrationDiscovery;
10+
use Tempest\Database\QueryStatement;
11+
12+
#[DoNotDiscover(except: [MigrationDiscovery::class])]
13+
final class HiddenMigratableMigration implements Migration
14+
{
15+
public function getName(): string
16+
{
17+
return 'hidden-migratable-migration';
18+
}
19+
20+
public function up(): ?QueryStatement
21+
{
22+
return null;
23+
}
24+
25+
public function down(): ?QueryStatement
26+
{
27+
return null;
28+
}
29+
}

tests/Integration/Core/LoadDiscoveryClassesTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
namespace Tests\Tempest\Integration\Core;
66

7-
use function PHPUnit\Framework\assertNotContains;
7+
use PHPUnit\Framework\Attributes\Test;
88
use Tempest\Core\Kernel\LoadDiscoveryClasses;
99
use Tempest\Database\DatabaseConfig;
1010
use Tempest\Database\MigrationDiscovery;
1111
use function Tempest\get;
12+
use Tests\Tempest\Fixtures\Discovery\HiddenMigratableMigration;
1213
use Tests\Tempest\Fixtures\Discovery\HiddenMigration;
1314
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
1415

@@ -17,7 +18,8 @@
1718
*/
1819
final class LoadDiscoveryClassesTest extends FrameworkIntegrationTestCase
1920
{
20-
public function test_hidden_from_discovery(): void
21+
#[Test]
22+
public function do_not_discover(): void
2123
{
2224
$this->kernel->discoveryClasses = [
2325
MigrationDiscovery::class,
@@ -31,6 +33,25 @@ public function test_hidden_from_discovery(): void
3133

3234
$migrations = get(DatabaseConfig::class)->getMigrations();
3335

34-
assertNotContains(HiddenMigration::class, $migrations);
36+
$this->assertNotContains(HiddenMigration::class, $migrations);
37+
}
38+
39+
#[Test]
40+
public function do_not_discover_except(): void
41+
{
42+
$this->kernel->discoveryClasses = [
43+
MigrationDiscovery::class,
44+
// TODO: update tests to add `PublishDiscovery` when it's merged
45+
];
46+
47+
$this->kernel->discoveryLocations = [
48+
realpath(__DIR__.'../../Fixtures/Discovery'),
49+
];
50+
51+
(new LoadDiscoveryClasses($this->kernel, $this->container));
52+
53+
$migrations = get(DatabaseConfig::class)->getMigrations();
54+
55+
$this->assertContains(HiddenMigratableMigration::class, $migrations);
3556
}
3657
}

0 commit comments

Comments
 (0)