Skip to content
This repository has been archived by the owner on Mar 24, 2020. It is now read-only.

Extraction the ExceptionPresenter and renamed StringPresenter to ValuePresenter #100

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions spec/PHPSpec2/Formatter/Presenter/ExceptionPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace spec\PHPSpec2\Formatter\Presenter;

use PHPSpec2\ObjectBehavior;
use PHPSpec2\Exception\Example\NotEqualException;
use PHPSpec2\Exception\Exception;
use PHPSpec2\Exception\Example\MockerException;

class ExceptionPresenter extends ObjectBehavior
{
/**
* @param PHPSpec2\Formatter\Presenter\ValuePresenter $valuePresenter
* @param PHPSpec2\Formatter\Presenter\Differ\Differ $differ
*/
function let($valuePresenter, $differ)
{
$this->beConstructedWith($valuePresenter, $differ);
}

function it_should_show_the_stacktrace_of_an_exception($valuePresenter)
{
$exc = new Exception('personal Exception');
$valuePresenter->presentValue->willReturn('personal Exception');
$valuePresenter->presentValue->willReturn('"stacktrace"');

$this->presentExceptionStackTrace($exc)->shouldBeLike('
0 spec/PHPSpec2/Formatter/Presenter/ExceptionPresenter.php:23
throw new PHPSpec2\Exception\Exception(personal Exception)
1 [internal]
spec\PHPSpec2\Formatter\Presenter\ExceptionPresenter->it_should_show_the_stacktrace_of_an_exception("stacktrace")
');
}

function it_should_return_a_string_for_different_kind_of_exception($valuePresenter, $mockerException)
{
throw new \PHPSpec2\Exception\Example\PendingException('Error here, "PHP Fatal error: Call to undefined method Mockery\Exception::setCause() in Formatter/PrettyFormatter.php on line 129"');
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got some trouble here, a suggestion is very appreciated.

$mockerException->beAMockOf('\PHPSpec2\Exception\Example\MockerException');
$mockerException->beConstructedWith('Message');

$mockerException->setCause('a');
$mockerException->getCode->willReturn(array());
$mockerException->getTrace->willReturn(array());
$mockerException->getFile->willReturn('filename');
$mockerException->getLine->willReturn(23);

$valuePresenter->presentValue->willReturn($mockerException);
$this->presentException($mockerException, false)->shuldReturn('AA');
}

function it_should_compare_the_exception_with_diff($differ)
{
$actual = new Exception();
$expected = new Exception();

$notEqualException = new NotEqualException('message', $actual, $expected);

$differ->compare($actual, $expected)->shouldBeCalled();
$this->presentExceptionDifference($notEqualException)->shouldReturn(null);
}


function it_should_show_the_trace_function($valuePresenter)
{
$valuePresenter->presentValue->willReturn('2');
$this->presentExceptionTraceFunction('function', array('2'))->shouldBeLike(" function(2)\n");
}


function it_should_show_the_trace_method($valuePresenter)
{
$valuePresenter->presentValue->willReturn('3');
$this->presentExceptionTraceMethod('class', 'type', 'method', array('3'))->shouldBeLike(" classtypemethod(3)\n");
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,8 @@

use PHPSpec2\ObjectBehavior;

class TaggedPresenter extends ObjectBehavior
class TaggedValuePresenter extends ObjectBehavior
{
/**
* @param PHPSpec2\Formatter\Presenter\Differ\Differ $differ
*/
function let($differ)
{
$this->beConstructedWith($differ);
}

function it_should_wrap_value_into_tags()
{
$this->presentValue('string')->shouldReturn('<value>"string"</value>');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@

use PHPSpec2\ObjectBehavior;

class StringPresenter extends ObjectBehavior
class ValuePresenter extends ObjectBehavior
{
/**
* @param PHPSpec2\Formatter\Presenter\Differ\Differ $differ
*/
function let($differ)
{
$this->beConstructedWith($differ);
}

function it_should_present_short_string_in_quotes()
{
Expand Down Expand Up @@ -56,14 +49,14 @@ function it_should_present_closure_as_type()
$this->presentValue(function(){})->shouldReturn('[closure]');
}

function it_should_present_exception_as_class_with_constructor()
function it_should_present_string_as_string()
{
$this->presentValue(new \RuntimeException('message'))
->shouldReturn('[exc:RuntimeException("message")]');
$this->presentString('some string')->shouldReturn('some string');
}

function it_should_present_string_as_string()
function it_should_present_exception_as_class_with_constructor()
{
$this->presentString('some string')->shouldReturn('some string');
$this->presentValue(new \RuntimeException('message'))
->shouldReturn('[exc:RuntimeException("message")]');
}
}
2 changes: 1 addition & 1 deletion spec/PHPSpec2/Matcher/ComparisonMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class ComparisonMatcher extends ObjectBehavior
{
/**
* @param PHPSpec2\Formatter\Presenter\StringPresenter $presenter
* @param PHPSpec2\Formatter\Presenter\ValuePresenter $presenter
*/
function let($presenter)
{
Expand Down
2 changes: 1 addition & 1 deletion spec/PHPSpec2/Matcher/CountMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class CountMatcher extends ObjectBehavior
{
/**
* @param PHPSpec2\Formatter\Presenter\StringPresenter $presenter
* @param PHPSpec2\Formatter\Presenter\ValuePresenter $presenter
*/
function let($presenter)
{
Expand Down
2 changes: 1 addition & 1 deletion spec/PHPSpec2/Matcher/IdentityMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class IdentityMatcher extends ObjectBehavior
{
/**
* @param PHPSpec2\Formatter\Presenter\StringPresenter $presenter
* @param PHPSpec2\Formatter\Presenter\ValuePresenter $presenter
*/
function let($presenter)
{
Expand Down
2 changes: 1 addition & 1 deletion spec/PHPSpec2/Matcher/ObjectStateMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class ObjectStateMatcher extends ObjectBehavior
{
/**
* @param PHPSpec2\Formatter\Presenter\StringPresenter $presenter
* @param PHPSpec2\Formatter\Presenter\ValuePresenter $presenter
*/
function let($presenter)
{
Expand Down
2 changes: 1 addition & 1 deletion spec/PHPSpec2/Matcher/ScalarMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class ScalarMatcher extends ObjectBehavior
{
/**
* @param PHPSpec2\Formatter\Presenter\StringPresenter $presenter
* @param PHPSpec2\Formatter\Presenter\ValuePresenter $presenter
*/
public function let($presenter)
{
Expand Down
2 changes: 1 addition & 1 deletion spec/PHPSpec2/Matcher/TypeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class TypeMatcher extends ObjectBehavior
{
/**
* @param PHPSpec2\Formatter\Presenter\StringPresenter $presenter
* @param PHPSpec2\Formatter\Presenter\ValuePresenter $presenter
*/
function let($presenter)
{
Expand Down
14 changes: 12 additions & 2 deletions src/PHPSpec2/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,18 @@ public function __construct($version)
}))
);

$c->extend('differ.engines',
$c->set('differ.engines.array', $c->share(function($c) {
return new Presenter\Differ\ArrayEngine();
}))
);

$c->set('value_presenter', $c->share(function($c) {
return new Presenter\TaggedPresenter($c('differ'));
return new Presenter\TaggedValuePresenter();
}));

$c->set('exception_presenter', $c->share(function($c) {
return new Presenter\ExceptionPresenter($c('value_presenter'), $c('differ'));
}));

$c->set('mocker', $c->share(function($c) {
Expand All @@ -103,7 +113,7 @@ public function __construct($version)
}

$formatter->setIO($c('io'));
$formatter->setPresenter($c('value_presenter'));
$formatter->setExceptionPresenter($c('exception_presenter'));
$formatter->setStatisticsCollector($c('statistics_collector'));

return $formatter;
Expand Down
14 changes: 13 additions & 1 deletion src/PHPSpec2/Exception/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

namespace PHPSpec2\Exception;

class Exception extends \Exception
class Exception extends \Exception implements ExceptionInterface
{
private $cause;


public function setCause($cause)
{
$this->cause = $cause;
}

public function getCause()
{
return $this->cause;
}
}
9 changes: 9 additions & 0 deletions src/PHPSpec2/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PHPSpec2\Exception;

Interface ExceptionInterface
{
public function getCause();
public function setCause($cause);
}
4 changes: 2 additions & 2 deletions src/PHPSpec2/Formatter/FormatterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

use PHPSpec2\Console\IO;
use PHPSpec2\Formatter\Presenter\PresenterInterface;
use PHPSpec2\Formatter\Presenter\ExceptionPresenterInterface;
use PHPSpec2\Listener\StatisticsCollector;

interface FormatterInterface extends EventSubscriberInterface
{
public function setIO(IO $io);
public function setPresenter(PresenterInterface $presenter);
public function setExceptionPresenter(ExceptionPresenterInterface $presenter);
public function setStatisticsCollector(StatisticsCollector $stats);
}
32 changes: 32 additions & 0 deletions src/PHPSpec2/Formatter/Presenter/Differ/ArrayEngine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace PHPSpec2\Formatter\Presenter\Differ;

class ArrayEngine implements EngineInterface
{
public function supports($expected, $actual)
{
return is_array($expected) && is_array($actual);
}

public function compare($expected, $actual)
{
$diff = new \Diff($expected, $actual, array());

$renderer = new \Diff_Renderer_Text_Unified;
$text = $diff->render($renderer);

$lines = array();
foreach (explode("\n", $text) as $line) {
if (0 === strpos($line, '-')) {
$lines[] = sprintf('<diff-del>%s</diff-del>', $line);
} elseif (0 === strpos($line, '+')) {
$lines[] = sprintf('<diff-add>%s</diff-add>', $line);
} else {
$lines[] = $line;
}
}

return sprintf("<code>\n%s</code>", implode("\n", $lines));
}
}
Loading