mock_objects_test.php.svn-base

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· SVN-BASE 代码 · 共 995 行 · 第 1/3 页

SVN-BASE
995
字号
    function __toString() { }}Mock::generate('ClassWithSpecialMethods');class TestOfSpecialMethods extends UnitTestCase {    function skip() {        $this->skipIf(version_compare(phpversion(), '5', '<='), 'Overloading not tested unless PHP 5+');    }    function testCanMockTheThingAtAll() {        $mock = new MockClassWithSpecialMethods();    }    function testReturnFromSpecialAccessor() {        $mock = &new MockClassWithSpecialMethods();        $mock->setReturnValue('__get', '1st Return', array('first'));        $mock->setReturnValue('__get', '2nd Return', array('second'));        $this->assertEqual($mock->first, '1st Return');        $this->assertEqual($mock->second, '2nd Return');    }    function testcanExpectTheSettingOfValue() {        $mock = &new MockClassWithSpecialMethods();        $mock->expectOnce('__set', array('a', 'A'));        $mock->a = 'A';    }    function testCanSimulateAnOverloadmethod() {        $mock = &new MockClassWithSpecialMethods();        $mock->expectOnce('__call', array('amOverloaded', array('A')));        $mock->setReturnValue('__call', 'aaa');        $this->assertIdentical($mock->amOverloaded('A'), 'aaa');    }    function testCanEmulateIsset() {        $mock = &new MockClassWithSpecialMethods();        $mock->setReturnValue('__isset', true);        $this->assertIdentical(isset($mock->a), true);    }    function testCanExpectUnset() {        $mock = &new MockClassWithSpecialMethods();        $mock->expectOnce('__unset', array('a'));        unset($mock->a);    }    function testToStringMagic() {        $mock = &new MockClassWithSpecialMethods();        $mock->expectOnce('__toString');        $mock->setReturnValue('__toString', 'AAA');        ob_start();        print $mock;        $output = ob_get_contents();        ob_end_clean();        $this->assertEqual($output, 'AAA');    }}if (version_compare(phpversion(), '5', '>=')) {    $class  = 'class WithStaticMethod { ';    $class .= '    static function aStaticMethod() { } ';    $class .= '}';    eval($class);}Mock::generate('WithStaticMethod');class TestOfMockingClassesWithStaticMethods extends UnitTestCase {    function skip() {        $this->skipUnless(version_compare(phpversion(), '5', '>='), 'Static methods not tested unless PHP 5+');    }        function testStaticMethodIsMockedAsStatic() {        $mock = new WithStaticMethod();        $reflection = new ReflectionClass($mock);        $method = $reflection->getMethod('aStaticMethod');        $this->assertTrue($method->isStatic());    }}if (version_compare(phpversion(), '5', '>=')) {    class MockTestException extends Exception { }}class TestOfThrowingExceptionsFromMocks extends UnitTestCase {    function skip() {        $this->skipUnless(version_compare(phpversion(), '5', '>='), 'Exception throwing not tested unless PHP 5+');    }    function testCanThrowOnMethodCall() {        $mock = new MockDummy();        $mock->throwOn('aMethod');        $this->expectException();        $mock->aMethod();    }    function testCanThrowSpecificExceptionOnMethodCall() {        $mock = new MockDummy();        $mock->throwOn('aMethod', new MockTestException());        $this->expectException();        $mock->aMethod();    }        function testThrowsOnlyWhenCallSignatureMatches() {        $mock = new MockDummy();        $mock->throwOn('aMethod', new MockTestException(), array(3));        $mock->aMethod(1);        $mock->aMethod(2);        $this->expectException();        $mock->aMethod(3);    }        function testCanThrowOnParticularInvocation() {        $mock = new MockDummy();        $mock->throwAt(2, 'aMethod', new MockTestException());        $mock->aMethod();        $mock->aMethod();        $this->expectException();        $mock->aMethod();    }}class TestOfThrowingErrorsFromMocks extends UnitTestCase {        function testCanGenerateErrorFromMethodCall() {        $mock = new MockDummy();        $mock->errorOn('aMethod', 'Ouch!');        $this->expectError('Ouch!');        $mock->aMethod();    }        function testGeneratesErrorOnlyWhenCallSignatureMatches() {        $mock = new MockDummy();        $mock->errorOn('aMethod', 'Ouch!', array(3));        $mock->aMethod(1);        $mock->aMethod(2);        $this->expectError();        $mock->aMethod(3);    }        function testCanGenerateErrorOnParticularInvocation() {        $mock = new MockDummy();        $mock->errorAt(2, 'aMethod', 'Ouch!');        $mock->aMethod();        $mock->aMethod();        $this->expectError();        $mock->aMethod();    }}Mock::generatePartial('Dummy', 'TestDummy', array('anotherMethod'));class TestOfPartialMocks extends UnitTestCase {    function testMethodReplacementWithNoBehaviourReturnsNull() {        $mock = &new TestDummy();        $this->assertEqual($mock->aMethod(99), 99);        $this->assertNull($mock->anotherMethod());    }    function testSettingReturns() {        $mock = &new TestDummy();        $mock->setReturnValue('anotherMethod', 33, array(3));        $mock->setReturnValue('anotherMethod', 22);        $mock->setReturnValueAt(2, 'anotherMethod', 44, array(3));        $this->assertEqual($mock->anotherMethod(), 22);        $this->assertEqual($mock->anotherMethod(3), 33);        $this->assertEqual($mock->anotherMethod(3), 44);    }    function testReferences() {        $mock = &new TestDummy();        $object = new Dummy();        $mock->setReturnReferenceAt(0, 'anotherMethod', $object, array(3));        $this->assertReference($zref =& $mock->anotherMethod(3), $object);    }    function testExpectations() {        $mock = &new TestDummy();        $mock->expectCallCount('anotherMethod', 2);        $mock->expect('anotherMethod', array(77));        $mock->expectAt(1, 'anotherMethod', array(66));        $mock->anotherMethod(77);        $mock->anotherMethod(66);    }    function testSettingExpectationOnMissingMethodThrowsError() {        $mock = &new TestDummy();        $mock->expectCallCount('aMissingMethod', 2);        $this->assertError();    }}class ConstructorSuperClass {    function ConstructorSuperClass() { }}class ConstructorSubClass extends ConstructorSuperClass {}class TestOfPHP4StyleSuperClassConstruct extends UnitTestCase {    /*     * This addresses issue #1231401.  Without the fix in place, this will     * generate a fatal PHP error.     */    function testBasicConstruct() {        Mock::generate('ConstructorSubClass');        $mock = &new MockConstructorSubClass();        $this->assertIsA($mock, 'ConstructorSubClass');        $this->assertTrue(method_exists($mock, 'ConstructorSuperClass'));    }}class TestOfPHP5StaticMethodMocking extends UnitTestCase {    function skip() {        $this->skipIf(version_compare(phpversion(), '5', '<='), 'Static methods not tested unless PHP 5+');    }    function testCanCreateAMockObjectWithStaticMethodsWithoutError() {        eval('            class SimpleObjectContainingStaticMethod {                static function someStatic() { }            }        ');        Mock::generate('SimpleObjectContainingStaticMethod');        $this->assertNoErrors();    }}class TestOfPHP5AbstractMethodMocking extends UnitTestCase {    function skip() {        $this->skipIf(version_compare(phpversion(), '5', '<='), 'Abstract class/methods not tested unless PHP 5+');    }    function testCanCreateAMockObjectFromAnAbstractWithProperFunctionDeclarations() {        eval('             abstract class SimpleAbstractClassContainingAbstractMethods {                abstract function anAbstract();                abstract function anAbstractWithParameter($foo);                abstract function anAbstractWithMultipleParameters($foo, $bar);            }        ');        Mock::generate('SimpleAbstractClassContainingAbstractMethods');        $this->assertNoErrors();        $this->assertTrue(            method_exists(                'MockSimpleAbstractClassContainingAbstractMethods',                'anAbstract'            )        );        $this->assertTrue(            method_exists(                'MockSimpleAbstractClassContainingAbstractMethods',                'anAbstractWithParameter'            )        );        $this->assertTrue(            method_exists(                'MockSimpleAbstractClassContainingAbstractMethods',                'anAbstractWithMultipleParameters'            )        );    }    function testMethodsDefinedAsAbstractInParentShouldHaveFullSignature() {        eval('             abstract class SimpleParentAbstractClassContainingAbstractMethods {                abstract function anAbstract();                abstract function anAbstractWithParameter($foo);                abstract function anAbstractWithMultipleParameters($foo, $bar);            }             class SimpleChildAbstractClassContainingAbstractMethods extends SimpleParentAbstractClassContainingAbstractMethods {                function anAbstract(){}                function anAbstractWithParameter($foo){}                function anAbstractWithMultipleParameters($foo, $bar){}            }            class EvenDeeperEmptyChildClass extends SimpleChildAbstractClassContainingAbstractMethods {}        ');        Mock::generate('SimpleChildAbstractClassContainingAbstractMethods');        $this->assertNoErrors();        $this->assertTrue(            method_exists(                'MockSimpleChildAbstractClassContainingAbstractMethods',                'anAbstract'            )        );        $this->assertTrue(            method_exists(                'MockSimpleChildAbstractClassContainingAbstractMethods',                'anAbstractWithParameter'            )        );        $this->assertTrue(            method_exists(                'MockSimpleChildAbstractClassContainingAbstractMethods',                'anAbstractWithMultipleParameters'            )        );                Mock::generate('EvenDeeperEmptyChildClass');        $this->assertNoErrors();        $this->assertTrue(            method_exists(                'MockEvenDeeperEmptyChildClass',                'anAbstract'            )        );        $this->assertTrue(            method_exists(                'MockEvenDeeperEmptyChildClass',                'anAbstractWithParameter'            )        );        $this->assertTrue(            method_exists(                'MockEvenDeeperEmptyChildClass',                'anAbstractWithMultipleParameters'            )        );    }}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?