From 434ceff871378601117499b6f141fe4e21498b88 Mon Sep 17 00:00:00 2001 From: Vaclav Prokes Date: Tue, 10 Jun 2014 18:18:44 +0200 Subject: [PATCH] Fix mock freeze: only expected methods must by frozen --- Mockista/Mock.php | 10 +++++----- tests/RegistryTest.php | 16 +++++++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Mockista/Mock.php b/Mockista/Mock.php index 4841a30..0299af7 100644 --- a/Mockista/Mock.php +++ b/Mockista/Mock.php @@ -20,7 +20,7 @@ public function __construct() public function assertExpectations() { - if ($this->frozen) { + if ($this->frozen && isset($this->methods['assertExpectations'])) { return $this->__call(__FUNCTION__, func_get_args()); } foreach ($this->methods as $method) { @@ -36,7 +36,7 @@ public function assertExpectations() */ public function getName() { - if ($this->frozen) { + if ($this->frozen && isset($this->methods['getName'])) { return $this->__call(__FUNCTION__, func_get_args()); } return $this->name; @@ -48,7 +48,7 @@ public function getName() */ public function setName($name) { - if ($this->frozen) { + if ($this->frozen && isset($this->methods['setName'])) { return $this->__call(__FUNCTION__, func_get_args()); } $this->name = $name; @@ -60,7 +60,7 @@ public function setName($name) */ public function freeze() { - if ($this->frozen) { + if ($this->frozen && isset($this->methods['freeze'])) { return $this->__call(__FUNCTION__, func_get_args()); } $this->frozen = TRUE; @@ -114,7 +114,7 @@ protected function findMethod($name, $args) */ public function expects($name) { - if ($this->frozen) { + if ($this->frozen && isset($this->methods['expects'])) { return $this->__call(__FUNCTION__, func_get_args()); } diff --git a/tests/RegistryTest.php b/tests/RegistryTest.php index 9e69e7d..c563adc 100644 --- a/tests/RegistryTest.php +++ b/tests/RegistryTest.php @@ -88,14 +88,20 @@ function testFreezeMock() $this->assertEquals("Already freezed", $mock->setName("name")); } - /** - * @expectedException Mockista\MockException - * @expectedMessage Unexpected call in mock Test::getName() - */ - function testFreezeMockExceptionOnUndefinedMethod() + function testFreezeMockWithAssertExpectations() + { + $mock1 = $this->object->create(); + $mock1->expects('abc')->once(); + $mock1->abc(); + $mock1->freeze(); + $this->object->assertExpectations(); + } + + function testFreezeMockNotFrozenMethodCall() { $mock = $this->object->create("Mockista\A"); $mock->freeze(); + $mock->setName('Test'); $this->assertEquals("Test", $mock->getName()); }