Skip to content

Commit c1c1634

Browse files
committed
feature #13937 [FrameworkBundle] Allow to disable Kernel reboot (sroze)
This PR was merged into the 2.7 branch. Discussion ---------- [FrameworkBundle] Allow to disable Kernel reboot | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT This PR introduce a `disableReboot` method on `Client` class. A use case of this method is to prevent rebooting kernel if we manually rebooted it before the request to manipulate registered services. Commits ------- 648aacb [FrameworkBundle] Allow to disable Kernel reboot
2 parents 2466cb6 + d1b5834 commit c1c1634

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

Client.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Client extends BaseClient
2929
{
3030
private $hasPerformedRequest = false;
3131
private $profiler = false;
32+
private $reboot = true;
3233

3334
/**
3435
* {@inheritdoc}
@@ -84,6 +85,26 @@ public function enableProfiler()
8485
}
8586
}
8687

88+
/**
89+
* By default, the Client reboots the Kernel for each request. This method
90+
* allows to keep the same kernel across requests.
91+
*/
92+
public function disableReboot()
93+
{
94+
$this->reboot = false;
95+
}
96+
97+
/**
98+
* Enable the kernel reboot behaviour.
99+
*
100+
* If the kernel reboot was previously disabled, you can re-enable it with
101+
* this method.
102+
*/
103+
public function enableReboot()
104+
{
105+
$this->reboot = true;
106+
}
107+
87108
/**
88109
* {@inheritdoc}
89110
*
@@ -95,7 +116,7 @@ protected function doRequest($request)
95116
{
96117
// avoid shutting down the Kernel if no request has been performed yet
97118
// WebTestCase::createClient() boots the Kernel but do not handle a request
98-
if ($this->hasPerformedRequest) {
119+
if ($this->hasPerformedRequest && $this->reboot) {
99120
$this->kernel->shutdown();
100121
} else {
101122
$this->hasPerformedRequest = true;

Tests/ClientTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests;
13+
14+
use Symfony\Bundle\FrameworkBundle\Client;
15+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\WebTestCase;
16+
use Symfony\Component\HttpFoundation\Response;
17+
18+
class ClientTest extends WebTestCase
19+
{
20+
public function testRebootKernelBetweenRequests()
21+
{
22+
$mock = $this->getKernelMock();
23+
$mock->expects($this->once())->method('shutdown');
24+
25+
$client = new Client($mock);
26+
$client->request('GET', '/');
27+
$client->request('GET', '/');
28+
}
29+
30+
public function testDisabledRebootKernel()
31+
{
32+
$mock = $this->getKernelMock();
33+
$mock->expects($this->never())->method('shutdown');
34+
35+
$client = new Client($mock);
36+
$client->disableReboot();
37+
$client->request('GET', '/');
38+
$client->request('GET', '/');
39+
}
40+
41+
public function testEnableRebootKernel()
42+
{
43+
$mock = $this->getKernelMock();
44+
$mock->expects($this->once())->method('shutdown');
45+
46+
$client = new Client($mock);
47+
$client->disableReboot();
48+
$client->request('GET', '/');
49+
$client->request('GET', '/');
50+
$client->enableReboot();
51+
$client->request('GET', '/');
52+
}
53+
54+
private function getKernelMock()
55+
{
56+
$mock = $this->getMockBuilder($this->getKernelClass())
57+
->setMethods(array('shutdown', 'boot', 'handle'))
58+
->disableOriginalConstructor()
59+
->getMock();
60+
61+
$mock->expects($this->any())->method('handle')->willReturn(new Response('foo'));
62+
63+
return $mock;
64+
}
65+
}

0 commit comments

Comments
 (0)