Skip to content

Commit 8b279a6

Browse files
authored
Merge pull request #70 from spiral/dispatchers
Add compatibility with Spiral Framework 3.12.0 dispatchers
2 parents 03808a8 + 912871b commit 8b279a6

File tree

3 files changed

+95
-6
lines changed

3 files changed

+95
-6
lines changed

src/Traits/InteractsWithDispatcher.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ trait InteractsWithDispatcher
1515
public function assertDispatcherCanBeServed(string $dispatcher): void
1616
{
1717
$this->assertTrue(
18-
$this->getContainer()->get($dispatcher)->canServe(),
18+
$this->getContainer()->invoke([$dispatcher, 'canServe']),
1919
\sprintf('Dispatcher [%s] can not be served.', $dispatcher)
2020
);
2121
}
@@ -26,7 +26,7 @@ public function assertDispatcherCanBeServed(string $dispatcher): void
2626
public function assertDispatcherCannotBeServed(string $dispatcher): void
2727
{
2828
$this->assertFalse(
29-
$this->getContainer()->get($dispatcher)->canServe(),
29+
$this->getContainer()->invoke([$dispatcher, 'canServe']),
3030
\sprintf('Dispatcher [%s] can be served.', $dispatcher)
3131
);
3232
}
@@ -77,8 +77,8 @@ public function assertDispatcherMissed(string $dispatcher): void
7777
*/
7878
public function getRegisteredDispatchers(): array
7979
{
80-
return array_map(static function ($dispatcher): string {
81-
return get_class($dispatcher);
80+
return \array_map(static function ($dispatcher): string {
81+
return \is_object($dispatcher) ? $dispatcher::class : $dispatcher;
8282
}, $this->getContainer()->get(KernelInterface::class)->getRegisteredDispatchers());
8383
}
8484
}

src/Traits/TestableKernel.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ public function getContainer(): Container
1515
return $this->container;
1616
}
1717

18-
/** @return DispatcherInterface[] */
18+
/** @return array<class-string<DispatcherInterface>> */
1919
public function getRegisteredDispatchers(): array
2020
{
21-
return $this->dispatchers;
21+
return \array_map(static fn (string|DispatcherInterface $dispatcher): string => \is_object($dispatcher)
22+
? $dispatcher::class
23+
: $dispatcher,
24+
$this->dispatchers
25+
);
2226
}
2327

2428
/** @return array<class-string> */
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\Testing\Tests\Traits;
6+
7+
use Spiral\Boot\DispatcherInterface;
8+
use Spiral\Boot\Environment;
9+
use Spiral\Boot\EnvironmentInterface;
10+
use Spiral\Testing\TestCase;
11+
use Spiral\Testing\Traits\InteractsWithDispatcher;
12+
13+
/**
14+
* @coversDefaultClass InteractsWithDispatcher
15+
*/
16+
final class InteractsWithDispatcherTest extends TestCase
17+
{
18+
public function testAssertDispatcherCanBeServed(): void
19+
{
20+
$dispatcher = new class implements DispatcherInterface {
21+
public function canServe(): bool
22+
{
23+
return true;
24+
}
25+
26+
public function serve(): void {}
27+
};
28+
29+
$this->assertDispatcherCanBeServed($dispatcher::class);
30+
}
31+
32+
public function testAssertDispatcherCanBeServedStaticMethodWithEnv(): void
33+
{
34+
$dispatcher = new class {
35+
public function canServe(EnvironmentInterface $env): bool
36+
{
37+
return $env->get('MODE') === 'http';
38+
}
39+
};
40+
41+
$this->getContainer()->bindSingleton(EnvironmentInterface::class, new Environment(['MODE' => 'http']), true);
42+
$this->assertDispatcherCanBeServed($dispatcher::class);
43+
}
44+
45+
public function testAssertDispatcherCannotBeServed(): void
46+
{
47+
$dispatcher = new class implements DispatcherInterface {
48+
public function canServe(): bool
49+
{
50+
return false;
51+
}
52+
53+
public function serve(): void {}
54+
};
55+
56+
$this->assertDispatcherCannotBeServed($dispatcher::class);
57+
}
58+
59+
public function testAssertDispatcherCannotBeServedStaticMethodWithEnv(): void
60+
{
61+
$dispatcher = new class {
62+
public function canServe(EnvironmentInterface $env): bool
63+
{
64+
return $env->get('MODE') === 'http';
65+
}
66+
};
67+
68+
$this->getContainer()->bindSingleton(EnvironmentInterface::class, new Environment(['MODE' => 'jobs']), true);
69+
$this->assertDispatcherCannotBeServed($dispatcher::class);
70+
}
71+
72+
public function testGetRegisteredDispatchers(): void
73+
{
74+
$dispatcherA = $this->createMock(DispatcherInterface::class);
75+
$dispatcherB = $this->createMock(DispatcherInterface::class);
76+
77+
$ref = new \ReflectionProperty($this->getApp(), 'dispatchers');
78+
$ref->setValue($this->getApp(), [$dispatcherA, $dispatcherB::class]);
79+
80+
$this->assertSame(
81+
[$dispatcherA::class, $dispatcherB::class],
82+
$this->getRegisteredDispatchers(),
83+
);
84+
}
85+
}

0 commit comments

Comments
 (0)