1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
<?php
namespace phpmock\phpunit;
use Mockery;
use phpmock\integration\MockDelegateFunctionBuilder;
use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
use PHPUnit\Framework\MockObject\ConfigurableMethod;
use PHPUnit\Framework\MockObject\InvocationHandler;
use PHPUnit\Framework\MockObject\Matcher\Invocation;
use PHPUnit\Framework\MockObject\Matcher\MethodName;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub\MatcherCollection;
use PHPUnit\Framework\TestCase;
use SebastianBergmann\Type\Type;
/**
* Tests MockObjectProxyTest.
*
* @author Markus Malkusch <markus@malkusch.de>
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
* @license http://www.wtfpl.net/txt/copying/ WTFPL
* @see MockObjectProxy
* @requires PHPUnit 4.5.0
*/
class MockObjectProxyTest extends TestCase
{
/**
* Tests expects()
*
* @test
*/
public function testExpects()
{
$matcher = $this->getMockBuilder(Invocation::class)->getMock();
if (class_exists(ConfigurableMethod::class)) {
if (class_exists(\PHPUnit\Runner\Version::class)
&& version_compare(\PHPUnit\Runner\Version::id(), '10.5.0') >= 0
) {
$methods = new ConfigurableMethod(
MockDelegateFunctionBuilder::METHOD,
[],
0,
Mockery::mock(Type::class)
);
} else {
$methods = new ConfigurableMethod(
MockDelegateFunctionBuilder::METHOD,
Mockery::mock(Type::class)
);
}
} else {
$methods = [MockDelegateFunctionBuilder::METHOD];
}
if (class_exists(\PHPUnit\Runner\Version::class)
&& version_compare(\PHPUnit\Runner\Version::id(), '8.4.0') >= 0
) {
$invocationHandler = new InvocationHandler([$methods], false);
$invocationMocker = $invocationHandler->expects($matcher);
} else {
$invocationMocker = new InvocationMocker(
$this->getMockBuilder(MatcherCollection::class)->getMock(),
$this->getMockBuilder(Invocation::class)->getMock(),
$methods
);
}
$mock = Mockery::mock(MockObject::class);
$mock->shouldReceive('expects')->with($matcher)->andReturn($invocationMocker);
$proxy = new MockObjectProxy($mock);
$result = $proxy->expects($matcher);
$this->assertEquals($invocationMocker, $result);
$this->assertSame(
(new MethodName(MockDelegateFunctionBuilder::METHOD))->toString(),
$this->getMethodMatcher($invocationMocker)->toString()
);
}
private function getMethodMatcher($invocationMocker)
{
$hasVersion = class_exists(\PHPUnit\Runner\Version::class);
if ($hasVersion
&& version_compare(\PHPUnit\Runner\Version::id(), '10.0.0') >= 0
) {
$reflection = new \ReflectionClass(InvocationMocker::class);
$property = $reflection->getProperty('matcher');
$property->setAccessible(true);
return $property->getValue($invocationMocker)->methodNameRule();
}
if ($hasVersion
&& version_compare(\PHPUnit\Runner\Version::id(), '8.4.0') >= 0
) {
$reflection = new \ReflectionClass(InvocationMocker::class);
$property = $reflection->getProperty('matcher');
$property->setAccessible(true);
return $property->getValue($invocationMocker)->getMethodNameRule();
}
return $invocationMocker->getMatcher()->methodNameMatcher ??
$invocationMocker->getMatcher()->getMethodNameMatcher();
}
/**
* Tests delegation of __phpunit_hasMatchers().
*
* Before PHPUnit-5 __phpunit_hasMatchers() was not part of the contract.
* But it was used within PHPUnit as it would be. Unfortunately the
* mocking framework Prophecy will not allow to mock this method.
*
* @test
* @requires PHPUnit 5
*/
public function testHasMatcher()
{
$mock = Mockery::mock(MockObject::class);
$mock->shouldReceive('__phpunit_hasMatchers')->andReturn(true);
$proxy = new MockObjectProxy($mock);
$result = $proxy->__phpunit_hasMatchers();
$this->assertTrue($result);
}
/**
* Tests calling the proxy forwards the call to the subject.
*
* @param string $method The proxy method.
* @param array $arguments The optional arguments.
*
* @test
* @dataProvider provideTestProxiedMethods
*/
public function testProxiedMethods($method, array $arguments = [], $expected = null)
{
$mock = Mockery::mock(MockObject::class);
if ($expected) {
$mock->shouldReceive($method)->withArgs($arguments)->andReturn($expected)->times(1);
} else {
$mock->shouldReceive($method)->withArgs($arguments)->times(1);
}
$proxy = new MockObjectProxy($mock);
$result = call_user_func_array([$proxy, $method], $arguments);
if ($expected) {
$this->assertSame($expected, $result);
}
}
/**
* Returns the test cases for testProxiedMethods().
*
* @return array Test cases.
*
* @SuppressWarnings(PHPMD)
*/
public static function provideTestProxiedMethods()
{
$return = [];
if (class_exists(\PHPUnit\Runner\Version::class)
&& version_compare(\PHPUnit\Runner\Version::id(), '8.4.0') >= 0
) {
$return[] = ['__phpunit_getInvocationHandler', [], new InvocationHandler([], false)];
} else {
$return[] = [
'__phpunit_getInvocationMocker',
[],
new \PHPUnit\Framework\MockObject\InvocationMocker([], true)
];
}
$return[] = ['__phpunit_setOriginalObject', [new \stdClass()]];
$return[] = ['__phpunit_verify', [true]];
return $return;
}
}
|