99use Akeneo \Pim \ApiClient \Api \MediaFileApiInterface ;
1010use Akeneo \Pim \ApiClient \Client \ResourceClientInterface ;
1111use Akeneo \Pim \ApiClient \Exception \RuntimeException ;
12+ use Akeneo \Pim \ApiClient \FileSystem \FileSystemInterface ;
1213use Akeneo \Pim \ApiClient \Pagination \PageInterface ;
1314use Akeneo \Pim \ApiClient \Pagination \PageFactoryInterface ;
1415use Akeneo \Pim \ApiClient \Pagination \ResourceCursorFactoryInterface ;
1516use Akeneo \Pim \ApiClient \Pagination \ResourceCursorInterface ;
1617use PhpSpec \ObjectBehavior ;
18+ use Prophecy \Argument ;
1719use Psr \Http \Message \ResponseInterface ;
1820use Psr \Http \Message \StreamInterface ;
1921
@@ -22,9 +24,10 @@ class ProductMediaFileApiSpec extends ObjectBehavior
2224 function let (
2325 ResourceClientInterface $ resourceClient ,
2426 PageFactoryInterface $ pageFactory ,
25- ResourceCursorFactoryInterface $ cursorFactory
27+ ResourceCursorFactoryInterface $ cursorFactory ,
28+ FileSystemInterface $ fileSystem
2629 ) {
27- $ this ->beConstructedWith ($ resourceClient , $ pageFactory , $ cursorFactory );
30+ $ this ->beConstructedWith ($ resourceClient , $ pageFactory , $ cursorFactory, $ fileSystem );
2831 }
2932
3033 function it_is_initializable ()
@@ -103,9 +106,11 @@ function it_returns_a_list_of_media_files_with_additional_query_parameters($reso
103106 $ this ->listPerPage (null , null , ['foo ' => 'bar ' ])->shouldReturn ($ page );
104107 }
105108
106- function it_creates_a_media_file ($ resourceClient , ResponseInterface $ response )
109+ function it_creates_a_media_file_from_a_path ($ resourceClient, $ fileSystem , ResponseInterface $ response )
107110 {
108111 $ fileResource = fopen ('php://stdin ' , 'r ' );
112+ $ fileSystem ->getResourceFromPath ('/images/akeneo.png ' )->willReturn ($ fileResource );
113+
109114 $ product = [
110115 'identifier ' => 'foo ' ,
111116 'attribute ' => 'picture ' ,
@@ -132,23 +137,43 @@ function it_creates_a_media_file($resourceClient, ResponseInterface $response)
132137 ->createMultipartResource (ProductMediaFileApi::MEDIA_FILES_URI , [], $ requestParts )
133138 ->willReturn ($ response );
134139
135- $ this ->create ($ fileResource , $ product )
140+ $ this ->create (' /images/akeneo.png ' , $ product )
136141 ->shouldReturn ('1/e/e/d/1eed10f108bde68b279d6f903f17b4b053e9d89d_akeneo.png ' );
137142 }
138143
139- function it_throws_an_exception_if_the_file_is_unreadable_when_creating_a_media_file ( )
144+ function it_creates_a_media_file_from_a_resource ( $ resourceClient , $ fileSystem , ResponseInterface $ response )
140145 {
141- $ this
142- ->shouldThrow (new RuntimeException ('The file "/foo.bar" could not be read. ' ))
143- ->during ('create ' , [
144- '/foo.bar ' ,
145- [
146- 'identifier ' => 'foo ' ,
147- 'attribute ' => 'picture ' ,
148- 'scope ' => 'e-commerce ' ,
149- 'locale ' => 'en_US ' ,
150- ]
151- ]);
146+ $ fileResource = fopen ('php://stdin ' , 'r ' );
147+ $ fileSystem ->getResourceFromPath (Argument::any ())->shouldNotBeCalled ();
148+
149+ $ product = [
150+ 'identifier ' => 'foo ' ,
151+ 'attribute ' => 'picture ' ,
152+ 'scope ' => 'e-commerce ' ,
153+ 'locale ' => 'en_US ' ,
154+ ];
155+
156+ $ requestParts = [
157+ [
158+ 'name ' => 'product ' ,
159+ 'contents ' => json_encode ($ product ),
160+ ],
161+ [
162+ 'name ' => 'file ' ,
163+ 'contents ' => $ fileResource ,
164+ ]
165+ ];
166+
167+ $ response ->getHeaders ()->willReturn (['Location ' => [
168+ 'http://localhost/api/rest/v1/media-files/1/e/e/d/1eed10f108bde68b279d6f903f17b4b053e9d89d_akeneo.png '
169+ ]]);
170+
171+ $ resourceClient
172+ ->createMultipartResource (ProductMediaFileApi::MEDIA_FILES_URI , [], $ requestParts )
173+ ->willReturn ($ response );
174+
175+ $ this ->create ($ fileResource , $ product )
176+ ->shouldReturn ('1/e/e/d/1eed10f108bde68b279d6f903f17b4b053e9d89d_akeneo.png ' );
152177 }
153178
154179 function it_throws_an_exception_if_the_response_does_not_contain_the_uri_of_the_created_media_file ($ resourceClient , ResponseInterface $ response )
0 commit comments