PageRenderTime 1130ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/scalr-2/tags/scalr-2.0.0/app/src/LibWebta/tests/simpletest/test/mock_objects_test.php

http://scalr.googlecode.com/
PHP | 834 lines | 719 code | 110 blank | 5 comment | 1 complexity | 35853b49c44e83dad13c333c5a32510f MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?php
  2. // $Id: mock_objects_test.php,v 1.36 2007/07/07 00:31:03 lastcraft Exp $
  3. require_once(dirname(__FILE__) . '/../autorun.php');
  4. require_once(dirname(__FILE__) . '/../expectation.php');
  5. require_once(dirname(__FILE__) . '/../mock_objects.php');
  6. class TestOfAnythingExpectation extends UnitTestCase {
  7. function testSimpleInteger() {
  8. $expectation = new AnythingExpectation();
  9. $this->assertTrue($expectation->test(33));
  10. $this->assertTrue($expectation->test(false));
  11. $this->assertTrue($expectation->test(null));
  12. }
  13. }
  14. class TestOfParametersExpectation extends UnitTestCase {
  15. function testEmptyMatch() {
  16. $expectation = new ParametersExpectation(array());
  17. $this->assertTrue($expectation->test(array()));
  18. $this->assertFalse($expectation->test(array(33)));
  19. }
  20. function testSingleMatch() {
  21. $expectation = new ParametersExpectation(array(0));
  22. $this->assertFalse($expectation->test(array(1)));
  23. $this->assertTrue($expectation->test(array(0)));
  24. }
  25. function testAnyMatch() {
  26. $expectation = new ParametersExpectation(false);
  27. $this->assertTrue($expectation->test(array()));
  28. $this->assertTrue($expectation->test(array(1, 2)));
  29. }
  30. function testMissingParameter() {
  31. $expectation = new ParametersExpectation(array(0));
  32. $this->assertFalse($expectation->test(array()));
  33. }
  34. function testNullParameter() {
  35. $expectation = new ParametersExpectation(array(null));
  36. $this->assertTrue($expectation->test(array(null)));
  37. $this->assertFalse($expectation->test(array()));
  38. }
  39. function testAnythingExpectations() {
  40. $expectation = new ParametersExpectation(array(new AnythingExpectation()));
  41. $this->assertFalse($expectation->test(array()));
  42. $this->assertIdentical($expectation->test(array(null)), true);
  43. $this->assertIdentical($expectation->test(array(13)), true);
  44. }
  45. function testOtherExpectations() {
  46. $expectation = new ParametersExpectation(
  47. array(new PatternExpectation('/hello/i')));
  48. $this->assertFalse($expectation->test(array('Goodbye')));
  49. $this->assertTrue($expectation->test(array('hello')));
  50. $this->assertTrue($expectation->test(array('Hello')));
  51. }
  52. function testIdentityOnly() {
  53. $expectation = new ParametersExpectation(array("0"));
  54. $this->assertFalse($expectation->test(array(0)));
  55. $this->assertTrue($expectation->test(array("0")));
  56. }
  57. function testLongList() {
  58. $expectation = new ParametersExpectation(
  59. array("0", 0, new AnythingExpectation(), false));
  60. $this->assertTrue($expectation->test(array("0", 0, 37, false)));
  61. $this->assertFalse($expectation->test(array("0", 0, 37, true)));
  62. $this->assertFalse($expectation->test(array("0", 0, 37)));
  63. }
  64. }
  65. class TestOfCallMap extends UnitTestCase {
  66. function testEmpty() {
  67. $map = new CallMap();
  68. $this->assertFalse($map->isMatch("any", array()));
  69. $this->assertNull($map->findFirstMatch("any", array()));
  70. }
  71. function testExactValue() {
  72. $map = new CallMap();
  73. $map->addValue(array(0), "Fred");
  74. $map->addValue(array(1), "Jim");
  75. $map->addValue(array("1"), "Tom");
  76. $this->assertTrue($map->isMatch(array(0)));
  77. $this->assertEqual($map->findFirstMatch(array(0)), "Fred");
  78. $this->assertTrue($map->isMatch(array(1)));
  79. $this->assertEqual($map->findFirstMatch(array(1)), "Jim");
  80. $this->assertEqual($map->findFirstMatch(array("1")), "Tom");
  81. }
  82. function testExactReference() {
  83. $map = new CallMap();
  84. $ref = "Fred";
  85. $map->addReference(array(0), $ref);
  86. $this->assertEqual($map->findFirstMatch(array(0)), "Fred");
  87. $ref2 = &$map->findFirstMatch(array(0));
  88. $this->assertReference($ref2, $ref);
  89. }
  90. function testWildcard() {
  91. $map = new CallMap();
  92. $map->addValue(array(new AnythingExpectation(), 1, 3), "Fred");
  93. $this->assertTrue($map->isMatch(array(2, 1, 3)));
  94. $this->assertEqual($map->findFirstMatch(array(2, 1, 3)), "Fred");
  95. }
  96. function testAllWildcard() {
  97. $map = new CallMap();
  98. $this->assertFalse($map->isMatch(array(2, 1, 3)));
  99. $map->addValue("", "Fred");
  100. $this->assertTrue($map->isMatch(array(2, 1, 3)));
  101. $this->assertEqual($map->findFirstMatch(array(2, 1, 3)), "Fred");
  102. }
  103. function testOrdering() {
  104. $map = new CallMap();
  105. $map->addValue(array(1, 2), "1, 2");
  106. $map->addValue(array(1, 3), "1, 3");
  107. $map->addValue(array(1), "1");
  108. $map->addValue(array(1, 4), "1, 4");
  109. $map->addValue(array(new AnythingExpectation()), "Any");
  110. $map->addValue(array(2), "2");
  111. $map->addValue("", "Default");
  112. $map->addValue(array(), "None");
  113. $this->assertEqual($map->findFirstMatch(array(1, 2)), "1, 2");
  114. $this->assertEqual($map->findFirstMatch(array(1, 3)), "1, 3");
  115. $this->assertEqual($map->findFirstMatch(array(1, 4)), "1, 4");
  116. $this->assertEqual($map->findFirstMatch(array(1)), "1");
  117. $this->assertEqual($map->findFirstMatch(array(2)), "Any");
  118. $this->assertEqual($map->findFirstMatch(array(3)), "Any");
  119. $this->assertEqual($map->findFirstMatch(array()), "Default");
  120. }
  121. }
  122. class Dummy {
  123. function Dummy() {
  124. }
  125. function aMethod() {
  126. return true;
  127. }
  128. function anotherMethod() {
  129. return true;
  130. }
  131. }
  132. Mock::generate('Dummy');
  133. Mock::generate('Dummy', 'AnotherMockDummy');
  134. Mock::generate('Dummy', 'MockDummyWithExtraMethods', array('extraMethod'));
  135. class TestOfMockGeneration extends UnitTestCase {
  136. function testCloning() {
  137. $mock = &new MockDummy();
  138. $this->assertTrue(method_exists($mock, "aMethod"));
  139. $this->assertNull($mock->aMethod());
  140. }
  141. function testCloningWithExtraMethod() {
  142. $mock = &new MockDummyWithExtraMethods();
  143. $this->assertTrue(method_exists($mock, "extraMethod"));
  144. }
  145. function testCloningWithChosenClassName() {
  146. $mock = &new AnotherMockDummy();
  147. $this->assertTrue(method_exists($mock, "aMethod"));
  148. }
  149. }
  150. class TestOfMockReturns extends UnitTestCase {
  151. function testDefaultReturn() {
  152. $mock = &new MockDummy();
  153. $mock->setReturnValue("aMethod", "aaa");
  154. $this->assertIdentical($mock->aMethod(), "aaa");
  155. $this->assertIdentical($mock->aMethod(), "aaa");
  156. }
  157. function testParameteredReturn() {
  158. $mock = &new MockDummy();
  159. $mock->setReturnValue('aMethod', 'aaa', array(1, 2, 3));
  160. $this->assertNull($mock->aMethod());
  161. $this->assertIdentical($mock->aMethod(1, 2, 3), 'aaa');
  162. }
  163. function testReferenceReturned() {
  164. $mock = &new MockDummy();
  165. $object = new Dummy();
  166. $mock->setReturnReference('aMethod', $object, array(1, 2, 3));
  167. $this->assertReference($zref = &$mock->aMethod(1, 2, 3), $object);
  168. }
  169. function testPatternMatchReturn() {
  170. $mock = &new MockDummy();
  171. $mock->setReturnValue(
  172. "aMethod",
  173. "aaa",
  174. array(new PatternExpectation('/hello/i')));
  175. $this->assertIdentical($mock->aMethod('Hello'), "aaa");
  176. $this->assertNull($mock->aMethod('Goodbye'));
  177. }
  178. function testMultipleMethods() {
  179. $mock = &new MockDummy();
  180. $mock->setReturnValue("aMethod", 100, array(1));
  181. $mock->setReturnValue("aMethod", 200, array(2));
  182. $mock->setReturnValue("anotherMethod", 10, array(1));
  183. $mock->setReturnValue("anotherMethod", 20, array(2));
  184. $this->assertIdentical($mock->aMethod(1), 100);
  185. $this->assertIdentical($mock->anotherMethod(1), 10);
  186. $this->assertIdentical($mock->aMethod(2), 200);
  187. $this->assertIdentical($mock->anotherMethod(2), 20);
  188. }
  189. function testReturnSequence() {
  190. $mock = &new MockDummy();
  191. $mock->setReturnValueAt(0, "aMethod", "aaa");
  192. $mock->setReturnValueAt(1, "aMethod", "bbb");
  193. $mock->setReturnValueAt(3, "aMethod", "ddd");
  194. $this->assertIdentical($mock->aMethod(), "aaa");
  195. $this->assertIdentical($mock->aMethod(), "bbb");
  196. $this->assertNull($mock->aMethod());
  197. $this->assertIdentical($mock->aMethod(), "ddd");
  198. }
  199. function testReturnReferenceSequence() {
  200. $mock = &new MockDummy();
  201. $object = new Dummy();
  202. $mock->setReturnReferenceAt(1, "aMethod", $object);
  203. $this->assertNull($mock->aMethod());
  204. $this->assertReference($zref =& $mock->aMethod(), $object);
  205. $this->assertNull($mock->aMethod());
  206. }
  207. function testComplicatedReturnSequence() {
  208. $mock = &new MockDummy();
  209. $object = new Dummy();
  210. $mock->setReturnValueAt(1, "aMethod", "aaa", array("a"));
  211. $mock->setReturnValueAt(1, "aMethod", "bbb");
  212. $mock->setReturnReferenceAt(2, "aMethod", $object, array('*', 2));
  213. $mock->setReturnValueAt(2, "aMethod", "value", array('*', 3));
  214. $mock->setReturnValue("aMethod", 3, array(3));
  215. $this->assertNull($mock->aMethod());
  216. $this->assertEqual($mock->aMethod("a"), "aaa");
  217. $this->assertReference($zref =& $mock->aMethod(1, 2), $object);
  218. $this->assertEqual($mock->aMethod(3), 3);
  219. $this->assertNull($mock->aMethod());
  220. }
  221. function testMultipleMethodSequences() {
  222. $mock = &new MockDummy();
  223. $mock->setReturnValueAt(0, "aMethod", "aaa");
  224. $mock->setReturnValueAt(1, "aMethod", "bbb");
  225. $mock->setReturnValueAt(0, "anotherMethod", "ccc");
  226. $mock->setReturnValueAt(1, "anotherMethod", "ddd");
  227. $this->assertIdentical($mock->aMethod(), "aaa");
  228. $this->assertIdentical($mock->anotherMethod(), "ccc");
  229. $this->assertIdentical($mock->aMethod(), "bbb");
  230. $this->assertIdentical($mock->anotherMethod(), "ddd");
  231. }
  232. function testSequenceFallback() {
  233. $mock = &new MockDummy();
  234. $mock->setReturnValueAt(0, "aMethod", "aaa", array('a'));
  235. $mock->setReturnValueAt(1, "aMethod", "bbb", array('a'));
  236. $mock->setReturnValue("aMethod", "AAA");
  237. $this->assertIdentical($mock->aMethod('a'), "aaa");
  238. $this->assertIdentical($mock->aMethod('b'), "AAA");
  239. }
  240. function testMethodInterference() {
  241. $mock = &new MockDummy();
  242. $mock->setReturnValueAt(0, "anotherMethod", "aaa");
  243. $mock->setReturnValue("aMethod", "AAA");
  244. $this->assertIdentical($mock->aMethod(), "AAA");
  245. $this->assertIdentical($mock->anotherMethod(), "aaa");
  246. }
  247. }
  248. class TestOfMockExpectationsThatPass extends UnitTestCase {
  249. function testAnyArgument() {
  250. $mock = &new MockDummy();
  251. $mock->expect('aMethod', array('*'));
  252. $mock->aMethod(1);
  253. $mock->aMethod('hello');
  254. }
  255. function testAnyTwoArguments() {
  256. $mock = &new MockDummy();
  257. $mock->expect('aMethod', array('*', '*'));
  258. $mock->aMethod(1, 2);
  259. }
  260. function testSpecificArgument() {
  261. $mock = &new MockDummy();
  262. $mock->expect('aMethod', array(1));
  263. $mock->aMethod(1);
  264. }
  265. function testExpectation() {
  266. $mock = &new MockDummy();
  267. $mock->expect('aMethod', array(new IsAExpectation('Dummy')));
  268. $mock->aMethod(new Dummy());
  269. }
  270. function testArgumentsInSequence() {
  271. $mock = &new MockDummy();
  272. $mock->expectAt(0, 'aMethod', array(1, 2));
  273. $mock->expectAt(1, 'aMethod', array(3, 4));
  274. $mock->aMethod(1, 2);
  275. $mock->aMethod(3, 4);
  276. }
  277. function testAtLeastOnceSatisfiedByOneCall() {
  278. $mock = &new MockDummy();
  279. $mock->expectAtLeastOnce('aMethod');
  280. $mock->aMethod();
  281. }
  282. function testAtLeastOnceSatisfiedByTwoCalls() {
  283. $mock = &new MockDummy();
  284. $mock->expectAtLeastOnce('aMethod');
  285. $mock->aMethod();
  286. $mock->aMethod();
  287. }
  288. function testOnceSatisfiedByOneCall() {
  289. $mock = &new MockDummy();
  290. $mock->expectOnce('aMethod');
  291. $mock->aMethod();
  292. }
  293. function testMinimumCallsSatisfiedByEnoughCalls() {
  294. $mock = &new MockDummy();
  295. $mock->expectMinimumCallCount('aMethod', 1);
  296. $mock->aMethod();
  297. }
  298. function testMinimumCallsSatisfiedByTooManyCalls() {
  299. $mock = &new MockDummy();
  300. $mock->expectMinimumCallCount('aMethod', 3);
  301. $mock->aMethod();
  302. $mock->aMethod();
  303. $mock->aMethod();
  304. $mock->aMethod();
  305. }
  306. function testMaximumCallsSatisfiedByEnoughCalls() {
  307. $mock = &new MockDummy();
  308. $mock->expectMaximumCallCount('aMethod', 1);
  309. $mock->aMethod();
  310. }
  311. function testMaximumCallsSatisfiedByNoCalls() {
  312. $mock = &new MockDummy();
  313. $mock->expectMaximumCallCount('aMethod', 1);
  314. }
  315. }
  316. class MockWithInjectedTestCase extends SimpleMock {
  317. function &_getCurrentTestCase() {
  318. $context = &SimpleTest::getContext();
  319. $test = &$context->getTest();
  320. return $test->getMockedTest();
  321. }
  322. }
  323. SimpleTest::setMockBaseClass('MockWithInjectedTestCase');
  324. Mock::generate('Dummy', 'MockDummyWithInjectedTestCase');
  325. SimpleTest::setMockBaseClass('SimpleMock');
  326. Mock::generate('SimpleTestCase');
  327. class LikeExpectation extends IdenticalExpectation {
  328. function LikeExpectation($expectation) {
  329. $expectation->_message = '';
  330. $this->IdenticalExpectation($expectation);
  331. }
  332. function test($compare) {
  333. $compare->_message = '';
  334. return parent::test($compare);
  335. }
  336. function testMessage($compare) {
  337. $compare->_message = '';
  338. return parent::testMessage($compare);
  339. }
  340. }
  341. class TestOfMockExpectations extends UnitTestCase {
  342. var $test;
  343. function setUp() {
  344. $this->test = &new MockSimpleTestCase();
  345. }
  346. function &getMockedTest() {
  347. return $this->test;
  348. }
  349. function testSettingExpectationOnNonMethodThrowsError() {
  350. $mock = &new MockDummyWithInjectedTestCase();
  351. $mock->expectMaximumCallCount('aMissingMethod', 2);
  352. $this->assertError();
  353. }
  354. function testMaxCallsDetectsOverrun() {
  355. $this->test->expectOnce('assert', array(
  356. new LikeExpectation(new MaximumCallCountExpectation('aMethod', 2)),
  357. 3));
  358. $mock = &new MockDummyWithInjectedTestCase();
  359. $mock->expectMaximumCallCount('aMethod', 2);
  360. $mock->aMethod();
  361. $mock->aMethod();
  362. $mock->aMethod();
  363. $mock->_mock->atTestEnd('testSomething', $this->test);
  364. }
  365. function testTallyOnMaxCallsSendsPassOnUnderrun() {
  366. $this->test->expectOnce('assert', array(
  367. new LikeExpectation(new MaximumCallCountExpectation('aMethod', 2)),
  368. 2));
  369. $mock = &new MockDummyWithInjectedTestCase();
  370. $mock->expectMaximumCallCount("aMethod", 2);
  371. $mock->aMethod();
  372. $mock->aMethod();
  373. $mock->_mock->atTestEnd('testSomething', $this->test);
  374. }
  375. function testExpectNeverDetectsOverrun() {
  376. $this->test->expectOnce('assert', array(
  377. new LikeExpectation(new MaximumCallCountExpectation('aMethod', 0)),
  378. 1));
  379. $mock = &new MockDummyWithInjectedTestCase();
  380. $mock->expectNever('aMethod');
  381. $mock->aMethod();
  382. $mock->_mock->atTestEnd('testSomething', $this->test);
  383. }
  384. function testTallyOnExpectNeverStillSendsPassOnUnderrun() {
  385. $this->test->expectOnce('assert', array(
  386. new LikeExpectation(new MaximumCallCountExpectation('aMethod', 0)),
  387. 0));
  388. $mock = &new MockDummyWithInjectedTestCase();
  389. $mock->expectNever('aMethod');
  390. $mock->_mock->atTestEnd('testSomething', $this->test);
  391. }
  392. function testMinCalls() {
  393. $this->test->expectOnce('assert', array(
  394. new LikeExpectation(new MinimumCallCountExpectation('aMethod', 2)),
  395. 2));
  396. $mock = &new MockDummyWithInjectedTestCase();
  397. $mock->expectMinimumCallCount('aMethod', 2);
  398. $mock->aMethod();
  399. $mock->aMethod();
  400. $mock->_mock->atTestEnd('testSomething', $this->test);
  401. }
  402. function testFailedNever() {
  403. $this->test->expectOnce('assert', array(
  404. new LikeExpectation(new MaximumCallCountExpectation('aMethod', 0)),
  405. 1));
  406. $mock = &new MockDummyWithInjectedTestCase();
  407. $mock->expectNever('aMethod');
  408. $mock->aMethod();
  409. $mock->_mock->atTestEnd('testSomething', $this->test);
  410. }
  411. function testUnderOnce() {
  412. $this->test->expectOnce('assert', array(
  413. new LikeExpectation(new CallCountExpectation('aMethod', 1)),
  414. 0));
  415. $mock = &new MockDummyWithInjectedTestCase();
  416. $mock->expectOnce('aMethod');
  417. $mock->_mock->atTestEnd('testSomething', $this->test);
  418. }
  419. function testOverOnce() {
  420. $this->test->expectOnce('assert', array(
  421. new LikeExpectation(new CallCountExpectation('aMethod', 1)),
  422. 2));
  423. $mock = &new MockDummyWithInjectedTestCase();
  424. $mock->expectOnce('aMethod');
  425. $mock->aMethod();
  426. $mock->aMethod();
  427. $mock->_mock->atTestEnd('testSomething', $this->test);
  428. }
  429. function testUnderAtLeastOnce() {
  430. $this->test->expectOnce('assert', array(
  431. new LikeExpectation(new MinimumCallCountExpectation('aMethod', 1)),
  432. 0));
  433. $mock = &new MockDummyWithInjectedTestCase();
  434. $mock->expectAtLeastOnce("aMethod");
  435. $mock->_mock->atTestEnd('testSomething', $this->test);
  436. }
  437. function testZeroArguments() {
  438. $this->test->expectOnce('assert', array(
  439. new LikeExpectation(new ParametersExpectation(array())),
  440. array(),
  441. '*'));
  442. $mock = &new MockDummyWithInjectedTestCase();
  443. $mock->expect("aMethod", array());
  444. $mock->aMethod();
  445. $mock->_mock->atTestEnd('testSomething', $this->test);
  446. }
  447. function testExpectedArguments() {
  448. $this->test->expectOnce('assert', array(
  449. new LikeExpectation(new ParametersExpectation(array(1, 2, 3))),
  450. array(1, 2, 3),
  451. '*'));
  452. $mock = &new MockDummyWithInjectedTestCase();
  453. $mock->expect('aMethod', array(1, 2, 3));
  454. $mock->aMethod(1, 2, 3);
  455. $mock->_mock->atTestEnd('testSomething', $this->test);
  456. }
  457. function testFailedArguments() {
  458. $this->test->expectOnce('assert', array(
  459. new LikeExpectation(new ParametersExpectation(array('this'))),
  460. array('that'),
  461. '*'));
  462. $mock = &new MockDummyWithInjectedTestCase();
  463. $mock->expect('aMethod', array('this'));
  464. $mock->aMethod('that');
  465. $mock->_mock->atTestEnd('testSomething', $this->test);
  466. }
  467. function testWildcardsAreTranslatedToAnythingExpectations() {
  468. $this->test->expectOnce('assert', array(
  469. new LikeExpectation(new ParametersExpectation(array(
  470. new AnythingExpectation(), 123, new AnythingExpectation()))),
  471. array(100, 123, 101),
  472. '*'));
  473. $mock = &new MockDummyWithInjectedTestCase($this);
  474. $mock->expect("aMethod", array('*', 123, '*'));
  475. $mock->aMethod(100, 123, 101);
  476. $mock->_mock->atTestEnd('testSomething', $this->test);
  477. }
  478. function testSpecificPassingSequence() {
  479. $this->test->expectAt(0, 'assert', array(
  480. new LikeExpectation(new ParametersExpectation(array(1, 2, 3))),
  481. array(1, 2, 3),
  482. '*'));
  483. $this->test->expectAt(1, 'assert', array(
  484. new LikeExpectation(new ParametersExpectation(array('Hello'))),
  485. array('Hello'),
  486. '*'));
  487. $mock = &new MockDummyWithInjectedTestCase();
  488. $mock->expectAt(1, 'aMethod', array(1, 2, 3));
  489. $mock->expectAt(2, 'aMethod', array('Hello'));
  490. $mock->aMethod();
  491. $mock->aMethod(1, 2, 3);
  492. $mock->aMethod('Hello');
  493. $mock->aMethod();
  494. $mock->_mock->atTestEnd('testSomething', $this->test);
  495. }
  496. function testNonArrayForExpectedParametersGivesError() {
  497. $mock = &new MockDummyWithInjectedTestCase();
  498. $mock->expect("aMethod", "foo");
  499. $this->assertErrorPattern('/\$args.*not an array/i');
  500. $mock->aMethod();
  501. $mock->tally();
  502. $mock->_mock->atTestEnd('testSomething', $this->test);
  503. }
  504. }
  505. class TestOfMockComparisons extends UnitTestCase {
  506. function testEqualComparisonOfMocksDoesNotCrash() {
  507. $expectation = &new EqualExpectation(new MockDummy());
  508. $this->assertTrue($expectation->test(new MockDummy(), true));
  509. }
  510. function testIdenticalComparisonOfMocksDoesNotCrash() {
  511. $expectation = &new IdenticalExpectation(new MockDummy());
  512. $this->assertTrue($expectation->test(new MockDummy()));
  513. }
  514. }
  515. class ClassWithSpecialMethods {
  516. function __get($name) { }
  517. function __set($name, $value) { }
  518. function __isset($name) { }
  519. function __unset($name) { }
  520. function __call($method, $arguments) { }
  521. function __toString() { }
  522. }
  523. Mock::generate('ClassWithSpecialMethods');
  524. class TestOfSpecialMethods extends UnitTestCase {
  525. function skip() {
  526. $this->skipIf(version_compare(phpversion(), '5', '<='), 'Overloading not tested for PHP 4');
  527. }
  528. function testCanMockTheThingAtAll() {
  529. $mock = new MockClassWithSpecialMethods();
  530. }
  531. function testReturnFromSpecialAccessor() {
  532. $mock = &new MockClassWithSpecialMethods();
  533. $mock->setReturnValue('__get', '1st Return', array('first'));
  534. $mock->setReturnValue('__get', '2nd Return', array('second'));
  535. $this->assertEqual($mock->first, '1st Return');
  536. $this->assertEqual($mock->second, '2nd Return');
  537. }
  538. function testcanExpectTheSettingOfValue() {
  539. $mock = &new MockClassWithSpecialMethods();
  540. $mock->expectOnce('__set', array('a', 'A'));
  541. $mock->a = 'A';
  542. }
  543. function testCanSimulateAnOverloadmethod() {
  544. $mock = &new MockClassWithSpecialMethods();
  545. $mock->expectOnce('__call', array('amOverloaded', array('A')));
  546. $mock->setReturnValue('__call', 'aaa');
  547. $this->assertIdentical($mock->amOverloaded('A'), 'aaa');
  548. }
  549. function testCanEmulateIsset() {
  550. $mock = &new MockClassWithSpecialMethods();
  551. $mock->setReturnValue('__isset', true);
  552. $this->assertIdentical(isset($mock->a), true);
  553. }
  554. function testCanExpectUnset() {
  555. $mock = &new MockClassWithSpecialMethods();
  556. $mock->expectOnce('__unset', array('a'));
  557. unset($mock->a);
  558. }
  559. function testToStringMagic() {
  560. $mock = &new MockClassWithSpecialMethods();
  561. $mock->expectOnce('__toString');
  562. $mock->setReturnValue('__toString', 'AAA');
  563. ob_start();
  564. print $mock;
  565. $output = ob_get_contents();
  566. ob_end_clean();
  567. $this->assertEqual($output, 'AAA');
  568. }
  569. }
  570. if (version_compare(phpversion(), '5', '>=')) {
  571. $class = 'class WithStaticMethod { ';
  572. $class .= ' static function aStaticMethod() { } ';
  573. $class .= '}';
  574. eval($class);
  575. }
  576. Mock::generate('WithStaticMethod');
  577. class TestOfMockingClassesWithStaticMethods extends UnitTestCase {
  578. function skip() {
  579. $this->skipUnless(version_compare(phpversion(), '5', '>='));
  580. }
  581. function testStaticMethodIsMockedAsStatic() {
  582. $mock = new WithStaticMethod();
  583. $reflection = new ReflectionClass($mock);
  584. $method = $reflection->getMethod('aStaticMethod');
  585. $this->assertTrue($method->isStatic());
  586. }
  587. }
  588. Mock::generatePartial('Dummy', 'TestDummy', array('anotherMethod'));
  589. class TestOfPartialMocks extends UnitTestCase {
  590. function testMethodReplacementWithNoBehaviourReturnsNull() {
  591. $mock = &new TestDummy();
  592. $this->assertEqual($mock->aMethod(99), 99);
  593. $this->assertNull($mock->anotherMethod());
  594. }
  595. function testSettingReturns() {
  596. $mock = &new TestDummy();
  597. $mock->setReturnValue('anotherMethod', 33, array(3));
  598. $mock->setReturnValue('anotherMethod', 22);
  599. $mock->setReturnValueAt(2, 'anotherMethod', 44, array(3));
  600. $this->assertEqual($mock->anotherMethod(), 22);
  601. $this->assertEqual($mock->anotherMethod(3), 33);
  602. $this->assertEqual($mock->anotherMethod(3), 44);
  603. }
  604. function testReferences() {
  605. $mock = &new TestDummy();
  606. $object = new Dummy();
  607. $mock->setReturnReferenceAt(0, 'anotherMethod', $object, array(3));
  608. $this->assertReference($zref =& $mock->anotherMethod(3), $object);
  609. }
  610. function testExpectations() {
  611. $mock = &new TestDummy();
  612. $mock->expectCallCount('anotherMethod', 2);
  613. $mock->expect('anotherMethod', array(77));
  614. $mock->expectAt(1, 'anotherMethod', array(66));
  615. $mock->anotherMethod(77);
  616. $mock->anotherMethod(66);
  617. }
  618. function testSettingExpectationOnMissingMethodThrowsError() {
  619. $mock = &new TestDummy();
  620. $mock->expectCallCount('aMissingMethod', 2);
  621. $this->assertError();
  622. }
  623. }
  624. class ConstructorSuperClass {
  625. function ConstructorSuperClass() { }
  626. }
  627. class ConstructorSubClass extends ConstructorSuperClass {
  628. }
  629. class TestOfPHP4StyleSuperClassConstruct extends UnitTestCase {
  630. /*
  631. * This addresses issue #1231401. Without the fix in place, this will
  632. * generate a fatal PHP error.
  633. */
  634. function testBasicConstruct() {
  635. Mock::generate('ConstructorSubClass');
  636. $mock = &new MockConstructorSubClass();
  637. $this->assertIsA($mock, 'ConstructorSubClass');
  638. $this->assertTrue(method_exists($mock, 'ConstructorSuperClass'));
  639. }
  640. }
  641. class TestOfPHP5StaticMethodMocking extends UnitTestCase {
  642. function skip() {
  643. $this->skipIf(version_compare(phpversion(), '5', '<='), 'Static methods not tested in PHP 4');
  644. }
  645. function testCanCreateAMockObjectWithStaticMethodsWithoutError() {
  646. eval('
  647. class SimpleObjectContainingStaticMethod {
  648. static function someStatic() { }
  649. }
  650. ');
  651. Mock::generate('SimpleObjectContainingStaticMethod');
  652. $this->assertNoErrors();
  653. }
  654. }
  655. class TestOfPHP5AbstractMethodMocking extends UnitTestCase {
  656. function skip() {
  657. $this->skipIf(version_compare(phpversion(), '5', '<='), 'Abstract class/methods not tested in PHP 4');
  658. }
  659. function testCanCreateAMockObjectFromAnAbstractWithProperFunctionDeclarations() {
  660. eval('
  661. abstract class SimpleAbstractClassContainingAbstractMethods {
  662. abstract function anAbstract();
  663. abstract function anAbstractWithParameter($foo);
  664. abstract function anAbstractWithMultipleParameters($foo, $bar);
  665. }
  666. ');
  667. Mock::generate('SimpleAbstractClassContainingAbstractMethods');
  668. $this->assertNoErrors();
  669. $this->assertTrue(
  670. method_exists(
  671. 'MockSimpleAbstractClassContainingAbstractMethods',
  672. 'anAbstract'
  673. )
  674. );
  675. $this->assertTrue(
  676. method_exists(
  677. 'MockSimpleAbstractClassContainingAbstractMethods',
  678. 'anAbstractWithParameter'
  679. )
  680. );
  681. $this->assertTrue(
  682. method_exists(
  683. 'MockSimpleAbstractClassContainingAbstractMethods',
  684. 'anAbstractWithMultipleParameters'
  685. )
  686. );
  687. }
  688. function testMethodsDefinedAsAbstractInParentShouldHaveFullSignature() {
  689. eval('
  690. abstract class SimpleParentAbstractClassContainingAbstractMethods {
  691. abstract function anAbstract();
  692. abstract function anAbstractWithParameter($foo);
  693. abstract function anAbstractWithMultipleParameters($foo, $bar);
  694. }
  695. class SimpleChildAbstractClassContainingAbstractMethods extends SimpleParentAbstractClassContainingAbstractMethods {
  696. function anAbstract(){}
  697. function anAbstractWithParameter($foo){}
  698. function anAbstractWithMultipleParameters($foo, $bar){}
  699. }
  700. ');
  701. Mock::generate('SimpleChildAbstractClassContainingAbstractMethods');
  702. $this->assertNoErrors();
  703. $this->assertTrue(
  704. method_exists(
  705. 'MockSimpleChildAbstractClassContainingAbstractMethods',
  706. 'anAbstract'
  707. )
  708. );
  709. $this->assertTrue(
  710. method_exists(
  711. 'MockSimpleChildAbstractClassContainingAbstractMethods',
  712. 'anAbstractWithParameter'
  713. )
  714. );
  715. $this->assertTrue(
  716. method_exists(
  717. 'MockSimpleChildAbstractClassContainingAbstractMethods',
  718. 'anAbstractWithMultipleParameters'
  719. )
  720. );
  721. }
  722. }
  723. ?>