Drupal investigation

MockObjectTest.php 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. <?php
  2. /*
  3. * This file is part of the PHPUnit_MockObject package.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. *
  12. *
  13. * @since Class available since Release 3.0.0
  14. */
  15. class Framework_MockObjectTest extends PHPUnit_Framework_TestCase
  16. {
  17. public function testMockedMethodIsNeverCalled()
  18. {
  19. $mock = $this->getMock('AnInterface');
  20. $mock->expects($this->never())
  21. ->method('doSomething');
  22. }
  23. public function testMockedMethodIsNeverCalledWithParameter()
  24. {
  25. $mock = $this->getMock('SomeClass');
  26. $mock->expects($this->never())
  27. ->method('doSomething')
  28. ->with('someArg');
  29. }
  30. public function testMockedMethodIsNotCalledWhenExpectsAnyWithParameter()
  31. {
  32. $mock = $this->getMock('SomeClass');
  33. $mock->expects($this->any())
  34. ->method('doSomethingElse')
  35. ->with('someArg');
  36. }
  37. public function testMockedMethodIsNotCalledWhenMethodSpecifiedDirectlyWithParameter()
  38. {
  39. $mock = $this->getMock('SomeClass');
  40. $mock->method('doSomethingElse')
  41. ->with('someArg');
  42. }
  43. public function testMockedMethodIsCalledAtLeastOnce()
  44. {
  45. $mock = $this->getMock('AnInterface');
  46. $mock->expects($this->atLeastOnce())
  47. ->method('doSomething');
  48. $mock->doSomething();
  49. }
  50. public function testMockedMethodIsCalledAtLeastOnce2()
  51. {
  52. $mock = $this->getMock('AnInterface');
  53. $mock->expects($this->atLeastOnce())
  54. ->method('doSomething');
  55. $mock->doSomething();
  56. $mock->doSomething();
  57. }
  58. public function testMockedMethodIsCalledAtLeastTwice()
  59. {
  60. $mock = $this->getMock('AnInterface');
  61. $mock->expects($this->atLeast(2))
  62. ->method('doSomething');
  63. $mock->doSomething();
  64. $mock->doSomething();
  65. }
  66. public function testMockedMethodIsCalledAtLeastTwice2()
  67. {
  68. $mock = $this->getMock('AnInterface');
  69. $mock->expects($this->atLeast(2))
  70. ->method('doSomething');
  71. $mock->doSomething();
  72. $mock->doSomething();
  73. $mock->doSomething();
  74. }
  75. public function testMockedMethodIsCalledAtMostTwice()
  76. {
  77. $mock = $this->getMock('AnInterface');
  78. $mock->expects($this->atMost(2))
  79. ->method('doSomething');
  80. $mock->doSomething();
  81. $mock->doSomething();
  82. }
  83. public function testMockedMethodIsCalledAtMosttTwice2()
  84. {
  85. $mock = $this->getMock('AnInterface');
  86. $mock->expects($this->atMost(2))
  87. ->method('doSomething');
  88. $mock->doSomething();
  89. }
  90. public function testMockedMethodIsCalledOnce()
  91. {
  92. $mock = $this->getMock('AnInterface');
  93. $mock->expects($this->once())
  94. ->method('doSomething');
  95. $mock->doSomething();
  96. }
  97. public function testMockedMethodIsCalledOnceWithParameter()
  98. {
  99. $mock = $this->getMock('SomeClass');
  100. $mock->expects($this->once())
  101. ->method('doSomethingElse')
  102. ->with($this->equalTo('something'));
  103. $mock->doSomethingElse('something');
  104. }
  105. public function testMockedMethodIsCalledExactly()
  106. {
  107. $mock = $this->getMock('AnInterface');
  108. $mock->expects($this->exactly(2))
  109. ->method('doSomething');
  110. $mock->doSomething();
  111. $mock->doSomething();
  112. }
  113. public function testStubbedException()
  114. {
  115. $mock = $this->getMock('AnInterface');
  116. $mock->expects($this->any())
  117. ->method('doSomething')
  118. ->will($this->throwException(new Exception));
  119. try {
  120. $mock->doSomething();
  121. } catch (Exception $e) {
  122. return;
  123. }
  124. $this->fail();
  125. }
  126. public function testStubbedWillThrowException()
  127. {
  128. $mock = $this->getMock('AnInterface');
  129. $mock->expects($this->any())
  130. ->method('doSomething')
  131. ->willThrowException(new Exception);
  132. try {
  133. $mock->doSomething();
  134. } catch (Exception $e) {
  135. return;
  136. }
  137. $this->fail();
  138. }
  139. public function testStubbedReturnValue()
  140. {
  141. $mock = $this->getMock('AnInterface');
  142. $mock->expects($this->any())
  143. ->method('doSomething')
  144. ->will($this->returnValue('something'));
  145. $this->assertEquals('something', $mock->doSomething());
  146. $mock = $this->getMock('AnInterface');
  147. $mock->expects($this->any())
  148. ->method('doSomething')
  149. ->willReturn('something');
  150. $this->assertEquals('something', $mock->doSomething());
  151. }
  152. public function testStubbedReturnValueMap()
  153. {
  154. $map = array(
  155. array('a', 'b', 'c', 'd'),
  156. array('e', 'f', 'g', 'h')
  157. );
  158. $mock = $this->getMock('AnInterface');
  159. $mock->expects($this->any())
  160. ->method('doSomething')
  161. ->will($this->returnValueMap($map));
  162. $this->assertEquals('d', $mock->doSomething('a', 'b', 'c'));
  163. $this->assertEquals('h', $mock->doSomething('e', 'f', 'g'));
  164. $this->assertEquals(null, $mock->doSomething('foo', 'bar'));
  165. $mock = $this->getMock('AnInterface');
  166. $mock->expects($this->any())
  167. ->method('doSomething')
  168. ->willReturnMap($map);
  169. $this->assertEquals('d', $mock->doSomething('a', 'b', 'c'));
  170. $this->assertEquals('h', $mock->doSomething('e', 'f', 'g'));
  171. $this->assertEquals(null, $mock->doSomething('foo', 'bar'));
  172. }
  173. public function testStubbedReturnArgument()
  174. {
  175. $mock = $this->getMock('AnInterface');
  176. $mock->expects($this->any())
  177. ->method('doSomething')
  178. ->will($this->returnArgument(1));
  179. $this->assertEquals('b', $mock->doSomething('a', 'b'));
  180. $mock = $this->getMock('AnInterface');
  181. $mock->expects($this->any())
  182. ->method('doSomething')
  183. ->willReturnArgument(1);
  184. $this->assertEquals('b', $mock->doSomething('a', 'b'));
  185. }
  186. public function testFunctionCallback()
  187. {
  188. $mock = $this->getMock('SomeClass', array('doSomething'), array(), '', false);
  189. $mock->expects($this->once())
  190. ->method('doSomething')
  191. ->will($this->returnCallback('functionCallback'));
  192. $this->assertEquals('pass', $mock->doSomething('foo', 'bar'));
  193. $mock = $this->getMock('SomeClass', array('doSomething'), array(), '', false);
  194. $mock->expects($this->once())
  195. ->method('doSomething')
  196. ->willReturnCallback('functionCallback');
  197. $this->assertEquals('pass', $mock->doSomething('foo', 'bar'));
  198. }
  199. public function testStubbedReturnSelf()
  200. {
  201. $mock = $this->getMock('AnInterface');
  202. $mock->expects($this->any())
  203. ->method('doSomething')
  204. ->will($this->returnSelf());
  205. $this->assertEquals($mock, $mock->doSomething());
  206. $mock = $this->getMock('AnInterface');
  207. $mock->expects($this->any())
  208. ->method('doSomething')
  209. ->willReturnSelf();
  210. $this->assertEquals($mock, $mock->doSomething());
  211. }
  212. public function testStubbedReturnOnConsecutiveCalls()
  213. {
  214. $mock = $this->getMock('AnInterface');
  215. $mock->expects($this->any())
  216. ->method('doSomething')
  217. ->will($this->onConsecutiveCalls('a', 'b', 'c'));
  218. $this->assertEquals('a', $mock->doSomething());
  219. $this->assertEquals('b', $mock->doSomething());
  220. $this->assertEquals('c', $mock->doSomething());
  221. $mock = $this->getMock('AnInterface');
  222. $mock->expects($this->any())
  223. ->method('doSomething')
  224. ->willReturnOnConsecutiveCalls('a', 'b', 'c');
  225. $this->assertEquals('a', $mock->doSomething());
  226. $this->assertEquals('b', $mock->doSomething());
  227. $this->assertEquals('c', $mock->doSomething());
  228. }
  229. public function testStaticMethodCallback()
  230. {
  231. $mock = $this->getMock('SomeClass', array('doSomething'), array(), '', false);
  232. $mock->expects($this->once())
  233. ->method('doSomething')
  234. ->will($this->returnCallback(array('MethodCallback', 'staticCallback')));
  235. $this->assertEquals('pass', $mock->doSomething('foo', 'bar'));
  236. }
  237. public function testPublicMethodCallback()
  238. {
  239. $mock = $this->getMock('SomeClass', array('doSomething'), array(), '', false);
  240. $mock->expects($this->once())
  241. ->method('doSomething')
  242. ->will($this->returnCallback(array(new MethodCallback, 'nonStaticCallback')));
  243. $this->assertEquals('pass', $mock->doSomething('foo', 'bar'));
  244. }
  245. public function testMockClassOnlyGeneratedOnce()
  246. {
  247. $mock1 = $this->getMock('AnInterface');
  248. $mock2 = $this->getMock('AnInterface');
  249. $this->assertEquals(get_class($mock1), get_class($mock2));
  250. }
  251. public function testMockClassDifferentForPartialMocks()
  252. {
  253. $mock1 = $this->getMock('PartialMockTestClass');
  254. $mock2 = $this->getMock('PartialMockTestClass', array('doSomething'));
  255. $mock3 = $this->getMock('PartialMockTestClass', array('doSomething'));
  256. $mock4 = $this->getMock('PartialMockTestClass', array('doAnotherThing'));
  257. $mock5 = $this->getMock('PartialMockTestClass', array('doAnotherThing'));
  258. $this->assertNotEquals(get_class($mock1), get_class($mock2));
  259. $this->assertNotEquals(get_class($mock1), get_class($mock3));
  260. $this->assertNotEquals(get_class($mock1), get_class($mock4));
  261. $this->assertNotEquals(get_class($mock1), get_class($mock5));
  262. $this->assertEquals(get_class($mock2), get_class($mock3));
  263. $this->assertNotEquals(get_class($mock2), get_class($mock4));
  264. $this->assertNotEquals(get_class($mock2), get_class($mock5));
  265. $this->assertEquals(get_class($mock4), get_class($mock5));
  266. }
  267. public function testMockClassStoreOverrulable()
  268. {
  269. $mock1 = $this->getMock('PartialMockTestClass');
  270. $mock2 = $this->getMock('PartialMockTestClass', array(), array(), 'MyMockClassNameForPartialMockTestClass1');
  271. $mock3 = $this->getMock('PartialMockTestClass');
  272. $mock4 = $this->getMock('PartialMockTestClass', array('doSomething'), array(), 'AnotherMockClassNameForPartialMockTestClass');
  273. $mock5 = $this->getMock('PartialMockTestClass', array(), array(), 'MyMockClassNameForPartialMockTestClass2');
  274. $this->assertNotEquals(get_class($mock1), get_class($mock2));
  275. $this->assertEquals(get_class($mock1), get_class($mock3));
  276. $this->assertNotEquals(get_class($mock1), get_class($mock4));
  277. $this->assertNotEquals(get_class($mock2), get_class($mock3));
  278. $this->assertNotEquals(get_class($mock2), get_class($mock4));
  279. $this->assertNotEquals(get_class($mock2), get_class($mock5));
  280. $this->assertNotEquals(get_class($mock3), get_class($mock4));
  281. $this->assertNotEquals(get_class($mock3), get_class($mock5));
  282. $this->assertNotEquals(get_class($mock4), get_class($mock5));
  283. }
  284. /**
  285. * @covers PHPUnit_Framework_MockObject_Generator::getMock
  286. */
  287. public function testGetMockWithFixedClassNameCanProduceTheSameMockTwice()
  288. {
  289. $mock = $this->getMockBuilder('StdClass')->setMockClassName('FixedName')->getMock();
  290. $mock = $this->getMockBuilder('StdClass')->setMockClassName('FixedName')->getMock();
  291. $this->assertInstanceOf('StdClass', $mock);
  292. }
  293. public function testOriginalConstructorSettingConsidered()
  294. {
  295. $mock1 = $this->getMock('PartialMockTestClass');
  296. $mock2 = $this->getMock('PartialMockTestClass', array(), array(), '', false);
  297. $this->assertTrue($mock1->constructorCalled);
  298. $this->assertFalse($mock2->constructorCalled);
  299. }
  300. public function testOriginalCloneSettingConsidered()
  301. {
  302. $mock1 = $this->getMock('PartialMockTestClass');
  303. $mock2 = $this->getMock('PartialMockTestClass', array(), array(), '', true, false);
  304. $this->assertNotEquals(get_class($mock1), get_class($mock2));
  305. }
  306. public function testGetMockForAbstractClass()
  307. {
  308. $mock = $this->getMock('AbstractMockTestClass');
  309. $mock->expects($this->never())
  310. ->method('doSomething');
  311. }
  312. public function traversableProvider()
  313. {
  314. return array(
  315. array('Traversable'),
  316. array('\Traversable'),
  317. array('TraversableMockTestInterface'),
  318. array(array('Traversable')),
  319. array(array('Iterator','Traversable')),
  320. array(array('\Iterator','\Traversable'))
  321. );
  322. }
  323. /**
  324. * @dataProvider traversableProvider
  325. */
  326. public function testGetMockForTraversable($type)
  327. {
  328. $mock = $this->getMock($type);
  329. $this->assertInstanceOf('Traversable', $mock);
  330. }
  331. public function testMultipleInterfacesCanBeMockedInSingleObject()
  332. {
  333. $mock = $this->getMock(array('AnInterface', 'AnotherInterface'));
  334. $this->assertInstanceOf('AnInterface', $mock);
  335. $this->assertInstanceOf('AnotherInterface', $mock);
  336. }
  337. /**
  338. * @requires PHP 5.4.0
  339. */
  340. public function testGetMockForTrait()
  341. {
  342. $mock = $this->getMockForTrait('AbstractTrait');
  343. $mock->expects($this->never())->method('doSomething');
  344. $parent = get_parent_class($mock);
  345. $traits = class_uses($parent, false);
  346. $this->assertContains('AbstractTrait', $traits);
  347. }
  348. public function testClonedMockObjectShouldStillEqualTheOriginal()
  349. {
  350. $a = $this->getMock('stdClass');
  351. $b = clone $a;
  352. $this->assertEquals($a, $b);
  353. }
  354. public function testMockObjectsConstructedIndepentantlyShouldBeEqual()
  355. {
  356. $a = $this->getMock('stdClass');
  357. $b = $this->getMock('stdClass');
  358. $this->assertEquals($a, $b);
  359. }
  360. public function testMockObjectsConstructedIndepentantlyShouldNotBeTheSame()
  361. {
  362. $a = $this->getMock('stdClass');
  363. $b = $this->getMock('stdClass');
  364. $this->assertNotSame($a, $b);
  365. }
  366. public function testClonedMockObjectCanBeUsedInPlaceOfOriginalOne()
  367. {
  368. $x = $this->getMock('stdClass');
  369. $y = clone $x;
  370. $mock = $this->getMock('stdClass', array('foo'));
  371. $mock->expects($this->once())->method('foo')->with($this->equalTo($x));
  372. $mock->foo($y);
  373. }
  374. public function testClonedMockObjectIsNotIdenticalToOriginalOne()
  375. {
  376. $x = $this->getMock('stdClass');
  377. $y = clone $x;
  378. $mock = $this->getMock('stdClass', array('foo'));
  379. $mock->expects($this->once())->method('foo')->with($this->logicalNot($this->identicalTo($x)));
  380. $mock->foo($y);
  381. }
  382. public function testObjectMethodCallWithArgumentCloningEnabled()
  383. {
  384. $expectedObject = new StdClass;
  385. $mock = $this->getMockBuilder('SomeClass')
  386. ->setMethods(array('doSomethingElse'))
  387. ->enableArgumentCloning()
  388. ->getMock();
  389. $actualArguments = array();
  390. $mock->expects($this->any())
  391. ->method('doSomethingElse')
  392. ->will($this->returnCallback(function () use (&$actualArguments) {
  393. $actualArguments = func_get_args();
  394. }));
  395. $mock->doSomethingElse($expectedObject);
  396. $this->assertEquals(1, count($actualArguments));
  397. $this->assertEquals($expectedObject, $actualArguments[0]);
  398. $this->assertNotSame($expectedObject, $actualArguments[0]);
  399. }
  400. public function testObjectMethodCallWithArgumentCloningDisabled()
  401. {
  402. $expectedObject = new StdClass;
  403. $mock = $this->getMockBuilder('SomeClass')
  404. ->setMethods(array('doSomethingElse'))
  405. ->disableArgumentCloning()
  406. ->getMock();
  407. $actualArguments = array();
  408. $mock->expects($this->any())
  409. ->method('doSomethingElse')
  410. ->will($this->returnCallback(function () use (&$actualArguments) {
  411. $actualArguments = func_get_args();
  412. }));
  413. $mock->doSomethingElse($expectedObject);
  414. $this->assertEquals(1, count($actualArguments));
  415. $this->assertSame($expectedObject, $actualArguments[0]);
  416. }
  417. public function testArgumentCloningOptionGeneratesUniqueMock()
  418. {
  419. $mockWithCloning = $this->getMockBuilder('SomeClass')
  420. ->setMethods(array('doSomethingElse'))
  421. ->enableArgumentCloning()
  422. ->getMock();
  423. $mockWithoutCloning = $this->getMockBuilder('SomeClass')
  424. ->setMethods(array('doSomethingElse'))
  425. ->disableArgumentCloning()
  426. ->getMock();
  427. $this->assertNotEquals($mockWithCloning, $mockWithoutCloning);
  428. }
  429. public function testVerificationOfMethodNameFailsWithoutParameters()
  430. {
  431. $mock = $this->getMock('SomeClass', array('right', 'wrong'), array(), '', true, true, true);
  432. $mock->expects($this->once())
  433. ->method('right');
  434. $mock->wrong();
  435. try {
  436. $mock->__phpunit_verify();
  437. $this->fail('Expected exception');
  438. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  439. $this->assertSame(
  440. "Expectation failed for method name is equal to <string:right> when invoked 1 time(s).\n"
  441. . "Method was expected to be called 1 times, actually called 0 times.\n",
  442. $e->getMessage()
  443. );
  444. }
  445. $this->resetMockObjects();
  446. }
  447. public function testVerificationOfMethodNameFailsWithParameters()
  448. {
  449. $mock = $this->getMock('SomeClass', array('right', 'wrong'), array(), '', true, true, true);
  450. $mock->expects($this->once())
  451. ->method('right');
  452. $mock->wrong();
  453. try {
  454. $mock->__phpunit_verify();
  455. $this->fail('Expected exception');
  456. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  457. $this->assertSame(
  458. "Expectation failed for method name is equal to <string:right> when invoked 1 time(s).\n"
  459. . "Method was expected to be called 1 times, actually called 0 times.\n",
  460. $e->getMessage()
  461. );
  462. }
  463. $this->resetMockObjects();
  464. }
  465. public function testVerificationOfMethodNameFailsWithWrongParameters()
  466. {
  467. $mock = $this->getMock('SomeClass', array('right', 'wrong'), array(), '', true, true, true);
  468. $mock->expects($this->once())
  469. ->method('right')
  470. ->with(array('first', 'second'));
  471. try {
  472. $mock->right(array('second'));
  473. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  474. $this->assertSame(
  475. "Expectation failed for method name is equal to <string:right> when invoked 1 time(s)\n"
  476. . "Parameter 0 for invocation SomeClass::right(Array (...)) does not match expected value.\n"
  477. . "Failed asserting that two arrays are equal.",
  478. $e->getMessage()
  479. );
  480. }
  481. try {
  482. $mock->__phpunit_verify();
  483. $this->fail('Expected exception');
  484. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  485. $this->assertSame(
  486. "Expectation failed for method name is equal to <string:right> when invoked 1 time(s).\n"
  487. . "Parameter 0 for invocation SomeClass::right(Array (...)) does not match expected value.\n"
  488. . "Failed asserting that two arrays are equal.\n"
  489. . "--- Expected\n"
  490. . "+++ Actual\n"
  491. . "@@ @@\n"
  492. . " Array (\n"
  493. . "- 0 => 'first'\n"
  494. . "- 1 => 'second'\n"
  495. . "+ 0 => 'second'\n"
  496. . " )\n",
  497. $e->getMessage()
  498. );
  499. }
  500. $this->resetMockObjects();
  501. }
  502. public function testVerificationOfNeverFailsWithEmptyParameters()
  503. {
  504. $mock = $this->getMock('SomeClass', array('right', 'wrong'), array(), '', true, true, true);
  505. $mock->expects($this->never())
  506. ->method('right')
  507. ->with();
  508. try {
  509. $mock->right();
  510. $this->fail('Expected exception');
  511. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  512. $this->assertSame(
  513. 'SomeClass::right() was not expected to be called.',
  514. $e->getMessage()
  515. );
  516. }
  517. $this->resetMockObjects();
  518. }
  519. public function testVerificationOfNeverFailsWithAnyParameters()
  520. {
  521. $mock = $this->getMock('SomeClass', array('right', 'wrong'), array(), '', true, true, true);
  522. $mock->expects($this->never())
  523. ->method('right')
  524. ->withAnyParameters();
  525. try {
  526. $mock->right();
  527. $this->fail('Expected exception');
  528. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  529. $this->assertSame(
  530. 'SomeClass::right() was not expected to be called.',
  531. $e->getMessage()
  532. );
  533. }
  534. $this->resetMockObjects();
  535. }
  536. /**
  537. * @ticket 199
  538. */
  539. public function testWithAnythingInsteadOfWithAnyParameters()
  540. {
  541. $mock = $this->getMock('SomeClass', array('right'), array(), '', true, true, true);
  542. $mock->expects($this->once())
  543. ->method('right')
  544. ->with($this->anything());
  545. try {
  546. $mock->right();
  547. $this->fail('Expected exception');
  548. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  549. $this->assertSame(
  550. "Expectation failed for method name is equal to <string:right> when invoked 1 time(s)\n" .
  551. "Parameter count for invocation SomeClass::right() is too low.\n" .
  552. "To allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead.",
  553. $e->getMessage()
  554. );
  555. }
  556. $this->resetMockObjects();
  557. }
  558. /**
  559. * See https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81
  560. */
  561. public function testMockArgumentsPassedByReference()
  562. {
  563. $foo = $this->getMockBuilder('MethodCallbackByReference')
  564. ->setMethods(array('bar'))
  565. ->disableOriginalConstructor()
  566. ->disableArgumentCloning()
  567. ->getMock();
  568. $foo->expects($this->any())
  569. ->method('bar')
  570. ->will($this->returnCallback(array($foo, 'callback')));
  571. $a = $b = $c = 0;
  572. $foo->bar($a, $b, $c);
  573. $this->assertEquals(1, $b);
  574. }
  575. /**
  576. * See https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81
  577. */
  578. public function testMockArgumentsPassedByReference2()
  579. {
  580. $foo = $this->getMockBuilder('MethodCallbackByReference')
  581. ->disableOriginalConstructor()
  582. ->disableArgumentCloning()
  583. ->getMock();
  584. $foo->expects($this->any())
  585. ->method('bar')
  586. ->will($this->returnCallback(
  587. function (&$a, &$b, $c) {
  588. $b = 1;
  589. }
  590. ));
  591. $a = $b = $c = 0;
  592. $foo->bar($a, $b, $c);
  593. $this->assertEquals(1, $b);
  594. }
  595. /**
  596. * https://github.com/sebastianbergmann/phpunit-mock-objects/issues/116
  597. */
  598. public function testMockArgumentsPassedByReference3()
  599. {
  600. $foo = $this->getMockBuilder('MethodCallbackByReference')
  601. ->setMethods(array('bar'))
  602. ->disableOriginalConstructor()
  603. ->disableArgumentCloning()
  604. ->getMock();
  605. $a = new stdClass();
  606. $b = $c = 0;
  607. $foo->expects($this->any())
  608. ->method('bar')
  609. ->with($a, $b, $c)
  610. ->will($this->returnCallback(array($foo, 'callback')));
  611. $foo->bar($a, $b, $c);
  612. }
  613. /**
  614. * https://github.com/sebastianbergmann/phpunit/issues/796
  615. */
  616. public function testMockArgumentsPassedByReference4()
  617. {
  618. $foo = $this->getMockBuilder('MethodCallbackByReference')
  619. ->setMethods(array('bar'))
  620. ->disableOriginalConstructor()
  621. ->disableArgumentCloning()
  622. ->getMock();
  623. $a = new stdClass();
  624. $b = $c = 0;
  625. $foo->expects($this->any())
  626. ->method('bar')
  627. ->with($this->isInstanceOf("stdClass"), $b, $c)
  628. ->will($this->returnCallback(array($foo, 'callback')));
  629. $foo->bar($a, $b, $c);
  630. }
  631. /**
  632. * @requires extension soap
  633. */
  634. public function testCreateMockFromWsdl()
  635. {
  636. $mock = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl', 'WsdlMock');
  637. $this->assertStringStartsWith(
  638. 'Mock_WsdlMock_',
  639. get_class($mock)
  640. );
  641. }
  642. /**
  643. * @requires extension soap
  644. */
  645. public function testCreateNamespacedMockFromWsdl()
  646. {
  647. $mock = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl', 'My\\Space\\WsdlMock');
  648. $this->assertStringStartsWith(
  649. 'Mock_WsdlMock_',
  650. get_class($mock)
  651. );
  652. }
  653. /**
  654. * @requires extension soap
  655. */
  656. public function testCreateTwoMocksOfOneWsdlFile()
  657. {
  658. $mock = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl');
  659. $mock = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl');
  660. }
  661. /**
  662. * @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/156
  663. * @ticket 156
  664. */
  665. public function testInterfaceWithStaticMethodCanBeStubbed()
  666. {
  667. $this->assertInstanceOf(
  668. 'InterfaceWithStaticMethod',
  669. $this->getMock('InterfaceWithStaticMethod')
  670. );
  671. }
  672. /**
  673. * @expectedException PHPUnit_Framework_MockObject_BadMethodCallException
  674. */
  675. public function testInvokingStubbedStaticMethodRaisesException()
  676. {
  677. $mock = $this->getMock('ClassWithStaticMethod');
  678. $mock->staticMethod();
  679. }
  680. /**
  681. * @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/171
  682. * @ticket 171
  683. */
  684. public function testStubForClassThatImplementsSerializableCanBeCreatedWithoutInvokingTheConstructor()
  685. {
  686. $this->assertInstanceOf(
  687. 'ClassThatImplementsSerializable',
  688. $this->getMockBuilder('ClassThatImplementsSerializable')
  689. ->disableOriginalConstructor()
  690. ->getMock()
  691. );
  692. }
  693. private function resetMockObjects()
  694. {
  695. $refl = new ReflectionObject($this);
  696. $refl = $refl->getParentClass();
  697. $prop = $refl->getProperty('mockObjects');
  698. $prop->setAccessible(true);
  699. $prop->setValue($this, array());
  700. }
  701. }