Is it possible to create a fake outside of the test case in PhpUnit?

This may sound silly, hope not, but I want to create a service that will return layouts for people who use my project so that they can mock all classes from my project and check their code.

My idea was to offer such a service so that it could be called inside other test examples of the project and get the appropriate layout for each test.

Is it possible? Or there are other ways to do this. By the way, I cannot use any mocking library due to project limitations.

+4
source share
2 answers

, . getMock PHPUnit_Framework_MockObject_Generator. :

PHPUnit_Framework_MockObject_Generator::getMock($originalClassName, $methods)

, $this->once(). :

$mock->expects(\PHPUnit_Framework_TestCase::once())

PHPUnit, ,

+7

TestCase, , .

mock builder PHPUnit ( ), testcase mock-, :

class MockBuilderService
{
    private $test;

    public function __construct(PHPUnit_Framework_TestCase $test)
    {
        $this->test = $test;
    }

    public function buildVeryComplexMock()
    {
        $mock = $this->test->getMock('MyClass');
        $mock->expects($this->test->once())
             ->method('foo')
             ->willReturn(1);      
        return $mock;      
    }
}

, :

class ATest extends PHPUnit_Framework_TestCase
{
    public function testFoo()
    {
        $mock_builder = new MockBuilderService($this);
        $complex_mock = $mock_builder->buildVeryComplexMock($mock_configuration);

       // the mock expectations will be checked as usual
    }

}
+2

All Articles