Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibility with Spiral Framework 3.12.0 dispatchers #70

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
);
}
}
Loading