55use Akeneo \Pim \ApiClient \AkeneoPimClientBuilder ;
66use Akeneo \Pim \ApiClient \AkeneoPimClientInterface ;
77use Akeneo \Pim \ApiClient \Api \AuthenticationApi ;
8+ use Akeneo \Pim \ApiClient \Api \ProductMediaFileApi ;
9+ use Akeneo \Pim \ApiClient \Exception \RuntimeException ;
810use donatj \MockWebServer \MockWebServer ;
911use donatj \MockWebServer \Response ;
1012use donatj \MockWebServer \ResponseStack ;
11- use Http \Message \StreamFactory ;
12- use Http \Discovery \StreamFactoryDiscovery ;
1313
1414/**
1515 * @author Laurent Petard <[email protected] > @@ -37,6 +37,14 @@ protected function setUp()
3737 );
3838 }
3939
40+ /**
41+ * {@inheritdoc}
42+ */
43+ protected function tearDown ()
44+ {
45+ $ this ->server ->stop ();
46+ }
47+
4048 /**
4149 * @return AkeneoPimClientInterface
4250 */
@@ -63,10 +71,41 @@ private function getAuthenticatedJson()
6371 }
6472
6573 /**
66- * @return StreamFactory
74+ * Extracts the code of a media-file from a creation response.
75+ *
76+ * @param $response
77+ *
78+ * @throws RuntimeException if unable to extract the code
79+ *
80+ * @return mixed
6781 */
68- protected function getStreamFactory ( )
82+ protected function extractCodeFromCreationResponse ( $ response )
6983 {
70- return StreamFactoryDiscovery::find ();
84+ $ headers = $ response ->getHeaders ();
85+
86+ if (!isset ($ headers ['Location ' ][0 ])) {
87+ throw new RuntimeException ('The response does not contain the URI of the created media-file. ' );
88+ }
89+
90+ $ matches = [];
91+ if (1 !== preg_match (ProductMediaFileApi::MEDIA_FILE_URI_CODE_REGEX , $ headers ['Location ' ][0 ], $ matches )) {
92+ throw new RuntimeException ('Unable to find the code in the URI of the created media-file. ' );
93+ }
94+
95+ return $ matches ['code ' ];
96+ }
97+
98+ /**
99+ * @param CursorInterface $result
100+ * @param array $expected
101+ */
102+ protected function assertSameResults ()
103+ {
104+ $ products = [];
105+ foreach ($ result as $ product ) {
106+ $ products [] = $ product ->getIdentifier ();
107+ }
108+
109+ $ this ->assertSame ($ products , $ expected );
71110 }
72111}
0 commit comments