Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #960 from Taluu/should-trigger
Possibility to test if a php error is triggered
- Loading branch information
Showing
10 changed files
with
541 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
features/matchers/developer_uses_trigger_matcher.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
Feature: Developer uses trigger matcher | ||
As a Developer | ||
I want a trigger matcher | ||
In order to validate triggered exceptions against my expectations | ||
|
||
Scenario: Checking if a deprecated error has been triggered | ||
Given the spec file "spec/Matchers/TriggerExample1/FooSpec.php" contains: | ||
""" | ||
<?php | ||
namespace spec\Matchers\TriggerExample1; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
class FooSpec extends ObjectBehavior | ||
{ | ||
function it_triggers_an_error_when_calling_something_deprecated() | ||
{ | ||
$this->shouldTrigger(E_USER_DEPRECATED)->duringDoDeprecatedStuff(); | ||
} | ||
} | ||
""" | ||
And the class file "src/Matchers/TriggerExample1/Foo.php" contains: | ||
""" | ||
<?php | ||
namespace Matchers\TriggerExample1; | ||
class Foo | ||
{ | ||
public function doDeprecatedStuff() | ||
{ | ||
trigger_error('Foo', E_USER_DEPRECATED); | ||
} | ||
} | ||
""" | ||
When I run phpspec | ||
Then the suite should pass | ||
|
||
Scenario: Checking that a deprecated error has the right message | ||
Given the spec file "spec/Matchers/TriggerExample2/FooSpec.php" contains: | ||
""" | ||
<?php | ||
namespace spec\Matchers\TriggerExample3; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
class FooSpec extends ObjectBehavior | ||
{ | ||
function it_triggers_a_specific_deprecated_error_when_calling_deprecated_method() | ||
{ | ||
$this->shouldTrigger(E_USER_DEPRECATED, 'This is deprecated')->duringDoDeprecatedStuff(); | ||
} | ||
} | ||
""" | ||
And the class file "src/Matchers/TriggerExample2/Foo.php" contains: | ||
""" | ||
<?php | ||
namespace Matchers\TriggerExample2; | ||
class Foo | ||
{ | ||
public function doDeprecatedStuff() | ||
{ | ||
trigger_error('This is deprecated', E_USER_DEPRECATED); | ||
} | ||
} | ||
""" | ||
When I run phpspec | ||
Then the suite should pass | ||
|
||
Scenario: "Trigger" alias matches using the trigger matcher and let the code continue afterwards | ||
Given the spec file "spec/Matchers/TriggerExample3/FooSpec.php" contains: | ||
""" | ||
<?php | ||
namespace spec\Matchers\TriggerExample3; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
class FooSpec extends ObjectBehavior | ||
{ | ||
function it_triggers_a_deprecated_error_when_calling_deprecated_method_but_do_not_interrupt() | ||
{ | ||
$this->shouldTrigger(E_USER_DEPRECATED, 'This is deprecated')->duringDoDeprecatedStuff(0); | ||
$this->getDeprecated()->shouldBe(0); | ||
} | ||
} | ||
""" | ||
And the class file "src/Matchers/TriggerExample3/Foo.php" contains: | ||
""" | ||
<?php | ||
namespace Matchers\TriggerExample3; | ||
class Foo | ||
{ | ||
private $deprecated; | ||
public function getDeprecated() | ||
{ | ||
return $this->deprecated; | ||
} | ||
public function doDeprecatedStuff($value) | ||
{ | ||
trigger_error('This is deprecated', E_USER_DEPRECATED); | ||
$this->deprecated = $value; | ||
} | ||
} | ||
""" | ||
When I run phpspec | ||
Then the suite should pass | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace spec\PhpSpec\Matcher; | ||
|
||
use PhpSpec\Factory\ReflectionFactory; | ||
use PhpSpec\ObjectBehavior; | ||
use PhpSpec\Wrapper\Unwrapper; | ||
use Prophecy\Argument; | ||
use PhpSpec\Formatter\Presenter\Presenter; | ||
use PhpSpec\Exception\Example\SkippingException; | ||
use ArrayObject; | ||
|
||
class TriggerMatcherSpec extends ObjectBehavior | ||
{ | ||
function let(Unwrapper $unwrapper) | ||
{ | ||
$unwrapper->unwrapAll(Argument::any())->willReturnArgument(); | ||
|
||
$this->beConstructedWith($unwrapper); | ||
} | ||
|
||
function it_supports_the_trigger_alias_for_object_and_exception_name() | ||
{ | ||
$this->supports('trigger', '', array())->shouldReturn(true); | ||
} | ||
|
||
function it_accepts_a_method_during_which_an_error_should_be_triggered(ArrayObject $arr) | ||
{ | ||
$arr->ksort()->will(function () { trigger_error('An error', E_USER_NOTICE); }); | ||
|
||
$this->positiveMatch('trigger', $arr, array(E_USER_NOTICE, 'An error'))->during('ksort', array()); | ||
} | ||
|
||
function it_accepts_a_method_during_which_any_error_should_be_triggered(ArrayObject $arr) | ||
{ | ||
$arr->ksort()->will(function () { trigger_error('An error', E_USER_NOTICE); }); | ||
|
||
$this->positiveMatch('trigger', $arr, array(null, null))->during('ksort', array()); | ||
} | ||
|
||
function it_accepts_a_method_during_which_an_error_should_not_be_triggered(ArrayObject $arr) | ||
{ | ||
$this->negativeMatch('trigger', $arr, array(E_USER_NOTICE, 'An error'))->during('ksort', array()); | ||
} | ||
|
||
function it_accepts_a_method_during_which_any_error_should_not_be_triggered(ArrayObject $arr) | ||
{ | ||
$this->negativeMatch('trigger', $arr, array(null, null))->during('ksort', array()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.