2222use ApiPlatform \Metadata \UrlGeneratorInterface ;
2323use ApiPlatform \State \ProcessorInterface ;
2424use PHPUnit \Framework \TestCase ;
25- use Symfony \Component \HttpFoundation \ParameterBag ;
2625use Symfony \Component \HttpFoundation \Request ;
2726use Symfony \Component \HttpFoundation \Response ;
28- use Symfony \Component \HttpFoundation \ResponseHeaderBag ;
2927
3028class AddTagsProcessorTest extends TestCase
3129{
3230 public function testAddTags (): void
3331 {
3432 $ operation = new Get ();
35- $ response = $ this ->createMock (Response::class);
36- $ response ->method ('isCacheable ' )->willReturn (true );
37- $ response ->headers = $ this ->createMock (ResponseHeaderBag::class);
38- $ response ->headers ->expects ($ this ->once ())->method ('set ' )->with ('Cache-Tags ' , 'a,b ' );
39- $ request = $ this ->createMock (Request::class);
40- $ request ->method ('isMethodCacheable ' )->willReturn (true );
41- $ request ->attributes = $ this ->createMock (ParameterBag::class);
42- $ request ->attributes ->method ('get ' )->with ('_resources ' , [])->willReturn (['a ' , 'b ' ]);
33+ $ response = new Response ('' , 200 , ['Cache-Control ' => 'public, max-age=10 ' ]);
34+ $ request = new Request (attributes: ['_resources ' => ['a ' , 'b ' ]]);
4335 $ context = ['request ' => $ request ];
4436 $ decorated = $ this ->createMock (ProcessorInterface::class);
4537 $ decorated ->method ('process ' )->willReturn ($ response );
4638 $ iriConverter = $ this ->createMock (IriConverterInterface::class);
4739 $ iriConverter ->expects ($ this ->never ())->method ('getIriFromResource ' );
4840 $ processor = new AddTagsProcessor ($ decorated , $ iriConverter );
4941 $ processor ->process ($ response , $ operation , [], $ context );
42+
43+ $ this ->assertSame ('a,b ' , $ response ->headers ->get ('Cache-Tags ' ));
5044 }
5145
5246 public function testAddTagsCollection (): void
5347 {
5448 $ operation = new GetCollection (class: \stdClass::class, uriVariables: ['id ' => new Link ()]);
55- $ response = $ this ->createMock (Response::class);
56- $ response ->method ('isCacheable ' )->willReturn (true );
57- $ response ->headers = $ this ->createMock (ResponseHeaderBag::class);
58- $ response ->headers ->expects ($ this ->once ())->method ('set ' )->with ('Cache-Tags ' , 'a,b,/foos/1/bars ' );
59- $ request = $ this ->createMock (Request::class);
60- $ request ->method ('isMethodCacheable ' )->willReturn (true );
61- $ request ->attributes = $ this ->createMock (ParameterBag::class);
62- $ request ->attributes ->method ('get ' )->with ('_resources ' , [])->willReturn (['a ' , 'b ' ]);
63- $ request ->attributes ->method ('all ' )->willReturn (['id ' => 1 ]);
49+ $ response = new Response ('' , 200 , ['Cache-Control ' => 'public, max-age=10 ' ]);
50+ $ request = new Request (attributes: ['_resources ' => ['a ' , 'b ' ], 'id ' => 1 ]);
6451 $ context = ['request ' => $ request ];
6552 $ decorated = $ this ->createMock (ProcessorInterface::class);
6653 $ decorated ->method ('process ' )->willReturn ($ response );
6754 $ iriConverter = $ this ->createMock (IriConverterInterface::class);
6855 $ iriConverter ->expects ($ this ->once ())->method ('getIriFromResource ' )->with (\stdClass::class, UrlGeneratorInterface::ABS_PATH , $ operation , ['uri_variables ' => ['id ' => 1 ]])->willReturn ('/foos/1/bars ' );
6956 $ processor = new AddTagsProcessor ($ decorated , $ iriConverter );
7057 $ processor ->process ($ response , $ operation , [], $ context );
58+
59+ $ this ->assertSame ('a,b,/foos/1/bars ' , $ response ->headers ->get ('Cache-Tags ' ));
7160 }
7261
7362 public function testAddTagsWithPurger (): void
7463 {
7564 $ operation = new Get ();
76- $ response = $ this ->createMock (Response::class);
77- $ response ->method ('isCacheable ' )->willReturn (true );
78- $ response ->headers = $ this ->createMock (ResponseHeaderBag::class);
79- $ response ->headers ->expects ($ this ->once ())->method ('set ' )->with ('Cache-Tags ' , 'a,b ' );
80- $ request = $ this ->createMock (Request::class);
81- $ request ->method ('isMethodCacheable ' )->willReturn (true );
82- $ request ->attributes = $ this ->createMock (ParameterBag::class);
83- $ request ->attributes ->method ('get ' )->with ('_resources ' , [])->willReturn (['a ' , 'b ' ]);
65+ $ response = new Response ('' , 200 , ['Cache-Control ' => 'public, max-age=10 ' ]);
66+ $ request = new Request (attributes: ['_resources ' => ['a ' , 'b ' ]]);
8467 $ context = ['request ' => $ request ];
8568 $ decorated = $ this ->createMock (ProcessorInterface::class);
8669 $ decorated ->method ('process ' )->willReturn ($ response );
@@ -90,5 +73,7 @@ public function testAddTagsWithPurger(): void
9073 $ purger ->expects ($ this ->once ())->method ('getResponseHeaders ' )->willReturn (['Cache-Tags ' => 'a,b ' ]);
9174 $ processor = new AddTagsProcessor ($ decorated , $ iriConverter , $ purger );
9275 $ processor ->process ($ response , $ operation , [], $ context );
76+
77+ $ this ->assertSame ('a,b ' , $ response ->headers ->get ('Cache-Tags ' ));
9378 }
9479}
0 commit comments