Created
July 20, 2023 14:50
-
-
Save oleg-andreyev/85c74dbf022237b03825c7e9f4439303 to your computer and use it in GitHub Desktop.
WithConsecutive polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
I'm having trouble using this with composer. Has someone had success including this gist in composer?
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
Thanks for the explanation. Yes, makes sense given how static variables work.