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