Skip to content

Commit

Permalink
minor: add feature tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Nov 25, 2024
1 parent 05f50eb commit 23fa558
Show file tree
Hide file tree
Showing 11 changed files with 376 additions and 33 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"symfony/serializer": "^6.4|^7.0",
"symfony/var-dumper": "^6.4|^7.0",
"zenstruck/console-test": "^1.5",
"zenstruck/foundry": "^2.2"
"zenstruck/foundry": "^2.2",
"zenstruck/messenger-test": "^1.11"
},
"suggest": {
"knplabs/knp-time-bundle": "For human readable timestamps and durations on your dashboard.",
Expand Down
34 changes: 34 additions & 0 deletions tests/Feature/BundleServicesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the zenstruck/messenger-monitor-bundle package.
*
* (c) Kevin Bond <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Messenger\Monitor\Tests\Feature;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Zenstruck\Messenger\Monitor\Tests\Fixture\TestService;

/**
* @author Kevin Bond <[email protected]>
*/
final class BundleServicesTest extends KernelTestCase
{
/**
* @test
*/
public function autowires_services(): void
{
/** @var TestService $service */
$service = self::getContainer()->get(TestService::class);

$this->assertCount(1, $service->transports);
$this->assertCount(0, $service->workers);
$this->assertCount(0, $service->schedules);
}
}
168 changes: 168 additions & 0 deletions tests/Feature/HistoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php

/*
* This file is part of the zenstruck/messenger-monitor-bundle package.
*
* (c) Kevin Bond <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Messenger\Monitor\Tests\Feature;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\Exception\NoHandlerForMessageException;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;
use Zenstruck\Messenger\Monitor\Tests\Fixture\Factory\ProcessedMessageFactory;
use Zenstruck\Messenger\Monitor\Tests\Fixture\Message\MessageA;
use Zenstruck\Messenger\Monitor\Tests\Fixture\Message\MessageAHandler;
use Zenstruck\Messenger\Monitor\Tests\Fixture\Message\MessageB;
use Zenstruck\Messenger\Monitor\Tests\Fixture\Message\MessageC;
use Zenstruck\Messenger\Monitor\Tests\Fixture\Message\MessageCHandler1;
use Zenstruck\Messenger\Monitor\Tests\Fixture\Message\MessageCHandler2;
use Zenstruck\Messenger\Test\InteractsWithMessenger;

/**
* @author Kevin Bond <[email protected]>
*/
final class HistoryTest extends KernelTestCase
{
use Factories, InteractsWithMessenger, ResetDatabase;

/**
* @test
*/
public function flow_for_single_handler(): void
{
ProcessedMessageFactory::assert()->empty();

$this->bus()->dispatch(new MessageA(return: 'foo'));

$this->transport()->processOrFail(1);

ProcessedMessageFactory::assert()->count(1);

$message = ProcessedMessageFactory::first();
$this->assertSame(MessageA::class, $message->type()->class());
$this->assertSame('async', $message->transport());
$this->assertFalse($message->isFailure());
$this->assertCount(1, $message->results());
$this->assertSame(['data' => 'foo'], $message->results()->all()[0]->data());
$this->assertFalse($message->results()->all()[0]->isFailure());
$this->assertSame(MessageAHandler::class, $message->results()->all()[0]->handler()?->class());
$this->assertNull($message->results()->all()[0]->handler()?->description());
}

/**
* @test
*/
public function flow_for_single_handler_failure(): void
{
ProcessedMessageFactory::assert()->empty();

$this->bus()->dispatch(new MessageA(return: 'foo', throw: true));

$this->transport()->processOrFail(1);

ProcessedMessageFactory::assert()->count(1);

$message = ProcessedMessageFactory::first();
$this->assertSame(MessageA::class, $message->type()->class());
$this->assertTrue($message->isFailure());
$this->assertSame(HandlerFailedException::class, $message->failure()?->class());
$this->assertSame(\sprintf('Handling "%s" failed: error', MessageA::class), $message->failure()->description());
$this->assertCount(1, $message->results());
$this->assertTrue($message->results()->all()[0]->isFailure());
$this->assertSame(\RuntimeException::class, $message->results()->all()[0]->failure()?->class());
$this->assertSame('error', $message->results()->all()[0]->failure()->description());
$this->assertSame(MessageAHandler::class, $message->results()->all()[0]->handler()?->class());
$this->assertSame(['stack_trace'], \array_keys($message->results()->all()[0]->data()));
}

/**
* @test
*/
public function flow_for_missing_handler(): void
{
ProcessedMessageFactory::assert()->empty();

$this->bus()->dispatch(new MessageB());

$this->transport()->processOrFail(1);

ProcessedMessageFactory::assert()->count(1);

$message = ProcessedMessageFactory::first();

$this->assertTrue($message->isFailure());
$this->assertSame(NoHandlerForMessageException::class, $message->failure()?->class());
$this->assertSame(\sprintf('No handler for message "%s".', MessageB::class), $message->failure()->description());
$this->assertEmpty($message->results()->all());
}

/**
* @test
*/
public function flow_for_multiple_handlers(): void
{
ProcessedMessageFactory::assert()->empty();

$this->bus()->dispatch(new MessageC(return1: 'foo', return2: 'bar'));

$this->transport()->processOrFail(1);

ProcessedMessageFactory::assert()->count(1);

$message = ProcessedMessageFactory::first();
$this->assertSame(MessageC::class, $message->type()->class());
$this->assertSame('async', $message->transport());
$this->assertFalse($message->isFailure());
$this->assertCount(2, $message->results());

$this->assertSame(['data' => 'foo'], $message->results()->all()[0]->data());
$this->assertFalse($message->results()->all()[0]->isFailure());
$this->assertSame(MessageCHandler1::class, $message->results()->all()[0]->handler()?->class());
$this->assertNull($message->results()->all()[0]->handler()?->description());

$this->assertSame(['data' => 'bar'], $message->results()->all()[1]->data());
$this->assertFalse($message->results()->all()[1]->isFailure());
$this->assertSame(MessageCHandler2::class, $message->results()->all()[1]->handler()?->class());
$this->assertSame('handle', $message->results()->all()[1]->handler()?->description());
}

/**
* @test
*/
public function flow_for_multiple_handlers_one_fails(): void
{
$this->bus()->dispatch(new MessageC(return1: 'foo', return2: 'bar', throw: true));

$this->transport()->processOrFail(1);

ProcessedMessageFactory::assert()->count(1);

$message = ProcessedMessageFactory::first();
$this->assertSame(MessageC::class, $message->type()->class());
$this->assertSame('async', $message->transport());
$this->assertTrue($message->isFailure());

$this->assertSame(HandlerFailedException::class, $message->failure()?->class());
$this->assertSame(\sprintf('Handling "%s" failed: error', MessageC::class), $message->failure()->description());
$this->assertCount(2, $message->results());

$this->assertSame(['data' => 'bar'], $message->results()->all()[0]->data());
$this->assertFalse($message->results()->all()[0]->isFailure());
$this->assertSame(MessageCHandler2::class, $message->results()->all()[0]->handler()?->class());
$this->assertSame('handle', $message->results()->all()[0]->handler()?->description());

$this->assertTrue($message->results()->all()[1]->isFailure());
$this->assertSame(MessageCHandler1::class, $message->results()->all()[1]->handler()?->class());
$this->assertNull($message->results()->all()[1]->handler()?->description());
$this->assertSame(\RuntimeException::class, $message->results()->all()[1]->failure()?->class());
$this->assertSame('error', $message->results()->all()[1]->failure()->description());
$this->assertSame(['stack_trace'], \array_keys($message->results()->all()[1]->data()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,19 @@
* file that was distributed with this source code.
*/

namespace Zenstruck\Messenger\Monitor\Tests\Integration;
namespace Zenstruck\Messenger\Monitor\Tests\Feature;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Clock\Test\ClockSensitiveTrait;
use Symfony\Component\Serializer\Serializer;
use Zenstruck\Console\Test\InteractsWithConsole;
use Zenstruck\Foundry\Test\ResetDatabase;
use Zenstruck\Messenger\Monitor\Stamp\MonitorStamp;
use Zenstruck\Messenger\Monitor\Tests\Fixture\TestService;

/**
* @author Kevin Bond <[email protected]>
*/
final class ZenstruckMessengerMonitorBundleTest extends KernelTestCase
final class SerializerTest extends KernelTestCase
{
use ClockSensitiveTrait, InteractsWithConsole, ResetDatabase;

/**
* @test
*/
public function autowires_services(): void
{
/** @var TestService $service */
$service = self::getContainer()->get(TestService::class);

$this->assertCount(1, $service->transports);
$this->assertCount(0, $service->workers);
$this->assertCount(0, $service->schedules);
}

/**
* @test
*/
public function run_messenger_monitor_command(): void
{
$this->executeConsoleCommand('messenger:monitor')
->assertSuccessful()
->assertOutputContains('[!] No workers running.')
->assertOutputContains('async n/a')
;
}
use ClockSensitiveTrait;

/**
* @test
Expand Down
5 changes: 5 additions & 0 deletions tests/Fixture/Message/MessageA.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@
*/
final class MessageA
{
public function __construct(
public readonly mixed $return = null,
public readonly bool $throw = false,
) {
}
}
30 changes: 30 additions & 0 deletions tests/Fixture/Message/MessageAHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the zenstruck/messenger-monitor-bundle package.
*
* (c) Kevin Bond <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Messenger\Monitor\Tests\Fixture\Message;

use Symfony\Component\Messenger\Attribute\AsMessageHandler;

/**
* @author Kevin Bond <[email protected]>
*/
#[AsMessageHandler]
final class MessageAHandler
{
public function __invoke(MessageA $message): mixed
{
if ($message->throw) {
throw new \RuntimeException('error');
}

return $message->return;
}
}
25 changes: 25 additions & 0 deletions tests/Fixture/Message/MessageC.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the zenstruck/messenger-monitor-bundle package.
*
* (c) Kevin Bond <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Messenger\Monitor\Tests\Fixture\Message;

/**
* @author Kevin Bond <[email protected]>
*/
final class MessageC
{
public function __construct(
public readonly mixed $return1 = null,
public readonly mixed $return2 = null,
public readonly bool $throw = false,
) {
}
}
30 changes: 30 additions & 0 deletions tests/Fixture/Message/MessageCHandler1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the zenstruck/messenger-monitor-bundle package.
*
* (c) Kevin Bond <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Messenger\Monitor\Tests\Fixture\Message;

use Symfony\Component\Messenger\Attribute\AsMessageHandler;

/**
* @author Kevin Bond <[email protected]>
*/
#[AsMessageHandler]
final class MessageCHandler1
{
public function __invoke(MessageC $message): mixed
{
if ($message->throw) {
throw new \RuntimeException('error');
}

return $message->return1;
}
}
26 changes: 26 additions & 0 deletions tests/Fixture/Message/MessageCHandler2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the zenstruck/messenger-monitor-bundle package.
*
* (c) Kevin Bond <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Messenger\Monitor\Tests\Fixture\Message;

use Symfony\Component\Messenger\Attribute\AsMessageHandler;

/**
* @author Kevin Bond <[email protected]>
*/
final class MessageCHandler2
{
#[AsMessageHandler]
public function handle(MessageC $message): mixed
{
return $message->return2;
}
}
Loading

0 comments on commit 23fa558

Please sign in to comment.