44
55namespace AutoMapper \Tests \Bundle ;
66
7- use ApiPlatform \Symfony \Bundle \Test \ApiTestCase ;
87use Symfony \Component \Filesystem \Filesystem ;
8+ use Symfony \Contracts \HttpClient \ResponseInterface ;
99
10- if (!class_exists (ApiTestCase::class)) {
10+ // API Platform 5 moved the test case out of the Symfony bundle namespace and deprecated the old one
11+ $ baseApiTestCase = match (true ) {
12+ class_exists (\ApiPlatform \Test \ApiTestCase::class) => \ApiPlatform \Test \ApiTestCase::class,
13+ class_exists (\ApiPlatform \Symfony \Bundle \Test \ApiTestCase::class) => \ApiPlatform \Symfony \Bundle \Test \ApiTestCase::class,
14+ default => null ,
15+ };
16+
17+ if (null === $ baseApiTestCase ) {
1118 class ApiPlatformTest extends \PHPUnit \Framework \TestCase
1219 {
1320 protected static function createClient (): void
@@ -19,7 +26,9 @@ protected static function createClient(): void
1926 return ;
2027}
2128
22- class ApiPlatformTest extends ApiTestCase
29+ class_alias ($ baseApiTestCase , BaseApiTestCase::class);
30+
31+ class ApiPlatformTest extends BaseApiTestCase
2332{
2433 protected function setUp (): void
2534 {
@@ -70,7 +79,7 @@ public function testGetBookCollectionOnApip(): void
7079 $ response = static ::createClient ()->request ('GET ' , '/books.jsonld ' );
7180
7281 $ this ->assertResponseIsSuccessful ();
73- $ this ->assertResponseHeaderSame ( ' content-type ' , 'application/ld+json; charset=utf-8 ' );
82+ $ this ->assertContentTypeSame ( $ response , 'application/ld+json ' );
7483
7584 $ this ->assertJsonContains ([
7685 '@context ' => '/contexts/Book ' ,
@@ -89,10 +98,10 @@ public function testGetBookCollectionOnApip(): void
8998
9099 public function testGetBook (): void
91100 {
92- static ::createClient ()->request ('GET ' , '/books/1.jsonld ' );
101+ $ response = static ::createClient ()->request ('GET ' , '/books/1.jsonld ' );
93102
94103 $ this ->assertResponseIsSuccessful ();
95- $ this ->assertResponseHeaderSame ( ' content-type ' , 'application/ld+json; charset=utf-8 ' );
104+ $ this ->assertContentTypeSame ( $ response , 'application/ld+json ' );
96105
97106 $ this ->assertJsonContains ([
98107 '@context ' => '/contexts/Book ' ,
@@ -113,7 +122,7 @@ public function testCreateBook(): void
113122 ]]);
114123
115124 $ this ->assertResponseStatusCodeSame (201 );
116- $ this ->assertResponseHeaderSame ( ' content-type ' , 'application/ld+json; charset=utf-8 ' );
125+ $ this ->assertContentTypeSame ( $ response , 'application/ld+json ' );
117126 $ this ->assertJsonContains ([
118127 '@context ' => '/contexts/Book ' ,
119128 '@type ' => 'Book ' ,
@@ -166,6 +175,16 @@ public function testCreateWithIriRelationInPlainJson(): void
166175 $ this ->assertSame ('/books/1 ' , $ response ->toArray ()['book ' ]);
167176 }
168177
178+ /**
179+ * API Platform 5 no longer appends "; charset=utf-8" to JSON based media types.
180+ */
181+ private function assertContentTypeSame (ResponseInterface $ response , string $ mimeType ): void
182+ {
183+ $ contentType = $ response ->getHeaders (false )['content-type ' ][0 ] ?? '' ;
184+
185+ $ this ->assertSame ($ mimeType , explode ('; ' , $ contentType , 2 )[0 ]);
186+ }
187+
169188 protected function tearDown (): void
170189 {
171190 parent ::tearDown ();
0 commit comments