Skip to content

Commit

Permalink
fix(package): minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm-ctrlz committed Feb 19, 2024
1 parent 9400cbc commit 42631f6
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 47 deletions.
4 changes: 3 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ parameters:
- ./test/phpstan/runkit7.stub.php
ignoreErrors:
- path: test/phpunit/Helpers/ClassWithMethods.php
message: '/^Method .*\\ClassWithMethods::privateMethod\(\) is unused\.$/'
message: '/^Method .*\\ClassWithMethods::privateMethod\(\) is unused\.$/'
- path: src/Phpunit/AbstractMocked.php
message: '/^Parameter #\d+ \S+ of method PHPUnit\\Framework\\MockObject\\Generator\\Generator::testDouble\(\)/'
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
backupGlobals="true"
cacheResultFile="./test/phpunit.cache.xml"
cacheResultFile="./test/caches/phpunit.cache.xml"
>
<coverage>
<include>
Expand Down
51 changes: 51 additions & 0 deletions src/Phpunit/AbstractMocked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace QratorLabs\Smocky\Phpunit;

use PHPUnit\Framework\MockObject\Generator\Generator;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use QratorLabs\Smocky\EmptyClass;
use ReflectionMethod;

use function assert;

abstract class AbstractMocked
{
/**
* @return MockObject&EmptyClass
*/
protected static function createEmptyMock(TestCase $testCase, string $method): MockObject
{
$args = [
EmptyClass::class, // string $type
true, // bool $mockObject
true, // bool $markAsMockObject
[$method], // ?array $methods = []
[], // array $arguments = []
'', // string $mockClassName = ''
false, // bool $callOriginalConstructor = true
false, // bool $callOriginalClone = true
true, // bool $callAutoload = true
false, // bool $cloneArguments = true
false, // bool $callOriginalMethods = false
null, // object $proxyTarget = null
false, // bool $allowMockingUnknownTypes = true
true, // bool $returnValueGeneration = true
];

if (count($args) > (new ReflectionMethod(Generator::class, 'testDouble'))->getNumberOfParameters()) {
// 10 -> 11 transition
unset($args[2]);
}

$mockObject = (new Generator())->testDouble(...$args);
assert($mockObject instanceof EmptyClass);
assert($mockObject instanceof MockObject);
$testCase->registerMockObject($mockObject);

return $mockObject;
}
}
27 changes: 2 additions & 25 deletions src/Phpunit/MockedFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@
namespace QratorLabs\Smocky\Phpunit;

use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
use PHPUnit\Framework\MockObject\Generator\Generator;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Rule\InvocationOrder;
use PHPUnit\Framework\TestCase;
use QratorLabs\Smocky\EmptyClass;
use QratorLabs\Smocky\Functions\MockedFunction as GenericMockedFunction;
use ReflectionException;

use function assert;

class MockedFunction
class MockedFunction extends AbstractMocked
{
/** @var GenericMockedFunction */
private $mockedFunction;
Expand Down Expand Up @@ -53,25 +48,7 @@ static function (...$args) use (&$mockObject, &$method) {
);

$method = $this->mockedFunction->getShortName();

$mockObject = (new Generator())->testDouble(
EmptyClass::class,
true,
true,
[$method],
[],
'',
false,
false,
true,
false,
false,
null,
false
);
assert($mockObject instanceof EmptyClass);
assert($mockObject instanceof MockObject);
$testCase->registerMockObject($mockObject);
$mockObject = self::createEmptyMock($testCase, $method);

if ($invocationRule === null) {
$this->invocationMocker = $mockObject->method($method);
Expand Down
22 changes: 2 additions & 20 deletions src/Phpunit/MockedMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use function assert;

class MockedMethod
class MockedMethod extends AbstractMocked
{
/**
* @var MockedClassMethod
Expand Down Expand Up @@ -47,25 +47,7 @@ public function __construct(
string $method,
InvocationOrder $invocationRule = null
) {
$mockObject = (new Generator())->testDouble(
EmptyClass::class,
true,
true,
[$method],
[],
'',
false,
false,
true,
false,
false,
null,
false
);
assert($mockObject instanceof EmptyClass);
assert($mockObject instanceof MockObject);
$this->mockObject = $mockObject;
$testCase->registerMockObject($this->mockObject);
$this->mockObject = self::createEmptyMock($testCase, $method);

if ($invocationRule === null) {
$this->invocationMocker = $this->mockObject->method($method);
Expand Down

0 comments on commit 42631f6

Please sign in to comment.