mock_objects_test.php.svn-base
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· SVN-BASE 代码 · 共 995 行 · 第 1/3 页
SVN-BASE
995 行
<?php// $Id: mock_objects_test.php 1700 2008-03-24 16:17:48Z lastcraft $require_once(dirname(__FILE__) . '/../autorun.php');require_once(dirname(__FILE__) . '/../expectation.php');require_once(dirname(__FILE__) . '/../mock_objects.php');class TestOfAnythingExpectation extends UnitTestCase { function testSimpleInteger() { $expectation = new AnythingExpectation(); $this->assertTrue($expectation->test(33)); $this->assertTrue($expectation->test(false)); $this->assertTrue($expectation->test(null)); }}class TestOfParametersExpectation extends UnitTestCase { function testEmptyMatch() { $expectation = new ParametersExpectation(array()); $this->assertTrue($expectation->test(array())); $this->assertFalse($expectation->test(array(33))); } function testSingleMatch() { $expectation = new ParametersExpectation(array(0)); $this->assertFalse($expectation->test(array(1))); $this->assertTrue($expectation->test(array(0))); } function testAnyMatch() { $expectation = new ParametersExpectation(false); $this->assertTrue($expectation->test(array())); $this->assertTrue($expectation->test(array(1, 2))); } function testMissingParameter() { $expectation = new ParametersExpectation(array(0)); $this->assertFalse($expectation->test(array())); } function testNullParameter() { $expectation = new ParametersExpectation(array(null)); $this->assertTrue($expectation->test(array(null))); $this->assertFalse($expectation->test(array())); } function testAnythingExpectations() { $expectation = new ParametersExpectation(array(new AnythingExpectation())); $this->assertFalse($expectation->test(array())); $this->assertIdentical($expectation->test(array(null)), true); $this->assertIdentical($expectation->test(array(13)), true); } function testOtherExpectations() { $expectation = new ParametersExpectation( array(new PatternExpectation('/hello/i'))); $this->assertFalse($expectation->test(array('Goodbye'))); $this->assertTrue($expectation->test(array('hello'))); $this->assertTrue($expectation->test(array('Hello'))); } function testIdentityOnly() { $expectation = new ParametersExpectation(array("0")); $this->assertFalse($expectation->test(array(0))); $this->assertTrue($expectation->test(array("0"))); } function testLongList() { $expectation = new ParametersExpectation( array("0", 0, new AnythingExpectation(), false)); $this->assertTrue($expectation->test(array("0", 0, 37, false))); $this->assertFalse($expectation->test(array("0", 0, 37, true))); $this->assertFalse($expectation->test(array("0", 0, 37))); }}class TestOfSimpleSignatureMap extends UnitTestCase { function testEmpty() { $map = new SimpleSignatureMap(); $this->assertFalse($map->isMatch("any", array())); $this->assertNull($map->findFirstAction("any", array())); } function testExactReference() { $map = new SimpleSignatureMap(); $ref = "Fred"; $map->add(array(0), $ref); $this->assertEqual($map->findFirstAction(array(0)), "Fred"); $ref2 = &$map->findFirstAction(array(0)); $this->assertReference($ref2, $ref); } function testDifferentCallSignaturesCanHaveDifferentReferences() { $map = new SimpleSignatureMap(); $fred = 'Fred'; $jim = 'jim'; $map->add(array(0), $fred); $map->add(array('0'), $jim); $this->assertReference($fred, $map->findFirstAction(array(0))); $this->assertReference($jim, $map->findFirstAction(array('0'))); } function testWildcard() { $fred = 'Fred'; $map = new SimpleSignatureMap(); $map->add(array(new AnythingExpectation(), 1, 3), $fred); $this->assertTrue($map->isMatch(array(2, 1, 3))); $this->assertReference($map->findFirstAction(array(2, 1, 3)), $fred); } function testAllWildcard() { $fred = 'Fred'; $map = new SimpleSignatureMap(); $this->assertFalse($map->isMatch(array(2, 1, 3))); $map->add('', $fred); $this->assertTrue($map->isMatch(array(2, 1, 3))); $this->assertReference($map->findFirstAction(array(2, 1, 3)), $fred); } function testOrdering() { $map = new SimpleSignatureMap(); $map->add(array(1, 2), new SimpleByValue("1, 2")); $map->add(array(1, 3), new SimpleByValue("1, 3")); $map->add(array(1), new SimpleByValue("1")); $map->add(array(1, 4), new SimpleByValue("1, 4")); $map->add(array(new AnythingExpectation()), new SimpleByValue("Any")); $map->add(array(2), new SimpleByValue("2")); $map->add("", new SimpleByValue("Default")); $map->add(array(), new SimpleByValue("None")); $this->assertEqual($map->findFirstAction(array(1, 2)), new SimpleByValue("1, 2")); $this->assertEqual($map->findFirstAction(array(1, 3)), new SimpleByValue("1, 3")); $this->assertEqual($map->findFirstAction(array(1, 4)), new SimpleByValue("1, 4")); $this->assertEqual($map->findFirstAction(array(1)), new SimpleByValue("1")); $this->assertEqual($map->findFirstAction(array(2)), new SimpleByValue("Any")); $this->assertEqual($map->findFirstAction(array(3)), new SimpleByValue("Any")); $this->assertEqual($map->findFirstAction(array()), new SimpleByValue("Default")); }}class TestOfCallSchedule extends UnitTestCase { function testCanBeSetToAlwaysReturnTheSameReference() { $a = 5; $schedule = &new SimpleCallSchedule(); $schedule->register('aMethod', false, new SimpleByReference($a)); $this->assertReference($schedule->respond(0, 'aMethod', array()), $a); $this->assertReference($schedule->respond(1, 'aMethod', array()), $a); } function testSpecificSignaturesOverrideTheAlwaysCase() { $any = 'any'; $one = 'two'; $schedule = &new SimpleCallSchedule(); $schedule->register('aMethod', array(1), new SimpleByReference($one)); $schedule->register('aMethod', false, new SimpleByReference($any)); $this->assertReference($schedule->respond(0, 'aMethod', array(2)), $any); $this->assertReference($schedule->respond(0, 'aMethod', array(1)), $one); } function testReturnsCanBeSetOverTime() { $one = 'one'; $two = 'two'; $schedule = &new SimpleCallSchedule(); $schedule->registerAt(0, 'aMethod', false, new SimpleByReference($one)); $schedule->registerAt(1, 'aMethod', false, new SimpleByReference($two)); $this->assertReference($schedule->respond(0, 'aMethod', array()), $one); $this->assertReference($schedule->respond(1, 'aMethod', array()), $two); } function testReturnsOverTimecanBeAlteredByTheArguments() { $one = '1'; $two = '2'; $two_a = '2a'; $schedule = &new SimpleCallSchedule(); $schedule->registerAt(0, 'aMethod', false, new SimpleByReference($one)); $schedule->registerAt(1, 'aMethod', array('a'), new SimpleByReference($two_a)); $schedule->registerAt(1, 'aMethod', false, new SimpleByReference($two)); $this->assertReference($schedule->respond(0, 'aMethod', array()), $one); $this->assertReference($schedule->respond(1, 'aMethod', array()), $two); $this->assertReference($schedule->respond(1, 'aMethod', array('a')), $two_a); } function testCanReturnByValue() { $a = 5; $schedule = &new SimpleCallSchedule(); $schedule->register('aMethod', false, new SimpleByValue($a)); $this->assertClone($schedule->respond(0, 'aMethod', array()), $a); } function testCanThrowException() { if (version_compare(phpversion(), '5', '>=')) { $schedule = &new SimpleCallSchedule(); $schedule->register('aMethod', false, new SimpleThrower(new Exception('Ouch'))); $this->expectException(new Exception('Ouch')); $schedule->respond(0, 'aMethod', array()); } } function testCanEmitError() { $schedule = &new SimpleCallSchedule(); $schedule->register('aMethod', false, new SimpleErrorThrower('Ouch', E_USER_WARNING)); $this->expectError('Ouch'); $schedule->respond(0, 'aMethod', array()); }}class Dummy { function Dummy() { } function aMethod() { return true; } function anotherMethod() { return true; }}Mock::generate('Dummy');Mock::generate('Dummy', 'AnotherMockDummy');Mock::generate('Dummy', 'MockDummyWithExtraMethods', array('extraMethod'));class TestOfMockGeneration extends UnitTestCase { function testCloning() { $mock = &new MockDummy(); $this->assertTrue(method_exists($mock, "aMethod")); $this->assertNull($mock->aMethod()); } function testCloningWithExtraMethod() { $mock = &new MockDummyWithExtraMethods(); $this->assertTrue(method_exists($mock, "extraMethod")); } function testCloningWithChosenClassName() { $mock = &new AnotherMockDummy(); $this->assertTrue(method_exists($mock, "aMethod")); }}class TestOfMockReturns extends UnitTestCase { function testDefaultReturn() { $mock = &new MockDummy(); $mock->setReturnValue("aMethod", "aaa"); $this->assertIdentical($mock->aMethod(), "aaa"); $this->assertIdentical($mock->aMethod(), "aaa"); } function testParameteredReturn() { $mock = &new MockDummy(); $mock->setReturnValue('aMethod', 'aaa', array(1, 2, 3)); $this->assertNull($mock->aMethod()); $this->assertIdentical($mock->aMethod(1, 2, 3), 'aaa'); } function testReferenceReturned() { $mock = &new MockDummy(); $object = new Dummy(); $mock->setReturnReference('aMethod', $object, array(1, 2, 3)); $this->assertReference($zref = &$mock->aMethod(1, 2, 3), $object); } function testPatternMatchReturn() { $mock = &new MockDummy(); $mock->setReturnValue( "aMethod", "aaa", array(new PatternExpectation('/hello/i'))); $this->assertIdentical($mock->aMethod('Hello'), "aaa"); $this->assertNull($mock->aMethod('Goodbye')); } function testMultipleMethods() { $mock = &new MockDummy(); $mock->setReturnValue("aMethod", 100, array(1)); $mock->setReturnValue("aMethod", 200, array(2)); $mock->setReturnValue("anotherMethod", 10, array(1)); $mock->setReturnValue("anotherMethod", 20, array(2)); $this->assertIdentical($mock->aMethod(1), 100); $this->assertIdentical($mock->anotherMethod(1), 10); $this->assertIdentical($mock->aMethod(2), 200); $this->assertIdentical($mock->anotherMethod(2), 20); } function testReturnSequence() { $mock = &new MockDummy(); $mock->setReturnValueAt(0, "aMethod", "aaa"); $mock->setReturnValueAt(1, "aMethod", "bbb"); $mock->setReturnValueAt(3, "aMethod", "ddd"); $this->assertIdentical($mock->aMethod(), "aaa"); $this->assertIdentical($mock->aMethod(), "bbb"); $this->assertNull($mock->aMethod()); $this->assertIdentical($mock->aMethod(), "ddd"); } function testReturnReferenceSequence() { $mock = &new MockDummy(); $object = new Dummy(); $mock->setReturnReferenceAt(1, "aMethod", $object); $this->assertNull($mock->aMethod()); $this->assertReference($zref =& $mock->aMethod(), $object); $this->assertNull($mock->aMethod()); } function testComplicatedReturnSequence() { $mock = &new MockDummy(); $object = new Dummy(); $mock->setReturnValueAt(1, "aMethod", "aaa", array("a")); $mock->setReturnValueAt(1, "aMethod", "bbb"); $mock->setReturnReferenceAt(2, "aMethod", $object, array('*', 2)); $mock->setReturnValueAt(2, "aMethod", "value", array('*', 3)); $mock->setReturnValue("aMethod", 3, array(3)); $this->assertNull($mock->aMethod()); $this->assertEqual($mock->aMethod("a"), "aaa"); $this->assertReference($zref =& $mock->aMethod(1, 2), $object); $this->assertEqual($mock->aMethod(3), 3); $this->assertNull($mock->aMethod()); } function testMultipleMethodSequences() { $mock = &new MockDummy(); $mock->setReturnValueAt(0, "aMethod", "aaa"); $mock->setReturnValueAt(1, "aMethod", "bbb"); $mock->setReturnValueAt(0, "anotherMethod", "ccc"); $mock->setReturnValueAt(1, "anotherMethod", "ddd"); $this->assertIdentical($mock->aMethod(), "aaa"); $this->assertIdentical($mock->anotherMethod(), "ccc"); $this->assertIdentical($mock->aMethod(), "bbb"); $this->assertIdentical($mock->anotherMethod(), "ddd");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?