From ca30817f1c87bd8bf2cb7fe0434bce8f5480e37f Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Tue, 31 May 2016 17:05:28 -0400 Subject: [PATCH 1/2] Tests: Add tests for invalid property access in Expectations --- src/Spies/InvalidExpectationException.php | 5 +++++ tests/ExpectationTest.php | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 src/Spies/InvalidExpectationException.php diff --git a/src/Spies/InvalidExpectationException.php b/src/Spies/InvalidExpectationException.php new file mode 100644 index 0000000..4dcfdfa --- /dev/null +++ b/src/Spies/InvalidExpectationException.php @@ -0,0 +1,5 @@ +assertInternalType( 'string', $expectation->verify() ); } + public function test_once_as_property_throws_an_error() { + $this->expectException( \Spies\InvalidExpectationException::class ); + $spy = \Spies\make_spy(); + \Spies\expect_spy( $spy )->to_have_been_called->once; + } + public function test_once_is_met_if_spy_is_called_once() { $spy = \Spies\make_spy(); $expectation = \Spies\expect_spy( $spy )->to_have_been_called->once(); @@ -100,6 +106,12 @@ public function test_once_is_not_met_if_spy_is_not_called() { $this->assertInternalType( 'string', $expectation->verify() ); } + public function test_twice_as_property_throws_an_error() { + $this->expectException( \Spies\InvalidExpectationException::class ); + $spy = \Spies\make_spy(); + \Spies\expect_spy( $spy )->to_have_been_called->twice; + } + public function test_once_is_not_met_if_spy_is_called_twice() { $spy = \Spies\make_spy(); $expectation = \Spies\expect_spy( $spy )->to_have_been_called->once(); From d67ed6f8601fcff9593b0925a2dd622dbfb8bee7 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Tue, 31 May 2016 17:06:11 -0400 Subject: [PATCH 2/2] Throw exception when accessing invalid property on an Expectation This should help prevent accidentlly ending an Expectation with `once` instead of `once()`. --- src/Spies/Expectation.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Spies/Expectation.php b/src/Spies/Expectation.php index d75d3e6..e6ccb8c 100644 --- a/src/Spies/Expectation.php +++ b/src/Spies/Expectation.php @@ -49,6 +49,7 @@ public function __get( $key ) { $this->negation = true; return $this; } + throw new InvalidExpectationException( 'Invalid property: "' . $key . '" does not exist on this Expectation' ); } /**