Skip to content

Instantly share code, notes, and snippets.

@oleg-andreyev
Created July 20, 2023 14:50
Show Gist options
  • Save oleg-andreyev/85c74dbf022237b03825c7e9f4439303 to your computer and use it in GitHub Desktop.
Save oleg-andreyev/85c74dbf022237b03825c7e9f4439303 to your computer and use it in GitHub Desktop.
WithConsecutive polyfill
<?php
/**
Usage: ->with(...WithConsecutive::create(...$withCodes))
*/
declare(strict_types=1);
namespace App\Tests;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsEqual;
use RuntimeException;
class WithConsecutive
{
/**
* @param array<mixed> $parameterGroups
*
* @return array<int, Callback<mixed>>
*/
public static function create(...$parameterGroups): array
{
$result = [];
$parametersCount = null;
$groups = [];
$values = [];
foreach ($parameterGroups as $index => $parameters) {
// initial
$parametersCount ??= count($parameters);
// compare
if ($parametersCount !== count($parameters)) {
throw new RuntimeException('Parameters count max much in all groups');
}
// prepare parameters
foreach ($parameters as $parameter) {
if (!$parameter instanceof Constraint) {
$parameter = new IsEqual($parameter);
}
$groups[$index][] = $parameter;
}
}
// collect values
foreach ($groups as $parameters) {
foreach ($parameters as $index => $parameter) {
$values[$index][] = $parameter;
}
}
// build callback
for ($index = 0; $index < $parametersCount; ++$index) {
$result[$index] = Assert::callback(static function ($value) use ($values, $index) {
static $map = null;
$map ??= $values[$index];
$expectedArg = array_shift($map);
if ($expectedArg === null) {
throw new RuntimeException('No more expected calls');
}
$expectedArg->evaluate($value);
return true;
});
}
return $result;
}
}
@janopae
Copy link

janopae commented Jul 20, 2023

Instead of

 static $map = null;
 $map ??= $values[$index];

couldn't you just write

static $map = $values[$index];

?

@oleg-andreyev
Copy link
Author

Instead of

 static $map = null;
 $map ??= $values[$index];

couldn't you just write

static $map = $values[$index];

?

You cannot.

<?php

$values = [[1], [2], [3]];
$i = 0;

(function () use ($values, $i) {
	static $map = $values[$i];
	
	return $map;
})();

will generate Fatal error: Constant expression contains invalid operations in /home/user/scripts/code.php on line 7

https://onlinephp.io/c/63121

@janopae
Copy link

janopae commented Jul 24, 2023

Thanks for the explanation. Yes, makes sense given how static variables work.

@azehintense
Copy link

I'm having trouble using this with composer. Has someone had success including this gist in composer?

@oleg-andreyev
Copy link
Author

I'm having trouble using this with composer. Has someone had success including this gist in composer?

What you are trying to achieve? This gist is not a composer package, usually you just copy and paste it.

If you need a composer package, tell me, I may create one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment