Skip to content

Commit

Permalink
Merge branch '10.5' into 11.3
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Sep 9, 2024
2 parents d04d3aa + 441ccc7 commit 48b15e2
Show file tree
Hide file tree
Showing 79 changed files with 190 additions and 82 deletions.
1 change: 1 addition & 0 deletions ChangeLog-11.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes of the PHPUnit 11.3 release series are documented in this fi
* [#4584](https://github.com/sebastianbergmann/phpunit/issues/4584): `assertJsonStringEqualsJsonString()` considers objects with sequential numeric keys equal to arrays
* [#4625](https://github.com/sebastianbergmann/phpunit/issues/4625): Generator yielding keys that are neither integer or string leads to hard-to-understand error message when used as data provider
* [#4674](https://github.com/sebastianbergmann/phpunit/issues/4674): JSON assertions should treat objects as unordered
* [#5891](https://github.com/sebastianbergmann/phpunit/issues/5891): `Callback` constraint does not handle variadic arguments correctly when used for mock object expectations
* [#5929](https://github.com/sebastianbergmann/phpunit/issues/5929): TestDox output containing `$` at the beginning gets truncated when used with a data provider
* [#5943](https://github.com/sebastianbergmann/phpunit/issues/5943): Tests configured to be in group(s) using the XML configuration file are also added to the `default` group

Expand Down
17 changes: 17 additions & 0 deletions src/Framework/Constraint/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
namespace PHPUnit\Framework\Constraint;

use ReflectionFunction;

/**
* @template CallbackInput of mixed
*
Expand Down Expand Up @@ -37,6 +39,17 @@ public function toString(): string
return 'is accepted by specified callback';
}

public function isVariadic(): bool
{
foreach ((new ReflectionFunction($this->callback))->getParameters() as $parameter) {
if ($parameter->isVariadic()) {
return true;
}
}

return false;
}

/**
* Evaluates the constraint for parameter $value. Returns true if the
* constraint is met, false otherwise.
Expand All @@ -45,6 +58,10 @@ public function toString(): string
*/
protected function matches(mixed $other): bool
{
if ($this->isVariadic()) {
return ($this->callback)(...$other);
}

return ($this->callback)($other);
}
}
11 changes: 10 additions & 1 deletion src/Framework/MockObject/Generator/MockMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use function preg_match;
use function preg_replace;
use function sprintf;
use function str_contains;
use function strlen;
use function strpos;
use function substr;
Expand Down Expand Up @@ -211,13 +212,21 @@ public function generateCode(): string

$template = $this->loadTemplate($templateFile);

$argumentsCount = 0;

if (str_contains($this->argumentsForCall, '...')) {
$argumentsCount = null;
} elseif (!empty($this->argumentsForCall)) {
$argumentsCount = substr_count($this->argumentsForCall, ',') + 1;
}

$template->setVar(
[
'arguments_decl' => $this->argumentsForDeclaration,
'arguments_call' => $this->argumentsForCall,
'return_declaration' => !empty($this->returnType->asString()) ? (': ' . $this->returnType->asString()) : '',
'return_type' => $this->returnType->asString(),
'arguments_count' => !empty($this->argumentsForCall) ? substr_count($this->argumentsForCall, ',') + 1 : 0,
'arguments_count' => $argumentsCount,
'class_name' => $this->className,
'method_name' => $this->methodName,
'modifier' => $this->modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
$__phpunit_arguments = [{arguments_call}];
$__phpunit_count = func_num_args();

if ($__phpunit_count > {arguments_count}) {
if ({arguments_count} !== null && $__phpunit_count > {arguments_count}) {
$__phpunit_arguments_tmp = func_get_args();
for ($__phpunit_i = {arguments_count}; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
8 changes: 7 additions & 1 deletion src/Framework/MockObject/Runtime/Rule/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use function count;
use function sprintf;
use Exception;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsAnything;
use PHPUnit\Framework\Constraint\IsEqual;
Expand Down Expand Up @@ -110,8 +111,13 @@ private function doVerify(): bool
}

foreach ($this->parameters as $i => $parameter) {
if ($parameter instanceof Callback && $parameter->isVariadic()) {
$other = $this->invocation->parameters();
} else {
$other = $this->invocation->parameters()[$i];
}
$parameter->evaluate(
$this->invocation->parameters()[$i],
$other,
sprintf(
'Parameter %s for invocation %s does not match expected ' .
'value.',
Expand Down
2 changes: 1 addition & 1 deletion tests/end-to-end/mock-objects/generator/232.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObjectInte
$__phpunit_arguments = [];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 0) {
if (0 !== null && $__phpunit_count > 0) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 0; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Issue3154Mock extends Is\Namespaced\Issue3154 implements PHPUnit\Framework
$__phpunit_arguments = [$i, $j, $v, $z];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 4) {
if (4 !== null && $__phpunit_count > 4) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 4; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/end-to-end/mock-objects/generator/3967.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class MockBaz extends Exception implements Baz, PHPUnit\Framework\MockObject\Moc
$__phpunit_arguments = [];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 0) {
if (0 !== null && $__phpunit_count > 0) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 0; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/end-to-end/mock-objects/generator/397.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MockC extends C implements PHPUnit\Framework\MockObject\MockObjectInternal
$__phpunit_arguments = [$other];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/end-to-end/mock-objects/generator/4139.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class %s implements PHPUnit\Framework\MockObject\MockObjectInternal, InterfaceWi
$__phpunit_arguments = [];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 0) {
if (0 !== null && $__phpunit_count > 0) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 0; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
6 changes: 3 additions & 3 deletions tests/end-to-end/mock-objects/generator/abstract_class.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObjectInte
$__phpunit_arguments = [];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 0) {
if (0 !== null && $__phpunit_count > 0) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 0; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down Expand Up @@ -94,7 +94,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObjectInte
$__phpunit_arguments = [];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 0) {
if (0 !== null && $__phpunit_count > 0) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 0; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down Expand Up @@ -131,7 +131,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObjectInte
$__phpunit_arguments = [];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 0) {
if (0 !== null && $__phpunit_count > 0) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 0; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
4 changes: 2 additions & 2 deletions tests/end-to-end/mock-objects/generator/class.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObjectInte
$__phpunit_arguments = [$foo];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down Expand Up @@ -94,7 +94,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObjectInte
$__phpunit_arguments = [$foo];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObjectInte
$__phpunit_arguments = [];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 0) {
if (0 !== null && $__phpunit_count > 0) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 0; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/end-to-end/mock-objects/generator/class_partial.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObjectInte
$__phpunit_arguments = [$foo];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class MockFoo extends ClassWithDeprecatedMethod implements PHPUnit\Framework\Moc
$__phpunit_arguments = [];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 0) {
if (0 !== null && $__phpunit_count > 0) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 0; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObjectInte
$__phpunit_arguments = [];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 0) {
if (0 !== null && $__phpunit_count > 0) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 0; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MockFoo extends ClassWithMethodWithNullableTypehintedVariadicArguments imp
$__phpunit_arguments = [$a];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MockFoo extends ClassWithMethodWithTypehintedVariadicArguments implements
$__phpunit_arguments = [$a];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MockFoo extends ClassWithMethodWithVariadicArguments implements PHPUnit\Fr
$__phpunit_arguments = [$a];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObjectInte
$__phpunit_arguments = [$baz];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/end-to-end/mock-objects/generator/interface.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class MockFoo implements PHPUnit\Framework\MockObject\MockObjectInternal, Foo
$__phpunit_arguments = [$foo];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObjectInte
$__phpunit_arguments = [$foo];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down Expand Up @@ -95,7 +95,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObjectInte
$__phpunit_arguments = [$foo];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
4 changes: 2 additions & 2 deletions tests/end-to-end/mock-objects/generator/namespaced_class.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObjectI
$__phpunit_arguments = [$foo];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down Expand Up @@ -96,7 +96,7 @@ class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObjectI
$__phpunit_arguments = [$foo];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObjectI
$__phpunit_arguments = [$foo];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MockFoo implements PHPUnit\Framework\MockObject\MockObjectInternal, NS\Foo
$__phpunit_arguments = [$foo];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObjectInte
$__phpunit_arguments = [$x];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObjectInte
$__phpunit_arguments = [$baz, $other];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 2) {
if (2 !== null && $__phpunit_count > 2) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 2; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObjectInte
$__phpunit_arguments = [];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 0) {
if (0 !== null && $__phpunit_count > 0) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 0; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/end-to-end/mock-objects/generator/parameter_dnf.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObjectInte
$__phpunit_arguments = [$baz];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class MockFoo implements PHPUnit\Framework\MockObject\MockObjectInternal, Foo
$__phpunit_arguments = [$baz];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObjectInte
$__phpunit_arguments = [$baz];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class MockFoo implements PHPUnit\Framework\MockObject\MockObjectInternal, Foo
$__phpunit_arguments = [$baz];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if (1 !== null && $__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
Expand Down
Loading

0 comments on commit 48b15e2

Please sign in to comment.