Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prolic committed Jul 17, 2017
1 parent 13a179b commit 6cadd12
Show file tree
Hide file tree
Showing 2 changed files with 246 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/HttplugEventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function fetchStreamMetadata(StreamName $streamName): array

switch ($response->getStatusCode()) {
case 200:
$metadata = json_decode($response->getBody()->getContents());
$metadata = json_decode($response->getBody()->getContents(), true);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException('Could not json decode response');
Expand Down Expand Up @@ -214,9 +214,9 @@ public function hasStream(StreamName $streamName): bool

switch ($response->getStatusCode()) {
case 200:
break;
return true;
case 404:
throw StreamNotFound::with($streamName);
return false;
case 403:
case 405:
throw new NotAllowed();
Expand Down
249 changes: 243 additions & 6 deletions tests/HttplugEventStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use ProophTest\EventStore\Mock\TestDomainEvent;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

class HttplugEventStoreTest extends TestCase
{
Expand Down Expand Up @@ -71,8 +72,9 @@ public function it_updates_stream_metadata(): void

/**
* @test
* @dataProvider forbiddenStatusCodes
*/
public function it_throws_exception_when_forbidden_to_update_metadata(): void
public function it_throws_exception_when_forbidden_to_update_metadata(int $forbiddenStatusCode): void
{
$this->expectException(NotAllowed::class);

Expand All @@ -92,7 +94,7 @@ public function it_throws_exception_when_forbidden_to_update_metadata(): void
->willReturn($request);

$response = $this->prophesize(ResponseInterface::class);
$response->getStatusCode()->willReturn(405)->shouldBeCalled();
$response->getStatusCode()->willReturn($forbiddenStatusCode)->shouldBeCalled();

$httpClient = $this->prophesize(HttpClient::class);
$httpClient->sendRequest($request)->willReturn($response->reveal())->shouldBeCalled();
Expand Down Expand Up @@ -313,8 +315,9 @@ public function it_creates_stream_and_throws_error_on_400(): void

/**
* @test
* @dataProvider forbiddenStatusCodes
*/
public function it_cannot_create_stream_when_not_allowed(): void
public function it_cannot_create_stream_when_not_allowed(int $forbiddenStatusCode): void
{
$this->expectException(NotAllowed::class);

Expand All @@ -340,7 +343,7 @@ public function it_cannot_create_stream_when_not_allowed(): void
->willReturn($request);

$response = $this->prophesize(ResponseInterface::class);
$response->getStatusCode()->willReturn(405)->shouldBeCalled();
$response->getStatusCode()->willReturn($forbiddenStatusCode)->shouldBeCalled();

$httpClient = $this->prophesize(HttpClient::class);
$httpClient->sendRequest($request)->willReturn($response->reveal())->shouldBeCalled();
Expand Down Expand Up @@ -517,8 +520,9 @@ public function it_cannot_delete_stream_when_not_found(): void

/**
* @test
* @dataProvider forbiddenStatusCodes
*/
public function it_cannot_delete_stream_when_not_allowed(): void
public function it_cannot_delete_stream_when_not_allowed(int $forbiddenStatusCode): void
{
$this->expectException(NotAllowed::class);

Expand All @@ -534,7 +538,7 @@ public function it_cannot_delete_stream_when_not_allowed(): void
->willReturn($request);

$response = $this->prophesize(ResponseInterface::class);
$response->getStatusCode()->willReturn(405)->shouldBeCalled();
$response->getStatusCode()->willReturn($forbiddenStatusCode)->shouldBeCalled();

$httpClient = $this->prophesize(HttpClient::class);
$httpClient->sendRequest($request)->willReturn($response->reveal())->shouldBeCalled();
Expand Down Expand Up @@ -584,4 +588,237 @@ public function it_handles_unknown_errors_on_delete(): void
}

//</editor-fold>

//<editor-fold desc="fetchStreamMetadata">

/**
* @test
*/
public function it_fetches_stream_metadata(): void
{
$request = $this->prophesize(RequestInterface::class);
$request = $request->reveal();

$requestFactory = $this->prophesize(RequestFactory::class);
$requestFactory
->createRequest(
'GET',
'streammetadata/somename',
[
'Accept' => 'application/json',
]
)
->willReturn($request);

$body = $this->prophesize(StreamInterface::class);
$body->getContents()->willReturn(json_encode(['foo' => 'bar']))->shouldBeCalled();

$response = $this->prophesize(ResponseInterface::class);
$response->getStatusCode()->willReturn(200)->shouldBeCalled();
$response->getBody()->willReturn($body->reveal())->shouldBeCalled();

$httpClient = $this->prophesize(HttpClient::class);
$httpClient->sendRequest($request)->willReturn($response->reveal())->shouldBeCalled();

$eventStore = new HttplugEventStore(
$this->prophesize(MessageFactory::class)->reveal(),
$this->prophesize(MessageConverter::class)->reveal(),
$httpClient->reveal(),
$requestFactory->reveal()
);

$streamMetadata = $eventStore->fetchStreamMetadata(new StreamName('somename'));

$this->assertSame(['foo' => 'bar'], $streamMetadata);
}

/**
* @test
* @dataProvider forbiddenStatusCodes
*/
public function it_throws_not_allowed_when_forbidden_to_fetch_stream_metadata(int $forbidenStatusCode): void
{
$this->expectException(NotAllowed::class);

$request = $this->prophesize(RequestInterface::class);
$request = $request->reveal();

$requestFactory = $this->prophesize(RequestFactory::class);
$requestFactory
->createRequest(
'GET',
'streammetadata/somename',
[
'Accept' => 'application/json',
]
)
->willReturn($request);

$response = $this->prophesize(ResponseInterface::class);
$response->getStatusCode()->willReturn($forbidenStatusCode)->shouldBeCalled();
$response->getBody()->shouldNotBeCalled();

$httpClient = $this->prophesize(HttpClient::class);
$httpClient->sendRequest($request)->willReturn($response->reveal())->shouldBeCalled();

$eventStore = new HttplugEventStore(
$this->prophesize(MessageFactory::class)->reveal(),
$this->prophesize(MessageConverter::class)->reveal(),
$httpClient->reveal(),
$requestFactory->reveal()
);

$eventStore->fetchStreamMetadata(new StreamName('somename'));
}

/**
* @test
*/
public function it_throws_stream_not_found_when_trying_to_fetch_unknown_stream_metadata(): void
{
$this->expectException(StreamNotFound::class);

$request = $this->prophesize(RequestInterface::class);
$request = $request->reveal();

$requestFactory = $this->prophesize(RequestFactory::class);
$requestFactory
->createRequest(
'GET',
'streammetadata/somename',
[
'Accept' => 'application/json',
]
)
->willReturn($request);

$response = $this->prophesize(ResponseInterface::class);
$response->getStatusCode()->willReturn(404)->shouldBeCalled();
$response->getBody()->shouldNotBeCalled();

$httpClient = $this->prophesize(HttpClient::class);
$httpClient->sendRequest($request)->willReturn($response->reveal())->shouldBeCalled();

$eventStore = new HttplugEventStore(
$this->prophesize(MessageFactory::class)->reveal(),
$this->prophesize(MessageConverter::class)->reveal(),
$httpClient->reveal(),
$requestFactory->reveal()
);

$eventStore->fetchStreamMetadata(new StreamName('somename'));
}

//</editor-fold>

//<editor-fold desc="hasStream">

/**
* @test
*/
public function it_returns_true_when_asking_for_existing_stream(): void
{
$request = $this->prophesize(RequestInterface::class);
$request = $request->reveal();

$requestFactory = $this->prophesize(RequestFactory::class);
$requestFactory
->createRequest(
'GET',
'has-stream/somename'
)
->willReturn($request);

$response = $this->prophesize(ResponseInterface::class);
$response->getStatusCode()->willReturn(200)->shouldBeCalled();

$httpClient = $this->prophesize(HttpClient::class);
$httpClient->sendRequest($request)->willReturn($response->reveal())->shouldBeCalled();

$eventStore = new HttplugEventStore(
$this->prophesize(MessageFactory::class)->reveal(),
$this->prophesize(MessageConverter::class)->reveal(),
$httpClient->reveal(),
$requestFactory->reveal()
);

$this->assertTrue($eventStore->hasStream(new StreamName('somename')));
}

/**
* @test
*/
public function it_returns_false_when_asking_for_non_existing_stream(): void
{
$request = $this->prophesize(RequestInterface::class);
$request = $request->reveal();

$requestFactory = $this->prophesize(RequestFactory::class);
$requestFactory
->createRequest(
'GET',
'has-stream/somename'
)
->willReturn($request);

$response = $this->prophesize(ResponseInterface::class);
$response->getStatusCode()->willReturn(404)->shouldBeCalled();

$httpClient = $this->prophesize(HttpClient::class);
$httpClient->sendRequest($request)->willReturn($response->reveal())->shouldBeCalled();

$eventStore = new HttplugEventStore(
$this->prophesize(MessageFactory::class)->reveal(),
$this->prophesize(MessageConverter::class)->reveal(),
$httpClient->reveal(),
$requestFactory->reveal()
);

$this->assertFalse($eventStore->hasStream(new StreamName('somename')));
}

/**
* @test
* @dataProvider forbiddenStatusCodes
*/
public function it_throws_not_allowed_when_forbidden_to_ask_for_existince_of_a_stream(int $forbidenStatusCode): void
{
$this->expectException(NotAllowed::class);

$request = $this->prophesize(RequestInterface::class);
$request = $request->reveal();

$requestFactory = $this->prophesize(RequestFactory::class);
$requestFactory
->createRequest(
'GET',
'has-stream/somename'
)
->willReturn($request);

$response = $this->prophesize(ResponseInterface::class);
$response->getStatusCode()->willReturn($forbidenStatusCode)->shouldBeCalled();

$httpClient = $this->prophesize(HttpClient::class);
$httpClient->sendRequest($request)->willReturn($response->reveal())->shouldBeCalled();

$eventStore = new HttplugEventStore(
$this->prophesize(MessageFactory::class)->reveal(),
$this->prophesize(MessageConverter::class)->reveal(),
$httpClient->reveal(),
$requestFactory->reveal()
);

$eventStore->hasStream(new StreamName('somename'));
}

//</editor-fold>

public function forbiddenStatusCodes(): array
{
return [
[403],
[405],
];
}
}

0 comments on commit 6cadd12

Please sign in to comment.