PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpunit/phpunit-mock-objects/tests/GeneratorTest.php

https://gitlab.com/jhonn/rest
PHP | 176 lines | 96 code | 23 blank | 57 comment | 1 complexity | abd9dd2e49d0444abfc3990bb3c2e301 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. }