Skip to content

Commit

Permalink
Merge pull request #219 from tayguara/tayguara/junit-result
Browse files Browse the repository at this point in the history
Implemented the SystemOut on xml result, if the test has fail or have error.
  • Loading branch information
julianseeger authored Aug 8, 2016
2 parents 54fb040 + f7763af commit ea73dc9
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/ParaTest/Logging/JUnit/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ protected function addDefect($collName, $type, $text)
);
}

/**
* Add systemOut result on test (if has failed or have error)
*
* @param mixed $node
* @return mixed
*/
public static function addSystemOut($node)
{
$sys = 'system-out';

if(!empty($node->failure)) {
$node->failure = (string)$node->failure . (string)$node->{$sys};
}

if(!empty($node->error)) {
$node->error = (string)$node->error . (string)$node->{$sys};
}

return $node;
}

/**
* Factory method that creates a TestCase object
* from a SimpleXMLElement
Expand All @@ -122,14 +143,19 @@ public static function caseFromNode(\SimpleXMLElement $node)
(string) $node['assertions'],
(string) $node['time']
);
$failures = $node->xpath('failure');
$errors = $node->xpath('error');

$node = self::addSystemOut($node);
$failures = $node->xpath('failure');
$errors = $node->xpath('error');

while (list( , $fail) = each($failures)) {
$case->addFailure((string)$fail['type'], (string)$fail);
}

while (list( , $err) = each($errors)) {
$case->addError((string)$err['type'], (string)$err);
}

return $case;
}
}
47 changes: 47 additions & 0 deletions test/fixtures/results/mixed-results-with-system-out.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="test/fixtures/tests/" tests="7" assertions="6" failures="2" errors="1" time="0.007625">
<testsuite name="UnitTestWithClassAnnotationTest" tests="3" assertions="3" failures="1" errors="0" time="0.006109" file="/home/brian/Projects/parallel-phpunit/test/fixtures/tests/UnitTestWithClassAnnotationTest.php">
<testcase name="testTruth" class="UnitTestWithClassAnnotationTest" file="/home/brian/Projects/parallel-phpunit/test/fixtures/tests/UnitTestWithClassAnnotationTest.php" line="10" assertions="1" time="0.001760"/>
<testcase name="testFalsehood" class="UnitTestWithClassAnnotationTest" file="/home/brian/Projects/parallel-phpunit/test/fixtures/tests/UnitTestWithClassAnnotationTest.php" line="18" assertions="1" time="0.001195">
<failure type="PHPUnit_Framework_ExpectationFailedException">UnitTestWithMethodAnnotationsTest::testFalsehood
Failed asserting that true is false.

/home/brian/Projects/parallel-phpunit/test/fixtures/tests/UnitTestWithMethodAnnotationsTest.php:18
</failure>
</testcase>
<testcase name="testArrayLength" class="UnitTestWithClassAnnotationTest" file="/home/brian/Projects/parallel-phpunit/test/fixtures/tests/UnitTestWithClassAnnotationTest.php" line="26" assertions="1" time="0.003154"/>
</testsuite>
<testsuite name="UnitTestWithErrorTest" file="/home/brian/Projects/parallel-phpunit/test/fixtures/tests/UnitTestWithErrorTest.php" tests="1" assertions="0" failures="0" errors="1" time="0.000397">
<testcase name="testTruth" class="UnitTestWithErrorTest" file="/home/brian/Projects/parallel-phpunit/test/fixtures/tests/UnitTestWithErrorTest.php" line="10" assertions="0" time="0.000397">
<error type="Exception">UnitTestWithErrorTest::testTruth
Exception: Error!!!

/home/brian/Projects/parallel-phpunit/test/fixtures/tests/UnitTestWithErrorTest.php:12
</error>
</testcase>
</testsuite>
<testsuite name="UnitTestWithErrorTest" file="/home/brian/Projects/parallel-phpunit/test/fixtures/tests/UnitTestWithErrorTest.php" tests="1" assertions="0" failures="0" errors="1" time="0.000397">
<testcase name="testTruth" class="UnitTestWithErrorTest" file="/home/brian/Projects/parallel-phpunit/test/fixtures/tests/UnitTestWithErrorTest.php" line="10" assertions="0" time="0.000397">
<error type="Exception">UnitTestWithErrorTest::testTruth
Exception: Error!!!

/home/brian/Projects/parallel-phpunit/test/fixtures/tests/UnitTestWithErrorTest.php:12
</error>
<system-out>Custom error log on result test with error!</system-out>
</testcase>
</testsuite>
<testsuite name="UnitTestWithMethodAnnotationsTest" file="/home/brian/Projects/parallel-phpunit/test/fixtures/tests/UnitTestWithMethodAnnotationsTest.php" tests="3" assertions="3" failures="1" errors="0" time="0.001119">
<testcase name="testTruth" class="UnitTestWithMethodAnnotationsTest" file="/home/brian/Projects/parallel-phpunit/test/fixtures/tests/UnitTestWithMethodAnnotationsTest.php" line="7" assertions="1" time="0.000341"/>
<testcase name="testFalsehood" class="UnitTestWithMethodAnnotationsTest" file="/home/brian/Projects/parallel-phpunit/test/fixtures/tests/UnitTestWithMethodAnnotationsTest.php" line="16" assertions="1" time="0.000402">
<failure type="PHPUnit_Framework_ExpectationFailedException">UnitTestWithMethodAnnotationsTest::testFalsehood
Failed asserting that true is false.

/home/brian/Projects/parallel-phpunit/test/fixtures/tests/UnitTestWithMethodAnnotationsTest.php:18
</failure>
<system-out>Custom error log on result test with failure!</system-out>
</testcase>
<testcase name="testArrayLength" class="UnitTestWithMethodAnnotationsTest" file="/home/brian/Projects/parallel-phpunit/test/fixtures/tests/UnitTestWithMethodAnnotationsTest.php" line="25" assertions="1" time="0.000376"/>
</testsuite>
</testsuite>
</testsuites>
30 changes: 30 additions & 0 deletions test/unit/ParaTest/Logging/JUnit/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,34 @@ public function testRemoveLog()
$reader->removeLog();
$this->assertFalse(file_exists($tmp));
}

/**
* Extraction of log from xml file to use in test of validation "SystemOut" result
*
* @return \stdClass $log
*/
public static function extractLog()
{
$log = new \stdClass();
$result = FIXTURES . DS . 'results' . DS . 'mixed-results-with-system-out.xml';
$node = new Reader($result);
$log->failure = $node->getSuites()[0]->suites[0]->cases[1]->failures[0]['text'];
$log->error = $node->getSuites()[0]->suites[1]->cases[0]->errors[0]['text'];

return $log;
}

public function testResultWithSystemOut()
{
$customLog = "\nCustom error log on result test with ";
$result = FIXTURES . DS . 'results' . DS . 'mixed-results-with-system-out.xml';
$failLog = self::extractLog()->failure . $customLog . "failure!";
$errorLog = self::extractLog()->error . $customLog . "error!";
$node = new Reader($result);
$resultFail = $node->getSuites()[0]->suites[2]->cases[1]->failures[0]['text'];
$resultError = $node->getSuites()[0]->suites[1]->cases[1]->errors[0]['text'];

$this->assertEquals($failLog, $resultFail);
$this->assertEquals($errorLog, $resultError);
}
}

0 comments on commit ea73dc9

Please sign in to comment.