PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/swiftmailer/swiftmailer/test-suite/lib/simpletest/test/mock_objects_test.php

https://bitbucket.org/renta/jobeet2.loc
PHP | 1012 lines | 881 code | 129 blank | 2 comment | 1 complexity | e2df8aa2a28b268e331e62cdd2a86b4d MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, LGPL-2.1, BSD-2-Clause, Apache-2.0, CC-BY-3.0
  1. <?php
  2. // $Id: mock_objects_test.php 1801 2008-09-01 23:22:06Z tswicegood $
  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 TestOfSimpleSignatureMap extends UnitTestCase {
  66. function testEmpty() {
  67. $map = new SimpleSignatureMap();
  68. $this->assertFalse($map->isMatch("any", array()));
  69. $this->assertNull($map->findFirstAction("any", array()));
  70. }
  71. function testDifferentCallSignaturesCanHaveDifferentReferences() {
  72. $map = new SimpleSignatureMap();
  73. $fred = 'Fred';
  74. $jim = 'jim';
  75. $map->add(array(0), $fred);
  76. $map->add(array('0'), $jim);
  77. $this->assertSame($fred, $map->findFirstAction(array(0)));
  78. $this->assertSame($jim, $map->findFirstAction(array('0')));
  79. }
  80. function testWildcard() {
  81. $fred = 'Fred';
  82. $map = new SimpleSignatureMap();
  83. $map->add(array(new AnythingExpectation(), 1, 3), $fred);
  84. $this->assertTrue($map->isMatch(array(2, 1, 3)));
  85. $this->assertSame($map->findFirstAction(array(2, 1, 3)), $fred);
  86. }
  87. function testAllWildcard() {
  88. $fred = 'Fred';
  89. $map = new SimpleSignatureMap();
  90. $this->assertFalse($map->isMatch(array(2, 1, 3)));
  91. $map->add('', $fred);
  92. $this->assertTrue($map->isMatch(array(2, 1, 3)));
  93. $this->assertSame($map->findFirstAction(array(2, 1, 3)), $fred);
  94. }
  95. function testOrdering() {
  96. $map = new SimpleSignatureMap();
  97. $map->add(array(1, 2), new SimpleByValue("1, 2"));
  98. $map->add(array(1, 3), new SimpleByValue("1, 3"));
  99. $map->add(array(1), new SimpleByValue("1"));
  100. $map->add(array(1, 4), new SimpleByValue("1, 4"));
  101. $map->add(array(new AnythingExpectation()), new SimpleByValue("Any"));
  102. $map->add(array(2), new SimpleByValue("2"));
  103. $map->add("", new SimpleByValue("Default"));
  104. $map->add(array(), new SimpleByValue("None"));
  105. $this->assertEqual($map->findFirstAction(array(1, 2)), new SimpleByValue("1, 2"));
  106. $this->assertEqual($map->findFirstAction(array(1, 3)), new SimpleByValue("1, 3"));
  107. $this->assertEqual($map->findFirstAction(array(1, 4)), new SimpleByValue("1, 4"));
  108. $this->assertEqual($map->findFirstAction(array(1)), new SimpleByValue("1"));
  109. $this->assertEqual($map->findFirstAction(array(2)), new SimpleByValue("Any"));
  110. $this->assertEqual($map->findFirstAction(array(3)), new SimpleByValue("Any"));
  111. $this->assertEqual($map->findFirstAction(array()), new SimpleByValue("Default"));
  112. }
  113. }
  114. class TestOfCallSchedule extends UnitTestCase {
  115. function testCanBeSetToAlwaysReturnTheSameReference() {
  116. $a = 5;
  117. $schedule = new SimpleCallSchedule();
  118. $schedule->register('aMethod', false, new SimpleByReference($a));
  119. $this->assertReference($schedule->respond(0, 'aMethod', array()), $a);
  120. $this->assertReference($schedule->respond(1, 'aMethod', array()), $a);
  121. }
  122. function testSpecificSignaturesOverrideTheAlwaysCase() {
  123. $any = 'any';
  124. $one = 'two';
  125. $schedule = new SimpleCallSchedule();
  126. $schedule->register('aMethod', array(1), new SimpleByReference($one));
  127. $schedule->register('aMethod', false, new SimpleByReference($any));
  128. $this->assertReference($schedule->respond(0, 'aMethod', array(2)), $any);
  129. $this->assertReference($schedule->respond(0, 'aMethod', array(1)), $one);
  130. }
  131. function testReturnsCanBeSetOverTime() {
  132. $one = 'one';
  133. $two = 'two';
  134. $schedule = new SimpleCallSchedule();
  135. $schedule->registerAt(0, 'aMethod', false, new SimpleByReference($one));
  136. $schedule->registerAt(1, 'aMethod', false, new SimpleByReference($two));
  137. $this->assertReference($schedule->respond(0, 'aMethod', array()), $one);
  138. $this->assertReference($schedule->respond(1, 'aMethod', array()), $two);
  139. }
  140. function testReturnsOverTimecanBeAlteredByTheArguments() {
  141. $one = '1';
  142. $two = '2';
  143. $two_a = '2a';
  144. $schedule = new SimpleCallSchedule();
  145. $schedule->registerAt(0, 'aMethod', false, new SimpleByReference($one));
  146. $schedule->registerAt(1, 'aMethod', array('a'), new SimpleByReference($two_a));
  147. $schedule->registerAt(1, 'aMethod', false, new SimpleByReference($two));
  148. $this->assertReference($schedule->respond(0, 'aMethod', array()), $one);
  149. $this->assertReference($schedule->respond(1, 'aMethod', array()), $two);
  150. $this->assertReference($schedule->respond(1, 'aMethod', array('a')), $two_a);
  151. }
  152. function testCanReturnByValue() {
  153. $a = 5;
  154. $schedule = new SimpleCallSchedule();
  155. $schedule->register('aMethod', false, new SimpleByValue($a));
  156. $this->assertCopy($schedule->respond(0, 'aMethod', array()), $a);
  157. }
  158. function testCanThrowException() {
  159. if (version_compare(phpversion(), '5', '>=')) {
  160. $schedule = new SimpleCallSchedule();
  161. $schedule->register('aMethod', false, new SimpleThrower(new Exception('Ouch')));
  162. $this->expectException(new Exception('Ouch'));
  163. $schedule->respond(0, 'aMethod', array());
  164. }
  165. }
  166. function testCanEmitError() {
  167. $schedule = new SimpleCallSchedule();
  168. $schedule->register('aMethod', false, new SimpleErrorThrower('Ouch', E_USER_WARNING));
  169. $this->expectError('Ouch');
  170. $schedule->respond(0, 'aMethod', array());
  171. }
  172. }
  173. class Dummy {
  174. function Dummy() {
  175. }
  176. function aMethod() {
  177. return true;
  178. }
  179. function &aReferenceMethod() {
  180. return true;
  181. }
  182. function anotherMethod() {
  183. return true;
  184. }
  185. }
  186. Mock::generate('Dummy');
  187. Mock::generate('Dummy', 'AnotherMockDummy');
  188. Mock::generate('Dummy', 'MockDummyWithExtraMethods', array('extraMethod'));
  189. class TestOfMockGeneration extends UnitTestCase {
  190. function testCloning() {
  191. $mock = new MockDummy();
  192. $this->assertTrue(method_exists($mock, "aMethod"));
  193. $this->assertNull($mock->aMethod());
  194. }
  195. function testCloningWithExtraMethod() {
  196. $mock = new MockDummyWithExtraMethods();
  197. $this->assertTrue(method_exists($mock, "extraMethod"));
  198. }
  199. function testCloningWithChosenClassName() {
  200. $mock = new AnotherMockDummy();
  201. $this->assertTrue(method_exists($mock, "aMethod"));
  202. }
  203. }
  204. class TestOfMockReturns extends UnitTestCase {
  205. function testDefaultReturn() {
  206. $mock = new MockDummy();
  207. $mock->setReturnValue("aMethod", "aaa");
  208. $this->assertIdentical($mock->aMethod(), "aaa");
  209. $this->assertIdentical($mock->aMethod(), "aaa");
  210. }
  211. function testParameteredReturn() {
  212. $mock = new MockDummy();
  213. $mock->setReturnValue('aMethod', 'aaa', array(1, 2, 3));
  214. $this->assertNull($mock->aMethod());
  215. $this->assertIdentical($mock->aMethod(1, 2, 3), 'aaa');
  216. }
  217. function testSetReturnGivesObjectReference() {
  218. $mock = new MockDummy();
  219. $object = new Dummy();
  220. $mock->returns('aMethod', $object, array(1, 2, 3));
  221. $this->assertSame($mock->aMethod(1, 2, 3), $object);
  222. }
  223. function testSetReturnReferenceGivesOriginalReference() {
  224. $mock = new MockDummy();
  225. $object = 1;
  226. $mock->setReturnReference('aReferenceMethod', $object, array(1, 2, 3));
  227. $this->assertReference($mock->aReferenceMethod(1, 2, 3), $object);
  228. }
  229. function testPatternMatchReturn() {
  230. $mock = new MockDummy();
  231. $mock->setReturnValue(
  232. "aMethod",
  233. "aaa",
  234. array(new PatternExpectation('/hello/i')));
  235. $this->assertIdentical($mock->aMethod('Hello'), "aaa");
  236. $this->assertNull($mock->aMethod('Goodbye'));
  237. }
  238. function testMultipleMethods() {
  239. $mock = new MockDummy();
  240. $mock->setReturnValue("aMethod", 100, array(1));
  241. $mock->setReturnValue("aMethod", 200, array(2));
  242. $mock->setReturnValue("anotherMethod", 10, array(1));
  243. $mock->setReturnValue("anotherMethod", 20, array(2));
  244. $this->assertIdentical($mock->aMethod(1), 100);
  245. $this->assertIdentical($mock->anotherMethod(1), 10);
  246. $this->assertIdentical($mock->aMethod(2), 200);
  247. $this->assertIdentical($mock->anotherMethod(2), 20);
  248. }
  249. function testReturnSequence() {
  250. $mock = new MockDummy();
  251. $mock->setReturnValueAt(0, "aMethod", "aaa");
  252. $mock->setReturnValueAt(1, "aMethod", "bbb");
  253. $mock->setReturnValueAt(3, "aMethod", "ddd");
  254. $this->assertIdentical($mock->aMethod(), "aaa");
  255. $this->assertIdentical($mock->aMethod(), "bbb");
  256. $this->assertNull($mock->aMethod());
  257. $this->assertIdentical($mock->aMethod(), "ddd");
  258. }
  259. function testSetReturnReferenceAtGivesOriginal() {
  260. $mock = new MockDummy();
  261. $object = 100;
  262. $mock->setReturnReferenceAt(1, "aReferenceMethod", $object);
  263. $this->assertNull($mock->aReferenceMethod());
  264. $this->assertReference($mock->aReferenceMethod(), $object);
  265. $this->assertNull($mock->aReferenceMethod());
  266. }
  267. function testReturnsAtGivesOriginalObjectHandle() {
  268. $mock = new MockDummy();
  269. $object = new Dummy();
  270. $mock->returnsAt(1, "aMethod", $object);
  271. $this->assertNull($mock->aMethod());
  272. $this->assertSame($mock->aMethod(), $object);
  273. $this->assertNull($mock->aMethod());
  274. }
  275. function testComplicatedReturnSequence() {
  276. $mock = new MockDummy();
  277. $object = new Dummy();
  278. $mock->returnsAt(1, "aMethod", "aaa", array("a"));
  279. $mock->returnsAt(1, "aMethod", "bbb");
  280. $mock->returnsAt(2, "aMethod", $object, array('*', 2));
  281. $mock->returnsAt(2, "aMethod", "value", array('*', 3));
  282. $mock->returns("aMethod", 3, array(3));
  283. $this->assertNull($mock->aMethod());
  284. $this->assertEqual($mock->aMethod("a"), "aaa");
  285. $this->assertSame($mock->aMethod(1, 2), $object);
  286. $this->assertEqual($mock->aMethod(3), 3);
  287. $this->assertNull($mock->aMethod());
  288. }
  289. function testMultipleMethodSequences() {
  290. $mock = new MockDummy();
  291. $mock->setReturnValueAt(0, "aMethod", "aaa");
  292. $mock->setReturnValueAt(1, "aMethod", "bbb");
  293. $mock->setReturnValueAt(0, "anotherMethod", "ccc");
  294. $mock->setReturnValueAt(1, "anotherMethod", "ddd");
  295. $this->assertIdentical($mock->aMethod(), "aaa");
  296. $this->assertIdentical($mock->anotherMethod(), "ccc");
  297. $this->assertIdentical($mock->aMethod(), "bbb");
  298. $this->assertIdentical($mock->anotherMethod(), "ddd");
  299. }
  300. function testSequenceFallback() {
  301. $mock = new MockDummy();
  302. $mock->setReturnValueAt(0, "aMethod", "aaa", array('a'));
  303. $mock->setReturnValueAt(1, "aMethod", "bbb", array('a'));
  304. $mock->setReturnValue("aMethod", "AAA");
  305. $this->assertIdentical($mock->aMethod('a'), "aaa");
  306. $this->assertIdentical($mock->aMethod('b'), "AAA");
  307. }
  308. function testMethodInterference() {
  309. $mock = new MockDummy();
  310. $mock->setReturnValueAt(0, "anotherMethod", "aaa");
  311. $mock->setReturnValue("aMethod", "AAA");
  312. $this->assertIdentical($mock->aMethod(), "AAA");
  313. $this->assertIdentical($mock->anotherMethod(), "aaa");
  314. }
  315. }
  316. class TestOfMockExpectationsThatPass extends UnitTestCase {
  317. function testAnyArgument() {
  318. $mock = new MockDummy();
  319. $mock->expect('aMethod', array('*'));
  320. $mock->aMethod(1);
  321. $mock->aMethod('hello');
  322. }
  323. function testAnyTwoArguments() {
  324. $mock = new MockDummy();
  325. $mock->expect('aMethod', array('*', '*'));
  326. $mock->aMethod(1, 2);
  327. }
  328. function testSpecificArgument() {
  329. $mock = new MockDummy();
  330. $mock->expect('aMethod', array(1));
  331. $mock->aMethod(1);
  332. }
  333. function testExpectation() {
  334. $mock = new MockDummy();
  335. $mock->expect('aMethod', array(new IsAExpectation('Dummy')));
  336. $mock->aMethod(new Dummy());
  337. }
  338. function testArgumentsInSequence() {
  339. $mock = new MockDummy();
  340. $mock->expectAt(0, 'aMethod', array(1, 2));
  341. $mock->expectAt(1, 'aMethod', array(3, 4));
  342. $mock->aMethod(1, 2);
  343. $mock->aMethod(3, 4);
  344. }
  345. function testAtLeastOnceSatisfiedByOneCall() {
  346. $mock = new MockDummy();
  347. $mock->expectAtLeastOnce('aMethod');
  348. $mock->aMethod();
  349. }
  350. function testAtLeastOnceSatisfiedByTwoCalls() {
  351. $mock = new MockDummy();
  352. $mock->expectAtLeastOnce('aMethod');
  353. $mock->aMethod();
  354. $mock->aMethod();
  355. }
  356. function testOnceSatisfiedByOneCall() {
  357. $mock = new MockDummy();
  358. $mock->expectOnce('aMethod');
  359. $mock->aMethod();
  360. }
  361. function testMinimumCallsSatisfiedByEnoughCalls() {
  362. $mock = new MockDummy();
  363. $mock->expectMinimumCallCount('aMethod', 1);
  364. $mock->aMethod();
  365. }
  366. function testMinimumCallsSatisfiedByTooManyCalls() {
  367. $mock = new MockDummy();
  368. $mock->expectMinimumCallCount('aMethod', 3);
  369. $mock->aMethod();
  370. $mock->aMethod();
  371. $mock->aMethod();
  372. $mock->aMethod();
  373. }
  374. function testMaximumCallsSatisfiedByEnoughCalls() {
  375. $mock = new MockDummy();
  376. $mock->expectMaximumCallCount('aMethod', 1);
  377. $mock->aMethod();
  378. }
  379. function testMaximumCallsSatisfiedByNoCalls() {
  380. $mock = new MockDummy();
  381. $mock->expectMaximumCallCount('aMethod', 1);
  382. }
  383. }
  384. class MockWithInjectedTestCase extends SimpleMock {
  385. protected function getCurrentTestCase() {
  386. return SimpleTest::getContext()->getTest()->getMockedTest();
  387. }
  388. }
  389. SimpleTest::setMockBaseClass('MockWithInjectedTestCase');
  390. Mock::generate('Dummy', 'MockDummyWithInjectedTestCase');
  391. SimpleTest::setMockBaseClass('SimpleMock');
  392. Mock::generate('SimpleTestCase');
  393. class LikeExpectation extends IdenticalExpectation {
  394. function __construct($expectation) {
  395. $expectation->message = '';
  396. parent::__construct($expectation);
  397. }
  398. function test($compare) {
  399. $compare->message = '';
  400. return parent::test($compare);
  401. }
  402. function testMessage($compare) {
  403. $compare->message = '';
  404. return parent::testMessage($compare);
  405. }
  406. }
  407. class TestOfMockExpectations extends UnitTestCase {
  408. private $test;
  409. function setUp() {
  410. $this->test = new MockSimpleTestCase();
  411. }
  412. function getMockedTest() {
  413. return $this->test;
  414. }
  415. function testSettingExpectationOnNonMethodThrowsError() {
  416. $mock = new MockDummyWithInjectedTestCase();
  417. $this->expectError();
  418. $mock->expectMaximumCallCount('aMissingMethod', 2);
  419. }
  420. function testMaxCallsDetectsOverrun() {
  421. $this->test->expectOnce('assert', array(
  422. new LikeExpectation(new MaximumCallCountExpectation('aMethod', 2)),
  423. 3));
  424. $mock = new MockDummyWithInjectedTestCase();
  425. $mock->expectMaximumCallCount('aMethod', 2);
  426. $mock->aMethod();
  427. $mock->aMethod();
  428. $mock->aMethod();
  429. $mock->mock->atTestEnd('testSomething', $this->test);
  430. }
  431. function testTallyOnMaxCallsSendsPassOnUnderrun() {
  432. $this->test->expectOnce('assert', array(
  433. new LikeExpectation(new MaximumCallCountExpectation('aMethod', 2)),
  434. 2));
  435. $mock = new MockDummyWithInjectedTestCase();
  436. $mock->expectMaximumCallCount("aMethod", 2);
  437. $mock->aMethod();
  438. $mock->aMethod();
  439. $mock->mock->atTestEnd('testSomething', $this->test);
  440. }
  441. function testExpectNeverDetectsOverrun() {
  442. $this->test->expectOnce('assert', array(
  443. new LikeExpectation(new MaximumCallCountExpectation('aMethod', 0)),
  444. 1));
  445. $mock = new MockDummyWithInjectedTestCase();
  446. $mock->expectNever('aMethod');
  447. $mock->aMethod();
  448. $mock->mock->atTestEnd('testSomething', $this->test);
  449. }
  450. function testTallyOnExpectNeverStillSendsPassOnUnderrun() {
  451. $this->test->expectOnce('assert', array(
  452. new LikeExpectation(new MaximumCallCountExpectation('aMethod', 0)),
  453. 0));
  454. $mock = new MockDummyWithInjectedTestCase();
  455. $mock->expectNever('aMethod');
  456. $mock->mock->atTestEnd('testSomething', $this->test);
  457. }
  458. function testMinCalls() {
  459. $this->test->expectOnce('assert', array(
  460. new LikeExpectation(new MinimumCallCountExpectation('aMethod', 2)),
  461. 2));
  462. $mock = new MockDummyWithInjectedTestCase();
  463. $mock->expectMinimumCallCount('aMethod', 2);
  464. $mock->aMethod();
  465. $mock->aMethod();
  466. $mock->mock->atTestEnd('testSomething', $this->test);
  467. }
  468. function testFailedNever() {
  469. $this->test->expectOnce('assert', array(
  470. new LikeExpectation(new MaximumCallCountExpectation('aMethod', 0)),
  471. 1));
  472. $mock = new MockDummyWithInjectedTestCase();
  473. $mock->expectNever('aMethod');
  474. $mock->aMethod();
  475. $mock->mock->atTestEnd('testSomething', $this->test);
  476. }
  477. function testUnderOnce() {
  478. $this->test->expectOnce('assert', array(
  479. new LikeExpectation(new CallCountExpectation('aMethod', 1)),
  480. 0));
  481. $mock = new MockDummyWithInjectedTestCase();
  482. $mock->expectOnce('aMethod');
  483. $mock->mock->atTestEnd('testSomething', $this->test);
  484. }
  485. function testOverOnce() {
  486. $this->test->expectOnce('assert', array(
  487. new LikeExpectation(new CallCountExpectation('aMethod', 1)),
  488. 2));
  489. $mock = new MockDummyWithInjectedTestCase();
  490. $mock->expectOnce('aMethod');
  491. $mock->aMethod();
  492. $mock->aMethod();
  493. $mock->mock->atTestEnd('testSomething', $this->test);
  494. }
  495. function testUnderAtLeastOnce() {
  496. $this->test->expectOnce('assert', array(
  497. new LikeExpectation(new MinimumCallCountExpectation('aMethod', 1)),
  498. 0));
  499. $mock = new MockDummyWithInjectedTestCase();
  500. $mock->expectAtLeastOnce("aMethod");
  501. $mock->mock->atTestEnd('testSomething', $this->test);
  502. }
  503. function testZeroArguments() {
  504. $this->test->expectOnce('assert', array(
  505. new LikeExpectation(new ParametersExpectation(array())),
  506. array(),
  507. '*'));
  508. $mock = new MockDummyWithInjectedTestCase();
  509. $mock->expect("aMethod", array());
  510. $mock->aMethod();
  511. $mock->mock->atTestEnd('testSomething', $this->test);
  512. }
  513. function testExpectedArguments() {
  514. $this->test->expectOnce('assert', array(
  515. new LikeExpectation(new ParametersExpectation(array(1, 2, 3))),
  516. array(1, 2, 3),
  517. '*'));
  518. $mock = new MockDummyWithInjectedTestCase();
  519. $mock->expect('aMethod', array(1, 2, 3));
  520. $mock->aMethod(1, 2, 3);
  521. $mock->mock->atTestEnd('testSomething', $this->test);
  522. }
  523. function testFailedArguments() {
  524. $this->test->expectOnce('assert', array(
  525. new LikeExpectation(new ParametersExpectation(array('this'))),
  526. array('that'),
  527. '*'));
  528. $mock = new MockDummyWithInjectedTestCase();
  529. $mock->expect('aMethod', array('this'));
  530. $mock->aMethod('that');
  531. $mock->mock->atTestEnd('testSomething', $this->test);
  532. }
  533. function testWildcardsAreTranslatedToAnythingExpectations() {
  534. $this->test->expectOnce('assert', array(
  535. new LikeExpectation(new ParametersExpectation(array(
  536. new AnythingExpectation(), 123, new AnythingExpectation()))),
  537. array(100, 123, 101),
  538. '*'));
  539. $mock = new MockDummyWithInjectedTestCase($this);
  540. $mock->expect("aMethod", array('*', 123, '*'));
  541. $mock->aMethod(100, 123, 101);
  542. $mock->mock->atTestEnd('testSomething', $this->test);
  543. }
  544. function testSpecificPassingSequence() {
  545. $this->test->expectAt(0, 'assert', array(
  546. new LikeExpectation(new ParametersExpectation(array(1, 2, 3))),
  547. array(1, 2, 3),
  548. '*'));
  549. $this->test->expectAt(1, 'assert', array(
  550. new LikeExpectation(new ParametersExpectation(array('Hello'))),
  551. array('Hello'),
  552. '*'));
  553. $mock = new MockDummyWithInjectedTestCase();
  554. $mock->expectAt(1, 'aMethod', array(1, 2, 3));
  555. $mock->expectAt(2, 'aMethod', array('Hello'));
  556. $mock->aMethod();
  557. $mock->aMethod(1, 2, 3);
  558. $mock->aMethod('Hello');
  559. $mock->aMethod();
  560. $mock->mock->atTestEnd('testSomething', $this->test);
  561. }
  562. function testNonArrayForExpectedParametersGivesError() {
  563. $mock = new MockDummyWithInjectedTestCase();
  564. $this->expectError(new PatternExpectation('/\$args.*not an array/i'));
  565. $mock->expect("aMethod", "foo");
  566. $mock->aMethod();
  567. $mock->mock->atTestEnd('testSomething', $this->test);
  568. }
  569. }
  570. class TestOfMockComparisons extends UnitTestCase {
  571. function testEqualComparisonOfMocksDoesNotCrash() {
  572. $expectation = new EqualExpectation(new MockDummy());
  573. $this->assertTrue($expectation->test(new MockDummy(), true));
  574. }
  575. function testIdenticalComparisonOfMocksDoesNotCrash() {
  576. $expectation = new IdenticalExpectation(new MockDummy());
  577. $this->assertTrue($expectation->test(new MockDummy()));
  578. }
  579. }
  580. class ClassWithSpecialMethods {
  581. function __get($name) { }
  582. function __set($name, $value) { }
  583. function __isset($name) { }
  584. function __unset($name) { }
  585. function __call($method, $arguments) { }
  586. function __toString() { }
  587. }
  588. Mock::generate('ClassWithSpecialMethods');
  589. class TestOfSpecialMethodsAfterPHP51 extends UnitTestCase {
  590. function skip() {
  591. $this->skipIf(version_compare(phpversion(), '5.1', '<'), '__isset and __unset overloading not tested unless PHP 5.1+');
  592. }
  593. function testCanEmulateIsset() {
  594. $mock = new MockClassWithSpecialMethods();
  595. $mock->setReturnValue('__isset', true);
  596. $this->assertIdentical(isset($mock->a), true);
  597. }
  598. function testCanExpectUnset() {
  599. $mock = new MockClassWithSpecialMethods();
  600. $mock->expectOnce('__unset', array('a'));
  601. unset($mock->a);
  602. }
  603. }
  604. class TestOfSpecialMethods extends UnitTestCase {
  605. function skip() {
  606. $this->skipIf(version_compare(phpversion(), '5', '<'), 'Overloading not tested unless PHP 5+');
  607. }
  608. function testCanMockTheThingAtAll() {
  609. $mock = new MockClassWithSpecialMethods();
  610. }
  611. function testReturnFromSpecialAccessor() {
  612. $mock = new MockClassWithSpecialMethods();
  613. $mock->setReturnValue('__get', '1st Return', array('first'));
  614. $mock->setReturnValue('__get', '2nd Return', array('second'));
  615. $this->assertEqual($mock->first, '1st Return');
  616. $this->assertEqual($mock->second, '2nd Return');
  617. }
  618. function testcanExpectTheSettingOfValue() {
  619. $mock = new MockClassWithSpecialMethods();
  620. $mock->expectOnce('__set', array('a', 'A'));
  621. $mock->a = 'A';
  622. }
  623. function testCanSimulateAnOverloadmethod() {
  624. $mock = new MockClassWithSpecialMethods();
  625. $mock->expectOnce('__call', array('amOverloaded', array('A')));
  626. $mock->setReturnValue('__call', 'aaa');
  627. $this->assertIdentical($mock->amOverloaded('A'), 'aaa');
  628. }
  629. function testToStringMagic() {
  630. $mock = new MockClassWithSpecialMethods();
  631. $mock->expectOnce('__toString');
  632. $mock->setReturnValue('__toString', 'AAA');
  633. ob_start();
  634. print $mock;
  635. $output = ob_get_contents();
  636. ob_end_clean();
  637. $this->assertEqual($output, 'AAA');
  638. }
  639. }
  640. class WithStaticMethod {
  641. static function aStaticMethod() { }
  642. }
  643. Mock::generate('WithStaticMethod');
  644. class TestOfMockingClassesWithStaticMethods extends UnitTestCase {
  645. function testStaticMethodIsMockedAsStatic() {
  646. $mock = new WithStaticMethod();
  647. $reflection = new ReflectionClass($mock);
  648. $method = $reflection->getMethod('aStaticMethod');
  649. $this->assertTrue($method->isStatic());
  650. }
  651. }
  652. class MockTestException extends Exception { }
  653. class TestOfThrowingExceptionsFromMocks extends UnitTestCase {
  654. function testCanThrowOnMethodCall() {
  655. $mock = new MockDummy();
  656. $mock->throwOn('aMethod');
  657. $this->expectException();
  658. $mock->aMethod();
  659. }
  660. function testCanThrowSpecificExceptionOnMethodCall() {
  661. $mock = new MockDummy();
  662. $mock->throwOn('aMethod', new MockTestException());
  663. $this->expectException();
  664. $mock->aMethod();
  665. }
  666. function testThrowsOnlyWhenCallSignatureMatches() {
  667. $mock = new MockDummy();
  668. $mock->throwOn('aMethod', new MockTestException(), array(3));
  669. $mock->aMethod(1);
  670. $mock->aMethod(2);
  671. $this->expectException();
  672. $mock->aMethod(3);
  673. }
  674. function testCanThrowOnParticularInvocation() {
  675. $mock = new MockDummy();
  676. $mock->throwAt(2, 'aMethod', new MockTestException());
  677. $mock->aMethod();
  678. $mock->aMethod();
  679. $this->expectException();
  680. $mock->aMethod();
  681. }
  682. }
  683. class TestOfThrowingErrorsFromMocks extends UnitTestCase {
  684. function testCanGenerateErrorFromMethodCall() {
  685. $mock = new MockDummy();
  686. $mock->errorOn('aMethod', 'Ouch!');
  687. $this->expectError('Ouch!');
  688. $mock->aMethod();
  689. }
  690. function testGeneratesErrorOnlyWhenCallSignatureMatches() {
  691. $mock = new MockDummy();
  692. $mock->errorOn('aMethod', 'Ouch!', array(3));
  693. $mock->aMethod(1);
  694. $mock->aMethod(2);
  695. $this->expectError();
  696. $mock->aMethod(3);
  697. }
  698. function testCanGenerateErrorOnParticularInvocation() {
  699. $mock = new MockDummy();
  700. $mock->errorAt(2, 'aMethod', 'Ouch!');
  701. $mock->aMethod();
  702. $mock->aMethod();
  703. $this->expectError();
  704. $mock->aMethod();
  705. }
  706. }
  707. Mock::generatePartial('Dummy', 'TestDummy', array('anotherMethod', 'aReferenceMethod'));
  708. class TestOfPartialMocks extends UnitTestCase {
  709. function testMethodReplacementWithNoBehaviourReturnsNull() {
  710. $mock = new TestDummy();
  711. $this->assertEqual($mock->aMethod(99), 99);
  712. $this->assertNull($mock->anotherMethod());
  713. }
  714. function testSettingReturns() {
  715. $mock = new TestDummy();
  716. $mock->setReturnValue('anotherMethod', 33, array(3));
  717. $mock->setReturnValue('anotherMethod', 22);
  718. $mock->setReturnValueAt(2, 'anotherMethod', 44, array(3));
  719. $this->assertEqual($mock->anotherMethod(), 22);
  720. $this->assertEqual($mock->anotherMethod(3), 33);
  721. $this->assertEqual($mock->anotherMethod(3), 44);
  722. }
  723. function testSetReturnReferenceGivesOriginal() {
  724. $mock = new TestDummy();
  725. $object = 99;
  726. $mock->setReturnReferenceAt(0, 'aReferenceMethod', $object, array(3));
  727. $this->assertReference($mock->aReferenceMethod(3), $object);
  728. }
  729. function testReturnsAtGivesOriginalObjectHandle() {
  730. $mock = new TestDummy();
  731. $object = new Dummy();
  732. $mock->returnsAt(0, 'anotherMethod', $object, array(3));
  733. $this->assertSame($mock->anotherMethod(3), $object);
  734. }
  735. function testExpectations() {
  736. $mock = new TestDummy();
  737. $mock->expectCallCount('anotherMethod', 2);
  738. $mock->expect('anotherMethod', array(77));
  739. $mock->expectAt(1, 'anotherMethod', array(66));
  740. $mock->anotherMethod(77);
  741. $mock->anotherMethod(66);
  742. }
  743. function testSettingExpectationOnMissingMethodThrowsError() {
  744. $mock = new TestDummy();
  745. $this->expectError();
  746. $mock->expectCallCount('aMissingMethod', 2);
  747. }
  748. }
  749. class ConstructorSuperClass {
  750. function ConstructorSuperClass() { }
  751. }
  752. class ConstructorSubClass extends ConstructorSuperClass { }
  753. class TestOfPHP4StyleSuperClassConstruct extends UnitTestCase {
  754. function testBasicConstruct() {
  755. Mock::generate('ConstructorSubClass');
  756. $mock = new MockConstructorSubClass();
  757. $this->assertIsA($mock, 'ConstructorSubClass');
  758. $this->assertTrue(method_exists($mock, 'ConstructorSuperClass'));
  759. }
  760. }
  761. class TestOfPHP5StaticMethodMocking extends UnitTestCase {
  762. function testCanCreateAMockObjectWithStaticMethodsWithoutError() {
  763. eval('
  764. class SimpleObjectContainingStaticMethod {
  765. static function someStatic() { }
  766. }
  767. ');
  768. Mock::generate('SimpleObjectContainingStaticMethod');
  769. }
  770. }
  771. class TestOfPHP5AbstractMethodMocking extends UnitTestCase {
  772. function testCanCreateAMockObjectFromAnAbstractWithProperFunctionDeclarations() {
  773. eval('
  774. abstract class SimpleAbstractClassContainingAbstractMethods {
  775. abstract function anAbstract();
  776. abstract function anAbstractWithParameter($foo);
  777. abstract function anAbstractWithMultipleParameters($foo, $bar);
  778. }
  779. ');
  780. Mock::generate('SimpleAbstractClassContainingAbstractMethods');
  781. $this->assertTrue(
  782. method_exists(
  783. // Testing with class name alone does not work in PHP 5.0
  784. new MockSimpleAbstractClassContainingAbstractMethods,
  785. 'anAbstract'
  786. )
  787. );
  788. $this->assertTrue(
  789. method_exists(
  790. new MockSimpleAbstractClassContainingAbstractMethods,
  791. 'anAbstractWithParameter'
  792. )
  793. );
  794. $this->assertTrue(
  795. method_exists(
  796. new MockSimpleAbstractClassContainingAbstractMethods,
  797. 'anAbstractWithMultipleParameters'
  798. )
  799. );
  800. }
  801. function testMethodsDefinedAsAbstractInParentShouldHaveFullSignature() {
  802. eval('
  803. abstract class SimpleParentAbstractClassContainingAbstractMethods {
  804. abstract function anAbstract();
  805. abstract function anAbstractWithParameter($foo);
  806. abstract function anAbstractWithMultipleParameters($foo, $bar);
  807. }
  808. class SimpleChildAbstractClassContainingAbstractMethods extends SimpleParentAbstractClassContainingAbstractMethods {
  809. function anAbstract(){}
  810. function anAbstractWithParameter($foo){}
  811. function anAbstractWithMultipleParameters($foo, $bar){}
  812. }
  813. class EvenDeeperEmptyChildClass extends SimpleChildAbstractClassContainingAbstractMethods {}
  814. ');
  815. Mock::generate('SimpleChildAbstractClassContainingAbstractMethods');
  816. $this->assertTrue(
  817. method_exists(
  818. new MockSimpleChildAbstractClassContainingAbstractMethods,
  819. 'anAbstract'
  820. )
  821. );
  822. $this->assertTrue(
  823. method_exists(
  824. new MockSimpleChildAbstractClassContainingAbstractMethods,
  825. 'anAbstractWithParameter'
  826. )
  827. );
  828. $this->assertTrue(
  829. method_exists(
  830. new MockSimpleChildAbstractClassContainingAbstractMethods,
  831. 'anAbstractWithMultipleParameters'
  832. )
  833. );
  834. Mock::generate('EvenDeeperEmptyChildClass');
  835. $this->assertTrue(
  836. method_exists(
  837. new MockEvenDeeperEmptyChildClass,
  838. 'anAbstract'
  839. )
  840. );
  841. $this->assertTrue(
  842. method_exists(
  843. new MockEvenDeeperEmptyChildClass,
  844. 'anAbstractWithParameter'
  845. )
  846. );
  847. $this->assertTrue(
  848. method_exists(
  849. new MockEvenDeeperEmptyChildClass,
  850. 'anAbstractWithMultipleParameters'
  851. )
  852. );
  853. }
  854. }
  855. class DummyWithProtected
  856. {
  857. public function aMethodCallsProtected() { return $this->aProtectedMethod(); }
  858. protected function aProtectedMethod() { return true; }
  859. }
  860. Mock::generatePartial('DummyWithProtected', 'TestDummyWithProtected', array('aProtectedMethod'));
  861. class TestOfProtectedMethodPartialMocks extends UnitTestCase
  862. {
  863. function testProtectedMethodExists() {
  864. $this->assertTrue(
  865. method_exists(
  866. new TestDummyWithProtected,
  867. 'aProtectedMethod'
  868. )
  869. );
  870. }
  871. function testProtectedMethodIsCalled() {
  872. $object = new DummyWithProtected();
  873. $this->assertTrue($object->aMethodCallsProtected(), 'ensure original was called');
  874. }
  875. function testMockedMethodIsCalled() {
  876. $object = new TestDummyWithProtected();
  877. $object->setReturnValue('aProtectedMethod', false);
  878. $this->assertFalse($object->aMethodCallsProtected());
  879. }
  880. }
  881. ?>