diff --git a/composer.json b/composer.json index 06dfd74..eb0cac6 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,9 @@ } ], "minimum-stability": "stable", - "require": {}, + "require": { + "symfony/http-client": "^5.0" + }, "autoload": { "psr-4": { "Opdavies\\Glassboxx\\": "src/Glassboxx/" diff --git a/src/Glassboxx/Request/AbstractRequest.php b/src/Glassboxx/Request/AbstractRequest.php new file mode 100644 index 0000000..b4a2d1f --- /dev/null +++ b/src/Glassboxx/Request/AbstractRequest.php @@ -0,0 +1,8 @@ +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()); + } +} diff --git a/tests/Glassboxx/Request/AuthTokenRequestTest.php b/tests/Glassboxx/Request/AuthTokenRequestTest.php new file mode 100644 index 0000000..dd45832 --- /dev/null +++ b/tests/Glassboxx/Request/AuthTokenRequestTest.php @@ -0,0 +1,52 @@ +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); + } +}