PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Server/SWXPHP/2.00/php/tests/simpletest/test/mock_objects_test.php

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