holgerk/simple-mock

该包已被废弃且不再维护。作者建议使用mockery/mockery包。

v2.0.1 2013-10-29 17:52 UTC

This package is auto-updated.

Last update: 2024-03-31 20:33:39 UTC


README

轻松创建PHPUnit模拟。

test/ExampleTest.php

<?php

require_once __DIR__ . '/../src/SimpleMock/TestCase.php';

class ExampleTest extends SimpleMock_TestCase {
    // SimpleMock_TestCase extends PHPUnit_Framework_TestCase and adds the method: "simpleMock".
    // If you don't want to extend SimpleMock_TestCase then you can copy the: "simpleMock"
    // method to your own BaseClass or whatever.

    public function testBasicMockUsage() {
        // create a mock which expects that someMethod is called once
        $mock = $this->simpleMock('SomeClass')
            ->expects('someMethod')
            ->create();
        // The param: "SomeClass" corresponds to phpunit's first param on the getMock method.
        // The +create+ call returns the mock. This is should allways be the last call.

        // Same with pure PHPUnit:
        //   $mock = $this->getMock('SomeClass', array('someMethod'));
        //   $mock->expects($this->once())
        //        ->method('someMethod');

        // satisfy the expectations
        $mock->someMethod();
    }

    public function testSetupParameterExpectations() {
        $mock = $this->simpleMock('SomeClass')
            ->expects('someMethod')
            ->with(1, 2, 3)
            ->create();

        // Same with pure PHPUnit:
        //   $mock = $this->getMock('SomeClass', array('someMethod'));
        //   $mock->expects($this->once())
        //        ->method('someMethod')
        //        ->with($this->equalTo(1), $this->equalTo(2), $this->equalTo(3));

        // satisfy the expectations
        $mock->someMethod(1, 2, 3);
    }

    public function testSetupReturnValue() {
        $mock = $this->simpleMock('SomeClass')
            ->expects('someMethod')
            ->with(1, 2, 3)
            ->returns('one two three')
            ->create();

        // Same with pure PHPUnit:
        //   $mock = $this->getMock('SomeClass', array('someMethod'));
        //   $mock->expects($this->once())
        //        ->method('someMethod')
        //        ->with($this->equalTo(1), $this->equalTo(2), $this->equalTo(3))
        //        ->will($this->returnValue('one two three'));

        // satisfy the expectations
        $this->assertEquals('one two three', $mock->someMethod(1, 2, 3));
    }

    public function testConsecutiveCalls() {
        $mock = $this->simpleMock('SomeClass')
            ->expects('someMethod')
                ->with(1)->returns('one') // first call
                ->with(2)->returns('two') // second call
            ->expects('someOtherMethod')
                ->returns('three')        // third call
            ->create();

        // Same with pure PHPUnit:
        //   $mock = $this->getMock('SomeClass', array('someMethod', 'someOtherMethod'));
        //   $mock->expects($this->at(0))
        //        ->method('someMethod')
        //        ->with($this->equalTo(1))
        //        ->will($this->returnValue('one'));
        //   $mock->expects($this->at(1))
        //        ->method('someMethod')
        //        ->with($this->equalTo(2))
        //        ->will($this->returnValue('two'));
        //   $mock->expects($this->at(2))
        //        ->method('someOtherMethod')
        //        ->with($this->equalTo(3))
        //        ->will($this->returnValue('three'));

        // satisfy the expectations
        $this->assertEquals('one', $mock->someMethod(1));
        $this->assertEquals('two', $mock->someMethod(2));
        $this->assertEquals('three', $mock->someOtherMethod(3));
    }

    public function testExceptionExpectation() {
        $mock = $this->simpleMock('SomeClass')
            ->expects('someMethod')
            ->raises(new Exception('damned'))
            ->create();

        // Same with pure PHPUnit:
        //   $mock = $this->getMock('SomeClass', array('someMethod'));
        //   $mock->expects($this->once())
        //        ->method('someMethod')
        //        ->will($this->throwException(new Exception('damned')));

        // satisfy the expectations
        try {
            $mock->someMethod();
            $this->fail('has not thrown');
        } catch (Exception $e) {
            $this->assertEquals('damned', $e->getMessage());
        }
    }

    public function testUsingPHPUnitMatchers() {
        $mock = $this->simpleMock('SomeClass')
            ->expects('someMethod')
            ->with($this->stringContains('hello'))
            ->create();

        // Same with pure PHPUnit:
        //   $mock = $this->getMock('SomeClass', array('someMethod'));
        //   $mock->expects($this->once())
        //        ->method('someMethod')
        //        ->with($this->stringContains('hello'));

        // satisfy the expectations
        $mock->someMethod('foo hello bar');
    }
}

严格模式

class DemoClass {
    public function method1($param1) {}
    public function method2($param1) {}
}
$simpleMock = $this->simpleMock('DemoClass')
    ->strict()           // <- add this if you want to ensure that mocked methods exist
    ->expects('method2')
    ->expects('method3') // <- throws because method3 does not exist
    ->create();

完整模式

class DemoClass {
    public function method1($param1) {}
    public function method2($param1) {}
}
$simpleMock = $this->simpleMock('DemoClass')
    ->complete()        // <- add if you want to ensure that only mocked methods can be called
    ->expects('method2')->returns(42)
    ->create();
$this->assertEquals(42, $simpleMock->method2()); // <- works because this method is mocked
$simpleMock->method1(); // <- throws because this method is not explicitly mocked

待办事项

  • 支持存根

致谢

感谢phpunit成为一款出色的单元测试框架。感谢ruby mocha启发的mock-api。

许可

php-simple-mock在MIT许可下发布