Skip to content

Commit 5bfc819

Browse files
scaytraseruudk
authored andcommitted
Add functional test for disabling finishes_command_before_handling_next middleware
1 parent f0fc755 commit 5bfc819

File tree

8 files changed

+212
-0
lines changed

8 files changed

+212
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace SimpleBus\SymfonyBridge\Tests\Functional;
4+
5+
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Nested\NestedCommand;
6+
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Nested\PostExecutionRecord;
7+
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Nested\PreExecutionRecord;
8+
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Nested\RecordsBag;
9+
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\TestKernel;
10+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
11+
12+
final class NestedCommandExecutionOrderConfigurationTest extends KernelTestCase
13+
{
14+
protected static function getKernelClass(): string
15+
{
16+
return TestKernel::class;
17+
}
18+
19+
/**
20+
* @test
21+
*/
22+
public function nested_commands_are_executed_sequentially_by_default(): void
23+
{
24+
self::bootKernel(['environment' => 'config1']);
25+
$container = self::$kernel->getContainer();
26+
27+
$commandBus = $container->get('command_bus');
28+
$command = new NestedCommand();
29+
$commandBus->handle($command);
30+
31+
/** @var RecordsBag $recorder */
32+
$recorder = $container->get('nesting_records_bag');
33+
34+
$this->assertEquals(
35+
[
36+
new PreExecutionRecord(0),
37+
new PostExecutionRecord(0),
38+
new PreExecutionRecord(1),
39+
new PostExecutionRecord(1),
40+
new PreExecutionRecord(2),
41+
new PostExecutionRecord(2),
42+
43+
],
44+
$recorder->records
45+
);
46+
}
47+
48+
/**
49+
* @test
50+
*/
51+
public function disabling_finishes_command_before_handling_next_middleware_keeps_command_nesting(): void
52+
{
53+
self::bootKernel(['environment' => 'config3']);
54+
$container = self::$kernel->getContainer();
55+
56+
$commandBus = $container->get('command_bus');
57+
$command = new NestedCommand();
58+
$commandBus->handle($command);
59+
60+
/** @var RecordsBag $recorder */
61+
$recorder = $container->get('nesting_records_bag');
62+
63+
$this->assertEquals(
64+
[
65+
new PreExecutionRecord(0),
66+
new PreExecutionRecord(1),
67+
new PreExecutionRecord(2),
68+
new PostExecutionRecord(2),
69+
new PostExecutionRecord(1),
70+
new PostExecutionRecord(0),
71+
72+
],
73+
$recorder->records
74+
);
75+
}
76+
77+
/** {@inheritdoc} */
78+
protected function tearDown()
79+
{
80+
parent::tearDown();
81+
82+
static::$class = null;
83+
static::$kernel = null;
84+
}
85+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Nested;
4+
5+
final class NestedCommand
6+
{
7+
public $level;
8+
9+
public function __construct(int $level = 0)
10+
{
11+
$this->level = $level;
12+
}
13+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Nested;
4+
5+
use SimpleBus\Message\Bus\MessageBus;
6+
7+
final class NestedCommandHandler
8+
{
9+
/**
10+
* @var int
11+
*/
12+
private $maxNestingLevel;
13+
/**
14+
* @var RecordsBag
15+
*/
16+
private $recordsBag;
17+
/**
18+
* @var MessageBus
19+
*/
20+
private $commandBus;
21+
22+
public function __construct(MessageBus $commandBus, RecordsBag $recordsBag, int $maxNestingLevel)
23+
{
24+
$this->commandBus = $commandBus;
25+
$this->recordsBag = $recordsBag;
26+
$this->maxNestingLevel = $maxNestingLevel;
27+
}
28+
29+
public function handle(NestedCommand $command): void
30+
{
31+
$this->recordsBag->records[] = new PreExecutionRecord($command->level);
32+
33+
if ($command->level < $this->maxNestingLevel) {
34+
$this->commandBus->handle(new NestedCommand($command->level + 1));
35+
}
36+
37+
$this->recordsBag->records[] = new PostExecutionRecord($command->level);
38+
}
39+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Nested;
4+
5+
final class PostExecutionRecord
6+
{
7+
/** @var int */
8+
public $level;
9+
10+
public function __construct(int $level)
11+
{
12+
$this->level = $level;
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Nested;
4+
5+
final class PreExecutionRecord
6+
{
7+
/** @var int */
8+
public $level;
9+
10+
public function __construct(int $level)
11+
{
12+
$this->level = $level;
13+
}
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Nested;
4+
5+
final class RecordsBag
6+
{
7+
public $records;
8+
}

tests/Functional/SmokeTest/config1.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ services:
3737
- '@command_bus'
3838
public: true
3939

40+
nesting_command_handler:
41+
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Nested\NestedCommandHandler
42+
arguments:
43+
- '@command_bus'
44+
- '@nesting_records_bag'
45+
- 2
46+
tags:
47+
- { name: command_handler, handles: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Nested\NestedCommand }
48+
public: true
49+
50+
nesting_records_bag:
51+
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Nested\RecordsBag
52+
public: true
53+
4054
command_bus:
4155
command_name_resolver_strategy: class_based
4256
middlewares:
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
imports:
2+
- { resource: config.yml }
3+
4+
services:
5+
nesting_command_handler:
6+
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Nested\NestedCommandHandler
7+
arguments:
8+
- '@command_bus'
9+
- '@nesting_records_bag'
10+
- 2
11+
tags:
12+
- { name: command_handler, handles: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Nested\NestedCommand }
13+
public: true
14+
15+
nesting_records_bag:
16+
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Nested\RecordsBag
17+
public: true
18+
19+
command_bus:
20+
command_name_resolver_strategy: class_based
21+
middlewares:
22+
finishes_command_before_handling_next: false
23+
24+
event_bus:
25+
event_name_resolver_strategy: named_message

0 commit comments

Comments
 (0)