Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.

Commit 79100b9

Browse files
committed
Adding some tests to cover promise-based events
1 parent 4c9fe52 commit 79100b9

File tree

5 files changed

+323
-7
lines changed

5 files changed

+323
-7
lines changed

Tests/AsyncFunctionalTest.php

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
namespace Symfony\Component\HttpKernel\Tests;
1717

1818
use Clue\React\Block;
19+
use Exception;
1920
use React\EventLoop\StreamSelectLoop;
21+
use React\Promise;
2022
use Symfony\Component\HttpFoundation\Request;
2123
use Symfony\Component\HttpFoundation\Response;
2224

@@ -25,27 +27,72 @@
2527
*/
2628
class AsyncFunctionalTest extends AsyncKernelFunctionalTest
2729
{
30+
/**
31+
* Decorate configuration.
32+
*
33+
* @param array $configuration
34+
*
35+
* @return array
36+
*/
37+
protected static function decorateConfiguration(array $configuration): array
38+
{
39+
$configuration = parent::decorateConfiguration($configuration);
40+
$configuration['services']['listener'] = [
41+
'class' => Listener::class,
42+
'tags' => [
43+
[
44+
'name' => 'kernel.event_listener',
45+
'event' => 'kernel.async_request',
46+
'method' => 'handleGetResponsePromiseNothing',
47+
],
48+
[
49+
'name' => 'kernel.event_listener',
50+
'event' => 'kernel.async_exception',
51+
'method' => 'handleGetExceptionNothing',
52+
],
53+
],
54+
];
55+
56+
return $configuration;
57+
}
58+
2859
/**
2960
* Everything should work as before in the world of sync requests.
3061
*/
3162
public function testSyncKernel()
3263
{
3364
$loop = new StreamSelectLoop();
34-
$request = new Request([], [], [], [], [], [
35-
'REQUEST_METHOD' => 'GET',
36-
'REQUEST_URI' => '/promise',
37-
]);
3865

39-
$promise = self::$kernel
40-
->handleAsync($request)
66+
$promise1 = self::$kernel
67+
->handleAsync(new Request([], [], [], [], [], [
68+
'REQUEST_METHOD' => 'GET',
69+
'REQUEST_URI' => '/promise',
70+
]))
4171
->then(function (Response $response) {
4272
$this->assertEquals(
4373
'Y',
4474
$response->getContent()
4575
);
4676
});
4777

78+
$promise2 = self::$kernel
79+
->handleAsync(new Request([], [], [], [], [], [
80+
'REQUEST_METHOD' => 'GET',
81+
'REQUEST_URI' => '/promise-exception',
82+
]))
83+
->then(null, function (Exception $exception) {
84+
$this->assertEquals(
85+
'E2',
86+
$exception->getMessage()
87+
);
88+
});
89+
4890
$loop->run();
49-
Block\await($promise, $loop);
91+
Block\await(
92+
Promise\all([
93+
$promise1,
94+
$promise2,
95+
]), $loop
96+
);
5097
}
5198
}

Tests/AsyncKernelFunctionalTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,22 @@ protected static function getKernel(): KernelInterface
7272
[
7373
'/value',
7474
Controller::class.':getValue',
75+
'value',
7576
],
7677
[
7778
'/promise',
7879
Controller::class.':getPromise',
80+
'promise',
7981
],
8082
[
8183
'/exception',
8284
Controller::class.':throwException',
85+
'exception',
8386
],
8487
[
8588
'/promise-exception',
8689
Controller::class.':getPromiseException',
90+
'promise-exception',
8791
],
8892
];
8993

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony Async Kernel
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <[email protected]>
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Symfony\Component\HttpKernel\Tests;
17+
18+
use Clue\React\Block;
19+
use Exception;
20+
use React\EventLoop\StreamSelectLoop;
21+
use React\Promise;
22+
use Symfony\Component\HttpFoundation\Request;
23+
use Symfony\Component\HttpFoundation\Response;
24+
25+
/**
26+
* Class GetResponsePromiseFunctionalTest.
27+
*/
28+
class GetResponsePromiseFunctionalTest extends AsyncKernelFunctionalTest
29+
{
30+
/**
31+
* Decorate configuration.
32+
*
33+
* @param array $configuration
34+
*
35+
* @return array
36+
*/
37+
protected static function decorateConfiguration(array $configuration): array
38+
{
39+
$configuration = parent::decorateConfiguration($configuration);
40+
$configuration['services']['listener'] = [
41+
'class' => Listener::class,
42+
'tags' => [
43+
[
44+
'name' => 'kernel.event_listener',
45+
'event' => 'kernel.async_request',
46+
'method' => 'handleGetResponsePromiseA',
47+
],
48+
[
49+
'name' => 'kernel.event_listener',
50+
'event' => 'kernel.async_exception',
51+
'method' => 'handleGetExceptionA',
52+
],
53+
],
54+
];
55+
56+
return $configuration;
57+
}
58+
59+
/**
60+
* Everything should work as before in the world of sync requests.
61+
*/
62+
public function testSyncKernel()
63+
{
64+
$loop = new StreamSelectLoop();
65+
66+
$promise1 = self::$kernel
67+
->handleAsync(new Request([], [], [], [], [], [
68+
'REQUEST_METHOD' => 'GET',
69+
'REQUEST_URI' => '/promise',
70+
]))
71+
->then(function (Response $response) {
72+
$this->assertEquals(
73+
'A',
74+
$response->getContent()
75+
);
76+
});
77+
78+
$promise2 = self::$kernel
79+
->handleAsync(new Request([], [], [], [], [], [
80+
'REQUEST_METHOD' => 'GET',
81+
'REQUEST_URI' => '/promise-exception',
82+
]))
83+
->then(null, function (Exception $exception) {
84+
$this->assertEquals(
85+
'EXC',
86+
$exception->getMessage()
87+
);
88+
});
89+
90+
$loop->run();
91+
Block\await(
92+
Promise\all([
93+
$promise1,
94+
$promise2,
95+
]), $loop
96+
);
97+
}
98+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony Async Kernel
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <[email protected]>
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Symfony\Component\HttpKernel\Tests;
17+
18+
use Clue\React\Block;
19+
use React\EventLoop\StreamSelectLoop;
20+
use Symfony\Component\HttpFoundation\Request;
21+
use Symfony\Component\HttpFoundation\Response;
22+
23+
/**
24+
* Class GetResponsesPromiseFunctionalTest.
25+
*/
26+
class GetResponsesPromiseFunctionalTest extends AsyncKernelFunctionalTest
27+
{
28+
/**
29+
* Decorate configuration.
30+
*
31+
* @param array $configuration
32+
*
33+
* @return array
34+
*/
35+
protected static function decorateConfiguration(array $configuration): array
36+
{
37+
$configuration = parent::decorateConfiguration($configuration);
38+
$configuration['services']['listener'] = [
39+
'class' => Listener::class,
40+
'tags' => [
41+
[
42+
'name' => 'kernel.event_listener',
43+
'event' => 'kernel.async_request',
44+
'method' => 'handleGetResponsePromiseB',
45+
],
46+
[
47+
'name' => 'kernel.event_listener',
48+
'event' => 'kernel.async_request',
49+
'method' => 'handleGetResponsePromiseA',
50+
],
51+
],
52+
];
53+
54+
return $configuration;
55+
}
56+
57+
/**
58+
* Everything should work as before in the world of sync requests.
59+
*/
60+
public function testSyncKernel()
61+
{
62+
$loop = new StreamSelectLoop();
63+
$request = new Request([], [], [], [], [], [
64+
'REQUEST_METHOD' => 'GET',
65+
'REQUEST_URI' => '/promise',
66+
]);
67+
68+
$promise = self::$kernel
69+
->handleAsync($request)
70+
->then(function (Response $response) {
71+
$this->assertEquals(
72+
'B',
73+
$response->getContent()
74+
);
75+
});
76+
77+
$loop->run();
78+
Block\await($promise, $loop);
79+
}
80+
}

Tests/Listener.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony Async Kernel
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <[email protected]>
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Symfony\Component\HttpKernel\Tests;
17+
18+
use React\Promise\FulfilledPromise;
19+
use Symfony\Component\HttpFoundation\Response;
20+
use Symfony\Component\HttpKernel\Event\GetResponsePromiseEvent;
21+
use Symfony\Component\HttpKernel\Event\GetResponsePromiseForExceptionEvent;
22+
23+
/**
24+
* Class Listener.
25+
*/
26+
class Listener
27+
{
28+
/**
29+
* Handle get Response.
30+
*
31+
* @param GetResponsePromiseEvent $event
32+
*/
33+
public function handleGetResponsePromiseA(GetResponsePromiseEvent $event)
34+
{
35+
$event->setPromise(
36+
new FulfilledPromise(
37+
new Response('A')
38+
)
39+
);
40+
}
41+
42+
/**
43+
* Handle get Response.
44+
*
45+
* @param GetResponsePromiseEvent $event
46+
*/
47+
public function handleGetResponsePromiseB(GetResponsePromiseEvent $event)
48+
{
49+
$event->setPromise(
50+
new FulfilledPromise(
51+
new Response('B')
52+
)
53+
);
54+
}
55+
56+
/**
57+
* Handle get Response.
58+
*
59+
* @param GetResponsePromiseEvent $event
60+
*/
61+
public function handleGetResponsePromiseNothing(GetResponsePromiseEvent $event)
62+
{
63+
}
64+
65+
/**
66+
* Handle get Exception.
67+
*
68+
* @param GetResponsePromiseForExceptionEvent $event
69+
*/
70+
public function handleGetExceptionNothing(GetResponsePromiseForExceptionEvent $event)
71+
{
72+
}
73+
74+
/**
75+
* Handle get Exception.
76+
*
77+
* @param GetResponsePromiseForExceptionEvent $event
78+
*/
79+
public function handleGetExceptionA(GetResponsePromiseForExceptionEvent $event)
80+
{
81+
$event->setPromise(
82+
new FulfilledPromise(
83+
new Response('EXC')
84+
)
85+
);
86+
}
87+
}

0 commit comments

Comments
 (0)