Mock with multiple tests #91

Closed
sharkodlak opened this Issue Dec 15, 2010 · 1 comment

2 participants

@sharkodlak

I have a problem with mock which shall be used in multiple tests. In second test it returns NULL instead of value from callback.

<?php

class Program {
public function isPublic() {
return FALSE;
}

public function isRecordable() {
    return $this->isPublic();
}

}

class MockTest extends PHPUnit_Framework_TestCase
{
private $public = FALSE;

public function isPublic() {
    return $this->public;
}

public function testIsRecordable() {
    $mockProgram = $this->getMockBuilder('Program')
        ->setMockClassName("ProgramMock")
        ->setMethods(array('isPublic'))
        ->disableOriginalConstructor()
        ->getMock();
    $mockProgram
        ->expects($this->any())
        ->method('isPublic')
        ->will($this->returnCallback(array($this, "isPublic")));

    $actual = $mockProgram->isRecordable();
    $this->assertFalse($actual);

    $this->public = TRUE;
    $actual = $mockProgram->isRecordable();
    $this->assertTrue($actual);

    return $mockProgram;
}

/**
 * @depends testIsRecordable
 **/
public function testSetRecord($mockProgram) {
    $actual = $mockProgram->isRecordable();
    $this->assertTrue($actual);

    $this->public = FALSE;
    $actual = $mockProgram->isRecordable();
    $this->assertFalse($actual);
}

}

@sebastianbergmann

Please open tickets related to mock objects in the phpunit-mock-objects project.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment