translator = Translator::factory(['locale' => 'ru']); $this->middleware = new LocaleMiddleware($this->translator); } /** @test */ public function whenNoHeaderIsPresentLocaleIsNotChanged(): void { $this->assertEquals('ru', $this->translator->getLocale()); $this->middleware->process(new ServerRequest(), TestUtils::createReqHandlerMock()->reveal()); $this->assertEquals('ru', $this->translator->getLocale()); } /** @test */ public function whenTheHeaderIsPresentLocaleIsChanged(): void { $this->assertEquals('ru', $this->translator->getLocale()); $request = (new ServerRequest())->withHeader('Accept-Language', 'es'); $this->middleware->process($request, TestUtils::createReqHandlerMock()->reveal()); $this->assertEquals('es', $this->translator->getLocale()); } /** * @test * @dataProvider provideLanguages */ public function localeGetsNormalized(string $lang, string $expected): void { $handler = TestUtils::createReqHandlerMock(); $this->assertEquals('ru', $this->translator->getLocale()); $request = (new ServerRequest())->withHeader('Accept-Language', $lang); $this->middleware->process($request, $handler->reveal()); $this->assertEquals($expected, $this->translator->getLocale()); } public function provideLanguages(): iterable { yield 'language only' => ['ru', 'ru']; yield 'country and language with underscore' => ['es_ES', 'es']; yield 'country and language with dash' => ['en-US', 'en']; } }