r2d2
R2D2 - DeathStar API consumer
Demo
Live API
https://death-star-api.herokuapp.com/
Requirements
docker
orcomposer
&&"php": ">=7.2", "ext-json": "*"
Installation & usage
- Clone repo
- Run
composer install
ormake build
- Run
bin/console r2d2
ormake r2d2
Implementation
ApiConsumer
Contract testing
Contract - The set of pre-defined requests and expected responses. In order to ensure that our integration has been implemented correctly we might need to perform an end to end test between an API provider and an API consumer. This test can assert that the API provider returns expected responses for a set of pre-defined requests by the API consumer. Unfortunately, "end to end" tests with API provider and API consumer involved are expensive in terms of time and resources. In case of any change in our API provider we might need to perform e2e testing again. I think it is much better to test against shared contract rather than perform e2e test every time, so I decided to introduce contract test-suite instead of end to end test-suite.
Ports & Adapters Pattern
In order to satisfy goal of Contract testing and follow the single responsibility principle, I decided to extract DeathStarApiAdapter
interface and use multiple implementations of this interface:
interface DeathStarApiAdapter
{
/**
* @param string $method
* @param string $uri
* @param array $options
*
* @throws DeathStarApiException
*
* @return array
*/
public function request(string $method, string $uri, array $options): array;
}
Implementations:
- HttpDeathStarApiAdapter - It uses guzzle and it performs real HTTP calls. It isn't in use during testing,
- LoggableDeathStarApiAdapter - It logs requests and responses,
- TranslatesBinarySpeakApiAdapter - It translates droid-speak to english,
- UsesContractsDeathStarApiAdapterStub - [TEST ENV ONLY] - It asserts that DeathStarApiConsumer is matching the expected contract.