Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz committed Jan 17, 2024
1 parent 1f3f453 commit 912871b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Traits/InteractsWithDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public function assertDispatcherMissed(string $dispatcher): void
*/
public function getRegisteredDispatchers(): array
{
return $this->getContainer()->get(KernelInterface::class)->getRegisteredDispatchers();
return \array_map(static function ($dispatcher): string {
return \is_object($dispatcher) ? $dispatcher::class : $dispatcher;
}, $this->getContainer()->get(KernelInterface::class)->getRegisteredDispatchers());
}
}
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 912871b

Please sign in to comment.