PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/simpletest/test/mock_objects_test.php

https://bitbucket.org/lipki/interlimo
PHP | 657 lines | 556 code | 96 blank | 5 comment | 1 complexity | 14ade8ace67a8af9316b7801a1db9112 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. // $Id: mock_objects_test.php,v 1.16 2006/02/05 19:35:31 lastcraft Exp $
  3. require_once(dirname(__FILE__) . '/../expectation.php');
  4. class TestOfAnythingExpectation extends UnitTestCase {
  5. function testSimpleInteger() {
  6. $expectation = new AnythingExpectation();
  7. $this->assertTrue($expectation->test(33));
  8. $this->assertPattern(
  9. '/matches.*33/i',
  10. $expectation->testMessage(33));
  11. }
  12. }
  13. class TestOfParametersExpectation extends UnitTestCase {
  14. function testEmptyMatch() {
  15. $expectation = new ParametersExpectation(array());
  16. $this->assertTrue($expectation->test(array()));
  17. $this->assertFalse($expectation->test(array(33)));
  18. }
  19. function testSingleMatch() {
  20. $expectation = new ParametersExpectation(array(0));
  21. $this->assertFalse($expectation->test(array(1)));
  22. $this->assertTrue($expectation->test(array(0)));
  23. }
  24. function testAnyMatch() {
  25. $expectation = new ParametersExpectation(false);
  26. $this->assertTrue($expectation->test(array()));
  27. $this->assertTrue($expectation->test(array(1, 2)));
  28. }
  29. function testMissingParameter() {
  30. $expectation = new ParametersExpectation(array(0));
  31. $this->assertFalse($expectation->test(array()));
  32. }
  33. function testNullParameter() {
  34. $expectation = new ParametersExpectation(array(null));
  35. $this->assertTrue($expectation->test(array(null)));
  36. $this->assertFalse($expectation->test(array()));
  37. }
  38. function testAnythingExpectations() {
  39. $expectation = new ParametersExpectation(array(new AnythingExpectation()));
  40. $this->assertFalse($expectation->test(array()));
  41. $this->assertIdentical($expectation->test(array(null)), true);
  42. $this->assertIdentical($expectation->test(array(13)), true);
  43. }
  44. function testOtherExpectations() {
  45. $expectation = new ParametersExpectation(
  46. array(new PatternExpectation('/hello/i')));
  47. $this->assertFalse($expectation->test(array('Goodbye')));
  48. $this->assertTrue($expectation->test(array('hello')));
  49. $this->assertTrue($expectation->test(array('Hello')));
  50. }
  51. function testIdentityOnly() {
  52. $expectation = new ParametersExpectation(array("0"));
  53. $this->assertFalse($expectation->test(array(0)));
  54. $this->assertTrue($expectation->test(array("0")));
  55. }
  56. function testLongList() {
  57. $expectation = new ParametersExpectation(
  58. array("0", 0, new AnythingExpectation(), false));
  59. $this->assertTrue($expectation->test(array("0", 0, 37, false)));
  60. $this->assertFalse($expectation->test(array("0", 0, 37, true)));
  61. $this->assertFalse($expectation->test(array("0", 0, 37)));
  62. }
  63. }
  64. class TestOfCallMap extends UnitTestCase {
  65. function testEmpty() {
  66. $map = new CallMap();
  67. $this->assertFalse($map->isMatch("any", array()));
  68. $this->assertNull($map->findFirstMatch("any", array()));
  69. }
  70. function testExactValue() {
  71. $map = new CallMap();
  72. $map->addValue(array(0), "Fred");
  73. $map->addValue(array(1), "Jim");
  74. $map->addValue(array("1"), "Tom");
  75. $this->assertTrue($map->isMatch(array(0)));
  76. $this->assertEqual($map->findFirstMatch(array(0)), "Fred");
  77. $this->assertTrue($map->isMatch(array(1)));
  78. $this->assertEqual($map->findFirstMatch(array(1)), "Jim");
  79. $this->assertEqual($map->findFirstMatch(array("1")), "Tom");
  80. }
  81. function testExactReference() {
  82. $map = new CallMap();
  83. $ref = "Fred";
  84. $map->addReference(array(0), $ref);
  85. $this->assertEqual($map->findFirstMatch(array(0)), "Fred");
  86. $ref2 = &$map->findFirstMatch(array(0));
  87. $this->assertReference($ref2, $ref);
  88. }
  89. function testWildcard() {
  90. $map = new CallMap();
  91. $map->addValue(array(new AnythingExpectation(), 1, 3), "Fred");
  92. $this->assertTrue($map->isMatch(array(2, 1, 3)));
  93. $this->assertEqual($map->findFirstMatch(array(2, 1, 3)), "Fred");
  94. }
  95. function testAllWildcard() {
  96. $map = new CallMap();
  97. $this->assertFalse($map->isMatch(array(2, 1, 3)));
  98. $map->addValue("", "Fred");
  99. $this->assertTrue($map->isMatch(array(2, 1, 3)));
  100. $this->assertEqual($map->findFirstMatch(array(2, 1, 3)), "Fred");
  101. }
  102. function testOrdering() {
  103. $map = new CallMap();
  104. $map->addValue(array(1, 2), "1, 2");
  105. $map->addValue(array(1, 3), "1, 3");
  106. $map->addValue(array(1), "1");
  107. $map->addValue(array(1, 4), "1, 4");
  108. $map->addValue(array(new AnythingExpectation()), "Any");
  109. $map->addValue(array(2), "2");
  110. $map->addValue("", "Default");
  111. $map->addValue(array(), "None");
  112. $this->assertEqual($map->findFirstMatch(array(1, 2)), "1, 2");
  113. $this->assertEqual($map->findFirstMatch(array(1, 3)), "1, 3");
  114. $this->assertEqual($map->findFirstMatch(array(1, 4)), "1, 4");
  115. $this->assertEqual($map->findFirstMatch(array(1)), "1");
  116. $this->assertEqual($map->findFirstMatch(array(2)), "Any");
  117. $this->assertEqual($map->findFirstMatch(array(3)), "Any");
  118. $this->assertEqual($map->findFirstMatch(array()), "Default");
  119. }
  120. }
  121. class Dummy {
  122. function Dummy() {
  123. }
  124. function aMethod() {
  125. return true;
  126. }
  127. function anotherMethod() {
  128. return true;
  129. }
  130. function __get($key) {
  131. return $key;
  132. }
  133. }
  134. Stub::generate('Dummy', 'StubDummy');
  135. Stub::generate('Dummy', 'AnotherStubDummy');
  136. Stub::generate('Dummy', 'StubDummyWithExtraMethods', array('extraMethod'));
  137. class SpecialSimpleStub extends SimpleMock {
  138. function SpecialSimpleStub() {
  139. $this->SimpleMock();
  140. }
  141. }
  142. SimpleTest::setMockBaseClass('SpecialSimpleStub');
  143. Stub::generate('Dummy', 'SpecialStubDummy');
  144. SimpleTest::setMockBaseClass('SimpleMock');
  145. class TestOfStubGeneration extends UnitTestCase {
  146. function testCloning() {
  147. $stub = &new StubDummy();
  148. $this->assertTrue(method_exists($stub, "aMethod"));
  149. $this->assertNull($stub->aMethod(null));
  150. }
  151. function testCloningWithExtraMethod() {
  152. $stub = &new StubDummyWithExtraMethods();
  153. $this->assertTrue(method_exists($stub, "extraMethod"));
  154. }
  155. function testCloningWithChosenClassName() {
  156. $stub = &new AnotherStubDummy();
  157. $this->assertTrue(method_exists($stub, "aMethod"));
  158. }
  159. function testCloningWithDifferentBaseClass() {
  160. $stub = &new SpecialStubDummy();
  161. $this->assertIsA($stub, "SpecialSimpleStub");
  162. $this->assertTrue(method_exists($stub, "aMethod"));
  163. }
  164. }
  165. class TestOfServerStubReturns extends UnitTestCase {
  166. function testDefaultReturn() {
  167. $stub = &new StubDummy();
  168. $stub->setReturnValue("aMethod", "aaa");
  169. $this->assertIdentical($stub->aMethod(), "aaa");
  170. $this->assertIdentical($stub->aMethod(), "aaa");
  171. }
  172. function testParameteredReturn() {
  173. $stub = &new StubDummy();
  174. $stub->setReturnValue("aMethod", "aaa", array(1, 2, 3));
  175. $this->assertNull($stub->aMethod());
  176. $this->assertIdentical($stub->aMethod(1, 2, 3), "aaa");
  177. }
  178. function testReferenceReturned() {
  179. $stub = &new StubDummy();
  180. $object = new Dummy();
  181. $stub->setReturnReference("aMethod", $object, array(1, 2, 3));
  182. $this->assertReference($zref =& $stub->aMethod(1, 2, 3), $object);
  183. }
  184. function testCallCount() {
  185. $stub = &new StubDummy();
  186. $this->assertEqual($stub->getCallCount("aMethod"), 0);
  187. $stub->aMethod();
  188. $this->assertEqual($stub->getCallCount("aMethod"), 1);
  189. $stub->aMethod();
  190. $this->assertEqual($stub->getCallCount("aMethod"), 2);
  191. }
  192. function testMultipleMethods() {
  193. $stub = &new StubDummy();
  194. $stub->setReturnValue("aMethod", 100, array(1));
  195. $stub->setReturnValue("aMethod", 200, array(2));
  196. $stub->setReturnValue("anotherMethod", 10, array(1));
  197. $stub->setReturnValue("anotherMethod", 20, array(2));
  198. $this->assertIdentical($stub->aMethod(1), 100);
  199. $this->assertIdentical($stub->anotherMethod(1), 10);
  200. $this->assertIdentical($stub->aMethod(2), 200);
  201. $this->assertIdentical($stub->anotherMethod(2), 20);
  202. }
  203. function testReturnSequence() {
  204. $stub = &new StubDummy();
  205. $stub->setReturnValueAt(0, "aMethod", "aaa");
  206. $stub->setReturnValueAt(1, "aMethod", "bbb");
  207. $stub->setReturnValueAt(3, "aMethod", "ddd");
  208. $this->assertIdentical($stub->aMethod(), "aaa");
  209. $this->assertIdentical($stub->aMethod(), "bbb");
  210. $this->assertNull($stub->aMethod());
  211. $this->assertIdentical($stub->aMethod(), "ddd");
  212. }
  213. function testReturnReferenceSequence() {
  214. $stub = &new StubDummy();
  215. $object = new Dummy();
  216. $stub->setReturnReferenceAt(1, "aMethod", $object);
  217. $this->assertNull($stub->aMethod());
  218. $this->assertReference($zref =& $stub->aMethod(), $object);
  219. $this->assertNull($stub->aMethod());
  220. }
  221. function testComplicatedReturnSequence() {
  222. $stub = &new StubDummy();
  223. $object = new Dummy();
  224. $stub->setReturnValueAt(1, "aMethod", "aaa", array("a"));
  225. $stub->setReturnValueAt(1, "aMethod", "bbb");
  226. $stub->setReturnReferenceAt(2, "aMethod", $object, array('*', 2));
  227. $stub->setReturnValueAt(2, "aMethod", "value", array('*', 3));
  228. $stub->setReturnValue("aMethod", 3, array(3));
  229. $this->assertNull($stub->aMethod());
  230. $this->assertEqual($stub->aMethod("a"), "aaa");
  231. $this->assertReference($zref =& $stub->aMethod(1, 2), $object);
  232. $this->assertEqual($stub->aMethod(3), 3);
  233. $this->assertNull($stub->aMethod());
  234. }
  235. function testMultipleMethodSequences() {
  236. $stub = &new StubDummy();
  237. $stub->setReturnValueAt(0, "aMethod", "aaa");
  238. $stub->setReturnValueAt(1, "aMethod", "bbb");
  239. $stub->setReturnValueAt(0, "anotherMethod", "ccc");
  240. $stub->setReturnValueAt(1, "anotherMethod", "ddd");
  241. $this->assertIdentical($stub->aMethod(), "aaa");
  242. $this->assertIdentical($stub->anotherMethod(), "ccc");
  243. $this->assertIdentical($stub->aMethod(), "bbb");
  244. $this->assertIdentical($stub->anotherMethod(), "ddd");
  245. }
  246. function testSequenceFallback() {
  247. $stub = &new StubDummy();
  248. $stub->setReturnValueAt(0, "aMethod", "aaa", array('a'));
  249. $stub->setReturnValueAt(1, "aMethod", "bbb", array('a'));
  250. $stub->setReturnValue("aMethod", "AAA");
  251. $this->assertIdentical($stub->aMethod('a'), "aaa");
  252. $this->assertIdentical($stub->aMethod('b'), "AAA");
  253. }
  254. function testMethodInterference() {
  255. $stub = &new StubDummy();
  256. $stub->setReturnValueAt(0, "anotherMethod", "aaa");
  257. $stub->setReturnValue("aMethod", "AAA");
  258. $this->assertIdentical($stub->aMethod(), "AAA");
  259. $this->assertIdentical($stub->anotherMethod(), "aaa");
  260. }
  261. }
  262. Mock::generate('Dummy');
  263. Mock::generate('Dummy', 'AnotherMockDummy');
  264. Mock::generate('Dummy', 'MockDummyWithExtraMethods', array('extraMethod'));
  265. class SpecialSimpleMock extends SimpleMock { }
  266. SimpleTest::setMockBaseClass("SpecialSimpleMock");
  267. Mock::generate("Dummy", "SpecialMockDummy");
  268. SimpleTest::setMockBaseClass("SimpleMock");
  269. class TestOfMockGeneration extends UnitTestCase {
  270. function testCloning() {
  271. $mock = &new MockDummy();
  272. $this->assertTrue(method_exists($mock, "aMethod"));
  273. $this->assertNull($mock->aMethod());
  274. }
  275. function testCloningWithExtraMethod() {
  276. $mock = &new MockDummyWithExtraMethods();
  277. $this->assertTrue(method_exists($mock, "extraMethod"));
  278. }
  279. function testCloningWithChosenClassName() {
  280. $mock = &new AnotherMockDummy();
  281. $this->assertTrue(method_exists($mock, "aMethod"));
  282. }
  283. function testCloningWithDifferentBaseClass() {
  284. $mock = &new SpecialMockDummy();
  285. $this->assertIsA($mock, "SpecialSimpleMock");
  286. $this->assertTrue(method_exists($mock, "aMethod"));
  287. }
  288. }
  289. class TestOfMockReturns extends UnitTestCase {
  290. function testParameteredReturn() {
  291. $mock = &new MockDummy();
  292. $mock->setReturnValue('aMethod', 'aaa', array(1, 2, 3));
  293. $this->assertNull($mock->aMethod());
  294. $this->assertIdentical($mock->aMethod(1, 2, 3), 'aaa');
  295. }
  296. function testReferenceReturned() {
  297. $mock = &new MockDummy();
  298. $object = new Dummy();
  299. $mock->setReturnReference("aMethod", $object, array(1, 2, 3));
  300. $this->assertReference($zref =& $mock->aMethod(1, 2, 3), $object);
  301. }
  302. function testWildcardReturn() {
  303. $mock = &new MockDummy();
  304. $mock->setWildcard('wild');
  305. $mock->setReturnValue('aMethod', 'a', array(1, 'wild', 3));
  306. $this->assertIdentical($mock->aMethod(1, 'something', 3), 'a');
  307. $this->assertIdentical($mock->aMethod(1, 'anything', 3), 'a');
  308. }
  309. function testPatternMatchReturn() {
  310. $mock = &new MockDummy();
  311. $mock->setReturnValue(
  312. "aMethod",
  313. "aaa",
  314. array(new PatternExpectation('/hello/i')));
  315. $this->assertIdentical($mock->aMethod('Hello'), "aaa");
  316. $this->assertNull($mock->aMethod('Goodbye'));
  317. }
  318. function testCallCount() {
  319. $mock = &new MockDummy();
  320. $this->assertEqual($mock->getCallCount("aMethod"), 0);
  321. $mock->aMethod();
  322. $this->assertEqual($mock->getCallCount("aMethod"), 1);
  323. $mock->aMethod();
  324. $this->assertEqual($mock->getCallCount("aMethod"), 2);
  325. }
  326. function testReturnReferenceSequence() {
  327. $mock = &new MockDummy();
  328. $object = new Dummy();
  329. $mock->setReturnReferenceAt(1, "aMethod", $object);
  330. $this->assertNull($mock->aMethod());
  331. $this->assertReference($zref =& $mock->aMethod(), $object);
  332. $this->assertNull($mock->aMethod());
  333. $this->swallowErrors();
  334. }
  335. }
  336. class TestOfSpecialMethods extends UnitTestCase {
  337. function testReturnFromSpecialMethod() {
  338. $mock = &new MockDummy();
  339. $mock->setReturnValue('__get', '1st Return', array('first'));
  340. $mock->setReturnValue('__get', '2nd Return', array('second'));
  341. $this->assertEqual($mock->__get('first'), '1st Return');
  342. $this->assertEqual($mock->__get('second'), '2nd Return');
  343. if (phpversion() >= 5) {
  344. $this->assertEqual($mock->first, $mock->__get('first'));
  345. $this->assertEqual($mock->second, $mock->__get('second'));
  346. }
  347. }
  348. }
  349. class TestOfMockTally extends UnitTestCase {
  350. function testZeroCallCount() {
  351. $mock = &new MockDummy();
  352. $mock->expectCallCount("aMethod", 0);
  353. }
  354. function testExpectedCallCount() {
  355. $mock = &new MockDummy();
  356. $mock->expectCallCount("aMethod", 2);
  357. $mock->aMethod();
  358. $mock->aMethod();
  359. }
  360. }
  361. class MockDummyWithInjectedTestCase extends MockDummy {
  362. function &_getCurrentTestCase() {
  363. $test = &SimpleTest::getCurrent();
  364. return $test->test;
  365. }
  366. }
  367. Mock::generate("SimpleTestCase");
  368. class TestOfMockExpectations extends UnitTestCase {
  369. var $_test;
  370. function setUp() {
  371. $this->test = &new MockSimpleTestCase();
  372. }
  373. function testSettingExpectationOnNonMethodThrowsError() {
  374. $mock = &new MockDummyWithInjectedTestCase();
  375. $mock->expectMaximumCallCount("aMissingMethod", 2);
  376. $this->assertError();
  377. }
  378. function testMaxCallsDetectsOverrun() {
  379. $this->test->expectOnce("assertTrue", array(false, '*'));
  380. $mock = &new MockDummyWithInjectedTestCase();
  381. $mock->expectMaximumCallCount("aMethod", 2);
  382. $mock->aMethod();
  383. $mock->aMethod();
  384. $mock->aMethod();
  385. $mock->atTestEnd('testSomething');
  386. }
  387. function testTallyOnMaxCallsSendsPassOnUnderrun() {
  388. $this->test->expectOnce("assertTrue", array(true, '*'));
  389. $mock = &new MockDummyWithInjectedTestCase();
  390. $mock->expectMaximumCallCount("aMethod", 2);
  391. $mock->aMethod();
  392. $mock->aMethod();
  393. $mock->atTestEnd('testSomething');
  394. }
  395. function testExpectNeverDetectsOverrun() {
  396. $this->test->expectOnce("assertTrue", array(false, '*'));
  397. $mock = &new MockDummyWithInjectedTestCase();
  398. $mock->expectNever("aMethod");
  399. $mock->aMethod();
  400. $mock->atTestEnd('testSomething');
  401. }
  402. function testTallyOnExpectNeverSendsPassOnUnderrun() {
  403. $this->test->expectOnce("assertTrue", array(true, '*'));
  404. $mock = &new MockDummyWithInjectedTestCase();
  405. $mock->expectNever("aMethod");
  406. $mock->atTestEnd('testSomething');
  407. }
  408. function testMinCalls() {
  409. $this->test->expectOnce("assertTrue", array(true, '*'));
  410. $mock = &new MockDummyWithInjectedTestCase();
  411. $mock->expectMinimumCallCount("aMethod", 2);
  412. $mock->aMethod();
  413. $mock->aMethod();
  414. $mock->atTestEnd('testSomething');
  415. }
  416. function testFailedNever() {
  417. $this->test->expectOnce("assertTrue", array(false, '*'));
  418. $mock = &new MockDummyWithInjectedTestCase();
  419. $mock->expectNever("aMethod");
  420. $mock->aMethod();
  421. $mock->atTestEnd('testSomething');
  422. }
  423. function testUnderOnce() {
  424. $this->test->expectOnce("assertTrue", array(false, '*'));
  425. $mock = &new MockDummyWithInjectedTestCase();
  426. $mock->expectOnce("aMethod");
  427. $mock->atTestEnd('testSomething');
  428. }
  429. function testOverOnce() {
  430. $this->test->expectOnce("assertTrue", array(false, '*'));
  431. $mock = &new MockDummyWithInjectedTestCase();
  432. $mock->expectOnce("aMethod");
  433. $mock->aMethod();
  434. $mock->aMethod();
  435. $mock->atTestEnd('testSomething');
  436. $this->swallowErrors();
  437. }
  438. function testUnderAtLeastOnce() {
  439. $this->test->expectOnce("assertTrue", array(false, '*'));
  440. $mock = &new MockDummyWithInjectedTestCase();
  441. $mock->expectAtLeastOnce("aMethod");
  442. $mock->atTestEnd('testSomething');
  443. }
  444. function testZeroArguments() {
  445. $mock = &new MockDummyWithInjectedTestCase();
  446. $mock->expectArguments("aMethod", array());
  447. $mock->aMethod();
  448. $mock->atTestEnd('testSomething');
  449. }
  450. function testExpectedArguments() {
  451. $mock = &new MockDummyWithInjectedTestCase();
  452. $mock->expectArguments("aMethod", array(1, 2, 3));
  453. $mock->aMethod(1, 2, 3);
  454. $mock->atTestEnd('testSomething');
  455. }
  456. function testFailedArguments() {
  457. $this->test->expectOnce("assertTrue", array(false, "*"));
  458. $mock = &new MockDummyWithInjectedTestCase();
  459. $mock->expectArguments("aMethod", array("this"));
  460. $mock->aMethod("that");
  461. $mock->atTestEnd('testSomething');
  462. }
  463. function testWildcardArguments() {
  464. $mock = &new MockDummyWithInjectedTestCase($this, "wild");
  465. $mock->expectArguments("aMethod", array("wild", 123, "wild"));
  466. $mock->aMethod(100, 123, 101);
  467. $mock->atTestEnd('testSomething');
  468. }
  469. function testSpecificSequence() {
  470. $mock = &new MockDummyWithInjectedTestCase();
  471. $mock->expectArgumentsAt(1, "aMethod", array(1, 2, 3));
  472. $mock->expectArgumentsAt(2, "aMethod", array("Hello"));
  473. $mock->aMethod();
  474. $mock->aMethod(1, 2, 3);
  475. $mock->aMethod("Hello");
  476. $mock->aMethod();
  477. $mock->atTestEnd('testSomething');
  478. }
  479. function testFailedSequence() {
  480. $this->test->expectArguments("assertTrue", array(false, "*"));
  481. $this->test->expectCallCount("assertTrue", 2);
  482. $mock = &new MockDummyWithInjectedTestCase();
  483. $mock->expectArgumentsAt(0, "aMethod", array(1, 2, 3));
  484. $mock->expectArgumentsAt(1, "aMethod", array("Hello"));
  485. $mock->aMethod(1, 2);
  486. $mock->aMethod("Goodbye");
  487. $mock->atTestEnd('testSomething');
  488. }
  489. function testBadArgParameter() {
  490. $mock = &new MockDummyWithInjectedTestCase();
  491. $mock->expectArguments("aMethod", "foo");
  492. $this->assertErrorPattern('/\$args.*not an array/i');
  493. $mock->aMethod();
  494. $mock->tally();
  495. $mock->atTestEnd('testSomething');
  496. }
  497. }
  498. class TestOfMockComparisons extends UnitTestCase {
  499. function testEqualComparisonOfMocksDoesNotCrash() {
  500. $expectation = &new EqualExpectation(new MockDummy());
  501. $this->assertTrue($expectation->test(new MockDummy(), true));
  502. }
  503. function testIdenticalComparisonOfMocksDoesNotCrash() {
  504. $expectation = &new IdenticalExpectation(new MockDummy());
  505. $this->assertTrue($expectation->test(new MockDummy()));
  506. }
  507. }
  508. Mock::generatePartial('Dummy', 'TestDummy', array('anotherMethod'));
  509. class TestOfPartialMocks extends UnitTestCase {
  510. function testMethodReplacement() {
  511. $mock = &new TestDummy();
  512. $this->assertEqual($mock->aMethod(99), 99);
  513. $this->assertNull($mock->anotherMethod());
  514. }
  515. function testSettingReturns() {
  516. $mock = &new TestDummy();
  517. $mock->setReturnValue('anotherMethod', 33, array(3));
  518. $mock->setReturnValue('anotherMethod', 22);
  519. $mock->setReturnValueAt(2, 'anotherMethod', 44, array(3));
  520. $this->assertEqual($mock->anotherMethod(), 22);
  521. $this->assertEqual($mock->anotherMethod(3), 33);
  522. $this->assertEqual($mock->anotherMethod(3), 44);
  523. }
  524. function testReferences() {
  525. $mock = &new TestDummy();
  526. $object = new Dummy();
  527. $mock->setReturnReferenceAt(0, 'anotherMethod', $object, array(3));
  528. $this->assertReference($zref =& $mock->anotherMethod(3), $object);
  529. }
  530. function testExpectations() {
  531. $mock = &new TestDummy();
  532. $mock->expectCallCount('anotherMethod', 2);
  533. $mock->expectArguments('anotherMethod', array(77));
  534. $mock->expectArgumentsAt(1, 'anotherMethod', array(66));
  535. $mock->anotherMethod(77);
  536. $mock->anotherMethod(66);
  537. }
  538. function testSettingExpectationOnMissingMethodThrowsError() {
  539. $mock = &new TestDummy();
  540. $mock->expectCallCount('aMissingMethod', 2);
  541. $this->assertError();
  542. }
  543. }
  544. class ConstructorSuperClass {
  545. function ConstructorSuperClass() { }
  546. }
  547. class ConstructorSubClass extends ConstructorSuperClass {
  548. }
  549. class TestOfPHP4StyleSuperClassConstruct extends UnitTestCase {
  550. /**
  551. * This addresses issue #1231401. Without the fix in place, this will
  552. * generate a fatal PHP error.
  553. */
  554. function testBasicConstruct() {
  555. Mock::generate('ConstructorSubClass');
  556. $mock = &new MockConstructorSubClass();
  557. $this->assertIsA($mock, 'SimpleMock');
  558. $this->assertTrue(method_exists($mock, 'ConstructorSuperClass'));
  559. }
  560. }
  561. ?>