Skip to content

Commit baabb7f

Browse files
committed
Merge pull request #110 from 99designs/phpunit-test-warnings-should-cause-failure
PHPUnit test failures should cause test run to fail
2 parents 0834c8d + e6cc87a commit baabb7f

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

src/ParaTest/Runners/PHPUnit/ExecutableTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ public function getStderr()
8181
return $this->process->getErrorOutput();
8282
}
8383

84+
/**
85+
* Return any warnings that are in the test output, or false if there are none
86+
* @return mixed
87+
*/
88+
public function getWarnings()
89+
{
90+
if(!$this->process)
91+
return false;
92+
93+
// PHPUnit has a bug where by it doesn't include warnings in the junit
94+
// output, but still fails. This is a hacky, imperfect method for extracting them
95+
// see https://github.com/sebastianbergmann/phpunit/issues/1317
96+
preg_match_all(
97+
'/^\d+\) Warning\n(.+?)$/ms',
98+
$this->process->getOutput(),
99+
$matches
100+
);
101+
102+
return isset($matches[1]) ? $matches[1] : false;
103+
}
104+
84105
/**
85106
* Stop the process and return it's
86107
* exit code

src/ParaTest/Runners/PHPUnit/ResultPrinter.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ class ResultPrinter
7373
*/
7474
protected $colors;
7575

76+
/**
77+
* Warnings generated by the cases
78+
*
79+
* @var array
80+
*/
81+
protected $warnings = array();
82+
7683
public function __construct(LogInterpreter $results)
7784
{
7885
$this->results = $results;
@@ -142,6 +149,7 @@ public function printResults()
142149
print $this->getHeader();
143150
print $this->getErrors();
144151
print $this->getFailures();
152+
print $this->getWarnings();
145153
print $this->getFooter();
146154
}
147155

@@ -158,6 +166,13 @@ public function printFeedback(ExecutableTest $test)
158166
$feedbackItems = $reader->getFeedback();
159167
foreach ($feedbackItems as $item)
160168
$this->printFeedbackItem($item);
169+
170+
$warnings = $test->getWarnings();
171+
if ($warnings) {
172+
$this->addWarnings($warnings);
173+
foreach ($warnings as $warning)
174+
$this->printFeedbackItem('W');
175+
}
161176
}
162177

163178
/**
@@ -186,6 +201,32 @@ public function getHeader()
186201
return "\n\n" . $this->timer->resourceUsage() . "\n\n";
187202
}
188203

204+
/**
205+
* Add an array of warning strings. These cause the test run to be shown
206+
* as failed
207+
*/
208+
public function addWarnings(array $warnings)
209+
{
210+
$this->warnings = array_merge($this->warnings, $warnings);
211+
}
212+
213+
/**
214+
* Returns warning messages as a string
215+
*/
216+
public function getWarnings()
217+
{
218+
return $this->getDefects($this->warnings, 'warning');
219+
}
220+
221+
/**
222+
* Whether the test run is successful and has no warnings
223+
* @return bool
224+
*/
225+
protected function isSuccessful()
226+
{
227+
return $this->results->isSuccessful() && count($this->warnings) == 0;
228+
}
229+
189230
/**
190231
* Return the footer information reporting success
191232
* or failure
@@ -194,7 +235,7 @@ public function getHeader()
194235
*/
195236
public function getFooter()
196237
{
197-
return $this->results->isSuccessful()
238+
return $this->isSuccessful()
198239
? $this->getSuccessFooter()
199240
: $this->getFailedFooter();
200241
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
class HasWarningsTest extends PHPUnit_Framework_TestCase
4+
{
5+
public function testPassingTest()
6+
{
7+
$this->assertTrue(true);
8+
}
9+
10+
private function testPrivateTest()
11+
{
12+
$this->assertTrue(true);
13+
}
14+
15+
/**
16+
* @dataProvider llamas
17+
*/
18+
private function testMissingDataProvider()
19+
{
20+
$this->assertTrue(true);
21+
}
22+
}

test/functional/PHPUnitTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,20 @@ public function testFunctionalModeEachTestCalledOnce()
283283
$this->assertTestsPassed($proc, 2, 2);
284284
}
285285

286+
public function testTestsWithWarningsResultInFailure()
287+
{
288+
$proc = $this->invokeParatest("warning-tests/HasWarningsTest.php",
289+
array('bootstrap' => BOOTSTRAP)
290+
);
291+
292+
$output = $proc->getOutput();
293+
294+
$this->assertEquals(1, $proc->getExitCode(), "Test should fail with 1");
295+
$this->assertRegExp("/There were 2 warnings/", $output);
296+
$this->assertRegExp("/FAILURES!\n".
297+
"Tests: 1, Assertions: 1, Failures: 0, Errors: 0./", $output);
298+
}
299+
286300
public function setsCoveragePhpDataProvider()
287301
{
288302
return array(

0 commit comments

Comments
 (0)