Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHPUnit 10] Add ifs as default way for withConsecutive, as more readable and opens upgrade during PHPUnit 9 #382

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/sets/phpunit90.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\PHPUnit100\Rector\StmtsAwareInterface\WithConsecutiveRector;
use Rector\PHPUnit\PHPUnit90\Rector\Class_\TestListenerToHooksRector;
use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\ExplicitPhpErrorApiRector;
use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\SpecificAssertContainsWithoutIdentityRector;
Expand All @@ -14,6 +15,7 @@
TestListenerToHooksRector::class,
ExplicitPhpErrorApiRector::class,
SpecificAssertContainsWithoutIdentityRector::class,
WithConsecutiveRector::class,
]);

$rectorConfig->ruleWithConfiguration(RenameMethodRector::class, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ final class AvoidDuplicatedVariables extends TestCase

$this->personServiceMock->expects($matcher)
->method('prepare')->willReturnCallback(function (...$parameters) use ($matcher, $value) {
match ($matcher->numberOfInvocations()) {
1 => self::assertEquals([1, $value], $parameters),
2 => self::assertEquals([2, $value], $parameters),
};
if ($matcher->numberOfInvocations() === 1) {
$this->assertSame(1, $parameters[0]);
$this->assertSame($value, $parameters[1]);
}
if ($matcher->numberOfInvocations() === 2) {
$this->assertSame(2, $parameters[0]);
$this->assertSame($value, $parameters[1]);
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ final class AvoidNestedUses extends TestCase

$this->personServiceMock->expects($matcher)
->method('prepare')->willReturnCallback(function (...$parameters) use ($matcher, $value) {
match ($matcher->numberOfInvocations()) {
1 => self::assertEquals([1, $value], $parameters),
2 => self::assertEquals([2, function ($nested) {
if ($matcher->numberOfInvocations() === 1) {
$this->assertSame(1, $parameters[0]);
$this->assertSame($value, $parameters[1]);
}
if ($matcher->numberOfInvocations() === 2) {
$this->assertSame(2, $parameters[0]);
$this->assertSame(function ($nested) {
$nestedReturn = 1000;
return $nestedReturn;
}], $parameters),
};
}, $parameters[1]);
}
});
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ final class CombineWithWillReturn extends TestCase
$this->userServiceMock->expects(self::exactly(2))
->method('prepare')
->withConsecutive(
[1, 2],
[3, 4],
[1],
)
->willReturn(5);
}
Expand All @@ -32,13 +31,15 @@ final class CombineWithWillReturn extends TestCase
{
$matcher = self::exactly(2);
$this->userServiceMock->expects($matcher)
->method('prepare')->willReturnCallback(function (...$parameters) use ($matcher) {
match ($matcher->numberOfInvocations()) {
1 => self::assertEquals([1, 2], $parameters),
2 => self::assertEquals([3, 4], $parameters),
};
return 5;
});
->method('prepare')
->willReturnCallback(
function (...$parameters) use ($matcher) {
if ($matcher->numberOfInvocations() === 1) {
$this->assertSame(1, $parameters[0]);
}
return 5;
},
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ final class CombineWithWillReturnArgument extends TestCase
$this->userServiceMock->expects(self::exactly(2))
->method('prepare')
->withConsecutive(
[1, 2],
[3, 4],
[1],
)
->willReturnArgument(0);
}
Expand All @@ -32,13 +31,15 @@ final class CombineWithWillReturnArgument extends TestCase
{
$matcher = self::exactly(2);
$this->userServiceMock->expects($matcher)
->method('prepare')->willReturnCallback(function (...$parameters) use ($matcher) {
match ($matcher->numberOfInvocations()) {
1 => self::assertEquals([1, 2], $parameters),
2 => self::assertEquals([3, 4], $parameters),
};
return $parameters[0];
});
->method('prepare')
->willReturnCallback(
function (...$parameters) use ($matcher) {
if ($matcher->numberOfInvocations() === 1) {
$this->assertSame(1, $parameters[0]);
}
return $parameters[0];
},
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ final class CombineWithWillReturnOnConsecutiveCalls extends TestCase
$this->userServiceMock->expects(self::exactly(2))
->method('prepare')
->withConsecutive(
[1, 2],
[3, 4],
[1],
)
->willReturnOnConsecutiveCalls(5, 6);
->willReturnOnConsecutiveCalls(5);
}
}

Expand All @@ -32,16 +31,15 @@ final class CombineWithWillReturnOnConsecutiveCalls extends TestCase
{
$matcher = self::exactly(2);
$this->userServiceMock->expects($matcher)
->method('prepare')->willReturnCallback(function (...$parameters) use ($matcher) {
if ($matcher->numberOfInvocations() === 1) {
$this->assertSame([1, 2], $parameters);
return 5;
}
if ($matcher->numberOfInvocations() === 2) {
$this->assertSame([3, 4], $parameters);
return 6;
}
});
->method('prepare')
->willReturnCallback(
function (...$parameters) use ($matcher) {
if ($matcher->numberOfInvocations() === 1) {
$this->assertSame([1], $parameters);
return 5;
}
},
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ final class CombineWithWillReturnReference extends TestCase
$this->userServiceMock->expects(self::exactly(2))
->method('prepare')
->withConsecutive(
[1, 2],
[3, 4],
[1],
)
->willReturnReference($value);
}
Expand All @@ -34,13 +33,15 @@ final class CombineWithWillReturnReference extends TestCase
$value = 42;
$matcher = self::exactly(2);
$this->userServiceMock->expects($matcher)
->method('prepare')->willReturnCallback(function &(...$parameters) use ($matcher, $value) {
match ($matcher->numberOfInvocations()) {
1 => self::assertEquals([1, 2], $parameters),
2 => self::assertEquals([3, 4], $parameters),
};
return $value;
});
->method('prepare')
->willReturnCallback(
function &(...$parameters) use ($matcher, $value) {
if ($matcher->numberOfInvocations() === 1) {
$this->assertSame(1, $parameters[0]);
}
return $value;
},
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ final class CombineWithWillReturnSelfThis extends TestCase
$this->userServiceMock->expects(self::exactly(2))
->method('prepare')
->withConsecutive(
[1, 2],
[3, 4],
[1],
)
->willReturnSelf();
}
Expand All @@ -32,13 +31,15 @@ final class CombineWithWillReturnSelfThis extends TestCase
{
$matcher = self::exactly(2);
$this->userServiceMock->expects($matcher)
->method('prepare')->willReturnCallback(function (...$parameters) use ($matcher) {
match ($matcher->numberOfInvocations()) {
1 => self::assertEquals([1, 2], $parameters),
2 => self::assertEquals([3, 4], $parameters),
};
return $this->userServiceMock;
});
->method('prepare')
->willReturnCallback(
function (...$parameters) use ($matcher) {
if ($matcher->numberOfInvocations() === 1) {
$this->assertSame(1, $parameters[0]);
}
return $this->userServiceMock;
},
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ final class CombineWithWillReturnSelfThis extends TestCase
$userServiceMock->expects(self::exactly(2))
->method('prepare')
->withConsecutive(
[1, 2],
[3, 4],
[1],
)
->willReturnSelf();
}
Expand All @@ -34,13 +33,15 @@ final class CombineWithWillReturnSelfThis extends TestCase
$userServiceMock = $this->createMock(\Rector\PHPUnit\Tests\PHPUnit100\Rector\StmtsAwareInterface\WithConsecutiveRector\Fixture\CombineWithWillReturnSelfThis::class);
$matcher = self::exactly(2);
$userServiceMock->expects($matcher)
->method('prepare')->willReturnCallback(function (...$parameters) use ($matcher, $userServiceMock) {
match ($matcher->numberOfInvocations()) {
1 => self::assertEquals([1, 2], $parameters),
2 => self::assertEquals([3, 4], $parameters),
};
return $userServiceMock;
});
->method('prepare')
->willReturnCallback(
function (...$parameters) use ($matcher, $userServiceMock) {
if ($matcher->numberOfInvocations() === 1) {
$this->assertSame(1, $parameters[0]);
}
return $userServiceMock;
},
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ final class CombineWithWillThrowException extends TestCase
$this->userServiceMock->expects(self::exactly(2))
->method('prepare')
->withConsecutive(
[1, 2],
[3, 4],
[1],
)
->willThrowException(new RuntimeException());
}
Expand All @@ -34,13 +33,15 @@ final class CombineWithWillThrowException extends TestCase
{
$matcher = self::exactly(2);
$this->userServiceMock->expects($matcher)
->method('prepare')->willReturnCallback(function (...$parameters) use ($matcher) {
match ($matcher->numberOfInvocations()) {
1 => self::assertEquals([1, 2], $parameters),
2 => self::assertEquals([3, 4], $parameters),
};
throw new RuntimeException();
});
->method('prepare')
->willReturnCallback(
function (...$parameters) use ($matcher) {
if ($matcher->numberOfInvocations() === 1) {
$this->assertSame(1, $parameters[0]);
}
throw new RuntimeException();
},
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ final class IncludeUseOfVariable extends TestCase
->method('prepare')
->withConsecutive(
[1, $value],
[3, 4],
);
}
}
Expand All @@ -35,12 +34,15 @@ final class IncludeUseOfVariable extends TestCase
$matcher = $this->exactly(2);

$this->personServiceMock->expects($matcher)
->method('prepare')->willReturnCallback(function (...$parameters) use ($matcher, $value) {
match ($matcher->numberOfInvocations()) {
1 => self::assertEquals([1, $value], $parameters),
2 => self::assertEquals([3, 4], $parameters),
};
});
->method('prepare')
->willReturnCallback(
function (...$parameters) use ($matcher, $value) {
if ($matcher->numberOfInvocations() === 1) {
$this->assertSame(1, $parameters[0]);
$this->assertSame($value, $parameters[1]);
}
},
);
}
}

Expand Down
Loading
Loading