Skip to content

Commit

Permalink
Adjust TicketparkApiClientTest
Browse files Browse the repository at this point in the history
  • Loading branch information
sprain committed Oct 11, 2023
1 parent 6650777 commit fde6dfa
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 258 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"require-dev": {
"phpunit/phpunit": "^9.6",
"rector/rector": "^0.18.3",
"friendsofphp/php-cs-fixer": "^3.27"
"friendsofphp/php-cs-fixer": "^3.27",
"phpspec/prophecy": "^1.17"
}
}
2 changes: 1 addition & 1 deletion lib/ApiClient/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Response as GuzzleResponse;

final class Client
final class Client implements ClientInterface
{
private GuzzleClient $guzzle;

Expand Down
20 changes: 20 additions & 0 deletions lib/ApiClient/Http/ClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Ticketpark\ApiClient\Http;

interface ClientInterface
{
public function head(string $url, array $headers): Response;

public function get(string $url, array $headers): Response;

public function post(string $url, string $content, array $headers): Response;

public function postForm(string $url, array $formData, array $headers): Response;

public function patch(string $url, string $content, array $headers): Response;

public function delete(string $url, array $headers): Response;
}
12 changes: 9 additions & 3 deletions lib/ApiClient/TicketparkApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ticketpark\ApiClient;

use Ticketpark\ApiClient\Http\Client;
use Ticketpark\ApiClient\Http\ClientInterface;
use Ticketpark\ApiClient\Http\Response;
use Ticketpark\ApiClient\Exception\TokenGenerationException;
use Ticketpark\ApiClient\Token\AccessToken;
Expand All @@ -13,7 +14,7 @@ class TicketparkApiClient
private const ROOT_URL = 'https://api.ticketpark.ch';
private const REFRESH_TOKEN_LIFETIME = 30 * 86400;

private ?Client $client = null;
private ?ClientInterface $client = null;
private ?string $username = null;
private ?string $password = null;
private ?RefreshToken $refreshToken = null;
Expand Down Expand Up @@ -124,7 +125,12 @@ public function generateTokens(): void
throw new TokenGenerationException('Failed to generate a access tokens. Make sure to provide a valid refresh token or user credentials.');
}

private function getClient(): Client
public function setClient(ClientInterface $client): void
{
$this->client = $client;
}

private function getClient(): ClientInterface
{
if (null === $this->client) {
$this->client = new Client();
Expand Down Expand Up @@ -160,7 +166,7 @@ private function doGenerateTokens(array $data): bool
$headers = [
'Content-Type' => 'application/x-www-form-urlencoded',
'Accept' => 'application/json',
'Authorization' => 'Basic '.base64_encode($this->apiKey . ':' . $this->apiSecret)
'Authorization' => 'Basic ' . base64_encode($this->apiKey . ':' . $this->apiSecret)
];

$response = $this->getClient()->postForm(
Expand Down
Loading

0 comments on commit fde6dfa

Please sign in to comment.