Skip to content

Commit

Permalink
Merge pull request #5 from enflow/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
mbardelmeijer authored Oct 26, 2022
2 parents 5be5015 + 9da6bcb commit b2f2809
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.0, 7.4]
php: [8.1]
dependency-version: [prefer-lowest, prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
}
],
"require": {
"php": "^7.4|^8.0",
"guzzlehttp/guzzle": "^6.3.1|^7.0.1"
"php": "^8.1",
"guzzlehttp/guzzle": "^7.5"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"mockery/mockery": "^1.4.2",
"friendsofphp/php-cs-fixer": "^3.0"
"phpunit/phpunit": "^9.5",
"mockery/mockery": "^1.5.1",
"friendsofphp/php-cs-fixer": "^3.3"
},
"config": {
"sort-packages": true
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/ManagesRedirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function updateRedirect(int $redirectId, array $data): Redirect
return new Redirect($attributes, $this);
}

public function deleteRedirect(int $redirectId)
public function deleteRedirect(int $redirectId): void
{
$this->delete("redirects/$redirectId");
}
Expand Down
15 changes: 15 additions & 0 deletions src/Actions/ManagesTeam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace RedirectPizza\PhpSdk\Actions;

use RedirectPizza\PhpSdk\Resources\Team;

trait ManagesTeam
{
public function team(): Team
{
$attributes = $this->get("team")['data'];

return new Team($attributes, $this);
}
}
8 changes: 2 additions & 6 deletions src/Exceptions/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@

class ValidationException extends Exception
{
public $errors = [];

public function __construct(array $errors)
public function __construct(public array $errors)
{
parent::__construct('The given data failed to pass validation.');

$this->errors = $errors;
parent::__construct($this->errors['message'] ?? 'The given data failed to pass validation.');
}

public function errors(): array
Expand Down
5 changes: 3 additions & 2 deletions src/MakesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RedirectPizza\PhpSdk;

use GuzzleHttp\RequestOptions;
use Psr\Http\Message\ResponseInterface;
use RedirectPizza\PhpSdk\Exceptions\ApiException;
use RedirectPizza\PhpSdk\Exceptions\NotFoundException;
Expand Down Expand Up @@ -34,11 +35,11 @@ protected function request(string $verb, string $uri, array $payload = [])
$response = $this->client->request(
$verb,
$uri,
empty($payload) ? [] : ['form_params' => $payload]
[RequestOptions::JSON => $payload],
);

if (! $this->isSuccessful($response)) {
return $this->handleRequestError($response);
$this->handleRequestError($response);
}

$responseBody = (string) $response->getBody();
Expand Down
14 changes: 4 additions & 10 deletions src/RedirectPizza.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@

use GuzzleHttp\Client;
use RedirectPizza\PhpSdk\Actions\ManagesRedirects;
use RedirectPizza\PhpSdk\Actions\ManagesTeam;

class RedirectPizza
{
use MakesHttpRequests;
use ManagesRedirects;
use ManagesTeam;

public string $apiToken;

public Client $client;

public function __construct(string $apiToken, Client $client = null)
public function __construct(public string $apiToken, protected ?Client $client = null)
{
$this->apiToken = $apiToken;

$this->client = $client ?: new Client([
'base_uri' => 'https://redirect.pizza/api/v1/',
'http_errors' => false,
Expand All @@ -31,8 +27,6 @@ public function __construct(string $apiToken, Client $client = null)

protected function transformCollection(array $collection, string $class): array
{
return array_map(function ($attributes) use ($class) {
return new $class($attributes, $this);
}, $collection);
return array_map(fn ($attributes) => new $class($attributes, $this), $collection);
}
}
4 changes: 4 additions & 0 deletions src/Resources/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class Domain extends ApiResource

public bool $isRootDomain;

public bool $hsts;

public bool $preventForeignEmbedding;

public array $dns;

public array $ssl;
Expand Down
10 changes: 4 additions & 6 deletions src/Resources/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ class Redirect extends ApiResource

public bool $tracking;

public array $tags;

public function __construct(array $attributes, $redirectPizza = null)
{
parent::__construct($attributes, $redirectPizza);

$this->sources = array_map(function (array $checkAttributes) {
return new Source($checkAttributes);
}, $this->sources ?? []);
$this->sources = array_map(fn (array $checkAttributes) => new Source($checkAttributes), $this->sources ?? []);

$this->domains = array_map(function (array $checkAttributes) {
return new Domain($checkAttributes);
}, $this->domains ?? []);
$this->domains = array_map(fn (array $checkAttributes) => new Domain($checkAttributes), $this->domains ?? []);
}

public function update(array $data): void
Expand Down
16 changes: 16 additions & 0 deletions src/Resources/Team.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace RedirectPizza\PhpSdk\Resources;

class Team extends ApiResource
{
public int $id;

public string $name;

public array $hostnames;

public array $hits;

public array $users;
}
55 changes: 29 additions & 26 deletions tests/RedirectPizzaSdkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
namespace RedirectPizza\PhpSdk\Tests;

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Response;
use Mockery;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use RedirectPizza\PhpSdk\Exceptions\NotFoundException;
use RedirectPizza\PhpSdk\Exceptions\ValidationException;
use RedirectPizza\PhpSdk\RedirectPizza;

class RedirectPizzaSdkTest extends TestCase
{
protected function tearDown(): void
private ClientInterface|MockObject $guzzleClient;
private RedirectPizza $redirectPizzaClient;

protected function setUp(): void
{
Mockery::close();
parent::setUp();

$this->guzzleClient = $this->createMock(Client::class);
$this->redirectPizzaClient = new RedirectPizza('123', $this->guzzleClient);
}

public function test_it_can_instantiate_an_object()
Expand All @@ -26,31 +33,27 @@ public function test_it_can_instantiate_an_object()

public function test_making_basic_requests()
{
$redirectPizza = new RedirectPizza('123', $http = Mockery::mock(Client::class));

$http->shouldReceive('request')->once()->with('GET', 'redirects', [])->andReturn(
$response = Mockery::mock(Response::class)
);
$response = new Response(200, [], json_encode(['data' => [['id' => 1]]]));

$response->shouldReceive('getStatusCode')->once()->andReturn(200);
$response->shouldReceive('getBody')->once()->andReturn('{"data": [{"id": "1"}]}');
$this->guzzleClient
->expects($this->once())
->method('request')
->willReturn($response);

$this->assertCount(1, $redirectPizza->redirects());
$this->assertCount(1, $this->redirectPizzaClient->redirects());
}

public function test_handling_validation_errors()
{
$redirectPizza = new RedirectPizza('123', $http = Mockery::mock(Client::class));
$response = new Response(422, [], json_encode(['name' => ['The destination is required.']]));

$http->shouldReceive('request')->once()->with('POST', 'redirects', [])->andReturn(
$response = Mockery::mock(Response::class)
);

$response->shouldReceive('getStatusCode')->andReturn(422);
$response->shouldReceive('getBody')->once()->andReturn('{"name": ["The destination is required."]}');
$this->guzzleClient
->expects($this->once())
->method('request')
->willReturn($response);

try {
$redirectPizza->createRedirect([]);
$this->redirectPizzaClient->createRedirect([]);
} catch (ValidationException $e) {
}

Expand All @@ -61,14 +64,14 @@ public function test_handling_404_errors()
{
$this->expectException(NotFoundException::class);

$redirectPizza = new RedirectPizza('123', $http = Mockery::mock(Client::class));

$http->shouldReceive('request')->once()->with('GET', 'redirects/123', [])->andReturn(
$response = Mockery::mock(Response::class)
);
$response = new Response(404, [], json_encode([]));

$response->shouldReceive('getStatusCode')->andReturn(404);
$this->guzzleClient
->expects($this->once())
->method('request')
->with('GET', 'redirects/123')
->willReturn($response);

$redirectPizza->redirect(123);
$this->redirectPizzaClient->redirect(123);
}
}

0 comments on commit b2f2809

Please sign in to comment.