Skip to content

Commit 2aa3caf

Browse files
committed
API: add vendor bundles endpoints to add, edit, delete a bundle and manage packages
1 parent 9650560 commit 2aa3caf

File tree

5 files changed

+383
-0
lines changed

5 files changed

+383
-0
lines changed

src/Api/VendorBundles.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/*
4+
* (c) Packagist Conductors GmbH <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace PrivatePackagist\ApiClient\Api;
11+
12+
class VendorBundles extends AbstractApi
13+
{
14+
/**
15+
* @return array[]
16+
*/
17+
public function all()
18+
{
19+
return $this->get('/vendor-bundles/');
20+
}
21+
22+
/**
23+
* @param int $vendorBundleId
24+
* @return array
25+
*/
26+
public function show($vendorBundleId)
27+
{
28+
return $this->get(sprintf('/vendor-bundles/%s/', $vendorBundleId));
29+
}
30+
31+
/**
32+
* @param string $name
33+
* @param string|null $minimumAccessibleStability
34+
* @param string|null $versionConstraint
35+
* @param bool $assignAllPackages
36+
* @param int[] $synchronizationIds
37+
*/
38+
public function create($name, $minimumAccessibleStability = null, $versionConstraint = null, $assignAllPackages = false, array $synchronizationIds = [])
39+
{
40+
return $this->post('/vendor-bundles/', [
41+
'name' => $name,
42+
'minimumAccessibleStability' => $minimumAccessibleStability,
43+
'versionConstraint' => $versionConstraint,
44+
'assignAllPackages' => $assignAllPackages,
45+
'synchronizationIds' => $synchronizationIds,
46+
]);
47+
}
48+
49+
/**
50+
* @param int $vendorBundleId
51+
* @param array{name: string, minimumAccessibleStability?: string, versionConstraint?: string, assignAllPackages: bool, synchronizationIds?: int[]} $bundle
52+
* @return array
53+
*/
54+
public function edit($vendorBundleId, array $bundle)
55+
{
56+
return $this->put(sprintf('/vendor-bundles/%s/', $vendorBundleId), $bundle);
57+
}
58+
59+
/**
60+
* @param int $vendorBundleId
61+
*/
62+
public function remove($vendorBundleId)
63+
{
64+
return $this->delete(sprintf('/vendor-bundles/%s/', $vendorBundleId));
65+
}
66+
67+
public function packages(): \PrivatePackagist\ApiClient\Api\VendorBundles\Packages
68+
{
69+
return new \PrivatePackagist\ApiClient\Api\VendorBundles\Packages($this->client);
70+
}
71+
}

src/Api/VendorBundles/Packages.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* (c) Packagist Conductors GmbH <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace PrivatePackagist\ApiClient\Api\VendorBundles;
11+
12+
use PrivatePackagist\ApiClient\Api\AbstractApi;
13+
use PrivatePackagist\ApiClient\Exception\InvalidArgumentException;
14+
15+
class Packages extends AbstractApi
16+
{
17+
/**
18+
* @param int $vendorBundleIds
19+
* @return array[]
20+
*/
21+
public function listPackages($vendorBundleIds)
22+
{
23+
return $this->get(sprintf('/vendor-bundles/%s/packages/', $vendorBundleIds));
24+
}
25+
26+
/**
27+
* @param int $vendorBundleId
28+
* @param array{name: string, versionConstraint?: string, minimumAccessibleStability?: string}[]
29+
* @return array[]
30+
*/
31+
public function addOrEditPackages($vendorBundleId, array $packages)
32+
{
33+
foreach ($packages as $package) {
34+
if (!isset($package['name'])) {
35+
throw new InvalidArgumentException('Parameter "name" is required.');
36+
}
37+
}
38+
39+
return $this->post(sprintf('/vendor-bundles/%s/packages/', $vendorBundleId), $packages);
40+
}
41+
42+
/**
43+
* @param int $vendorBundleId
44+
* @param string $packageName
45+
* @return null
46+
*/
47+
public function removePackage($vendorBundleId, $packageName)
48+
{
49+
return $this->delete(sprintf('/vendor-bundles/%s/packages/%s/', $vendorBundleId, $packageName));
50+
}
51+
}

src/Client.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ public function synchronizations()
117117
return new Api\Synchronizations($this, $this->responseMediator);
118118
}
119119

120+
public function vendorBundles()
121+
{
122+
return new Api\VendorBundles($this, $this->responseMediator);
123+
}
124+
120125
public function getHttpClient()
121126
{
122127
return $this->getHttpClientBuilder()->getHttpClient();
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/*
4+
* (c) Packagist Conductors GmbH <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace PrivatePackagist\ApiClient\Api\VendorBundles;
11+
12+
use PHPUnit\Framework\MockObject\MockObject;
13+
use PrivatePackagist\ApiClient\Api\ApiTestCase;
14+
use PrivatePackagist\ApiClient\Exception\InvalidArgumentException;
15+
16+
class PackagesTest extends ApiTestCase
17+
{
18+
public function testListPackages()
19+
{
20+
$expected = [
21+
[
22+
'versionConstraint' => null,
23+
'minimumAccessibleStability' => null,
24+
'package' => [],
25+
],
26+
];
27+
28+
/** @var Packages&MockObject $api */
29+
$api = $this->getApiMock();
30+
$api->expects($this->once())
31+
->method('get')
32+
->with($this->identicalTo('/vendor-bundles/1/packages/'))
33+
->willReturn($expected);
34+
35+
$this->assertSame($expected, $api->listPackages(1));
36+
}
37+
38+
public function testAddOrEditPackages()
39+
{
40+
$expected = [
41+
[
42+
'versionConstraint' => null,
43+
'minimumAccessibleStability' => null,
44+
'package' => [],
45+
],
46+
];
47+
48+
$packages = [['name' => 'acme/package']];
49+
50+
/** @var Packages&MockObject $api */
51+
$api = $this->getApiMock();
52+
$api->expects($this->once())
53+
->method('post')
54+
->with($this->identicalTo('/vendor-bundles/1/packages/'), $this->identicalTo($packages))
55+
->willReturn($expected);
56+
57+
$this->assertSame($expected, $api->addOrEditPackages(1, $packages));
58+
}
59+
60+
public function testAddOrEditPackagesMissingName()
61+
{
62+
$this->expectException(InvalidArgumentException::class);
63+
$this->expectExceptionMessage('Parameter "name" is required.');
64+
65+
/** @var Packages&MockObject $api */
66+
$api = $this->getApiMock();
67+
68+
$api->addOrEditPackages(1, [[]]);
69+
}
70+
71+
public function testRemovePackage()
72+
{
73+
$expected = '';
74+
$packageName = 'composer/composer';
75+
76+
/** @var Packages&MockObject $api */
77+
$api = $this->getApiMock();
78+
$api->expects($this->once())
79+
->method('delete')
80+
->with($this->identicalTo(sprintf('/vendor-bundles/1/packages/%s/', $packageName)))
81+
->willReturn($expected);
82+
83+
$this->assertSame($expected, $api->removePackage(1, $packageName));
84+
}
85+
86+
protected function getApiClass()
87+
{
88+
return Packages::class;
89+
}
90+
}

tests/Api/VendorBundlesTest.php

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?php
2+
3+
/*
4+
* (c) Packagist Conductors GmbH <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace PrivatePackagist\ApiClient\Api;
11+
12+
use PHPUnit\Framework\MockObject\MockObject;
13+
14+
class VendorBundlesTest extends ApiTestCase
15+
{
16+
public function testAll()
17+
{
18+
$expected = [
19+
[
20+
'id' => 1,
21+
'name' => 'Bundle',
22+
'versionConstraint' => null,
23+
'minimumAccessibleStability' => null,
24+
'assignAllPackages' => false,
25+
'synchronizations' => [],
26+
],
27+
];
28+
29+
/** @var VendorBundles&MockObject $api */
30+
$api = $this->getApiMock();
31+
$api->expects($this->once())
32+
->method('get')
33+
->with($this->identicalTo('/vendor-bundles/'))
34+
->willReturn($expected);
35+
36+
$this->assertSame($expected, $api->all());
37+
}
38+
39+
public function testShow()
40+
{
41+
$expected = [
42+
'id' => 1,
43+
'name' => 'Bundle',
44+
'versionConstraint' => null,
45+
'minimumAccessibleStability' => null,
46+
'assignAllPackages' => false,
47+
'synchronizations' => [],
48+
];
49+
50+
/** @var VendorBundles&MockObject $api */
51+
$api = $this->getApiMock();
52+
$api->expects($this->once())
53+
->method('get')
54+
->with($this->identicalTo('/vendor-bundles/1/'))
55+
->willReturn($expected);
56+
57+
$this->assertSame($expected, $api->show(1));
58+
}
59+
60+
public function testCreate()
61+
{
62+
$expected = [
63+
'id' => 1,
64+
'name' => 'Bundle',
65+
'versionConstraint' => null,
66+
'minimumAccessibleStability' => null,
67+
'assignAllPackages' => false,
68+
'synchronizations' => [],
69+
];
70+
71+
/** @var VendorBundles&MockObject $api */
72+
$api = $this->getApiMock();
73+
$api->expects($this->once())
74+
->method('post')
75+
->with($this->identicalTo('/vendor-bundles/'), $this->identicalTo([
76+
'name' => 'Bundle',
77+
'minimumAccessibleStability' => null,
78+
'versionConstraint' => null,
79+
'assignAllPackages' => false,
80+
'synchronizationIds' => [],
81+
]))
82+
->willReturn($expected);
83+
84+
$this->assertSame($expected, $api->create('Bundle'));
85+
}
86+
87+
public function testCreateAllParameters()
88+
{
89+
$expected = [
90+
'id' => 1,
91+
'name' => 'Bundle',
92+
'versionConstraint' => '^1.0',
93+
'minimumAccessibleStability' => 'stable',
94+
'assignAllPackages' => true,
95+
'synchronizations' => [
96+
['id' => 123],
97+
],
98+
];
99+
100+
/** @var VendorBundles&MockObject $api */
101+
$api = $this->getApiMock();
102+
$api->expects($this->once())
103+
->method('post')
104+
->with($this->identicalTo('/vendor-bundles/'), $this->identicalTo([
105+
'name' => 'Bundle',
106+
'minimumAccessibleStability' => 'stable',
107+
'versionConstraint' => '^1.0',
108+
'assignAllPackages' => true,
109+
'synchronizationIds' => [123],
110+
]))
111+
->willReturn($expected);
112+
113+
$this->assertSame($expected, $api->create('Bundle', 'stable', '^1.0', true, [123]));
114+
}
115+
116+
public function tesEdit()
117+
{
118+
$expected = [
119+
'id' => 1,
120+
'name' => 'Bundle',
121+
'versionConstraint' => '^1.0',
122+
'minimumAccessibleStability' => 'stable',
123+
'assignAllPackages' => true,
124+
'synchronizations' => [
125+
['id' => 123],
126+
],
127+
];
128+
129+
$bundle = [
130+
'id' => 1,
131+
'name' => 'Bundle',
132+
'versionConstraint' => '^1.0',
133+
'minimumAccessibleStability' => 'stable',
134+
'assignAllPackages' => true,
135+
'synchronizationIds' => [123],
136+
];
137+
138+
/** @var VendorBundles&MockObject $api */
139+
$api = $this->getApiMock();
140+
$api->expects($this->once())
141+
->method('put')
142+
->with($this->identicalTo('/vendor-bundles/1/'), $this->identicalTo($bundle))
143+
->willReturn($expected);
144+
145+
$this->assertSame($expected, $api->edit(1, $bundle));
146+
}
147+
148+
public function testRemove()
149+
{
150+
$expected = '';
151+
152+
/** @var VendorBundles&MockObject $api */
153+
$api = $this->getApiMock();
154+
$api->expects($this->once())
155+
->method('delete')
156+
->with($this->identicalTo('/vendor-bundles/1/'))
157+
->willReturn($expected);
158+
159+
$this->assertSame($expected, $api->remove(1));
160+
}
161+
162+
protected function getApiClass()
163+
{
164+
return VendorBundles::class;
165+
}
166+
}

0 commit comments

Comments
 (0)