Repository: symfony/symfony
File: src/Symfony/Component/HttpKernel/Tests/EventListener/CacheAttributeListenerTest.php (PHP)

Code before:
        $response = new Response();
        $event = new ResponseEvent($this->getKernel(), $request, HttpKernelInterface::MAIN_REQUEST, $response);
        $this->listener->onKernelResponse($event);

        $this->assertSame(CacheAttributeController::METHOD_SMAXAGE, $response->getMaxAge());

        $request = new Request();
        $event = new ControllerArgumentsEvent($this->getKernel(), [new CacheAttributeController(), 'bar'], [], $request, null);
        $this->listener->onKernelControllerArguments($event);

        $response = new Response();
        $event = new ResponseEvent($this->getKernel(), $request, HttpKernelInterface::MAIN_REQUEST, $response);
        $this->listener->onKernelResponse($event);

        $this->assertSame(CacheAttributeController::CLASS_SMAXAGE, $response->getMaxAge());
    }

    /**
     * @dataProvider provideVaryHeaderScenarios
     */
    public function testHasRelevantVaryHeaderBehavior(
        array $responseVary,
        array $cacheVary,
        bool $varyByLanguage,
        array $expectedVary,
    ): void {
        $request = $this->createRequest(new Cache(vary: $cacheVary));
        $request->attributes->set('_vary_by_language', $varyByLanguage);

        $response = new Response();
        $response->setVary($responseVary);

        $listener = new CacheAttributeListener();
        $event = new ResponseEvent($this->getKernel(), $request, HttpKernelInterface::MAIN_REQUEST, $response);
        $listener->onKernelResponse($event);

        $this->assertSame($expectedVary, $response->getVary());
    }

    public static function provideVaryHeaderScenarios(): array
    {
        return [
            'no vary headers at all' => [
                'responseVary' => [],
                'cacheVary' => ['X-Foo'],
                'varyByLanguage' => false,
                'expectedVary' => ['X-Foo'],
            ],
            'response vary accept-language only, vary_by_language true (append new)' => [
                'responseVary' => ['Accept-Language'],
                'cacheVary' => ['X-Bar'],

Review comment (performance):
```suggestion
    public function testHasRelevantVaryHeaderBehavior(array $responseVary, array $cacheVary, bool $varyByLanguage, array $expectedVary)
    {
```

Code after:
        $response = new Response();
        $event = new ResponseEvent($this->getKernel(), $request, HttpKernelInterface::MAIN_REQUEST, $response);
        $this->listener->onKernelResponse($event);

        $this->assertSame(CacheAttributeController::METHOD_SMAXAGE, $response->getMaxAge());

        $request = new Request();
        $event = new ControllerArgumentsEvent($this->getKernel(), [new CacheAttributeController(), 'bar'], [], $request, null);
        $this->listener->onKernelControllerArguments($event);

        $response = new Response();
        $event = new ResponseEvent($this->getKernel(), $request, HttpKernelInterface::MAIN_REQUEST, $response);
        $this->listener->onKernelResponse($event);

        $this->assertSame(CacheAttributeController::CLASS_SMAXAGE, $response->getMaxAge());
    }

    /**
     * @dataProvider provideVaryHeaderScenarios
     */
    public function testHasRelevantVaryHeaderBehavior(array $responseVary, array $cacheVary, bool $varyByLanguage, array $expectedVary)
    {
        $request = $this->createRequest(new Cache(vary: $cacheVary));
        $request->attributes->set('_vary_by_language', $varyByLanguage);

        $response = new Response();
        $response->setVary($responseVary);

        $listener = new CacheAttributeListener();
        $event = new ResponseEvent($this->getKernel(), $request, HttpKernelInterface::MAIN_REQUEST, $response);
        $listener->onKernelResponse($event);

        $this->assertSame($expectedVary, $response->getVary());
    }

    public static function provideVaryHeaderScenarios(): \Traversable
    {
        yield 'no vary headers at all' => [
            'responseVary' => [],
            'cacheVary' => ['X-Foo'],
            'varyByLanguage' => false,
            'expectedVary' => ['X-Foo'],
        ];
        yield 'response vary accept-language only, vary_by_language true (append new)' => [
            'responseVary' => ['Accept-Language'],
            'cacheVary' => ['X-Bar'],
            'varyByLanguage' => true,
            'expectedVary' => ['Accept-Language', 'X-Bar'], // X-Bar is added
        ];
        yield 'response vary accept-language only, vary_by_language false (no append)' => [
            'responseVary' => ['Accept-Language'],