handler = $this->createMock(RequestHandlerInterface::class); $this->middleware = new DefaultShortCodesLengthMiddleware(8); } #[Test, DataProvider('provideBodies')] public function defaultValueIsInjectedInBodyWhenNotProvided(array $body, int $expectedLength): void { $request = ServerRequestFactory::fromGlobals()->withParsedBody($body); $this->handler->expects($this->once())->method('handle')->with($this->callback( function (ServerRequestInterface $req) use ($expectedLength) { $parsedBody = (array) $req->getParsedBody(); Assert::assertArrayHasKey(ShortUrlInputFilter::SHORT_CODE_LENGTH, $parsedBody); Assert::assertEquals($expectedLength, $parsedBody[ShortUrlInputFilter::SHORT_CODE_LENGTH]); return true; }, ))->willReturn(new Response()); $this->middleware->process($request, $this->handler); } public static function provideBodies(): iterable { yield 'value provided' => [[ShortUrlInputFilter::SHORT_CODE_LENGTH => 6], 6]; yield 'value not provided' => [[], 8]; } }