Skip to content

Commit 7464064

Browse files
authored
Merge pull request symfony#104 from maidmaid/server-ext
Keep alive the webserver between test suites
2 parents d67f9db + bdf6414 commit 7464064

File tree

7 files changed

+131
-40
lines changed

7 files changed

+131
-40
lines changed

README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,23 +166,34 @@ Unlike testing and web scraping libraries you're used to, Panther:
166166
* supports custom [Selenium server](https://www.seleniumhq.org) installations
167167
* supports remote browser testing services including [SauceLabs](https://saucelabs.com/) and [BrowserStack](https://www.browserstack.com/)
168168

169-
### Using the `ServerListener` to Always Have a Running Web Server
169+
### Improve Performances by Having a Persistent Web Server Running
170170

171-
When you use the Panther client, the web server running in background will be started at runtime and stopped at test's
172-
teardown.
171+
When you use the Panther client, the web server running in background will be started on demand at the first call to
172+
`createPantherClient()`, `createGoutteClient()` or `startWebServer()` and it will be stopped at `tearDownAfterClass()`.
173173

174-
If you want to improve performances and launch the server at PHPUnit startup, you can add the `ServerListener` to
175-
your PHPUnit configuration:
174+
If you want to improve performances, you can hook to PHPUnit in your `phpunit.xml.dist` configuration file with the
175+
Panther's server extension:
176176

177177
```xml
178178
<!-- phpunit.xml.dist -->
179+
<extensions>
180+
<extension class="Symfony\Component\Panther\ServerExtension" />
181+
</extensions>
182+
```
183+
184+
This extension will start the web server on demand like previously, but it will stop it after the very last test.
185+
186+
It should be noted that the Panther's extension only works with PHPUnit `>= 7.3`. Nonetheless, if you are using an
187+
anterior PHPUnit version, you can also hook to PHPUnit with the Panther's server listener:
179188

189+
```xml
190+
<!-- phpunit.xml.dist -->
180191
<listeners>
181192
<listener class="Symfony\Component\Panther\ServerListener" />
182193
</listeners>
183194
```
184195

185-
The listener will start the webserver when the test suite is started, and will stop it when all your tests are executed.
196+
This listener will start the web server on demand like previously, but it will stop it after each test suite.
186197

187198
## Documentation
188199

src/PantherTestCaseTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ trait PantherTestCaseTrait
5858

5959
public static function tearDownAfterClass()
6060
{
61-
if (true === self::$stopServerOnTeardown) {
61+
if (self::$stopServerOnTeardown) {
6262
static::stopWebServer();
6363
}
6464
}

src/ServerExtension.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Panther project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.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+
declare(strict_types=1);
13+
14+
namespace Symfony\Component\Panther;
15+
16+
use PHPUnit\Runner\AfterLastTestHook;
17+
use PHPUnit\Runner\BeforeFirstTestHook;
18+
19+
/**
20+
* @author Dany Maillard <danymaillard93b@gmail.com>
21+
*/
22+
final class ServerExtension implements BeforeFirstTestHook, AfterLastTestHook
23+
{
24+
use ServerTrait;
25+
26+
public function executeBeforeFirstTest(): void
27+
{
28+
$this->keepServerOnTeardown();
29+
}
30+
31+
public function executeAfterLastTest(): void
32+
{
33+
$this->stopWebServer();
34+
}
35+
}

src/ServerListener.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@
2020
final class ServerListener implements TestListener
2121
{
2222
use TestListenerDefaultImplementation;
23+
use ServerTrait;
2324

2425
public function startTestSuite(TestSuite $suite): void
2526
{
26-
echo "Starting Panther server for test suite {$suite->getName()}...\n";
27-
PantherTestCaseTrait::$stopServerOnTeardown = true;
28-
PantherTestCaseTrait::startWebServer();
27+
$this->keepServerOnTeardown();
2928
}
3029

3130
public function endTestSuite(TestSuite $suite): void
3231
{
33-
echo "\nShutting down Panther server...\n";
34-
PantherTestCaseTrait::stopWebServer();
32+
$this->stopWebServer();
3533
}
3634
}

src/ServerTrait.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Panther project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.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+
declare(strict_types=1);
13+
14+
namespace Symfony\Component\Panther;
15+
16+
/**
17+
* @author Dany Maillard <danymaillard93b@gmail.com>
18+
*
19+
* @internal
20+
*/
21+
trait ServerTrait
22+
{
23+
private function keepServerOnTeardown(): void
24+
{
25+
PantherTestCase::$stopServerOnTeardown = false;
26+
}
27+
28+
private function stopWebServer(): void
29+
{
30+
PantherTestCase::stopWebServer();
31+
}
32+
}

tests/ServerExtensionTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Panther project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.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+
declare(strict_types=1);
13+
14+
namespace Symfony\Component\Panther\Tests;
15+
16+
use Symfony\Component\Panther\PantherTestCase;
17+
use Symfony\Component\Panther\ServerExtension;
18+
19+
class ServerExtensionTest extends TestCase
20+
{
21+
public static function tearDownAfterClass()
22+
{
23+
PantherTestCase::$stopServerOnTeardown = true;
24+
}
25+
26+
public function testStartAndStop(): void
27+
{
28+
$listener = new ServerExtension();
29+
30+
$listener->executeBeforeFirstTest();
31+
static::assertFalse(PantherTestCase::$stopServerOnTeardown);
32+
33+
$listener->executeAfterLastTest();
34+
static::assertNull(PantherTestCase::$webServerManager);
35+
}
36+
}

tests/ServerListenerTest.php

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,25 @@
1414
namespace Symfony\Component\Panther\Tests;
1515

1616
use PHPUnit\Framework\TestSuite;
17+
use Symfony\Component\Panther\PantherTestCase;
1718
use Symfony\Component\Panther\ServerListener;
1819

1920
class ServerListenerTest extends TestCase
2021
{
21-
private function createTestSuite()
22+
public static function tearDownAfterClass()
2223
{
23-
$suite = $this->createMock(TestSuite::class);
24-
$suite->expects($this->once())->method('getName')->willReturn('Dummy test suite');
25-
26-
return $suite;
24+
PantherTestCase::$stopServerOnTeardown = true;
2725
}
2826

2927
public function testStartAndStop(): void
3028
{
31-
$this->expectOutputString("Starting Panther server for test suite Dummy test suite...\n\nShutting down Panther server...\n");
32-
33-
$_SERVER['PANTHER_WEB_SERVER_DIR'] = static::$webServerDir;
34-
35-
$streamContext = stream_context_create(['http' => [
36-
'ignore_errors' => true,
37-
'protocol_version' => '1.1',
38-
'header' => ['Connection: close'],
39-
'timeout' => 1,
40-
]]);
41-
42-
$healthCheck = function () use ($streamContext) {
43-
return @file_get_contents('http://127.0.0.1:9000', false, $streamContext);
44-
};
45-
46-
$testSuite = $this->createTestSuite();
47-
29+
$testSuite = new TestSuite();
4830
$listener = new ServerListener();
49-
$listener->startTestSuite($testSuite);
5031

51-
// Means the server rendered a 404, so server is running.
52-
static::assertContains('<title>404 Not Found</title>', $healthCheck());
32+
$listener->startTestSuite($testSuite);
33+
static::assertFalse(PantherTestCase::$stopServerOnTeardown);
5334

5435
$listener->endTestSuite($testSuite);
55-
56-
// False means that ping failed.
57-
static::assertFalse($healthCheck());
36+
static::assertNull(PantherTestCase::$webServerManager);
5837
}
5938
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy