Skip to content

Commit 82d2e7a

Browse files
Add Droplet list filters
1 parent dbece58 commit 82d2e7a

3 files changed

Lines changed: 127 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGE LOG
99
* Fixed hydration of object-backed App Platform and project resource fields
1010
* Fixed `LoadBalancer` hydration and update serialization
1111
* Fixed `Volume` hydration when `droplet_ids` is null
12+
* Add support for listing Droplets by name or type
1213

1314

1415
## 5.0.5 (03/05/2025)

src/Api/Droplet.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,29 @@
2727
class Droplet extends AbstractApi
2828
{
2929
/**
30+
* @param 'droplets'|'gpus'|null $type
31+
*
3032
* @throws ExceptionInterface
3133
*
3234
* @return DropletEntity[]
3335
*/
34-
public function getAll(?string $tag = null): array
36+
public function getAll(?string $tag = null, ?string $name = null, ?string $type = null): array
3537
{
36-
$droplets = $this->get('droplets', null === $tag ? [] : ['tag_name' => $tag]);
38+
$query = [];
39+
40+
if (null !== $tag) {
41+
$query['tag_name'] = $tag;
42+
}
43+
44+
if (null !== $name) {
45+
$query['name'] = $name;
46+
}
47+
48+
if (null !== $type && \in_array($type, ['droplets', 'gpus'], true)) {
49+
$query['type'] = $type;
50+
}
51+
52+
$droplets = $this->get('droplets', $query);
3753

3854
return \array_map(function ($droplet) {
3955
return new DropletEntity($droplet);

tests/Api/DropletTest.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the DigitalOcean API library.
7+
*
8+
* (c) Antoine Kirk <contact@sbin.dk>
9+
* (c) Graham Campbell <hello@gjcampbell.co.uk>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace DigitalOceanV2\Tests\Api;
16+
17+
use DigitalOceanV2\Api\Droplet;
18+
use DigitalOceanV2\Client;
19+
use DigitalOceanV2\Entity\Droplet as DropletEntity;
20+
use GuzzleHttp\Psr7\Response;
21+
use GuzzleHttp\Psr7\Utils;
22+
use Http\Client\Common\HttpMethodsClientInterface;
23+
use PHPUnit\Framework\TestCase;
24+
25+
/**
26+
* @author Graham Campbell <hello@gjcampbell.co.uk>
27+
*/
28+
class DropletTest extends TestCase
29+
{
30+
public function testItCreatesAnArrayOfDropletEntities(): void
31+
{
32+
$droplets = $this->createApiExpectingGet('/v2/droplets')->getAll();
33+
34+
self::assertInstanceOf(DropletEntity::class, $droplets[0]);
35+
self::assertSame('example.com', $droplets[0]->name);
36+
}
37+
38+
public function testItFiltersDropletsByTagName(): void
39+
{
40+
$droplets = $this->createApiExpectingGet('/v2/droplets?tag_name=awesome')->getAll('awesome');
41+
42+
self::assertInstanceOf(DropletEntity::class, $droplets[0]);
43+
self::assertSame('example.com', $droplets[0]->name);
44+
}
45+
46+
public function testItFiltersDropletsByName(): void
47+
{
48+
$droplets = $this->createApiExpectingGet('/v2/droplets?name=example.com')->getAll(name: 'example.com');
49+
50+
self::assertInstanceOf(DropletEntity::class, $droplets[0]);
51+
self::assertSame('example.com', $droplets[0]->name);
52+
}
53+
54+
public function testItFiltersDropletsByType(): void
55+
{
56+
$droplets = $this->createApiExpectingGet('/v2/droplets?type=gpus')->getAll(type: 'gpus');
57+
58+
self::assertInstanceOf(DropletEntity::class, $droplets[0]);
59+
self::assertSame('example.com', $droplets[0]->name);
60+
}
61+
62+
public function testItFiltersDropletsByStandardType(): void
63+
{
64+
$droplets = $this->createApiExpectingGet('/v2/droplets?type=droplets')->getAll(type: 'droplets');
65+
66+
self::assertInstanceOf(DropletEntity::class, $droplets[0]);
67+
self::assertSame('example.com', $droplets[0]->name);
68+
}
69+
70+
public function testItFiltersDropletsByNameAndType(): void
71+
{
72+
$droplets = $this->createApiExpectingGet('/v2/droplets?name=example.com&type=gpus')
73+
->getAll(name: 'example.com', type: 'gpus');
74+
75+
self::assertInstanceOf(DropletEntity::class, $droplets[0]);
76+
self::assertSame('example.com', $droplets[0]->name);
77+
}
78+
79+
private function createApiExpectingGet(string $uri): Droplet
80+
{
81+
$client = $this->createMock(Client::class);
82+
$client->expects(self::once())
83+
->method('getHttpClient')
84+
->willReturn($httpClient = $this->createMock(HttpMethodsClientInterface::class));
85+
86+
$httpClient->expects(self::once())
87+
->method('get')
88+
->with($uri)
89+
->willReturn(self::createResponse());
90+
91+
return new Droplet($client);
92+
}
93+
94+
private static function createResponse(): Response
95+
{
96+
return new Response(
97+
200,
98+
['Content-Type' => ['application/json']],
99+
Utils::streamFor(\json_encode(['droplets' => [
100+
[
101+
'id' => 3164444,
102+
'name' => 'example.com',
103+
'features' => [],
104+
],
105+
]]))
106+
);
107+
}
108+
}

0 commit comments

Comments
 (0)