GeneratorTest.php 7.5 KB

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