Drupal investigation

GeneratorTest.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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_Exception
  15. */
  16. public function testGetMockFailsWhenInvalidFunctionNameIsPassedInAsAFunctionToMock()
  17. {
  18. $this->generator->getMock('StdClass', array(0));
  19. }
  20. /**
  21. * @covers PHPUnit_Framework_MockObject_Generator::getMock
  22. */
  23. public function testGetMockCanCreateNonExistingFunctions()
  24. {
  25. $mock = $this->generator->getMock('StdClass', array('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, foo"
  32. */
  33. public function testGetMockGeneratorFails()
  34. {
  35. $mock = $this->generator->getMock('StdClass', array('foo', 'foo'));
  36. }
  37. /**
  38. * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
  39. */
  40. public function testGetMockForAbstractClassDoesNotFailWhenFakingInterfaces()
  41. {
  42. $mock = $this->generator->getMockForAbstractClass('Countable');
  43. $this->assertTrue(method_exists($mock, 'count'));
  44. }
  45. /**
  46. * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
  47. */
  48. public function testGetMockForAbstractClassStubbingAbstractClass()
  49. {
  50. $mock = $this->generator->getMockForAbstractClass('AbstractMockTestClass');
  51. $this->assertTrue(method_exists($mock, 'doSomething'));
  52. }
  53. /**
  54. * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
  55. */
  56. public function testGetMockForAbstractClassWithNonExistentMethods()
  57. {
  58. $mock = $this->generator->getMockForAbstractClass(
  59. 'AbstractMockTestClass',
  60. array(),
  61. '',
  62. true,
  63. true,
  64. true,
  65. array('nonexistentMethod')
  66. );
  67. $this->assertTrue(method_exists($mock, 'nonexistentMethod'));
  68. $this->assertTrue(method_exists($mock, 'doSomething'));
  69. }
  70. /**
  71. * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
  72. */
  73. public function testGetMockForAbstractClassShouldCreateStubsOnlyForAbstractMethodWhenNoMethodsWereInformed()
  74. {
  75. $mock = $this->generator->getMockForAbstractClass('AbstractMockTestClass');
  76. $mock->expects($this->any())
  77. ->method('doSomething')
  78. ->willReturn('testing');
  79. $this->assertEquals('testing', $mock->doSomething());
  80. $this->assertEquals(1, $mock->returnAnything());
  81. }
  82. /**
  83. * @dataProvider getMockForAbstractClassExpectsInvalidArgumentExceptionDataprovider
  84. * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
  85. * @expectedException PHPUnit_Framework_Exception
  86. */
  87. public function testGetMockForAbstractClassExpectingInvalidArgumentException($className, $mockClassName)
  88. {
  89. $mock = $this->generator->getMockForAbstractClass($className, array(), $mockClassName);
  90. }
  91. /**
  92. * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
  93. * @expectedException PHPUnit_Framework_MockObject_RuntimeException
  94. */
  95. public function testGetMockForAbstractClassAbstractClassDoesNotExist()
  96. {
  97. $mock = $this->generator->getMockForAbstractClass('Tux');
  98. }
  99. /**
  100. * Dataprovider for test "testGetMockForAbstractClassExpectingInvalidArgumentException"
  101. */
  102. public static function getMockForAbstractClassExpectsInvalidArgumentExceptionDataprovider()
  103. {
  104. return array(
  105. 'className not a string' => array(array(), ''),
  106. 'mockClassName not a string' => array('Countable', new StdClass),
  107. );
  108. }
  109. /**
  110. * @covers PHPUnit_Framework_MockObject_Generator::getMockForTrait
  111. * @requires PHP 5.4.0
  112. */
  113. public function testGetMockForTraitWithNonExistentMethodsAndNonAbstractMethods()
  114. {
  115. $mock = $this->generator->getMockForTrait(
  116. 'AbstractTrait',
  117. array(),
  118. '',
  119. true,
  120. true,
  121. true,
  122. array('nonexistentMethod')
  123. );
  124. $this->assertTrue(method_exists($mock, 'nonexistentMethod'));
  125. $this->assertTrue(method_exists($mock, 'doSomething'));
  126. $this->assertTrue($mock->mockableMethod());
  127. $this->assertTrue($mock->anotherMockableMethod());
  128. }
  129. /**
  130. * @covers PHPUnit_Framework_MockObject_Generator::getMockForTrait
  131. * @requires PHP 5.4.0
  132. */
  133. public function testGetMockForTraitStubbingAbstractMethod()
  134. {
  135. $mock = $this->generator->getMockForTrait('AbstractTrait');
  136. $this->assertTrue(method_exists($mock, 'doSomething'));
  137. }
  138. /**
  139. * @requires PHP 5.4.0
  140. */
  141. public function testGetMockForSingletonWithReflectionSuccess()
  142. {
  143. // Probably, this should be moved to tests/autoload.php
  144. require_once __DIR__ . '/_fixture/SingletonClass.php';
  145. $mock = $this->generator->getMock('SingletonClass', array('doSomething'), array(), '', false);
  146. $this->assertInstanceOf('SingletonClass', $mock);
  147. }
  148. /**
  149. * Same as "testGetMockForSingletonWithReflectionSuccess", but we expect
  150. * warning for PHP < 5.4.0 since PHPUnit will try to execute private __wakeup
  151. * on unserialize
  152. */
  153. public function testGetMockForSingletonWithUnserializeFail()
  154. {
  155. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  156. $this->markTestSkipped('Only for PHP < 5.4.0');
  157. }
  158. $this->setExpectedException('PHPUnit_Framework_MockObject_RuntimeException');
  159. // Probably, this should be moved to tests/autoload.php
  160. require_once __DIR__ . '/_fixture/SingletonClass.php';
  161. $mock = $this->generator->getMock('SingletonClass', array('doSomething'), array(), '', false);
  162. }
  163. /**
  164. * ReflectionClass::getMethods for SoapClient on PHP 5.3 produces PHP Fatal Error
  165. * @runInSeparateProcess
  166. */
  167. public function testGetMockForSoapClientReflectionMethodsDuplication()
  168. {
  169. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  170. $this->markTestSkipped('Only for PHP < 5.4.0');
  171. }
  172. $mock = $this->generator->getMock('SoapClient', array(), array(), '', false);
  173. $this->assertInstanceOf('SoapClient', $mock);
  174. }
  175. }