No Description

GeneratorTest.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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', array(), '', true,
  60. true, true, array('nonexistentMethod')
  61. );
  62. $this->assertTrue(method_exists($mock, 'nonexistentMethod'));
  63. $this->assertTrue(method_exists($mock, 'doSomething'));
  64. }
  65. /**
  66. * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
  67. */
  68. public function testGetMockForAbstractClassShouldCreateStubsOnlyForAbstractMethodWhenNoMethodsWereInformed()
  69. {
  70. $mock = $this->generator->getMockForAbstractClass('AbstractMockTestClass');
  71. $mock->expects($this->any())
  72. ->method('doSomething')
  73. ->willReturn('testing');
  74. $this->assertEquals('testing', $mock->doSomething());
  75. $this->assertEquals(1, $mock->returnAnything());
  76. }
  77. /**
  78. * @dataProvider getMockForAbstractClassExpectsInvalidArgumentExceptionDataprovider
  79. * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
  80. * @expectedException PHPUnit_Framework_Exception
  81. */
  82. public function testGetMockForAbstractClassExpectingInvalidArgumentException($className, $mockClassName)
  83. {
  84. $mock = $this->generator->getMockForAbstractClass($className, array(), $mockClassName);
  85. }
  86. /**
  87. * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
  88. * @expectedException PHPUnit_Framework_MockObject_RuntimeException
  89. */
  90. public function testGetMockForAbstractClassAnstractClassDoesNotExist()
  91. {
  92. $mock = $this->generator->getMockForAbstractClass('Tux');
  93. }
  94. /**
  95. * Dataprovider for test "testGetMockForAbstractClassExpectingInvalidArgumentException"
  96. */
  97. public static function getMockForAbstractClassExpectsInvalidArgumentExceptionDataprovider()
  98. {
  99. return array(
  100. 'className not a string' => array(array(), ''),
  101. 'mockClassName not a string' => array('Countable', new StdClass),
  102. );
  103. }
  104. /**
  105. * @covers PHPUnit_Framework_MockObject_Generator::getMockForTrait
  106. * @requires PHP 5.4.0
  107. */
  108. public function testGetMockForTraitWithNonExistentMethodsAndNonAbstractMethods()
  109. {
  110. $mock = $this->generator->getMockForTrait(
  111. 'AbstractTrait', array(), '', true,
  112. true, true, array('nonexistentMethod')
  113. );
  114. $this->assertTrue(method_exists($mock, 'nonexistentMethod'));
  115. $this->assertTrue(method_exists($mock, 'doSomething'));
  116. $this->assertTrue($mock->mockableMethod());
  117. $this->assertTrue($mock->anotherMockableMethod());
  118. }
  119. /**
  120. * @covers PHPUnit_Framework_MockObject_Generator::getMockForTrait
  121. * @requires PHP 5.4.0
  122. */
  123. public function testGetMockForTraitStubbingAbstractMethod()
  124. {
  125. $mock = $this->generator->getMockForTrait('AbstractTrait');
  126. $this->assertTrue(method_exists($mock, 'doSomething'));
  127. }
  128. /**
  129. * @requires PHP 5.4.0
  130. */
  131. public function testGetMockForSingletonWithReflectionSuccess()
  132. {
  133. // Probably, this should be moved to tests/autoload.php
  134. require_once __DIR__ . '/_fixture/SingletonClass.php';
  135. $mock = $this->generator->getMock('SingletonClass', array('doSomething'), array(), '', false);
  136. $this->assertInstanceOf('SingletonClass', $mock);
  137. }
  138. /**
  139. * Same as "testGetMockForSingletonWithReflectionSuccess", but we expect
  140. * warning for PHP < 5.4.0 since PHPUnit will try to execute private __wakeup
  141. * on unserialize
  142. */
  143. public function testGetMockForSingletonWithUnserializeFail()
  144. {
  145. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  146. $this->markTestSkipped('Only for PHP < 5.4.0');
  147. }
  148. $this->setExpectedException('PHPUnit_Framework_MockObject_RuntimeException');
  149. // Probably, this should be moved to tests/autoload.php
  150. require_once __DIR__ . '/_fixture/SingletonClass.php';
  151. $mock = $this->generator->getMock('SingletonClass', array('doSomething'), array(), '', false);
  152. }
  153. }