Skip to content

Commit c650a6d

Browse files
ahocquardDoodoune
authored andcommitted
API-599: fix composer install by deleting composer.lock and add documentation to test with docker
1 parent f82bfe6 commit c650a6d

File tree

9 files changed

+74
-13
lines changed

9 files changed

+74
-13
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ cache:
1313

1414
before_install:
1515
- phpenv config-rm xdebug.ini
16+
- phpenv config-add travis.php.ini
1617
- echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
1718
- composer self-update --no-interaction
1819

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
convertErrorsToExceptions="true"
88
convertNoticesToExceptions="true"
99
convertWarningsToExceptions="true"
10-
processIsolation="true"
10+
processIsolation="false"
1111
stopOnFailure="false"
1212
syntaxCheck="false">
1313

spec/Api/ProductMediaFileApiSpec.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ function it_creates_a_media_file($resourceClient, ResponseInterface $response)
133133
->willReturn($response);
134134

135135
$this->create($fileResource, $product)
136-
->shouldReturn('1/e/e/d/1eed10f108bde68b279d6f903f17b4b053e9d89d_akeneo.png');
136+
->shouldReturn($response);
137137
}
138138

139139
function it_throws_an_exception_if_the_file_is_unreadable_when_creating_a_media_file()

src/Api/MediaFileApiInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Akeneo\Pim\ApiClient\Api\Operation\ListableResourceInterface;
88
use Akeneo\Pim\ApiClient\Exception\HttpException;
99
use Akeneo\Pim\ApiClient\Exception\RuntimeException;
10+
use GuzzleHttp\Psr7\Response;
1011

1112
/**
1213
* API to manage the media files.
@@ -29,7 +30,7 @@ interface MediaFileApiInterface extends
2930
* @throws HttpException If the request failed.
3031
* @throws RuntimeException If the file could not be opened.
3132
*
32-
* @return string returns the code of created media file
33+
* @return Response returns the code of created media file
3334
*/
3435
public function create($mediaFile, array $data);
3536
}

src/Api/ProductMediaFileApi.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,18 @@ public function create($mediaFile, array $productData)
9999
];
100100

101101
$response = $this->resourceClient->createMultipartResource(static::MEDIA_FILES_URI, [], $requestParts);
102+
$headers = $response->getHeaders();
103+
104+
if (!isset($headers['Location'][0])) {
105+
throw new RuntimeException('The response does not contain the URI of the created media-file.');
106+
}
102107

103-
return $this->extractCodeFromCreationResponse($response);
108+
$matches = [];
109+
if (1 !== preg_match(ProductMediaFileApi::MEDIA_FILE_URI_CODE_REGEX, $headers['Location'][0], $matches)) {
110+
throw new RuntimeException('Unable to find the code in the URI of the created media-file.');
111+
}
112+
113+
return $response;
104114
}
105115

106116
/**

tests/Api/ApiTestCase.php

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
use Akeneo\Pim\ApiClient\AkeneoPimClientBuilder;
66
use Akeneo\Pim\ApiClient\AkeneoPimClientInterface;
77
use Akeneo\Pim\ApiClient\Api\AuthenticationApi;
8+
use Akeneo\Pim\ApiClient\Api\ProductMediaFileApi;
9+
use Akeneo\Pim\ApiClient\Exception\RuntimeException;
810
use donatj\MockWebServer\MockWebServer;
911
use donatj\MockWebServer\Response;
1012
use 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
}

tests/Api/CreateProductMediaFileTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Akeneo\Pim\ApiClient\tests\Api;
44

55
use Akeneo\Pim\ApiClient\Api\ProductMediaFileApi;
6+
use donatj\MockWebServer\RequestInfo;
67
use donatj\MockWebServer\Response;
78
use donatj\MockWebServer\ResponseStack;
89
use PHPUnit\Framework\Assert;
@@ -22,13 +23,20 @@ public function test_create_media_file()
2223
$api = $this->createClient()->getProductMediaFileApi();
2324
$mediaFile = realpath(__DIR__ . '/../fixtures/akeneo.png');
2425

25-
$response = $api->create($mediaFile, [
26+
$productInfos = [
2627
'identifier' => 'medium_boot',
2728
'attribute' => 'side_view',
2829
'scope' => null,
2930
'locale' => null,
30-
]);
31+
];
3132

32-
Assert::assertSame('f/b/0/6/fb068ccc9e3c5609d73c28d852812ba5faeeab28_akeneo.png', $response);
33+
$response = $api->create($mediaFile, $productInfos);
34+
35+
Assert::assertSame($this->server->getLastRequest()->jsonSerialize()[RequestInfo::JSON_KEY_POST]['product'], json_encode($productInfos));
36+
Assert::assertSame($this->server->getLastRequest()->jsonSerialize()[RequestInfo::JSON_KEY_REQUEST_URI], '/'. ProductMediaFileApi::MEDIA_FILES_URI);
37+
38+
Assert::assertSame(201, $response->getStatusCode());
39+
Assert::assertSame('', $response->getBody()->getContents());
40+
Assert::assertSame('f/b/0/6/fb068ccc9e3c5609d73c28d852812ba5faeeab28_akeneo.png', $this->extractCodeFromCreationResponse($response));
3341
}
3442
}

tests/Api/UpsertListProductTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Akeneo\Pim\ApiClient\Api\ProductApi;
66
use donatj\MockWebServer\Response;
77
use donatj\MockWebServer\ResponseStack;
8+
use Http\Discovery\StreamFactoryDiscovery;
89
use PHPUnit\Framework\Assert;
910

1011
class UpsertListProductTest extends ApiTestCase
@@ -87,7 +88,7 @@ public function test_upsert_list_from_stream()
8788
fwrite($resources, $resourcesContent);
8889
rewind($resources);
8990

90-
$streamedResources = $this->getStreamFactory()->createStream($resources);
91+
$streamedResources = StreamFactoryDiscovery::find()->createStream($resources);
9192
$api = $this->createClient()->getProductAPi();
9293
$response = $api->upsertList($streamedResources);
9394

travis.php.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
always_populate_raw_post_data = -1

0 commit comments

Comments
 (0)