Skip to content

Commit 5791523

Browse files
committed
Refactor logic in multiRespond(), update getComposer() doc block, fix tests
1 parent 9bc68b0 commit 5791523

File tree

6 files changed

+52
-27
lines changed

6 files changed

+52
-27
lines changed

spec/Packagist/Api/ClientSpec.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,42 @@ public function it_gets_package_details(HttpClient $client, Factory $factory, Re
9898

9999
public function it_gets_composer_package_details(HttpClient $client, Factory $factory, Response $response, Stream $body): void
100100
{
101-
$data = file_get_contents('spec/Packagist/Api/Fixture/get_composer.json');
101+
$data1 = file_get_contents('spec/Packagist/Api/Fixture/v2_get.json');
102+
$data2 = file_get_contents('spec/Packagist/Api/Fixture/v2_get_dev.json');
102103
$response->getBody()->shouldBeCalled()->willReturn($body);
103-
$body->getContents()->shouldBeCalled()->willReturn($data);
104+
$body->getContents()->shouldBeCalledTimes(2)->willReturn($data1, $data2);
105+
106+
$client->request('GET', 'https://packagist.org/p2/sylius/sylius.json')
107+
->shouldBeCalled()
108+
->willReturn($response);
104109

105110
$client->request('GET', 'https://packagist.org/p2/sylius/sylius~dev.json')
106111
->shouldBeCalled()
107112
->willReturn($response);
108113

109-
$packages = [
110-
'1.0.0' => ['name' => 'sylius/sylius', 'version' => '1.0.0']
114+
$data1 = json_decode($data1, true);
115+
$data2 = json_decode($data2, true);
116+
$factoryInput = $data1;
117+
$factoryInput['packages']['sylius/sylius'] = [
118+
...$data1['packages']['sylius/sylius'],
119+
...$data2['packages']['sylius/sylius'],
111120
];
112121

113-
$factory->create(json_decode($data, true))->shouldBeCalled()->willReturn($packages);
122+
$factory->create($factoryInput)->shouldBeCalled()->willReturn([
123+
'packages' => [
124+
'sylius/sylius' => [
125+
['name' => 'sylius/sylius', 'version' => '1.0.0'],
126+
['name' => 'sylius/sylius', 'version' => 'dev-master'],
127+
],
128+
],
129+
]);
114130

115-
$this->getComposer('sylius/sylius')->shouldBe($packages);
131+
$this->getComposer('sylius/sylius');
116132
}
117133

118134
public function it_gets_composer_releases_package_details(HttpClient $client, Factory $factory, Response $response, Stream $body): void
119135
{
120-
$data = file_get_contents('spec/Packagist/Api/Fixture/get_composer_releases.json');
136+
$data = file_get_contents('spec/Packagist/Api/Fixture/v2_get.json');
121137
$response->getBody()->shouldBeCalled()->willReturn($body);
122138
$body->getContents()->shouldBeCalled()->willReturn($data);
123139

spec/Packagist/Api/Fixture/get_composer_releases.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

spec/Packagist/Api/Fixture/v2_get.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

spec/Packagist/Api/Fixture/v2_get_dev.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

spec/Packagist/Api/Result/FactorySpec.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,32 @@ public function it_creates_popular_results()
4040
}
4141
}
4242

43-
public function it_creates_packages()
43+
public function it_creates_packages(): void
4444
{
4545
$data = json_decode(file_get_contents('spec/Packagist/Api/Fixture/get.json'), true);
4646

4747
$this->create($data)->shouldHaveType(Package::class);
4848
}
4949

50-
public function it_creates_composer_packages()
50+
public function it_creates_composer_packages(): void
5151
{
5252
$data = json_decode(file_get_contents('spec/Packagist/Api/Fixture/get_composer.json'), true);
5353

5454
$results = $this->create($data);
5555
$results->shouldHaveCount(1);
5656
$results->shouldBeArray();
5757
foreach ($results as $result) {
58-
$result->shouldBeAnInstanceOf('Packagist\Api\Result\Package');
58+
$result->shouldBeAnInstanceOf(Package::class);
5959

6060
foreach ($result->getVersions() as $version) {
61-
$version->shouldBeAnInstanceOf('Packagist\Api\Result\Version');
61+
$version->shouldBeAnInstanceOf(Package\Version::class);
6262
}
6363
}
6464
}
6565

6666
public function it_creates_composer_releases_packages(): void
6767
{
68-
$data = json_decode(file_get_contents('spec/Packagist/Api/Fixture/get_composer_releases.json'), true);
68+
$data = json_decode(file_get_contents('spec/Packagist/Api/Fixture/v2_get.json'), true);
6969

7070
$results = $this->create($data);
7171
$results->shouldHaveCount(1);

src/Packagist/Api/Client.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,17 @@ public function get(string $package)
9999
}
100100

101101
/**
102-
* @see https://packagist.org/apidoc#get-package-data
103-
* Contains tagged releases and dev branches
102+
* Similar to {@link get()}, but uses Composer metadata which is Packagist's preferred
103+
* way of retrieving details, since responses are cached efficiently as static files
104+
* by the Packagist service. The response lacks some metadata that is provided
105+
* by {@link get()}, see https://packagist.org/apidoc for details.
106+
*
107+
* Caution: Returns an array of packages, you need to select the correct one
108+
* from the indexed array.
109+
*
110+
* @since 1.6
104111
* @param string $package Full qualified name ex : myname/mypackage
105-
* @return Package[] An array of packages, including the requested one.
112+
* @return array An array of packages, including the requested one, containing releases and dev branch versions
106113
*/
107114
public function getComposer(string $package): array
108115
{
@@ -207,7 +214,8 @@ protected function respond(string $url)
207214
}
208215

209216
/**
210-
* Execute two urls request, parse and merge the responses
217+
* Execute two URLs request, parse and merge the responses by adding the versions from the second URL
218+
* into the versions from the first URL.
211219
*
212220
* @param string $url1
213221
* @param string $url2
@@ -216,19 +224,19 @@ protected function respond(string $url)
216224
protected function multiRespond(string $url1, string $url2)
217225
{
218226
$response1 = $this->request($url1);
219-
$response1 = $this->parse((string)$response1);
227+
$response1 = $this->parse((string) $response1);
220228

221229
$response2 = $this->request($url2);
222-
$response2 = $this->parse((string)$response2);
223-
224-
foreach ($response1['packages'] as $k1 => $package1) {
225-
foreach ($response2['packages'] as $k2 => $package2) {
226-
if ($k1 === $k2) {
227-
foreach ($package2 as $version) {
228-
$response1['packages'][$k1][] = $version;
229-
}
230-
}
230+
$response2 = $this->parse((string) $response2);
231+
232+
foreach ($response1['packages'] as $name => $package1) {
233+
if (empty($response2['packages'][$name])) {
234+
continue;
231235
}
236+
$response1['packages'][$name] = [
237+
...$response1['packages'][$name],
238+
...$response2['packages'][$name],
239+
];
232240
}
233241

234242
return $this->create($response1);

0 commit comments

Comments
 (0)