Skip to content
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
17 changes: 6 additions & 11 deletions Async/AsyncAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ protected function executeCommand(

return null;
});
}, function (\Exception $exception) use ($from, $outputPrinter, $command, $ok, $ko) {
})
->otherwise(function (\Throwable $exception) use ($from, $outputPrinter, $command, $ok, $ko) {
$to = microtime(true);
$ignorable = $exception instanceof MissingHandlerException;

Expand All @@ -162,16 +163,10 @@ protected function executeCommand(
: CommandConsumedLineMessage::REJECTED
))->print($outputPrinter);

return (
$ignorable
? (resolve())
->then(function () use ($ok) {
return $ok();
})
: (resolve())
->then(function () use ($ko) {
return $ko();
})
return
(resolve($ignorable
? $ok()
: $ko())
)
->then(function () {
return null;
Expand Down
3 changes: 1 addition & 2 deletions Bus/CommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ public function execute($command): PromiseInterface
return $this
->handle($command)
->then(function () {
return;
});
} catch (\Exception $exception) {
} catch (\Throwable $exception) {
return reject($exception);
}
}
Expand Down
40 changes: 40 additions & 0 deletions Tests/Async/AsyncAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
use Drift\CommandBus\Tests\Command\ChangeYetAnotherThing;
use Drift\CommandBus\Tests\Command\NotRecoverableCommand;
use Drift\CommandBus\Tests\Command\RejectException;
use Drift\CommandBus\Tests\Command\ThrowError;
use Drift\CommandBus\Tests\Command\ThrowException;
use Drift\CommandBus\Tests\CommandHandler\ChangeAnotherThingHandler;
use Drift\CommandBus\Tests\CommandHandler\ChangeAThingHandler;
use Drift\CommandBus\Tests\CommandHandler\ChangeYetAnotherThingHandler;
use Drift\CommandBus\Tests\CommandHandler\NotRecoverableCommandHandler;
use Drift\CommandBus\Tests\CommandHandler\RejectExceptionHandler;
use Drift\CommandBus\Tests\CommandHandler\ThrowErrorHandler;
use Drift\CommandBus\Tests\CommandHandler\ThrowExceptionHandler;
use Drift\CommandBus\Tests\Context;
use Drift\CommandBus\Tests\Middleware\Middleware1;
Expand Down Expand Up @@ -86,6 +88,12 @@ protected static function decorateConfiguration(array $configuration): array
],
];

$configuration['services'][ThrowErrorHandler::class] = [
'tags' => [
['name' => 'command_handler', 'method' => 'handle'],
],
];

$configuration['imports'] = [
['resource' => __DIR__.'/../autowiring.yml'],
];
Expand Down Expand Up @@ -285,6 +293,11 @@ public function testReturnRejectedPromise()
$process->stop();
}

/**
* @return void
*
* @throws \Drift\CommandBus\Exception\InvalidCommandException
*/
public function testNoRecoverableCommand()
{
$this->resetInfrastructure();
Expand All @@ -307,6 +320,33 @@ public function testNoRecoverableCommand()
$process->stop();
}

/**
* @return void
*
* @throws \Drift\CommandBus\Exception\InvalidCommandException
*/
public function testErrorIsThrownInHandler()
{
$this->resetInfrastructure();

$process = $this->runAsyncCommand([
'command-bus:consume-commands',
]);

usleep(500000);

$promises[] = $this
->getCommandBus()
->execute(new ThrowError());

awaitAll($promises, $this->getLoop());
usleep(500000);
$output = $process->getOutput();
$this->assertTrue(1 === substr_count($output, 'Rejected'));
$this->assertStringContainsString("\033[01;31mRejected\033[0m ThrowError", $output);
$process->stop();
}

/**
* Reset infrastructure.
*
Expand Down
5 changes: 5 additions & 0 deletions Tests/Async/InMemoryAsyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public function testNoRecoverableCommand()
$this->markTestSkipped('InMemory adapter should not fire this test');
}

public function testErrorIsThrownInHandler()
{
$this->markTestSkipped('InMemory adapter should not fire this test');
}

/**
* Consume commands.
*
Expand Down
22 changes: 22 additions & 0 deletions Tests/Command/ThrowError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the DriftPHP Project
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <yuhu@mmoreram.com>
*/

declare(strict_types=1);

namespace Drift\CommandBus\Tests\Command;

use Drift\CommandBus\Bus\NonRecoverableCommand;

class ThrowError implements NonRecoverableCommand
{
}
34 changes: 34 additions & 0 deletions Tests/CommandHandler/ThrowErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the DriftPHP Project
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <yuhu@mmoreram.com>
*/

declare(strict_types=1);

namespace Drift\CommandBus\Tests\CommandHandler;

use Drift\CommandBus\Tests\Command\ThrowError;

/**
* Class ThrowErrorHandler.
*/
class ThrowErrorHandler
{
/**
* Handle.
*/
public function handle(ThrowError $throwError)
{
var_dump('N');
(function (string $a) {
})(1);
}
}