GeneratorTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /*
  3. * This file is part of the phpunit-mock-objects 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. use PHPUnit\Framework\TestCase;
  11. /**
  12. * @covers PHPUnit_Framework_MockObject_Generator
  13. *
  14. * @uses PHPUnit_Framework_MockObject_InvocationMocker
  15. * @uses PHPUnit_Framework_MockObject_Builder_InvocationMocker
  16. * @uses PHPUnit_Framework_MockObject_Invocation_Object
  17. * @uses PHPUnit_Framework_MockObject_Invocation_Static
  18. * @uses PHPUnit_Framework_MockObject_Matcher
  19. * @uses PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
  20. * @uses PHPUnit_Framework_MockObject_Matcher_MethodName
  21. * @uses PHPUnit_Framework_MockObject_Stub_Return
  22. * @uses PHPUnit_Framework_MockObject_Matcher_InvokedCount
  23. */
  24. class Framework_MockObject_GeneratorTest extends TestCase
  25. {
  26. /**
  27. * @var PHPUnit_Framework_MockObject_Generator
  28. */
  29. private $generator;
  30. protected function setUp()
  31. {
  32. $this->generator = new PHPUnit_Framework_MockObject_Generator;
  33. }
  34. /**
  35. * @expectedException PHPUnit_Framework_MockObject_RuntimeException
  36. */
  37. public function testGetMockFailsWhenInvalidFunctionNameIsPassedInAsAFunctionToMock()
  38. {
  39. $this->generator->getMock(stdClass::class, [0]);
  40. }
  41. public function testGetMockCanCreateNonExistingFunctions()
  42. {
  43. $mock = $this->generator->getMock(stdClass::class, ['testFunction']);
  44. $this->assertTrue(method_exists($mock, 'testFunction'));
  45. }
  46. /**
  47. * @expectedException PHPUnit_Framework_MockObject_RuntimeException
  48. * @expectedExceptionMessage duplicates: "foo, bar, foo" (duplicate: "foo")
  49. */
  50. public function testGetMockGeneratorFails()
  51. {
  52. $this->generator->getMock(stdClass::class, ['foo', 'bar', 'foo']);
  53. }
  54. /**
  55. * @requires PHP 7
  56. */
  57. public function testGetMockBlacklistedMethodNamesPhp7()
  58. {
  59. $mock = $this->generator->getMock(InterfaceWithSemiReservedMethodName::class);
  60. $this->assertTrue(method_exists($mock, 'unset'));
  61. $this->assertInstanceOf(InterfaceWithSemiReservedMethodName::class, $mock);
  62. }
  63. public function testGetMockForAbstractClassDoesNotFailWhenFakingInterfaces()
  64. {
  65. $mock = $this->generator->getMockForAbstractClass(Countable::class);
  66. $this->assertTrue(method_exists($mock, 'count'));
  67. }
  68. public function testGetMockForAbstractClassStubbingAbstractClass()
  69. {
  70. $mock = $this->generator->getMockForAbstractClass(AbstractMockTestClass::class);
  71. $this->assertTrue(method_exists($mock, 'doSomething'));
  72. }
  73. public function testGetMockForAbstractClassWithNonExistentMethods()
  74. {
  75. $mock = $this->generator->getMockForAbstractClass(
  76. AbstractMockTestClass::class,
  77. [],
  78. '',
  79. true,
  80. true,
  81. true,
  82. ['nonexistentMethod']
  83. );
  84. $this->assertTrue(method_exists($mock, 'nonexistentMethod'));
  85. $this->assertTrue(method_exists($mock, 'doSomething'));
  86. }
  87. public function testGetMockForAbstractClassShouldCreateStubsOnlyForAbstractMethodWhenNoMethodsWereInformed()
  88. {
  89. $mock = $this->generator->getMockForAbstractClass(AbstractMockTestClass::class);
  90. $mock->expects($this->any())
  91. ->method('doSomething')
  92. ->willReturn('testing');
  93. $this->assertEquals('testing', $mock->doSomething());
  94. $this->assertEquals(1, $mock->returnAnything());
  95. }
  96. /**
  97. * @dataProvider getMockForAbstractClassExpectsInvalidArgumentExceptionDataprovider
  98. * @expectedException PHPUnit\Framework\Exception
  99. */
  100. public function testGetMockForAbstractClassExpectingInvalidArgumentException($className, $mockClassName)
  101. {
  102. $this->generator->getMockForAbstractClass($className, [], $mockClassName);
  103. }
  104. /**
  105. * @expectedException PHPUnit_Framework_MockObject_RuntimeException
  106. */
  107. public function testGetMockForAbstractClassAbstractClassDoesNotExist()
  108. {
  109. $this->generator->getMockForAbstractClass('Tux');
  110. }
  111. public function getMockForAbstractClassExpectsInvalidArgumentExceptionDataprovider()
  112. {
  113. return [
  114. 'className not a string' => [[], ''],
  115. 'mockClassName not a string' => [Countable::class, new stdClass],
  116. ];
  117. }
  118. public function testGetMockForTraitWithNonExistentMethodsAndNonAbstractMethods()
  119. {
  120. $mock = $this->generator->getMockForTrait(
  121. AbstractTrait::class,
  122. [],
  123. '',
  124. true,
  125. true,
  126. true,
  127. ['nonexistentMethod']
  128. );
  129. $this->assertTrue(method_exists($mock, 'nonexistentMethod'));
  130. $this->assertTrue(method_exists($mock, 'doSomething'));
  131. $this->assertTrue($mock->mockableMethod());
  132. $this->assertTrue($mock->anotherMockableMethod());
  133. }
  134. public function testGetMockForTraitStubbingAbstractMethod()
  135. {
  136. $mock = $this->generator->getMockForTrait(AbstractTrait::class);
  137. $this->assertTrue(method_exists($mock, 'doSomething'));
  138. }
  139. public function testGetMockForSingletonWithReflectionSuccess()
  140. {
  141. $mock = $this->generator->getMock(SingletonClass::class, ['doSomething'], [], '', false);
  142. $this->assertInstanceOf('SingletonClass', $mock);
  143. }
  144. /**
  145. * @expectedException PHPUnit_Framework_MockObject_RuntimeException
  146. */
  147. public function testExceptionIsRaisedForMutuallyExclusiveOptions()
  148. {
  149. $this->generator->getMock(stdClass::class, [], [], '', false, true, true, true, true);
  150. }
  151. /**
  152. * @requires PHP 7
  153. */
  154. public function testCanImplementInterfacesThatHaveMethodsWithReturnTypes()
  155. {
  156. $stub = $this->generator->getMock([AnInterfaceWithReturnType::class, AnInterface::class]);
  157. $this->assertInstanceOf(AnInterfaceWithReturnType::class, $stub);
  158. $this->assertInstanceOf(AnInterface::class, $stub);
  159. $this->assertInstanceOf(PHPUnit_Framework_MockObject_MockObject::class, $stub);
  160. }
  161. public function testCanConfigureMethodsForDoubleOfNonExistentClass()
  162. {
  163. $className = 'X' . md5(microtime());
  164. $mock = $this->generator->getMock($className, ['someMethod']);
  165. $this->assertInstanceOf($className, $mock);
  166. }
  167. public function testCanInvokeMethodsOfNonExistentClass()
  168. {
  169. $className = 'X' . md5(microtime());
  170. $mock = $this->generator->getMock($className, ['someMethod']);
  171. $mock->expects($this->once())->method('someMethod');
  172. $this->assertNull($mock->someMethod());
  173. }
  174. }