Skip to content

Commit 42631f6

Browse files
committed
fix(package): minor fix
1 parent 9400cbc commit 42631f6

File tree

5 files changed

+59
-47
lines changed

5 files changed

+59
-47
lines changed

phpstan.neon.dist

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ parameters:
1111
- ./test/phpstan/runkit7.stub.php
1212
ignoreErrors:
1313
- path: test/phpunit/Helpers/ClassWithMethods.php
14-
message: '/^Method .*\\ClassWithMethods::privateMethod\(\) is unused\.$/'
14+
message: '/^Method .*\\ClassWithMethods::privateMethod\(\) is unused\.$/'
15+
- path: src/Phpunit/AbstractMocked.php
16+
message: '/^Parameter #\d+ \S+ of method PHPUnit\\Framework\\MockObject\\Generator\\Generator::testDouble\(\)/'

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
55
backupGlobals="true"
6-
cacheResultFile="./test/phpunit.cache.xml"
6+
cacheResultFile="./test/caches/phpunit.cache.xml"
77
>
88
<coverage>
99
<include>

src/Phpunit/AbstractMocked.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace QratorLabs\Smocky\Phpunit;
6+
7+
use PHPUnit\Framework\MockObject\Generator\Generator;
8+
use PHPUnit\Framework\MockObject\MockObject;
9+
use PHPUnit\Framework\TestCase;
10+
use QratorLabs\Smocky\EmptyClass;
11+
use ReflectionMethod;
12+
13+
use function assert;
14+
15+
abstract class AbstractMocked
16+
{
17+
/**
18+
* @return MockObject&EmptyClass
19+
*/
20+
protected static function createEmptyMock(TestCase $testCase, string $method): MockObject
21+
{
22+
$args = [
23+
EmptyClass::class, // string $type
24+
true, // bool $mockObject
25+
true, // bool $markAsMockObject
26+
[$method], // ?array $methods = []
27+
[], // array $arguments = []
28+
'', // string $mockClassName = ''
29+
false, // bool $callOriginalConstructor = true
30+
false, // bool $callOriginalClone = true
31+
true, // bool $callAutoload = true
32+
false, // bool $cloneArguments = true
33+
false, // bool $callOriginalMethods = false
34+
null, // object $proxyTarget = null
35+
false, // bool $allowMockingUnknownTypes = true
36+
true, // bool $returnValueGeneration = true
37+
];
38+
39+
if (count($args) > (new ReflectionMethod(Generator::class, 'testDouble'))->getNumberOfParameters()) {
40+
// 10 -> 11 transition
41+
unset($args[2]);
42+
}
43+
44+
$mockObject = (new Generator())->testDouble(...$args);
45+
assert($mockObject instanceof EmptyClass);
46+
assert($mockObject instanceof MockObject);
47+
$testCase->registerMockObject($mockObject);
48+
49+
return $mockObject;
50+
}
51+
}

src/Phpunit/MockedFunction.php

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@
55
namespace QratorLabs\Smocky\Phpunit;
66

77
use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
8-
use PHPUnit\Framework\MockObject\Generator\Generator;
9-
use PHPUnit\Framework\MockObject\MockObject;
108
use PHPUnit\Framework\MockObject\Rule\InvocationOrder;
119
use PHPUnit\Framework\TestCase;
12-
use QratorLabs\Smocky\EmptyClass;
1310
use QratorLabs\Smocky\Functions\MockedFunction as GenericMockedFunction;
1411
use ReflectionException;
1512

16-
use function assert;
17-
18-
class MockedFunction
13+
class MockedFunction extends AbstractMocked
1914
{
2015
/** @var GenericMockedFunction */
2116
private $mockedFunction;
@@ -53,25 +48,7 @@ static function (...$args) use (&$mockObject, &$method) {
5348
);
5449

5550
$method = $this->mockedFunction->getShortName();
56-
57-
$mockObject = (new Generator())->testDouble(
58-
EmptyClass::class,
59-
true,
60-
true,
61-
[$method],
62-
[],
63-
'',
64-
false,
65-
false,
66-
true,
67-
false,
68-
false,
69-
null,
70-
false
71-
);
72-
assert($mockObject instanceof EmptyClass);
73-
assert($mockObject instanceof MockObject);
74-
$testCase->registerMockObject($mockObject);
51+
$mockObject = self::createEmptyMock($testCase, $method);
7552

7653
if ($invocationRule === null) {
7754
$this->invocationMocker = $mockObject->method($method);

src/Phpunit/MockedMethod.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use function assert;
1717

18-
class MockedMethod
18+
class MockedMethod extends AbstractMocked
1919
{
2020
/**
2121
* @var MockedClassMethod
@@ -47,25 +47,7 @@ public function __construct(
4747
string $method,
4848
InvocationOrder $invocationRule = null
4949
) {
50-
$mockObject = (new Generator())->testDouble(
51-
EmptyClass::class,
52-
true,
53-
true,
54-
[$method],
55-
[],
56-
'',
57-
false,
58-
false,
59-
true,
60-
false,
61-
false,
62-
null,
63-
false
64-
);
65-
assert($mockObject instanceof EmptyClass);
66-
assert($mockObject instanceof MockObject);
67-
$this->mockObject = $mockObject;
68-
$testCase->registerMockObject($this->mockObject);
50+
$this->mockObject = self::createEmptyMock($testCase, $method);
6951

7052
if ($invocationRule === null) {
7153
$this->invocationMocker = $this->mockObject->method($method);

0 commit comments

Comments
 (0)