菜谱项目

GeneratorTest.php 6.3KB

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