Skip to content

Commit 629f48a

Browse files
committed
progress
1 parent 923f29f commit 629f48a

File tree

10 files changed

+103
-34
lines changed

10 files changed

+103
-34
lines changed

src/Framework/TestCase.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,11 @@ final public function wasPrepared(): bool
961961
return $this->wasPrepared;
962962
}
963963

964+
final public function getExpectedProcessExitCode(): ?int
965+
{
966+
return $this->expectProcessExit;
967+
}
968+
964969
/**
965970
* Returns a matcher that matches when the method is executed
966971
* zero or more times.
@@ -1050,11 +1055,6 @@ final protected function expectProcessExit(int $exitCode): void
10501055
$this->expectProcessExit = $exitCode;
10511056
}
10521057

1053-
final public function getExpectedProcessExitCode(): ?int
1054-
{
1055-
return $this->expectProcessExit;
1056-
}
1057-
10581058
final protected function expectErrorLog(): void
10591059
{
10601060
$this->expectErrorLog = true;

src/Framework/TestRunner/ChildProcessResultProcessor.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public function __construct(Facade $eventFacade, Emitter $emitter, PassedTests $
3939

4040
public function process(Test $test, string $serializedProcessResult, string $stderr, int $exitCode): void
4141
{
42+
assert($test instanceof TestCase);
43+
4244
if ($stderr !== '') {
4345
$exception = new Exception(trim($stderr));
4446

45-
assert($test instanceof TestCase);
46-
4747
$this->emitter->testErrored(
4848
TestMethodBuilder::fromTestCase($test),
4949
ThrowableBuilder::from($exception),
@@ -59,8 +59,6 @@ public function process(Test $test, string $serializedProcessResult, string $std
5959

6060
$exception = new AssertionFailedError('Test was run in child process and ended unexpectedly');
6161

62-
assert($test instanceof TestCase);
63-
6462
$this->emitter->testErrored(
6563
TestMethodBuilder::fromTestCase($test),
6664
ThrowableBuilder::from($exception),
@@ -74,21 +72,25 @@ public function process(Test $test, string $serializedProcessResult, string $std
7472
return;
7573
}
7674

77-
if ($childResult->expectedProcessExit !== null && $childResult->testCalledExit === true) {
78-
assert($test instanceof TestCase);
79-
80-
$test->assertSame($childResult->expectedProcessExit, $exitCode, 'Process exit-code expectation failed');
81-
} elseif ($childResult->expectedProcessExit !== null && $childResult->testCalledExit === false) {
82-
$test->fail('Process expected exit() to be called but test did not call it');
83-
} elseif ($childResult->expectedProcessExit === null && $childResult->testCalledExit === false) {
84-
$test->fail('Process called exit() but the test did not expect it');
75+
try {
76+
if ($childResult->expectedProcessExit !== null && $childResult->testCalledExit === true) {
77+
Assert::assertSame($childResult->expectedProcessExit, $exitCode, 'Process exit-code expectation failed');
78+
} elseif ($childResult->expectedProcessExit !== null && $childResult->testCalledExit === false) {
79+
Assert::fail('Process expected exit() to be called but test did not call it');
80+
} elseif ($childResult->expectedProcessExit === null && $childResult->testCalledExit === true) {
81+
Assert::fail('Process called exit() but the test did not expect it');
82+
}
83+
} catch (AssertionFailedError $e) {
84+
$this->emitter->testFailed(
85+
TestMethodBuilder::fromTestCase($test),
86+
ThrowableBuilder::from($e),
87+
null,
88+
);
8589
}
8690

8791
$this->eventFacade->forward($childResult->events);
8892
$this->passedTests->import($childResult->passedTests);
8993

90-
assert($test instanceof TestCase);
91-
9294
$test->setResult($childResult->testResult);
9395
$test->addToAssertionCount($childResult->numAssertions);
9496

src/Framework/TestRunner/templates/class.tpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ function __phpunit_run_isolated_test()
7676

7777
ob_end_clean();
7878
$output = '';
79+
7980
$testCalledExit = true;
80-
register_shutdown_function(function() use ($test, $output, $dispatcher, $testCalledExit) {
81+
register_shutdown_function(function() use ($test, $output, $dispatcher, &$testCalledExit) {
8182
file_put_contents(
8283
'{processResultFile}',
8384
serialize(

src/Framework/TestRunner/templates/method.tpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ function __phpunit_run_isolated_test()
7676

7777
ob_end_clean();
7878
$output = '';
79+
7980
$testCalledExit = true;
80-
register_shutdown_function(function() use ($test, $output, $dispatcher, $testCalledExit) {
81+
register_shutdown_function(function() use ($test, $output, $dispatcher, &$testCalledExit) {
8182
file_put_contents(
8283
'{processResultFile}',
8384
serialize(

src/Util/PHP/DefaultJobRunner.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use function is_array;
2424
use function is_resource;
2525
use function proc_close;
26+
use function proc_get_status;
2627
use function proc_open;
2728
use function stream_get_contents;
2829
use function sys_get_temp_dir;
@@ -147,8 +148,9 @@ private function runProcess(Job $job, ?string $temporaryFile): Result
147148
fclose($pipes[2]);
148149
}
149150

150-
$exitCode = 0;
151+
$exitCode = 0;
151152
$processStatus = proc_get_status($process);
153+
152154
if ($processStatus['running'] === false) {
153155
$exitCode = $processStatus['exitcode'];
154156
}

src/Util/PHP/Result.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
public function __construct(string $stdout, string $stderr, int $exitCode)
2626
{
27-
$this->stdout = $stdout;
28-
$this->stderr = $stderr;
27+
$this->stdout = $stdout;
28+
$this->stderr = $stderr;
2929
$this->exitCode = $exitCode;
3030
}
3131

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture;
11+
12+
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
13+
use PHPUnit\Framework\TestCase;
14+
15+
final class SeparateProcessesExpectedExitTest extends TestCase
16+
{
17+
#[RunInSeparateProcess]
18+
public function testExitExpectationMatched(): void
19+
{
20+
$this->expectProcessExit(0);
21+
$this->assertTrue(true);
22+
23+
exit(0);
24+
}
25+
26+
#[RunInSeparateProcess]
27+
public function testWrongExitExpectation(): void
28+
{
29+
$this->expectProcessExit(0);
30+
$this->assertTrue(true);
31+
32+
exit(1);
33+
}
34+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
phpunit --no-configuration ../../_files/SeparateProcessesTest.php
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = __DIR__ . '/../../_files/SeparateProcessesExpectedExitTest.php';
8+
9+
require_once __DIR__ . '/../../bootstrap.php';
10+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
11+
--EXPECTF--
12+
PHPUnit %s by Sebastian Bergmann and contributors.
13+
14+
Runtime: %s
15+
16+
EE 2 / 2 (100%)
17+
18+
Time: %s, Memory: %s
19+
20+
There were 2 errors:
21+
22+
1) PHPUnit\TestFixture\SeparateProcessesTest::testFoo
23+
Process called exit() but the test did not expect it
24+
25+
2) PHPUnit\TestFixture\SeparateProcessesTest::testBar
26+
Process called exit() but the test did not expect it
27+
28+
ERRORS!
29+
Tests: 2, Assertions: 0, Errors: 2.

tests/end-to-end/generic/separate-processes-test.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ PHPUnit %s by Sebastian Bergmann and contributors.
1313

1414
Runtime: %s
1515

16-
EE 2 / 2 (100%)
16+
EE 2 / 2 (100%)
1717

1818
Time: %s, Memory: %s
1919

2020
There were 2 errors:
2121

2222
1) PHPUnit\TestFixture\SeparateProcessesTest::testFoo
23-
Test was run in child process and ended unexpectedly
23+
Process called exit() but the test did not expect it
2424

2525
2) PHPUnit\TestFixture\SeparateProcessesTest::testBar
26-
Test was run in child process and ended unexpectedly
26+
Process called exit() but the test did not expect it
2727

2828
ERRORS!
2929
Tests: 2, Assertions: 0, Errors: 2.

tests/unit/Util/PHP/DefaultJobRunnerTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class DefaultJobRunnerTest extends TestCase
3030
public static function provider(): Generator
3131
{
3232
yield 'output to stdout' => [
33-
new Result('test', ''),
33+
new Result('test', '', 0),
3434
new Job(
3535
<<<'EOT'
3636
<?php declare(strict_types=1);
@@ -41,7 +41,7 @@ public static function provider(): Generator
4141
];
4242

4343
yield 'output to stderr' => [
44-
new Result('', 'test'),
44+
new Result('', 'test', 0),
4545
new Job(
4646
<<<'EOT'
4747
<?php declare(strict_types=1);
@@ -52,7 +52,7 @@ public static function provider(): Generator
5252
];
5353

5454
yield 'output to stdout and stderr' => [
55-
new Result('test-stdout', 'test-stderr'),
55+
new Result('test-stdout', 'test-stderr', 0),
5656
new Job(
5757
<<<'EOT'
5858
<?php declare(strict_types=1);
@@ -64,7 +64,7 @@ public static function provider(): Generator
6464
];
6565

6666
yield 'stderr redirected to stdout' => [
67-
new Result('test', ''),
67+
new Result('test', '', 0),
6868
new Job(
6969
<<<'EOT'
7070
<?php declare(strict_types=1);
@@ -76,7 +76,7 @@ public static function provider(): Generator
7676
];
7777

7878
yield 'configured environment variables' => [
79-
new Result('test', ''),
79+
new Result('test', '', 0),
8080
new Job(
8181
<<<'EOT'
8282
<?php declare(strict_types=1);
@@ -88,7 +88,7 @@ public static function provider(): Generator
8888
];
8989

9090
yield 'arguments' => [
91-
new Result('test', ''),
91+
new Result('test', '', 0),
9292
new Job(
9393
<<<'EOT'
9494
<?php declare(strict_types=1);
@@ -100,7 +100,7 @@ public static function provider(): Generator
100100
];
101101

102102
yield 'input from stdin' => [
103-
new Result('test', ''),
103+
new Result('test', '', 0),
104104
new Job(
105105
<<<'EOT'
106106
<?php declare(strict_types=1);

0 commit comments

Comments
 (0)