Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
saltandvinegarcrisps committed Mar 22, 2022
1 parent ea07545 commit 0ed0d14
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
27 changes: 17 additions & 10 deletions spec/PingSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,36 @@ class PingSpec extends ObjectBehavior
public function it_should_perform_start(ClientInterface $httpClient, ResponseInterface $response)
{
$httpClient->sendRequest(new TypeToken(RequestInterface::class))->shouldBeCalled()->willReturn($response);
$this->beConstructedWith('none', 'https://foo.dev', null, $httpClient);
$this->beConstructedWith('none', 'https://foo.dev', $httpClient);
$this->start();
}

public function it_should_perform_success(ClientInterface $httpClient, ResponseInterface $response)
{
$httpClient->sendRequest(new TypeToken(RequestInterface::class))->shouldBeCalled()->willReturn($response);
$this->beConstructedWith('none', 'https://foo.dev', null, $httpClient);
$this->beConstructedWith('none', 'https://foo.dev', $httpClient);
$this->success();
}

public function it_should_perform_fail(ClientInterface $httpClient, ResponseInterface $response)
{
$httpClient->sendRequest(new TypeToken(RequestInterface::class))->shouldBeCalled()->willReturn($response);
$this->beConstructedWith('none', 'https://foo.dev', null, $httpClient);
$this->beConstructedWith('none', 'https://foo.dev', $httpClient);
$this->fail();
}

public function it_should_change_uuid(ClientInterface $httpClient, ResponseInterface $response)
public function it_should_change_uuid()
{
$this->beConstructedWith('none', 'https://foo.dev');
$this->withUuid('test')->shouldReturnAnInstanceOf(Ping::class);
}

public function it_should_change_http_client(ClientInterface $httpClient)
{
$this->beConstructedWith('none', 'https://foo.dev');
$this->withHttpClient($httpClient)->shouldReturnAnInstanceOf(Ping::class);
}

public function it_should_handle_HTTP_BAD_REQUEST(ClientInterface $httpClient, ResponseInterface $response, ClientExceptionInterface $error)
{
$httpClient->sendRequest(new TypeToken(RequestInterface::class))->shouldBeCalled()
Expand All @@ -58,7 +65,7 @@ public function getResponse(): ResponseInterface
})
;
$response->getStatusCode()->shouldBeCalled()->willReturn(400);
$this->beConstructedWith('none', 'https://foo.dev', null, $httpClient);
$this->beConstructedWith('none', 'https://foo.dev', $httpClient);
$this->shouldThrow(Exceptions\InvalidPayloadError::class)->duringStart();
}

Expand All @@ -79,7 +86,7 @@ public function getResponse(): ResponseInterface
})
;
$response->getStatusCode()->shouldBeCalled()->willReturn(401);
$this->beConstructedWith('none', 'https://foo.dev', null, $httpClient);
$this->beConstructedWith('none', 'https://foo.dev', $httpClient);
$this->shouldThrow(Exceptions\UnauthorisedError::class)->duringStart();
}

Expand All @@ -100,7 +107,7 @@ public function getResponse(): ResponseInterface
})
;
$response->getStatusCode()->shouldBeCalled()->willReturn(403);
$this->beConstructedWith('none', 'https://foo.dev', null, $httpClient);
$this->beConstructedWith('none', 'https://foo.dev', $httpClient);
$this->shouldThrow(Exceptions\AccountLimitReached::class)->duringStart();
}

Expand All @@ -121,7 +128,7 @@ public function getResponse(): ResponseInterface
})
;
$response->getStatusCode()->shouldBeCalled()->willReturn(404);
$this->beConstructedWith('none', 'https://foo.dev', null, $httpClient);
$this->beConstructedWith('none', 'https://foo.dev', $httpClient);
$this->shouldThrow(Exceptions\UuidNotFound::class)->duringStart();
}

Expand All @@ -142,7 +149,7 @@ public function getResponse(): ResponseInterface
})
;
$response->getStatusCode()->shouldBeCalled()->willReturn(500);
$this->beConstructedWith('none', 'https://foo.dev', null, $httpClient);
$this->beConstructedWith('none', 'https://foo.dev', $httpClient);
$this->shouldThrow(Exceptions\FailureError::class)->duringStart();
}

Expand All @@ -152,7 +159,7 @@ public function it_should_handle_Generic_HTTP_5xx(ClientInterface $httpClient, C
->willThrow(new class('test') extends \Exception implements ClientExceptionInterface {
})
;
$this->beConstructedWith('none', 'https://foo.dev', null, $httpClient);
$this->beConstructedWith('none', 'https://foo.dev', $httpClient);
$this->shouldThrow(Exceptions\FailureError::class)->duringStart();
}
}
8 changes: 5 additions & 3 deletions src/Ping.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,31 @@ class Ping

protected string $uuid;

protected string $url = 'https://hc-ping.com';
protected string $url;

protected RequestBuilder $requestBuilder;

final public function __construct(
string $uuid,
string $url = 'https://hc-ping.com',
?ClientInterface $httpClient = null,
?RequestBuilder $requestBuilder = null
) {
$this->uuid = $uuid;
$this->url = $url;
$factory = new Psr17Factory();
$this->httpClient = $httpClient ?? new HttpClient($factory, $factory);
$this->requestBuilder = $requestBuilder ?? new RequestBuilder($factory, $factory);
}

public function withUuid(string $uuid): self
{
return new self($uuid, $this->httpClient, $this->requestBuilder);
return new self($uuid, $this->url, $this->httpClient, $this->requestBuilder);
}

public function withHttpClient(ClientInterface $httpClient): self
{
return new self($this->uuid, $httpClient, $this->requestBuilder);
return new self($this->uuid, $this->url, $httpClient, $this->requestBuilder);
}

protected function ping(string $action = ''): bool
Expand Down

0 comments on commit 0ed0d14

Please # to comment.