mirror of
https://github.com/opdavies/glassboxx-sdk-php.git
synced 2025-01-22 12:07:32 +00:00
parent
1c6a7c2ee4
commit
352ef9d14f
|
@ -9,7 +9,9 @@
|
|||
}
|
||||
],
|
||||
"minimum-stability": "stable",
|
||||
"require": {},
|
||||
"require": {
|
||||
"symfony/http-client": "^5.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Opdavies\\Glassboxx\\": "src/Glassboxx/"
|
||||
|
|
8
src/Glassboxx/Request/AbstractRequest.php
Normal file
8
src/Glassboxx/Request/AbstractRequest.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace Opdavies\Glassboxx\Request;
|
||||
|
||||
abstract class AbstractRequest
|
||||
{
|
||||
public const BASE_URL = 'https://server.glassboxx.co.uk/rest/V1';
|
||||
}
|
47
src/Glassboxx/Request/AuthTokenAbstractRequest.php
Normal file
47
src/Glassboxx/Request/AuthTokenAbstractRequest.php
Normal file
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
namespace Opdavies\Glassboxx\Request;
|
||||
|
||||
use Opdavies\Glassboxx\Config;
|
||||
use Symfony\Component\HttpClient\HttpClient;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
final class AuthTokenAbstractRequest extends AbstractRequest
|
||||
{
|
||||
public const ENDPOINT = '/integration/admin/token';
|
||||
|
||||
/** @var Config */
|
||||
private $config;
|
||||
|
||||
/** @var HttpClient */
|
||||
private $client;
|
||||
|
||||
public function __construct(HttpClientInterface $client)
|
||||
{
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
public function withConfig(Config $config): self
|
||||
{
|
||||
$this->config = $config;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getToken(): string
|
||||
{
|
||||
$response = $this->client
|
||||
->request(
|
||||
'POST',
|
||||
self::BASE_URL.self::ENDPOINT,
|
||||
[
|
||||
'query' => [
|
||||
'password' => $this->config->getPassword(),
|
||||
'username' => $this->config->getUsername(),
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
return json_decode($response->getContent());
|
||||
}
|
||||
}
|
52
tests/Glassboxx/Request/AuthTokenRequestTest.php
Normal file
52
tests/Glassboxx/Request/AuthTokenRequestTest.php
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace Opdavies\Glassboxx\Tests\Glassboxx\Request;
|
||||
|
||||
use Opdavies\Glassboxx\Config;
|
||||
use Opdavies\Glassboxx\Request\AuthTokenAbstractRequest;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpClient\MockHttpClient;
|
||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||
|
||||
class AuthTokenRequestTest extends TestCase
|
||||
{
|
||||
public function testThatItGetsAnAuthCode(): void
|
||||
{
|
||||
$config = $this->getMockBuilder(Config::class)
|
||||
->onlyMethods([])
|
||||
->setConstructorArgs(
|
||||
[
|
||||
'vendor_id' => 123,
|
||||
'username' => 'opdavies',
|
||||
'password' => 'secret',
|
||||
]
|
||||
)
|
||||
->getMock();
|
||||
|
||||
$mockRepsonse = $this->getMockBuilder(ResponseInterface::class)
|
||||
->getMock();
|
||||
$mockRepsonse->method('getContent')->willReturn('"abc123"');
|
||||
|
||||
$client = $this->getMockBuilder(MockHttpClient::class)->getMock();
|
||||
$client->expects($this->once())
|
||||
->method('request')
|
||||
->with(
|
||||
'POST',
|
||||
AuthTokenAbstractRequest::BASE_URL
|
||||
.AuthTokenAbstractRequest::ENDPOINT,
|
||||
[
|
||||
'query' => [
|
||||
'password' => 'secret',
|
||||
'username' => 'opdavies',
|
||||
],
|
||||
]
|
||||
)
|
||||
->willReturn($mockRepsonse);
|
||||
|
||||
$token = (new AuthTokenAbstractRequest($client))
|
||||
->withConfig($config)
|
||||
->getToken();
|
||||
|
||||
$this->assertSame('abc123', $token);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue