Skip to content

Commit 7da0f24

Browse files
authored
Merge pull request #146 from php-school/generic-comparison-result
Generic comparison failure result
2 parents 6fc9062 + d012e0d commit 7da0f24

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed

src/Result/ComparisonFailure.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\Result;
4+
5+
/**
6+
* @author Aydin Hassan <[email protected]>
7+
*/
8+
class ComparisonFailure implements FailureInterface
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private $name;
14+
15+
/**
16+
* @var string
17+
*/
18+
private $expectedValue;
19+
20+
/**
21+
* @var string
22+
*/
23+
private $actualValue;
24+
25+
/**
26+
* @param string $name
27+
* @param string $expectedValue
28+
* @param string $actualValue
29+
*/
30+
public function __construct($name, $expectedValue, $actualValue)
31+
{
32+
$this->name = $name;
33+
$this->expectedValue = $expectedValue;
34+
$this->actualValue = $actualValue;
35+
}
36+
37+
/**
38+
* @param string $name
39+
* @param string $expectedValue
40+
* @param string $actualValue
41+
* @return static
42+
*/
43+
public static function fromNameAndValues($name, $expectedValue, $actualValue)
44+
{
45+
return new static($name, $expectedValue, $actualValue);
46+
}
47+
48+
/**
49+
* Get the name of the check that this result was produced from.
50+
*
51+
* @return string
52+
*/
53+
public function getCheckName()
54+
{
55+
return $this->name;
56+
}
57+
58+
/**
59+
* Get the expected value.
60+
*
61+
* @return string
62+
*/
63+
public function getExpectedValue()
64+
{
65+
return $this->expectedValue;
66+
}
67+
68+
/**
69+
* Get the actual value.
70+
*
71+
* @return string
72+
*/
73+
public function getActualValue()
74+
{
75+
return $this->actualValue;
76+
}
77+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\ResultRenderer;
4+
5+
use PhpSchool\PhpWorkshop\Result\Cli\RequestFailure;
6+
use PhpSchool\PhpWorkshop\Result\ComparisonFailure;
7+
8+
/**
9+
* Renderer for `PhpSchool\PhpWorkshop\Result\ComparisonFailure`.
10+
*
11+
* @package PhpSchool\PhpWorkshop\ResultRenderer
12+
*/
13+
class ComparisonFailureRenderer implements ResultRendererInterface
14+
{
15+
/**
16+
* @var RequestFailure
17+
*/
18+
private $result;
19+
20+
/**
21+
* @param ComparisonFailure $result The failure.
22+
*/
23+
public function __construct(ComparisonFailure $result)
24+
{
25+
$this->result = $result;
26+
}
27+
28+
/**
29+
* Print the actual and expected output.
30+
*
31+
* @param ResultsRenderer $renderer
32+
* @return string
33+
*/
34+
public function render(ResultsRenderer $renderer)
35+
{
36+
return sprintf(
37+
" %s\n%s\n\n %s\n%s\n",
38+
$renderer->style('YOUR OUTPUT:', ['bold', 'yellow']),
39+
$this->indent($renderer->style(sprintf('"%s"', $this->result->getActualValue()), 'red')),
40+
$renderer->style('EXPECTED OUTPUT:', ['bold', 'yellow']),
41+
$this->indent($renderer->style(sprintf('"%s"', $this->result->getExpectedValue()), 'green'))
42+
);
43+
}
44+
45+
/**
46+
* @param string $string
47+
* @return string
48+
*/
49+
private function indent($string)
50+
{
51+
return implode(
52+
"\n",
53+
array_map(
54+
function ($line) {
55+
return sprintf(' %s', $line);
56+
},
57+
explode("\n", $string)
58+
)
59+
);
60+
}
61+
}

test/Result/ComparisonFailureTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshopTest\Result;
4+
5+
use PhpSchool\PhpWorkshop\Result\ComparisonFailure;
6+
use PHPUnit_Framework_TestCase;
7+
use PhpSchool\PhpWorkshop\Result\StdOutFailure;
8+
9+
/**
10+
* @author Aydin Hassan <[email protected]>
11+
*/
12+
class ComparisonFailureTest extends PHPUnit_Framework_TestCase
13+
{
14+
public function testGetters()
15+
{
16+
$failure = new ComparisonFailure('Name', 'Expected Output', 'Actual Output');
17+
self::assertSame('Name', $failure->getCheckName());
18+
self::assertEquals('Expected Output', $failure->getExpectedValue());
19+
self::assertEquals('Actual Output', $failure->getActualValue());
20+
}
21+
22+
public function testFailureFromArgsAndOutput()
23+
{
24+
$failure = ComparisonFailure::fromNameAndValues('Name', 'Expected Output', 'Actual Output');
25+
self::assertSame('Name', $failure->getCheckName());
26+
self::assertEquals('Expected Output', $failure->getExpectedValue());
27+
self::assertEquals('Actual Output', $failure->getActualValue());
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshopTest\ResultRenderer;
4+
5+
use PhpSchool\PhpWorkshop\Result\ComparisonFailure;
6+
use PhpSchool\PhpWorkshop\ResultRenderer\ComparisonFailureRenderer;
7+
8+
/**
9+
* @author Aydin Hassan <[email protected]>
10+
*/
11+
class ComparisonFailureRendererTest extends AbstractResultRendererTest
12+
{
13+
public function testRender()
14+
{
15+
$failure = new ComparisonFailure('Name', 'EXPECTED OUTPUT', 'ACTUAL OUTPUT');
16+
$renderer = new ComparisonFailureRenderer($failure);
17+
18+
$expected = " \e[33m\e[1mYOUR OUTPUT:\e[0m\e[0m\n";
19+
$expected .= " \e[31m\"ACTUAL OUTPUT\"\e[0m\n\n";
20+
$expected .= " \e[33m\e[1mEXPECTED OUTPUT:\e[0m\e[0m\n";
21+
$expected .= " \e[32m\"EXPECTED OUTPUT\"\e[0m\n";
22+
23+
$this->assertEquals($expected, $renderer->render($this->getRenderer()));
24+
}
25+
}

0 commit comments

Comments
 (0)