minor fix to prior commit
[openemr.git] / vendor / phpunit / phpunit-mock-objects / tests / GeneratorTest.php
blob72f32c25861b2d50035961396380a2b7cf189646
1 <?php
2 /**
3 * @covers PHPUnit_Framework_MockObject_Generator
5 * @uses PHPUnit_Framework_MockObject_InvocationMocker
6 * @uses PHPUnit_Framework_MockObject_Builder_InvocationMocker
7 * @uses PHPUnit_Framework_MockObject_Invocation_Object
8 * @uses PHPUnit_Framework_MockObject_Invocation_Static
9 * @uses PHPUnit_Framework_MockObject_Matcher
10 * @uses PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
11 * @uses PHPUnit_Framework_MockObject_Matcher_MethodName
12 * @uses PHPUnit_Framework_MockObject_Stub_Return
13 * @uses PHPUnit_Framework_MockObject_Matcher_InvokedCount
15 class Framework_MockObject_GeneratorTest extends PHPUnit_Framework_TestCase
17 /**
18 * @var PHPUnit_Framework_MockObject_Generator
20 private $generator;
22 protected function setUp()
24 $this->generator = new PHPUnit_Framework_MockObject_Generator;
27 /**
28 * @expectedException PHPUnit_Framework_MockObject_RuntimeException
30 public function testGetMockFailsWhenInvalidFunctionNameIsPassedInAsAFunctionToMock()
32 $this->generator->getMock(stdClass::class, [0]);
35 public function testGetMockCanCreateNonExistingFunctions()
37 $mock = $this->generator->getMock(stdClass::class, ['testFunction']);
39 $this->assertTrue(method_exists($mock, 'testFunction'));
42 /**
43 * @expectedException PHPUnit_Framework_MockObject_RuntimeException
44 * @expectedExceptionMessage duplicates: "foo, bar, foo" (duplicate: "foo")
46 public function testGetMockGeneratorFails()
48 $this->generator->getMock(stdClass::class, ['foo', 'bar', 'foo']);
51 /**
52 * @requires PHP 7
54 public function testGetMockBlacklistedMethodNamesPhp7()
56 $mock = $this->generator->getMock(InterfaceWithSemiReservedMethodName::class);
58 $this->assertTrue(method_exists($mock, 'unset'));
59 $this->assertInstanceOf(InterfaceWithSemiReservedMethodName::class, $mock);
62 public function testGetMockForAbstractClassDoesNotFailWhenFakingInterfaces()
64 $mock = $this->generator->getMockForAbstractClass(Countable::class);
66 $this->assertTrue(method_exists($mock, 'count'));
69 public function testGetMockForAbstractClassStubbingAbstractClass()
71 $mock = $this->generator->getMockForAbstractClass(AbstractMockTestClass::class);
73 $this->assertTrue(method_exists($mock, 'doSomething'));
76 public function testGetMockForAbstractClassWithNonExistentMethods()
78 $mock = $this->generator->getMockForAbstractClass(
79 AbstractMockTestClass::class,
80 [],
81 '',
82 true,
83 true,
84 true,
85 ['nonexistentMethod']
88 $this->assertTrue(method_exists($mock, 'nonexistentMethod'));
89 $this->assertTrue(method_exists($mock, 'doSomething'));
92 public function testGetMockForAbstractClassShouldCreateStubsOnlyForAbstractMethodWhenNoMethodsWereInformed()
94 $mock = $this->generator->getMockForAbstractClass(AbstractMockTestClass::class);
96 $mock->expects($this->any())
97 ->method('doSomething')
98 ->willReturn('testing');
100 $this->assertEquals('testing', $mock->doSomething());
101 $this->assertEquals(1, $mock->returnAnything());
105 * @dataProvider getMockForAbstractClassExpectsInvalidArgumentExceptionDataprovider
106 * @expectedException PHPUnit_Framework_Exception
108 public function testGetMockForAbstractClassExpectingInvalidArgumentException($className, $mockClassName)
110 $this->generator->getMockForAbstractClass($className, [], $mockClassName);
114 * @expectedException PHPUnit_Framework_MockObject_RuntimeException
116 public function testGetMockForAbstractClassAbstractClassDoesNotExist()
118 $this->generator->getMockForAbstractClass('Tux');
121 public function getMockForAbstractClassExpectsInvalidArgumentExceptionDataprovider()
123 return [
124 'className not a string' => [[], ''],
125 'mockClassName not a string' => [Countable::class, new stdClass],
129 public function testGetMockForTraitWithNonExistentMethodsAndNonAbstractMethods()
131 $mock = $this->generator->getMockForTrait(
132 AbstractTrait::class,
135 true,
136 true,
137 true,
138 ['nonexistentMethod']
141 $this->assertTrue(method_exists($mock, 'nonexistentMethod'));
142 $this->assertTrue(method_exists($mock, 'doSomething'));
143 $this->assertTrue($mock->mockableMethod());
144 $this->assertTrue($mock->anotherMockableMethod());
147 public function testGetMockForTraitStubbingAbstractMethod()
149 $mock = $this->generator->getMockForTrait(AbstractTrait::class);
151 $this->assertTrue(method_exists($mock, 'doSomething'));
154 public function testGetMockForSingletonWithReflectionSuccess()
156 $mock = $this->generator->getMock(SingletonClass::class, ['doSomething'], [], '', false);
158 $this->assertInstanceOf('SingletonClass', $mock);
162 * @expectedException PHPUnit_Framework_MockObject_RuntimeException
164 public function testExceptionIsRaisedForMutuallyExclusiveOptions()
166 $this->generator->getMock(stdClass::class, [], [], '', false, true, true, true, true);
170 * @requires PHP 7
172 public function testCanImplementInterfacesThatHaveMethodsWithReturnTypes()
174 $stub = $this->generator->getMock([AnInterfaceWithReturnType::class, AnInterface::class]);
176 $this->assertInstanceOf(AnInterfaceWithReturnType::class, $stub);
177 $this->assertInstanceOf(AnInterface::class, $stub);
178 $this->assertInstanceOf(PHPUnit_Framework_MockObject_MockObject::class, $stub);
181 public function testCanConfigureMethodsForDoubleOfNonExistentClass()
183 $className = 'X' . md5(microtime());
185 $mock = $this->generator->getMock($className, ['someMethod']);
187 $this->assertInstanceOf($className, $mock);
190 public function testCanInvokeMethodsOfNonExistentClass()
192 $className = 'X' . md5(microtime());
194 $mock = $this->generator->getMock($className, ['someMethod']);
196 $mock->expects($this->once())->method('someMethod');
198 $this->assertNull($mock->someMethod());