comparison vendor/phpunit/phpunit-mock-objects/tests/GeneratorTest.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php 1 <?php
2 class Framework_MockObject_GeneratorTest extends PHPUnit_Framework_TestCase 2 /*
3 * This file is part of the phpunit-mock-objects package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 use PHPUnit\Framework\MockObject\Generator;
12 use PHPUnit\Framework\MockObject\MockObject;
13 use PHPUnit\Framework\TestCase;
14
15 /**
16 * @covers \PHPUnit\Framework\MockObject\Generator
17 *
18 * @uses \PHPUnit\Framework\MockObject\InvocationMocker
19 * @uses \PHPUnit\Framework\MockObject\Builder\InvocationMocker
20 * @uses \PHPUnit\Framework\MockObject\Invocation\ObjectInvocation
21 * @uses \PHPUnit\Framework\MockObject\Invocation\StaticInvocation
22 * @uses \PHPUnit\Framework\MockObject\Matcher
23 * @uses \PHPUnit\Framework\MockObject\Matcher\InvokedRecorder
24 * @uses \PHPUnit\Framework\MockObject\Matcher\MethodName
25 * @uses \PHPUnit\Framework\MockObject\Stub\ReturnStub
26 * @uses \PHPUnit\Framework\MockObject\Matcher\InvokedCount
27 */
28 class GeneratorTest extends TestCase
3 { 29 {
4 /** 30 /**
5 * @var PHPUnit_Framework_MockObject_Generator 31 * @var Generator
6 */ 32 */
7 protected $generator; 33 private $generator;
8 34
9 protected function setUp() 35 protected function setUp()
10 { 36 {
11 $this->generator = new PHPUnit_Framework_MockObject_Generator; 37 $this->generator = new Generator;
12 } 38 }
13 39
14 /**
15 * @covers PHPUnit_Framework_MockObject_Generator::getMock
16 * @expectedException PHPUnit_Framework_Exception
17 */
18 public function testGetMockFailsWhenInvalidFunctionNameIsPassedInAsAFunctionToMock() 40 public function testGetMockFailsWhenInvalidFunctionNameIsPassedInAsAFunctionToMock()
19 { 41 {
20 $this->generator->getMock('StdClass', array(0)); 42 $this->expectException(\PHPUnit\Framework\MockObject\RuntimeException::class);
21 } 43
22 44 $this->generator->getMock(stdClass::class, [0]);
23 /** 45 }
24 * @covers PHPUnit_Framework_MockObject_Generator::getMock 46
25 */
26 public function testGetMockCanCreateNonExistingFunctions() 47 public function testGetMockCanCreateNonExistingFunctions()
27 { 48 {
28 $mock = $this->generator->getMock('StdClass', array('testFunction')); 49 $mock = $this->generator->getMock(stdClass::class, ['testFunction']);
50
29 $this->assertTrue(method_exists($mock, 'testFunction')); 51 $this->assertTrue(method_exists($mock, 'testFunction'));
30 } 52 }
31 53
32 /**
33 * @covers PHPUnit_Framework_MockObject_Generator::getMock
34 * @expectedException PHPUnit_Framework_MockObject_RuntimeException
35 * @expectedExceptionMessage duplicates: "foo, foo"
36 */
37 public function testGetMockGeneratorFails() 54 public function testGetMockGeneratorFails()
38 { 55 {
39 $mock = $this->generator->getMock('StdClass', array('foo', 'foo')); 56 $this->expectException(\PHPUnit\Framework\MockObject\RuntimeException::class);
40 } 57 $this->expectExceptionMessage('duplicates: "foo, bar, foo" (duplicate: "foo")');
41 58
42 /** 59 $this->generator->getMock(stdClass::class, ['foo', 'bar', 'foo']);
43 * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass 60 }
44 */ 61
62 /**
63 * @requires PHP 7
64 */
65 public function testGetMockBlacklistedMethodNamesPhp7()
66 {
67 $mock = $this->generator->getMock(InterfaceWithSemiReservedMethodName::class);
68
69 $this->assertTrue(method_exists($mock, 'unset'));
70 $this->assertInstanceOf(InterfaceWithSemiReservedMethodName::class, $mock);
71 }
72
45 public function testGetMockForAbstractClassDoesNotFailWhenFakingInterfaces() 73 public function testGetMockForAbstractClassDoesNotFailWhenFakingInterfaces()
46 { 74 {
47 $mock = $this->generator->getMockForAbstractClass('Countable'); 75 $mock = $this->generator->getMockForAbstractClass(Countable::class);
76
48 $this->assertTrue(method_exists($mock, 'count')); 77 $this->assertTrue(method_exists($mock, 'count'));
49 } 78 }
50 79
51 /**
52 * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
53 */
54 public function testGetMockForAbstractClassStubbingAbstractClass() 80 public function testGetMockForAbstractClassStubbingAbstractClass()
55 { 81 {
56 $mock = $this->generator->getMockForAbstractClass('AbstractMockTestClass'); 82 $mock = $this->generator->getMockForAbstractClass(AbstractMockTestClass::class);
57 $this->assertTrue(method_exists($mock, 'doSomething')); 83
58 } 84 $this->assertTrue(method_exists($mock, 'doSomething'));
59 85 }
60 /** 86
61 * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
62 */
63 public function testGetMockForAbstractClassWithNonExistentMethods() 87 public function testGetMockForAbstractClassWithNonExistentMethods()
64 { 88 {
65 $mock = $this->generator->getMockForAbstractClass( 89 $mock = $this->generator->getMockForAbstractClass(
66 'AbstractMockTestClass', 90 AbstractMockTestClass::class,
67 array(), 91 [],
68 '', 92 '',
69 true, 93 true,
70 true, 94 true,
71 true, 95 true,
72 array('nonexistentMethod') 96 ['nonexistentMethod']
73 ); 97 );
74 98
75 $this->assertTrue(method_exists($mock, 'nonexistentMethod')); 99 $this->assertTrue(method_exists($mock, 'nonexistentMethod'));
76 $this->assertTrue(method_exists($mock, 'doSomething')); 100 $this->assertTrue(method_exists($mock, 'doSomething'));
77 } 101 }
78 102
79 /**
80 * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
81 */
82 public function testGetMockForAbstractClassShouldCreateStubsOnlyForAbstractMethodWhenNoMethodsWereInformed() 103 public function testGetMockForAbstractClassShouldCreateStubsOnlyForAbstractMethodWhenNoMethodsWereInformed()
83 { 104 {
84 $mock = $this->generator->getMockForAbstractClass('AbstractMockTestClass'); 105 $mock = $this->generator->getMockForAbstractClass(AbstractMockTestClass::class);
85 106
86 $mock->expects($this->any()) 107 $mock->expects($this->any())
87 ->method('doSomething') 108 ->method('doSomething')
88 ->willReturn('testing'); 109 ->willReturn('testing');
89 110
91 $this->assertEquals(1, $mock->returnAnything()); 112 $this->assertEquals(1, $mock->returnAnything());
92 } 113 }
93 114
94 /** 115 /**
95 * @dataProvider getMockForAbstractClassExpectsInvalidArgumentExceptionDataprovider 116 * @dataProvider getMockForAbstractClassExpectsInvalidArgumentExceptionDataprovider
96 * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
97 * @expectedException PHPUnit_Framework_Exception
98 */ 117 */
99 public function testGetMockForAbstractClassExpectingInvalidArgumentException($className, $mockClassName) 118 public function testGetMockForAbstractClassExpectingInvalidArgumentException($className, $mockClassName)
100 { 119 {
101 $mock = $this->generator->getMockForAbstractClass($className, array(), $mockClassName); 120 $this->expectException(PHPUnit\Framework\Exception::class);
102 } 121
103 122 $this->generator->getMockForAbstractClass($className, [], $mockClassName);
104 /** 123 }
105 * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass 124
106 * @expectedException PHPUnit_Framework_MockObject_RuntimeException
107 */
108 public function testGetMockForAbstractClassAbstractClassDoesNotExist() 125 public function testGetMockForAbstractClassAbstractClassDoesNotExist()
109 { 126 {
110 $mock = $this->generator->getMockForAbstractClass('Tux'); 127 $this->expectException(\PHPUnit\Framework\MockObject\RuntimeException::class);
111 } 128
112 129 $this->generator->getMockForAbstractClass('Tux');
113 /** 130 }
114 * Dataprovider for test "testGetMockForAbstractClassExpectingInvalidArgumentException" 131
115 */ 132 public function getMockForAbstractClassExpectsInvalidArgumentExceptionDataprovider()
116 public static function getMockForAbstractClassExpectsInvalidArgumentExceptionDataprovider() 133 {
117 { 134 return [
118 return array( 135 'className not a string' => [[], ''],
119 'className not a string' => array(array(), ''), 136 'mockClassName not a string' => [Countable::class, new stdClass],
120 'mockClassName not a string' => array('Countable', new StdClass), 137 ];
121 ); 138 }
122 } 139
123
124 /**
125 * @covers PHPUnit_Framework_MockObject_Generator::getMockForTrait
126 * @requires PHP 5.4.0
127 */
128 public function testGetMockForTraitWithNonExistentMethodsAndNonAbstractMethods() 140 public function testGetMockForTraitWithNonExistentMethodsAndNonAbstractMethods()
129 { 141 {
130 $mock = $this->generator->getMockForTrait( 142 $mock = $this->generator->getMockForTrait(
131 'AbstractTrait', 143 AbstractTrait::class,
132 array(), 144 [],
133 '', 145 '',
134 true, 146 true,
135 true, 147 true,
136 true, 148 true,
137 array('nonexistentMethod') 149 ['nonexistentMethod']
138 ); 150 );
139 151
140 $this->assertTrue(method_exists($mock, 'nonexistentMethod')); 152 $this->assertTrue(method_exists($mock, 'nonexistentMethod'));
141 $this->assertTrue(method_exists($mock, 'doSomething')); 153 $this->assertTrue(method_exists($mock, 'doSomething'));
142 $this->assertTrue($mock->mockableMethod()); 154 $this->assertTrue($mock->mockableMethod());
143 $this->assertTrue($mock->anotherMockableMethod()); 155 $this->assertTrue($mock->anotherMockableMethod());
144 } 156 }
145 157
146 /**
147 * @covers PHPUnit_Framework_MockObject_Generator::getMockForTrait
148 * @requires PHP 5.4.0
149 */
150 public function testGetMockForTraitStubbingAbstractMethod() 158 public function testGetMockForTraitStubbingAbstractMethod()
151 { 159 {
152 $mock = $this->generator->getMockForTrait('AbstractTrait'); 160 $mock = $this->generator->getMockForTrait(AbstractTrait::class);
153 $this->assertTrue(method_exists($mock, 'doSomething')); 161
154 } 162 $this->assertTrue(method_exists($mock, 'doSomething'));
155 163 }
156 /** 164
157 * @requires PHP 5.4.0
158 */
159 public function testGetMockForSingletonWithReflectionSuccess() 165 public function testGetMockForSingletonWithReflectionSuccess()
160 { 166 {
161 // Probably, this should be moved to tests/autoload.php 167 $mock = $this->generator->getMock(SingletonClass::class, ['doSomething'], [], '', false);
162 require_once __DIR__ . '/_fixture/SingletonClass.php'; 168
163
164 $mock = $this->generator->getMock('SingletonClass', array('doSomething'), array(), '', false);
165 $this->assertInstanceOf('SingletonClass', $mock); 169 $this->assertInstanceOf('SingletonClass', $mock);
166 } 170 }
167 171
168 /** 172 public function testExceptionIsRaisedForMutuallyExclusiveOptions()
169 * Same as "testGetMockForSingletonWithReflectionSuccess", but we expect 173 {
170 * warning for PHP < 5.4.0 since PHPUnit will try to execute private __wakeup 174 $this->expectException(\PHPUnit\Framework\MockObject\RuntimeException::class);
171 * on unserialize 175
172 */ 176 $this->generator->getMock(stdClass::class, [], [], '', false, true, true, true, true);
173 public function testGetMockForSingletonWithUnserializeFail() 177 }
174 { 178
175 if (version_compare(PHP_VERSION, '5.4.0', '>=')) { 179 /**
176 $this->markTestSkipped('Only for PHP < 5.4.0'); 180 * @requires PHP 7
177 } 181 */
178 182 public function testCanImplementInterfacesThatHaveMethodsWithReturnTypes()
179 $this->setExpectedException('PHPUnit_Framework_MockObject_RuntimeException'); 183 {
180 184 $stub = $this->generator->getMock([AnInterfaceWithReturnType::class, AnInterface::class]);
181 // Probably, this should be moved to tests/autoload.php 185
182 require_once __DIR__ . '/_fixture/SingletonClass.php'; 186 $this->assertInstanceOf(AnInterfaceWithReturnType::class, $stub);
183 187 $this->assertInstanceOf(AnInterface::class, $stub);
184 $mock = $this->generator->getMock('SingletonClass', array('doSomething'), array(), '', false); 188 $this->assertInstanceOf(MockObject::class, $stub);
185 } 189 }
186 190
187 /** 191 public function testCanConfigureMethodsForDoubleOfNonExistentClass()
188 * ReflectionClass::getMethods for SoapClient on PHP 5.3 produces PHP Fatal Error 192 {
189 * @runInSeparateProcess 193 $className = 'X' . md5(microtime());
190 */ 194
191 public function testGetMockForSoapClientReflectionMethodsDuplication() 195 $mock = $this->generator->getMock($className, ['someMethod']);
192 { 196
193 if (version_compare(PHP_VERSION, '5.4.0', '>=')) { 197 $this->assertInstanceOf($className, $mock);
194 $this->markTestSkipped('Only for PHP < 5.4.0'); 198 }
195 } 199
196 200 public function testCanInvokeMethodsOfNonExistentClass()
197 $mock = $this->generator->getMock('SoapClient', array(), array(), '', false); 201 {
198 $this->assertInstanceOf('SoapClient', $mock); 202 $className = 'X' . md5(microtime());
203
204 $mock = $this->generator->getMock($className, ['someMethod']);
205
206 $mock->expects($this->once())->method('someMethod');
207
208 $this->assertNull($mock->someMethod());
199 } 209 }
200 } 210 }