GeneratorTest.php 7.2 KB

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