Skip to content

Commit

Permalink
Merge pull request #70 from spiral/dispatchers
Browse files Browse the repository at this point in the history
Add compatibility with Spiral Framework 3.12.0 dispatchers
  • Loading branch information
butschster committed Jan 22, 2024
2 parents 03808a8 + 912871b commit 8b279a6
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/Traits/InteractsWithDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait InteractsWithDispatcher
public function assertDispatcherCanBeServed(string $dispatcher): void
{
$this->assertTrue(
$this->getContainer()->get($dispatcher)->canServe(),
$this->getContainer()->invoke([$dispatcher, 'canServe']),
\sprintf('Dispatcher [%s] can not be served.', $dispatcher)
);
}
Expand All @@ -26,7 +26,7 @@ public function assertDispatcherCanBeServed(string $dispatcher): void
public function assertDispatcherCannotBeServed(string $dispatcher): void
{
$this->assertFalse(
$this->getContainer()->get($dispatcher)->canServe(),
$this->getContainer()->invoke([$dispatcher, 'canServe']),
\sprintf('Dispatcher [%s] can be served.', $dispatcher)
);
}
Expand Down Expand Up @@ -77,8 +77,8 @@ public function assertDispatcherMissed(string $dispatcher): void
*/
public function getRegisteredDispatchers(): array
{
return array_map(static function ($dispatcher): string {
return get_class($dispatcher);
return \array_map(static function ($dispatcher): string {
return \is_object($dispatcher) ? $dispatcher::class : $dispatcher;
}, $this->getContainer()->get(KernelInterface::class)->getRegisteredDispatchers());
}
}
8 changes: 6 additions & 2 deletions src/Traits/TestableKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ public function getContainer(): Container
return $this->container;
}

/** @return DispatcherInterface[] */
/** @return array<class-string<DispatcherInterface>> */
public function getRegisteredDispatchers(): array
{
return $this->dispatchers;
return \array_map(static fn (string|DispatcherInterface $dispatcher): string => \is_object($dispatcher)
? $dispatcher::class
: $dispatcher,
$this->dispatchers
);
}

/** @return array<class-string> */
Expand Down
85 changes: 85 additions & 0 deletions tests/src/Traits/InteractsWithDispatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\Traits;

use Spiral\Boot\DispatcherInterface;
use Spiral\Boot\Environment;
use Spiral\Boot\EnvironmentInterface;
use Spiral\Testing\TestCase;
use Spiral\Testing\Traits\InteractsWithDispatcher;

/**
* @coversDefaultClass InteractsWithDispatcher
*/
final class InteractsWithDispatcherTest extends TestCase
{
public function testAssertDispatcherCanBeServed(): void
{
$dispatcher = new class implements DispatcherInterface {
public function canServe(): bool
{
return true;
}

public function serve(): void {}
};

$this->assertDispatcherCanBeServed($dispatcher::class);
}

public function testAssertDispatcherCanBeServedStaticMethodWithEnv(): void
{
$dispatcher = new class {
public function canServe(EnvironmentInterface $env): bool
{
return $env->get('MODE') === 'http';
}
};

$this->getContainer()->bindSingleton(EnvironmentInterface::class, new Environment(['MODE' => 'http']), true);
$this->assertDispatcherCanBeServed($dispatcher::class);
}

public function testAssertDispatcherCannotBeServed(): void
{
$dispatcher = new class implements DispatcherInterface {
public function canServe(): bool
{
return false;
}

public function serve(): void {}
};

$this->assertDispatcherCannotBeServed($dispatcher::class);
}

public function testAssertDispatcherCannotBeServedStaticMethodWithEnv(): void
{
$dispatcher = new class {
public function canServe(EnvironmentInterface $env): bool
{
return $env->get('MODE') === 'http';
}
};

$this->getContainer()->bindSingleton(EnvironmentInterface::class, new Environment(['MODE' => 'jobs']), true);
$this->assertDispatcherCannotBeServed($dispatcher::class);
}

public function testGetRegisteredDispatchers(): void
{
$dispatcherA = $this->createMock(DispatcherInterface::class);
$dispatcherB = $this->createMock(DispatcherInterface::class);

$ref = new \ReflectionProperty($this->getApp(), 'dispatchers');
$ref->setValue($this->getApp(), [$dispatcherA, $dispatcherB::class]);

$this->assertSame(
[$dispatcherA::class, $dispatcherB::class],
$this->getRegisteredDispatchers(),
);
}
}

0 comments on commit 8b279a6

Please sign in to comment.