8000 Merge pull request #113 from maidmaid/pauseonfail · chalasr/symfony@bb5b0e5 · GitHub
[go: up one dir, main page]

Skip to content

Commit bb5b0e5

Browse files
authored
Merge pull request symfony#113 from maidmaid/pauseonfail
Pause on fail
2 parents b6bedb1 + 9c42663 commit bb5b0e5

10 files changed

+143
-6
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ install:
2929
fi
3030

3131
script:
32-
- phpunit
32+
- vendor/bin/phpunit
3333
- if [[ $lint = '1' ]]; then php php-cs-fixer.phar fix --dry-run --diff --no-ansi; fi
3434
- if [[ $lint = '1' ]]; then php phpstan.phar analyse src tests --level=6; fi

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ class E2eTest extends PantherTestCase
130130
}
131131
```
132132

133+
### Interactive Mode
134+
135+
Panther can make a pause in your tests suites after a failure.
136+
It is a break time really appreciated for investigating the problem through the web browser.
137+
For enabling this mode, you need the `--debug` PHPUnit option without the headless mode:
138+
139+
$ export PANTHER_NO_HEADLESS=1
140+
$ phpunit --debug
141+
142+
Test 'App\AdminTest::testLogin' started
143+
Error: something is wrong.
144+
145+
Press enter to continue...
146+
133147
## Features
134148

135149
Unlike testing and web scraping libraries you're used to, Panther:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"require-dev": {
4444
"guzzlehttp/guzzle": "^6.3",
4545
"fabpot/goutte": "^3.2.3",
46-
"phpunit/phpunit": "^7.0",
46+
"phpunit/phpunit": "^7.3",
4747
"symfony/css-selector": "^3.4 || ^4.0",
4848
"symfony/framework-bundle": "^3.4 || ^4.0"
4949
}

src/PantherTestCaseTrait.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ public static function startWebServer(array $options = []): void
121121
self::$baseUri = sprintf('http://%s:%s', $options['hostname'], $options['port']);
122122
}
123123

124+
public static function isWebServerStarted()
125+
{
126+
return self::$webServerManager->isStarted();
127+
}
128+
124129
/**
125130
* @param array $options see {@see $defaultOptions}
126131
* @param array $kernelOptions

src/ProcessManager/WebServerManager.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,9 @@ public function quit(): void
7979
{
8080
$this->process->stop();
8181
}
82+
83+
public function isStarted()
84+
{
85+
return $this->process->isStarted();
86+
}
8287
}

src/ServerExtension.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
namespace Symfony\Component\Panther;
1515

1616
use PHPUnit\Runner\AfterLastTestHook;
17+
use PHPUnit\Runner\AfterTestErrorHook;
18+
use PHPUnit\Runner\AfterTestFailureHook;
1719
use PHPUnit\Runner\BeforeFirstTestHook;
1820

1921
/**
2022
* @author Dany Maillard <danymaillard93b@gmail.com>
2123
*/
22-
final class ServerExtension implements BeforeFirstTestHook, AfterLastTestHook
24+
final class ServerExtension implements BeforeFirstTestHook, AfterLastTestHook, AfterTestErrorHook, AfterTestFailureHook
2325
{
2426
use ServerTrait;
2527

@@ -32,4 +34,14 @@ public function executeAfterLastTest(): void
3234
{
3335
$this->stopWebServer();
3436
}
37+
38+
public function executeAfterTestError(string $test, string $message, float $time): void
39+
{
40+
$this->pause(sprintf('Error: %s', $message));
41+
}
42+
43+
public function executeAfterTestFailure(string $test, string $message, float $time): void
44+
{
45+
$this->pause(sprintf('Failure: %s', $message));
46+
}
3547
}

src/ServerListener.php

Lines changed: 12 additions & 0 deletions
14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

14
namespace Symfony\Component\Panther;
1515

16+
use PHPUnit\Framework\AssertionFailedError;
17+
use PHPUnit\Framework\Test;
1618
use PHPUnit\Framework\TestListener;
1719
use PHPUnit\Framework\TestListenerDefaultImplementation;
1820
use PHPUnit\Framework\TestSuite;
@@ -31,4 +33,14 @@ public function endTestSuite(TestSuite $suite): void
3133
{
3234
$this->stopWebServer();
3335
}
36+
37+
public function addError(Test $test, \Throwable $t, float $time): void
38+
{
39+
$this->pause(sprintf('Error: %s', $t->getMessage()));
40+
}
41+
42+
public function addFailure(Test $test, AssertionFailedError $e, float $time): void
43+
{
44+
$this->pause(sprintf('Failure: %s', $e->getMessage()));
45+
}
3446
}

src/ServerTrait.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
trait ServerTrait
2222
{
23+
public $testing = false;
24+
2325
private function keepServerOnTeardown(): void
2426
{
2527
PantherTestCase::$stopServerOnTeardown = false;
@@ -29,4 +31,17 @@ private function stopWebServer(): void
2931
{
3032
PantherTestCase::stopWebServer();
3133
}
34+
35+
private function pause($message): void
36+
{
37+
if (PantherTestCase::isWebServerStarted()
38+
&& \in_array('--debug', $_SERVER['argv'], true)
39+
&& $_SERVER['PANTHER_NO_HEADLESS'] ?? false
40+
) {
41+
echo "$message\n\nPress enter to continue...";
42+
if (!$this->testing) {
43+
fgets(STDIN);
44+
}
45+
}
46+
}
3247
}

tests/ServerExtensionTest.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,48 @@ public static function tearDownAfterClass()
2525

2626
public function testStartAndStop(): void
2727
{
28-
$listener = new ServerExtension();
28+
$extension = new ServerExtension();
2929

30-
$listener->executeBeforeFirstTest();
30+
$extension->executeBeforeFirstTest();
3131
static::assertFalse(PantherTestCase::$stopServerOnTeardown);
3232

33-
$listener->executeAfterLastTest();
33+
$extension->executeAfterLastTest();
3434
static::assertNull(PantherTestCase::$webServerManager);
3535
}
36+
37+
/**
38+
* @dataProvider provideTestPauseOnFailure
39+
*/
40+
public function testPauseOnFailure(string $method, string $expected): void
41+
{
42+
$extension = new ServerExtension();
43+
$extension->testing = true;
44+
45+
// stores current state
46+
$argv = $_SERVER['argv'];
47+
$noHeadless = $_SERVER['PANTHER_NO_HEADLESS'] ?? false;
48+
49+
self::startWebServer();
50+
$_SERVER['argv'][] = '--debug';
51+
$_SERVER['PANTHER_NO_HEADLESS'] = 1;
52+
53+
$extension->{$method}('test', 'message', 0);
54+
$this->expectOutputString($expected);
55+
56+
// restores previous state
57+
$_SERVER['argv'] = $argv;
58+
if (false === $noHeadless) {
59+
unset($_SERVER['PANTHER_NO_HEADLESS']);
60+
} else {
61+
$_SERVER['PANTHER_NO_HEADLESS'] = $noHeadless;
62+
}
63+
}
64+
65+
public function provideTestPauseOnFailure()
66+
{
67+
return [
68+
['executeAfterTestError', "Error: message\n\nPress enter to continue..."],
69+
['executeAfterTestFailure', "Failure: message\n\nPress enter to continue..."],
70+
];
71+
}
3672
}

tests/ServerListenerTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace Symfony\Component\Panther\Tests;
1515

16+
use PHPUnit\Framework\AssertionFailedError;
17+
use PHPUnit\Framework\Test;
1618
use PHPUnit\Framework\TestSuite;
1719
use Symfony\Component\Panther\PantherTestCase;
1820
use Symfony\Component\Panther\ServerListener;
@@ -35,4 +37,40 @@ public function testStartAndStop(): void
3537
$listener->endTestSuite($testSuite);
3638
static::assertNull(PantherTestCase::$webServerManager);
3739
}
40+
41+
/**
42+
* @dataProvider provideTestPauseOnFailure
43+
*/
44+
public function testPauseOnFailure(string $method, string $expected): void
45+
{
46+
$listener = new ServerListener();
47+
$listener->testing = true;
48+
49+
// stores current state
50+
$argv = $_SERVER['argv'];
51+
$noHeadless = $_SERVER['PANTHER_NO_HEADLESS'] ?? false;
52+
53+
self::startWebServer();
54+
$_SERVER['argv'][] = '--debug';
55+
$_SERVER['PANTHER_NO_HEADLESS'] = 1;
56+
57+
$listener->{$method}($this->getMockForAbstractClass(Test::class), new AssertionFailedError('message'), 0);
58+
$this->expectOutputString($expected);
59+
60+
// restores previous state
61+
$_SERVER['argv'] = $argv;
62+
if (false === $noHeadless) {
63+
unset($_SERVER['PANTHER_NO_HEADLESS']);
64+
} else {
65+
$_SERVER['PANTHER_NO_HEADLESS'] = $noHeadless;
66+
}
67+
}
68+
69+
public function provideTestPauseOnFailure()
70+
{
71+
return [
72+
['addError', "Error: message\n\nPress enter to continue..."],
73+
['addFailure', "Failure: message\n\nPress enter to continue..."],
74+
];
75+
}
3876
}

0 commit comments

Comments
 (0)
0