Skip to content

Commit 44156e7

Browse files
committed
fix(tests): introduce rector
1 parent d8268ab commit 44156e7

8 files changed

Lines changed: 101 additions & 24 deletions

File tree

.github/workflows/php.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ jobs:
2323
- name: Run phpstan
2424
run: composer run-script phpstan
2525

26+
- name: Run rector:test
27+
run: composer run-script rector:test
28+
2629
- name: Run phpcs
2730
run: composer run-script phpcs
2831

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
},
1111
"require-dev": {
1212
"squizlabs/php_codesniffer": "^3.12",
13-
"phpstan/phpstan": "^2.1"
13+
"phpstan/phpstan": "^2.1",
14+
"rector/rector": "^2.3"
1415
},
1516
"license": "MIT",
1617
"autoload": {
@@ -37,6 +38,8 @@
3738
"phpstan:baseline": "@phpstan --generate-baseline --allow-empty-baseline -vv",
3839
"phpunit": "phpunit",
3940
"phpcs": "phpcs",
40-
"phpcbf": "phpcbf"
41+
"phpcbf": "phpcbf",
42+
"rector:test": "@rector:fix --dry-run",
43+
"rector:fix": "rector process"
4144
}
4245
}

rector.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/** @noinspection UsingInclusionReturnValueInspection */
4+
5+
declare(strict_types=1);
6+
7+
use Rector\CodeQuality\Rector\BooleanOr\RepeatedOrEqualToInArrayRector;
8+
use Rector\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector;
9+
use Rector\CodeQuality\Rector\ClassMethod\LocallyCalledStaticMethodToNonStaticRector;
10+
use Rector\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector;
11+
use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector;
12+
use Rector\CodeQuality\Rector\Ternary\SwitchNegatedTernaryRector;
13+
use Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector;
14+
use Rector\CodingStyle\Rector\ClassLike\NewlineBetweenClassLikeStmtsRector;
15+
use Rector\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector;
16+
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
17+
use Rector\CodingStyle\Rector\String_\SimplifyQuoteEscapeRector;
18+
use Rector\Config\RectorConfig;
19+
use Rector\EarlyReturn\Rector\If_\ChangeOrIfContinueToMultiContinueRector;
20+
use Rector\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector;
21+
use Rector\EarlyReturn\Rector\StmtsAwareInterface\ReturnEarlyIfVariableRector;
22+
use Rector\Naming\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector;
23+
use Rector\Naming\Rector\Class_\RenamePropertyToMatchTypeRector;
24+
use Rector\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector;
25+
use Rector\Naming\Rector\ClassMethod\RenameVariableToMatchNewTypeRector;
26+
use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector;
27+
use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchMethodCallReturnTypeRector;
28+
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector;
29+
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
30+
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector;
31+
32+
$config = RectorConfig::configure();
33+
34+
$config->withPreparedSets(
35+
true,
36+
true,
37+
true,
38+
true,
39+
true,
40+
true,
41+
true,
42+
true,
43+
true,
44+
false,
45+
true,
46+
true,
47+
true,
48+
);
49+
50+
$config
51+
->withPaths(
52+
[
53+
'./src',
54+
'./test/phpunit',
55+
// './.php-cs-fixer.dist.php',
56+
'./rector.php',
57+
]
58+
)
59+
->withSkip(
60+
[
61+
// Control
62+
DisallowedEmptyRuleFixerRector::class,
63+
FlipTypeControlToUseExclusiveTypeRector::class,
64+
65+
// New lines
66+
NewlineAfterStatementRector::class,
67+
NewlineBeforeNewAssignSetRector::class,
68+
NewlineBetweenClassLikeStmtsRector::class,
69+
70+
// Naming
71+
RenamePropertyToMatchTypeRector::class,
72+
RenameParamToMatchTypeRector::class,
73+
RenameVariableToMatchNewTypeRector::class,
74+
RenameVariableToMatchMethodCallReturnTypeRector::class,
75+
76+
// PHPUnit
77+
PreferPHPUnitThisCallRector::class,
78+
]
79+
)
80+
;
81+
82+
return $config;

src/MockedFunction.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,7 @@ static function (...$args) use (&$mockObject, &$method) {
5050
: $mockObject->expects($invocationRule)->method($method);
5151
}
5252

53-
/**
54-
* @param mixed ...$args
55-
*
56-
* @return mixed
57-
*/
58-
public function callOriginal(...$args): mixed
53+
public function callOriginal(mixed ...$args): mixed
5954
{
6055
return $this->mockedFunction->callOriginal(...$args);
6156
}

src/MockedMethod.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@ class MockedMethod extends AbstractMocked
2020
/**
2121
* MockedMethod constructor.
2222
*
23-
* @param TestCase $testCase
24-
* @param string $class
23+
* @param class-string $class
2524
* @param non-empty-string $method
26-
* @param InvocationOrder|null $invocationRule
2725
*
2826
* @throws ReflectionException
29-
*
30-
* @phpstan-param class-string $class
3127
*/
3228
public function __construct(
3329
TestCase $testCase,

test/phpunit/MockedFunctionTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99

1010
use function QratorLabs\SmockyPHPUnit\Test\fixtures\someFunction;
1111

12-
class MockedFunctionTest extends RunkitDependantTestCase
12+
final class MockedFunctionTest extends RunkitDependantTestCase
1313
{
1414
/**
15-
* @return void
1615
* @throws ReflectionException
1716
*/
1817
public function testCallOriginal(): void
@@ -26,27 +25,27 @@ public function testCallOriginal(): void
2625
// This was a bit tricky: we have to use `&$mock` to maintain variable-ref but not just object-ref
2726
// to do proper object destruction
2827
$mock->getMocker()->willReturnCallback(
29-
static function () use (&$mock, &$extValue) {
28+
static function () use (&$mock, &$extValue): string {
3029
$extValue = $mock->callOriginal();
3130

3231
return 'someFunction';
3332
}
3433
);
3534

3635
// is there any change?
37-
self::assertNotEquals($originalValue, $function());
36+
self::assertNotSame($originalValue, $function());
3837

3938
// call from outside
40-
self::assertEquals($originalValue, $mock->callOriginal());
39+
self::assertSame($originalValue, $mock->callOriginal());
4140

4241
// call from closure
43-
self::assertEquals($originalValue, $extValue);
42+
self::assertSame($originalValue, $extValue);
4443

4544
// assigment is used instead of `unset` because closure have link (ref) to mock-object
4645
// unsetting of local variable will not destruct object, but assigning variable to `null`
4746
// will do the job
4847
$mock = null;
49-
self::assertEquals($originalValue, $function());
48+
self::assertSame($originalValue, $function());
5049
}
5150

5251
/**

test/phpunit/MockedMethodTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* @internal
1616
*/
17-
class MockedMethodTest extends RunkitDependantTestCase
17+
final class MockedMethodTest extends RunkitDependantTestCase
1818
{
1919
/**
2020
* @throws ReflectionException
@@ -177,7 +177,7 @@ public function getValue(): string
177177

178178
self::assertSame('initial', $object->getValue());
179179
$mock = new MockedMethod($this, get_class($object), 'getValue', $this->once());
180-
$mock->getMocker()->willReturnCallback(static function () use ($object) {
180+
$mock->getMocker()->willReturnCallback(static function () use ($object): string {
181181
$object->value = 'changed';
182182

183183
return 'mocked';

test/phpunit/fixtures/ClassWithMethods.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public static function publicStaticMethod(): string
1919
}
2020

2121
/**
22-
* @return Generator
23-
* @phpstan-return Generator<string, array{class-string, string}>
22+
* @return Generator<string, array{class-string, string}>
2423
* @throws ReflectionException
2524
*/
2625
public static function getDataForTests(): Generator

0 commit comments

Comments
 (0)