Skip to content

Commit daf8309

Browse files
Make functional tests more robust and informative (#177)
The log output of the functional tests is now captured and displayed in the event of a failure, making it easier to diagnose issues.
1 parent 3510bee commit daf8309

23 files changed

Lines changed: 774 additions & 350 deletions

‎.github/workflows/functional.yml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545

4646
- uses: shivammathur/setup-php@f3e473d116dcccaddc5834248c87452386958240 # 2.37.2
4747
with:
48-
php-version: "${{ matrix.php-version }}"
48+
php-version: ${{ matrix.php-version }}
4949
coverage: xdebug
5050

5151
- uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0
@@ -54,10 +54,10 @@ jobs:
5454

5555
- name: Run tests
5656
if: ${{ !matrix.experimental }}
57-
# Include --fail-on-skipped when min PHP is > 7.2.5
58-
run: vendor/bin/phpunit --group functional
57+
# Include --fail-on-skipped when min PHP is > 7.2.5 (for PHPUnit 9.5+)
58+
run: vendor/bin/phpunit --group xdebug-functional
5959

6060
# Show deprecations on latest PHP
6161
- name: Run tests (experimental)
6262
if: ${{ matrix.experimental }}
63-
run: vendor/bin/phpunit --group functional --fail-on-skipped --display-deprecations
63+
run: vendor/bin/phpunit --group xdebug-functional --fail-on-skipped --display-deprecations

‎phpunit.xml.dist‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groups>
1313
<exclude>
14-
<group>functional</group>
14+
<group>xdebug-functional</group>
1515
</exclude>
1616
</groups>
1717
</phpunit>

‎tests/App/Framework/AppHelper.php‎

Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,31 @@
1616
use Composer\XdebugHandler\Process;
1717
use Composer\XdebugHandler\XdebugHandler;
1818

19+
/**
20+
* This class runs and provides functionality for the xdebug-handler functional
21+
* test apps.
22+
*
23+
* It sets up the logger and outputs debug and status messages to stdout.
24+
*
25+
* The test apps can be run from the command-line and will show detailed output
26+
* with the --display and --inis options.
27+
*/
1928
class AppHelper extends AppRunner
2029
{
2130
public const ENV_IS_DISPLAY = 'XDEBUG_HANDLER_TEST_DISPLAY';
2231
public const ENV_SHOW_INIS = 'XDEBUG_HANDLER_TEST_INIS';
2332

2433
/** @var string */
2534
private $appName;
35+
2636
/** @var string */
2737
private $envPrefix;
38+
2839
/** @var non-empty-list<string> */
2940
private $serverArgv;
41+
3042
/** @var class-string|null */
3143
private $className;
32-
/** @var bool */
33-
private $display;
34-
/** @var bool */
35-
private $showInis;
3644

3745
/** @var Logger */
3846
private $logger;
@@ -41,21 +49,19 @@ class AppHelper extends AppRunner
4149
private $output;
4250

4351
/** @var Status */
44-
private $status = null;
52+
private $status;
4553

4654
public function __construct(string $script)
4755
{
4856
parent::__construct(dirname($script));
4957
$this->appName = $this->getAppName($script);
5058
$this->envPrefix = $this->getEnvPrefix($script);
5159

52-
$this->display = $this->getDisplayFromServer();
53-
$this->showInis = $this->getInisFromServer();
54-
$this->setServerArgv();
60+
[$display, $showInis] = $this->setServerArgvAndOptions();
5561

56-
$this->output = new Output($this->display);
62+
$this->output = new Output($display);
5763
$this->logger = new Logger($this->output);
58-
$this->status = new Status($this->display, $this->showInis);
64+
$this->status = new Status($display, $showInis);
5965
}
6066

6167
/**
@@ -73,9 +79,10 @@ public function getServerArgv0(): string
7379
}
7480

7581
/**
82+
* Starts an instance of XdebugHandler
7683
*
77-
* @param class-string $class
78-
* @param array<string, string> $settings
84+
* @param class-string $class Extending class name
85+
* @param array<string, string> $settings XdebugHandler settings
7986
*/
8087
public function getXdebugHandler(?string $class = null, ?array $settings = null): XdebugHandler
8188
{
@@ -102,16 +109,40 @@ public function getXdebugHandler(?string $class = null, ?array $settings = null)
102109
return $xdebug;
103110
}
104111

112+
/**
113+
* Runs a PHP sub-process
114+
*
115+
* @param string $script The file to run
116+
* @param PhpOptions $options
117+
*/
105118
public function runScript(string $script, ?PhpOptions $options = null): void
106119
{
107-
parent::runScript($script, $options);
120+
$script = $this->checkScript($script);
121+
122+
if ($options === null) {
123+
$options = new PhpOptions();
124+
}
125+
126+
// set passthru in child proccesses so output gets collected
127+
$options->setPassthru(true);
128+
$this->phpExecutor->run($script, $options, $this->scriptDir);
108129
}
109130

131+
/**
132+
* Outputs a message prefixed appname[pid] to stdout
133+
*
134+
* @param string $message
135+
*/
110136
public function write(string $message): void
111137
{
112138
$this->logger->write($message, $this->appName);
113139
}
114140

141+
/**
142+
* Outputs the Xdebug loaded status to stdout
143+
*
144+
* More details can be shown with the --display and --inis options.
145+
*/
115146
public function writeXdebugStatus(): void
116147
{
117148
$className = $this->className ?? XdebugHandler::class;
@@ -122,10 +153,20 @@ public function writeXdebugStatus(): void
122153
}
123154
}
124155

125-
private function setServerArgv(): void
156+
/**
157+
* Checks $_SERVER['argv'] and returns $display and $showInis values
158+
*
159+
* The flags to display the full status and ini file list are passed on the
160+
* command-line, then removed and set in the environment.
161+
*
162+
* @return array<bool, bool>
163+
*/
164+
private function setServerArgvAndOptions(): array
126165
{
127166
$args = [];
128167
$errors = false;
168+
$display = false;
169+
$showInis = false;
129170

130171
if (isset($_SERVER['argv']) && is_array($_SERVER['argv'])) {
131172
foreach ($_SERVER['argv'] as $value) {
@@ -134,6 +175,18 @@ private function setServerArgv(): void
134175
break;
135176
}
136177

178+
if ($value === '--display') {
179+
$display = true;
180+
Process::setEnv(self::ENV_IS_DISPLAY, '1');
181+
continue;
182+
}
183+
184+
if ($value === '--inis') {
185+
$showInis = true;
186+
Process::setEnv(self::ENV_SHOW_INIS, '1');
187+
continue;
188+
}
189+
137190
$args[] = $value;
138191
}
139192
}
@@ -142,46 +195,12 @@ private function setServerArgv(): void
142195
throw new \RuntimeException('$_SERVER[argv] is not as expected');
143196
}
144197

198+
$_SERVER['argv'] = $args;
145199
$this->serverArgv = $args;
146-
}
147-
148-
private function getDisplayFromServer(): bool
149-
{
150-
$result = false;
151-
152-
if (isset($_SERVER['argv']) && is_array($_SERVER['argv'])) {
153-
$key = array_search('--display', $_SERVER['argv'], true);
154-
155-
if (is_int($key)) {
156-
$result = true;
157-
Process::setEnv(self::ENV_IS_DISPLAY, '1');
158-
// @phpstan-ignore argument.type
159-
array_splice($_SERVER['argv'], $key, 1);
160-
} else {
161-
$result = false !== getenv(self::ENV_IS_DISPLAY);
162-
}
163-
}
164-
165-
return $result;
166-
}
167-
168-
private function getInisFromServer(): bool
169-
{
170-
$result = false;
171-
172-
if (isset($_SERVER['argv']) && is_array($_SERVER['argv'])) {
173-
$key = array_search('--inis', $_SERVER['argv'], true);
174-
175-
if (is_int($key)) {
176-
$result = true;
177-
Process::setEnv(self::ENV_SHOW_INIS, '1');
178-
// @phpstan-ignore argument.type
179-
array_splice($_SERVER['argv'], $key, 1);
180-
} else {
181-
$result = false !== getenv(self::ENV_SHOW_INIS);
182-
}
183-
}
184200

185-
return $result;
201+
return [
202+
$display || false !== getenv(self::ENV_IS_DISPLAY),
203+
$showInis || false !== getenv(self::ENV_SHOW_INIS),
204+
];
186205
}
187206
}

‎tests/App/Framework/AppRunner.php‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313

1414
namespace Composer\XdebugHandler\Tests\App\Framework;
1515

16+
/**
17+
* This class runs and provides functionality for the xdebug-handler functional
18+
* tests.
19+
*/
1620
class AppRunner
1721
{
1822
/** @var string */
19-
private $scriptDir;
23+
protected $scriptDir;
2024
/** @var PhpExecutor */
21-
private $phpExecutor;
25+
protected $phpExecutor;
2226

2327
public function __construct(?string $scriptDir = null)
2428
{
@@ -31,6 +35,9 @@ public function __construct(?string $scriptDir = null)
3135
$this->phpExecutor = new PhpExecutor();
3236
}
3337

38+
/**
39+
* Runs a script and returns its captured output
40+
*/
3441
public function run(string $script, ?PhpOptions $options = null, bool $allow = false): Logs
3542
{
3643
$script = $this->checkScript($script);
@@ -54,19 +61,6 @@ public function run(string $script, ?PhpOptions $options = null, bool $allow = f
5461
return new Logs($outputLines);
5562
}
5663

57-
public function runScript(string $script, ?PhpOptions $options = null): void
58-
{
59-
$script = $this->checkScript($script);
60-
61-
if ($options === null) {
62-
$options = new PhpOptions();
63-
}
64-
65-
// set passthru in child proccesses so output gets collected
66-
$options->setPassthru(true);
67-
$this->phpExecutor->run($script, $options, $this->scriptDir);
68-
}
69-
7064
public function getAppName(string $script): string
7165
{
7266
return basename($script, '.php');
@@ -84,7 +78,7 @@ public function getEnvPrefix(string $script): string
8478
return strtoupper(str_replace(array('-', ' '), '_', $name));
8579
}
8680

87-
private function checkScript(string $script): string
81+
protected function checkScript(string $script): string
8882
{
8983
if (file_exists($script)) {
9084
return $script;

0 commit comments

Comments
 (0)