Skip to content

Commit 0c22f7b

Browse files
lucasmichotLucas Michot
andauthored
[13.x] testsuite (#59702)
* Add rules for testsuite * Modernize assertions and mocks --------- Co-authored-by: Lucas Michot <lucas@zaiple.com>
1 parent ff97ab4 commit 0c22f7b

31 files changed

Lines changed: 346 additions & 317 deletions

rector.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,55 @@
3434
use Rector\Php83\Rector\ClassConst\AddTypeToConstRector;
3535
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;
3636
use Rector\Php83\Rector\FuncCall\DynamicClassConstFetchRector;
37+
use Rector\PHPUnit\CodeQuality\Rector\CallLike\DirectInstanceOverMockArgRector;
38+
use Rector\PHPUnit\CodeQuality\Rector\Class_\ConstructClassMethodToSetUpTestCaseRector;
39+
use Rector\PHPUnit\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector;
40+
use Rector\PHPUnit\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector;
41+
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector;
42+
use Rector\PHPUnit\CodeQuality\Rector\Class_\RemoveNeverUsedMockPropertyRector;
43+
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\EntityDocumentCreateMockToDirectNewRector;
44+
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\RemoveEmptyTestMethodRector;
45+
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\RemoveStandaloneCreateMockRector;
46+
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\ReplaceTestAnnotationWithPrefixedFunctionRector;
47+
use Rector\PHPUnit\CodeQuality\Rector\Foreach_\SimplifyForeachInstanceOfRector;
48+
use Rector\PHPUnit\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector;
49+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\MergeWithCallableAndWillReturnRector;
50+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\NarrowIdenticalWithConsecutiveRector;
51+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\NarrowSingleWillReturnCallbackRector;
52+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\RemoveExpectAnyFromMockRector;
53+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\SimplerWithIsInstanceOfRector;
54+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector;
55+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWillMethodRector;
56+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWithMethodRector;
57+
use Rector\PHPUnit\PHPUnit60\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector;
58+
use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector;
3759
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;
3860

61+
$testsuiteRules = [
62+
AssertFuncCallToPHPUnitAssertRector::class,
63+
ConstructClassMethodToSetUpTestCaseRector::class,
64+
DirectInstanceOverMockArgRector::class,
65+
EntityDocumentCreateMockToDirectNewRector::class,
66+
GetMockBuilderGetMockToCreateMockRector::class,
67+
InlineStubPropertyToCreateStubMethodCallRector::class,
68+
MergeWithCallableAndWillReturnRector::class,
69+
NarrowIdenticalWithConsecutiveRector::class,
70+
NarrowSingleWillReturnCallbackRector::class,
71+
NarrowUnusedSetUpDefinedPropertyRector::class,
72+
PreferPHPUnitThisCallRector::class,
73+
RemoveEmptyTestMethodRector::class,
74+
RemoveExpectAnyFromMockRector::class,
75+
RemoveNeverUsedMockPropertyRector::class,
76+
RemoveStandaloneCreateMockRector::class,
77+
ReplaceAtMethodWithDesiredMatcherRector::class,
78+
ReplaceTestAnnotationWithPrefixedFunctionRector::class,
79+
SimplerWithIsInstanceOfRector::class,
80+
SimplifyForeachInstanceOfRector::class,
81+
SingleWithConsecutiveToWithRector::class,
82+
UseSpecificWillMethodRector::class,
83+
UseSpecificWithMethodRector::class,
84+
];
85+
3986
return RectorConfig::configure()
4087
->withRootFiles()
4188
->withPaths([
@@ -78,6 +125,7 @@
78125
'tests/Foundation/fixtures/bad-syntax-strategy.php',
79126
])
80127
->withRules([
128+
...$testsuiteRules,
81129
CountArrayToEmptyArrayComparisonRector::class,
82130
StrlenZeroToIdenticalEmptyStringRector::class,
83131
])

tests/Auth/AuthGuardTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function testAttemptReturnsUserInterface()
116116
$guard->getProvider()->shouldReceive('retrieveByCredentials')->once()->andReturn($user);
117117
$guard->getProvider()->shouldReceive('validateCredentials')->with($user, ['foo'])->andReturn(true);
118118
$guard->getProvider()->shouldReceive('rehashPasswordIfRequired')->with($user, ['foo'])->once();
119-
$guard->expects($this->once())->method('login')->with($this->equalTo($user));
119+
$guard->expects($this->once())->method('login')->with($user);
120120
$this->assertTrue($guard->attempt(['foo']));
121121
}
122122

@@ -160,15 +160,15 @@ public function testAttemptAndWithCallbacks()
160160
$mock->getProvider()->shouldReceive('rehashPasswordIfRequired')->with($user, ['foo'])->once();
161161

162162
$this->assertTrue($mock->attemptWhen(['foo'], function ($user, $guard) {
163-
static::assertInstanceOf(Authenticatable::class, $user);
164-
static::assertInstanceOf(SessionGuard::class, $guard);
163+
$this->assertInstanceOf(Authenticatable::class, $user);
164+
$this->assertInstanceOf(SessionGuard::class, $guard);
165165

166166
return true;
167167
}));
168168

169169
$this->assertFalse($mock->attemptWhen(['foo'], function ($user, $guard) {
170-
static::assertInstanceOf(Authenticatable::class, $user);
171-
static::assertInstanceOf(SessionGuard::class, $guard);
170+
$this->assertInstanceOf(Authenticatable::class, $user);
171+
$this->assertInstanceOf(SessionGuard::class, $guard);
172172

173173
return false;
174174
}));
@@ -196,7 +196,7 @@ public function testAttemptRehashesPasswordWhenRequired()
196196
$guard->getProvider()->shouldReceive('retrieveByCredentials')->once()->andReturn($user);
197197
$guard->getProvider()->shouldReceive('validateCredentials')->with($user, ['foo'])->andReturn(true);
198198
$guard->getProvider()->shouldReceive('rehashPasswordIfRequired')->with($user, ['foo'])->once();
199-
$guard->expects($this->once())->method('login')->with($this->equalTo($user));
199+
$guard->expects($this->once())->method('login')->with($user);
200200
$this->assertTrue($guard->attempt(['foo']));
201201
}
202202

@@ -216,7 +216,7 @@ public function testAttemptDoesntRehashPasswordWhenDisabled()
216216
$guard->getProvider()->shouldReceive('retrieveByCredentials')->once()->andReturn($user);
217217
$guard->getProvider()->shouldReceive('validateCredentials')->with($user, ['foo'])->andReturn(true);
218218
$guard->getProvider()->shouldNotReceive('rehashPasswordIfRequired');
219-
$guard->expects($this->once())->method('login')->with($this->equalTo($user));
219+
$guard->expects($this->once())->method('login')->with($user);
220220
$this->assertTrue($guard->attempt(['foo']));
221221
}
222222

tests/Auth/AuthListenersSendEmailVerificationNotificationHandleFunctionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class AuthListenersSendEmailVerificationNotificationHandleFunctionTest extends T
1616
*/
1717
public function testWillExecuted()
1818
{
19-
$user = $this->getMockBuilder(MustVerifyEmail::class)->getMock();
19+
$user = $this->createMock(MustVerifyEmail::class);
2020
$user->method('hasVerifiedEmail')->willReturn(false);
2121
$user->expects($this->once())->method('sendEmailVerificationNotification');
2222

@@ -43,7 +43,7 @@ public function testUserIsNotInstanceOfMustVerifyEmail()
4343
*/
4444
public function testHasVerifiedEmailAsTrue()
4545
{
46-
$user = $this->getMockBuilder(MustVerifyEmail::class)->getMock();
46+
$user = $this->createMock(MustVerifyEmail::class);
4747
$user->method('hasVerifiedEmail')->willReturn(true);
4848
$user->expects($this->never())->method('sendEmailVerificationNotification');
4949

tests/Cache/CacheApcStoreTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class CacheApcStoreTest extends TestCase
1212
public function testGetReturnsNullWhenNotFound()
1313
{
1414
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['get'])->getMock();
15-
$apc->expects($this->once())->method('get')->with($this->equalTo('foobar'))->willReturn(null);
15+
$apc->expects($this->once())->method('get')->with('foobar')->willReturn(null);
1616
$store = new ApcStore($apc, 'foo');
1717
$this->assertNull($store->get('bar'));
1818
}
@@ -53,7 +53,7 @@ public function testSetMethodProperlyCallsAPC()
5353
{
5454
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['put'])->getMock();
5555
$apc->expects($this->once())
56-
->method('put')->with($this->equalTo('foo'), $this->equalTo('bar'), $this->equalTo(60))
56+
->method('put')->with('foo', 'bar', 60)
5757
->willReturn(true);
5858
$store = new ApcStore($apc);
5959
$result = $store->put('foo', 'bar', 60);
@@ -91,15 +91,15 @@ public function testSetMultipleMethodProperlyCallsAPC()
9191
public function testIncrementMethodProperlyCallsAPC()
9292
{
9393
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['increment'])->getMock();
94-
$apc->expects($this->once())->method('increment')->with($this->equalTo('foo'), $this->equalTo(5));
94+
$apc->expects($this->once())->method('increment')->with('foo', 5);
9595
$store = new ApcStore($apc);
9696
$store->increment('foo', 5);
9797
}
9898

9999
public function testDecrementMethodProperlyCallsAPC()
100100
{
101101
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['decrement'])->getMock();
102-
$apc->expects($this->once())->method('decrement')->with($this->equalTo('foo'), $this->equalTo(5));
102+
$apc->expects($this->once())->method('decrement')->with('foo', 5);
103103
$store = new ApcStore($apc);
104104
$store->decrement('foo', 5);
105105
}
@@ -108,7 +108,7 @@ public function testStoreItemForeverProperlyCallsAPC()
108108
{
109109
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['put'])->getMock();
110110
$apc->expects($this->once())
111-
->method('put')->with($this->equalTo('foo'), $this->equalTo('bar'), $this->equalTo(0))
111+
->method('put')->with('foo', 'bar', 0)
112112
->willReturn(true);
113113
$store = new ApcStore($apc);
114114
$result = $store->forever('foo', 'bar');
@@ -118,7 +118,7 @@ public function testStoreItemForeverProperlyCallsAPC()
118118
public function testForgetMethodProperlyCallsAPC()
119119
{
120120
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['delete'])->getMock();
121-
$apc->expects($this->once())->method('delete')->with($this->equalTo('foo'))->willReturn(true);
121+
$apc->expects($this->once())->method('delete')->with('foo')->willReturn(true);
122122
$store = new ApcStore($apc);
123123
$result = $store->forget('foo');
124124
$this->assertTrue($result);
@@ -131,8 +131,8 @@ public function testTouchMethodProperlyCallsAPC(): void
131131

132132
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['get', 'put'])->getMock();
133133

134-
$apc->expects($this->once())->method('get')->with($this->equalTo($key))->willReturn('bar');
135-
$apc->expects($this->once())->method('put')->with($this->equalTo($key), $this->equalTo('bar'), $this->equalTo($ttl))->willReturn(true);
134+
$apc->expects($this->once())->method('get')->with($key)->willReturn('bar');
135+
$apc->expects($this->once())->method('put')->with($key, 'bar', $ttl)->willReturn(true);
136136

137137
$this->assertTrue((new ApcStore($apc))->touch($key, $ttl));
138138
}

tests/Cache/CacheArrayStoreTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function testStoreItemForeverProperlyStoresInArray()
9090
{
9191
$mock = $this->getMockBuilder(ArrayStore::class)->onlyMethods(['put'])->getMock();
9292
$mock->expects($this->once())
93-
->method('put')->with($this->equalTo('foo'), $this->equalTo('bar'), $this->equalTo(0))
93+
->method('put')->with('foo', 'bar', 0)
9494
->willReturn(true);
9595
$result = $mock->forever('foo', 'bar');
9696
$this->assertTrue($result);

tests/Cache/CacheDatabaseStoreTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function testValueIsUpsertedOnSqlite()
114114
public function testForeverCallsStoreItemWithReallyLongTime()
115115
{
116116
$store = $this->getMockBuilder(DatabaseStore::class)->onlyMethods(['put'])->setConstructorArgs($this->getMocks())->getMock();
117-
$store->expects($this->once())->method('put')->with($this->equalTo('foo'), $this->equalTo('bar'), $this->equalTo(315360000))->willReturn(true);
117+
$store->expects($this->once())->method('put')->with('foo', 'bar', 315360000)->willReturn(true);
118118
$result = $store->forever('foo', 'bar');
119119
$this->assertTrue($result);
120120
}

0 commit comments

Comments
 (0)