MockObjectTest.php 32 KB

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