|
| 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