Skip to content

Commit a606c0f

Browse files
committed
Testing
1 parent d991965 commit a606c0f

9 files changed

Lines changed: 118 additions & 31 deletions

File tree

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"nesbot/carbon": "^3.8",
7575
"nyholm/psr7": "^1.8",
7676
"patrickbussmann/oauth2-apple": "^0.3",
77+
"php-di/php-di": "^7.0",
7778
"phpat/phpat": "^0.11.0",
7879
"phpbench/phpbench": "^1.4",
7980
"phpstan/phpstan": "2.1.40",
@@ -218,6 +219,7 @@
218219
"Tempest\\Database\\Tests\\": "packages/database/tests",
219220
"Tempest\\DateTime\\Tests\\": "packages/datetime/tests",
220221
"Tempest\\Debug\\Tests\\": "packages/debug/tests",
222+
"Tempest\\Discovery\\Tests\\": "packages/discovery/tests",
221223
"Tempest\\EventBus\\Tests\\": "packages/event-bus/tests",
222224
"Tempest\\Generation\\Tests\\": "packages/generation/tests",
223225
"Tempest\\HttpClient\\Tests\\": "packages/http-client/tests",

packages/core/src/Commands/DiscoveryGenerateCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ public function __invoke(): void
4545

4646
$this->clearDiscoveryCache();
4747

48-
$this->console->task(
49-
label: "Generating discovery cache using the `{$strategy->value}` strategy",
50-
handler: fn (Closure $log) => $this->generateDiscoveryCache($strategy, $log),
51-
);
48+
$this->generateDiscoveryCache($strategy, fn () => null);
49+
// $this->console->task(
50+
// label: "Generating discovery cache using the `{$strategy->value}` strategy",
51+
// handler: fn (Closure $log) => $this->generateDiscoveryCache($strategy, $log),
52+
// );
5253
}
5354

5455
public function clearDiscoveryCache(): void

packages/discovery/composer.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,18 @@
1010
"psr/container": "^2.0",
1111
"symfony/cache": "^7.3"
1212
},
13+
"require-dev": {
14+
"tempest/container": "3.x-dev",
15+
"php-di/php-di": "^7.0"
16+
},
1317
"autoload": {
1418
"psr-4": {
1519
"Tempest\\Discovery\\": "src"
1620
}
21+
},
22+
"autoload-dev": {
23+
"psr-4": {
24+
"Tempest\\Discovery\\Tests": "tests"
25+
}
1726
}
1827
}

packages/discovery/src/BootDiscovery.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public function build(
5050

5151
if ($discoveryClasses === null) {
5252
// DiscoveryDiscovery needs to be applied before we can build all other discoveries
53-
$discoveryDiscovery = $this->resolveDiscovery(DiscoveryDiscovery::class);
54-
$discoveryDiscovery->setRegistry($this->registry);
53+
$discoveryDiscovery = new DiscoveryDiscovery($this->registry);
54+
$discoveryDiscovery->setItems(new DiscoveryItems());
5555

5656
// The first pass over all directories to find all discovery classes
5757
$this->discover([$discoveryDiscovery], $discoveryLocations);

packages/discovery/src/DiscoveryDiscovery.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@ final class DiscoveryDiscovery implements Discovery
1010
{
1111
use IsDiscovery;
1212

13-
private ?Registry $registry = null;
14-
15-
public function setRegistry(Registry $registry): self
16-
{
17-
$this->registry = $registry;
18-
19-
return $this;
20-
}
13+
public function __construct(
14+
private readonly Registry $registry,
15+
) {}
2116

2217
public function discover(DiscoveryLocation $location, ClassReflector $class): void
2318
{
@@ -34,10 +29,6 @@ public function discover(DiscoveryLocation $location, ClassReflector $class): vo
3429

3530
public function apply(): void
3631
{
37-
if ($this->registry === null) {
38-
throw new RegistryWasNotSet();
39-
}
40-
4132
foreach ($this->discoveryItems as $className) {
4233
$this->registry->classes[] = $className;
4334
}

packages/discovery/src/RegistryWasNotSet.php

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Tempest\tests;
4+
5+
use DI\Container;
6+
use PHPUnit\Framework\TestCase;
7+
use Tempest\Container\GenericContainer;
8+
use Tempest\Discovery\BootDiscovery;
9+
use Tempest\Discovery\DiscoveryLocation;
10+
use Tempest\Discovery\Registry;
11+
use Tempest\Discovery\Tests\Fixtures\MyDiscoveryClass;
12+
13+
final class DiscoveryTest extends TestCase
14+
{
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
MyDiscoveryClass::$discoveredItem = null;
20+
}
21+
22+
public function test_standalone_discovery(): void
23+
{
24+
$container = new GenericContainer();
25+
26+
new BootDiscovery(
27+
container: $container,
28+
registry: new Registry(locations: [
29+
new DiscoveryLocation(
30+
namespace: 'Tempest\Discovery\Tests\Fixtures',
31+
path: __DIR__ . '/Fixtures',
32+
),
33+
]),
34+
)();
35+
36+
self::assertNotNull(MyDiscoveryClass::$discoveredItem);
37+
$this->assertSame('check', MyDiscoveryClass::$discoveredItem->name);
38+
}
39+
40+
public function test_discovery_with_other_container(): void
41+
{
42+
$container = new Container();
43+
44+
new BootDiscovery(
45+
container: $container,
46+
registry: new Registry(locations: [
47+
new DiscoveryLocation(
48+
namespace: 'Tempest\Discovery\Tests\Fixtures',
49+
path: __DIR__ . '/Fixtures',
50+
),
51+
]),
52+
)();
53+
54+
self::assertNotNull(MyDiscoveryClass::$discoveredItem);
55+
$this->assertSame('check', MyDiscoveryClass::$discoveredItem->name);
56+
}
57+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Tempest\Discovery\Tests\Fixtures;
4+
5+
final class DiscoveredItem
6+
{
7+
public function __construct(
8+
public string $name,
9+
) {}
10+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Tempest\Discovery\Tests\Fixtures;
4+
5+
use Tempest\Discovery\Discovery;
6+
use Tempest\Discovery\DiscoveryLocation;
7+
use Tempest\Discovery\IsDiscovery;
8+
use Tempest\Reflection\ClassReflector;
9+
10+
final class MyDiscoveryClass implements Discovery
11+
{
12+
use IsDiscovery;
13+
14+
public static ?DiscoveredItem $discoveredItem = null;
15+
16+
public function discover(DiscoveryLocation $location, ClassReflector $class): void
17+
{
18+
if ($class->is(DiscoveredItem::class)) {
19+
$this->discoveryItems->add($location, $class);
20+
}
21+
}
22+
23+
public function apply(): void
24+
{
25+
/** @var ClassReflector $class */
26+
foreach ($this->discoveryItems as $class) {
27+
self::$discoveredItem = $class->newInstanceArgs(['name' => 'check']);
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)