Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mock freeze: only expected methods must by frozen #26

Open
wants to merge 1 commit into
base: master
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
10 changes: 5 additions & 5 deletions Mockista/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down
16 changes: 11 additions & 5 deletions tests/RegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down