. */ namespace SP\Tests; use PHPUnit\Framework\Constraint\Callback; use PHPUnit\Framework\Constraint\Constraint; /** * Trait PHPUnitHelper * * @method static assertSameSize(array $firstCallArguments, array $consecutiveCallArguments, string $string) * @method static assertThat(mixed $actualArgument, Constraint $expected) * @method static assertEquals(mixed $expected, mixed $actualArgument) */ trait PHPUnitHelper { /** * @param array $firstCallArguments * @param array ...$consecutiveCallsArguments * * @return iterable */ public static function withConsecutive(array $firstCallArguments, array ...$consecutiveCallsArguments): iterable { foreach ($consecutiveCallsArguments as $consecutiveCallArguments) { self::assertSameSize( $firstCallArguments, $consecutiveCallArguments, 'Each expected arguments list need to have the same size.' ); } $allConsecutiveCallsArguments = [$firstCallArguments, ...$consecutiveCallsArguments]; $numberOfArguments = count($firstCallArguments); $argumentList = []; for ($argumentPosition = 0; $argumentPosition < $numberOfArguments; $argumentPosition++) { $argumentList[$argumentPosition] = array_column($allConsecutiveCallsArguments, $argumentPosition); } $mockedMethodCall = 0; $callbackCall = 0; foreach ($argumentList as $index => $argument) { yield new Callback( static function (mixed $actualArgument) use ( $argumentList, &$mockedMethodCall, &$callbackCall, $index, $numberOfArguments ): bool { $expected = $argumentList[$index][$mockedMethodCall] ?? null; $callbackCall++; $mockedMethodCall = (int)($callbackCall / $numberOfArguments); if ($expected instanceof Constraint) { self::assertThat($actualArgument, $expected); } else { self::assertEquals($expected, $actualArgument); } return true; }, ); } } /** * Return a Callback that implements a generator function * * @param array $values * @return Callback */ public static function withGenerator(array $values): Callback { return new Callback(function () use ($values) { foreach ($values as $value) { yield $value; } }); } public static function withResolveCallableCallback(): Callback { return new Callback(function (callable $callable) { $callable(); return true; }); } public static function getRepositoryStubMethods(string $class): array { return array_filter( get_class_methods($class), static fn(string $method) => $method != 'transactionAware' ); } }