From 7d4aee5e9c061b673ab4261507ae064679e04391 Mon Sep 17 00:00:00 2001 From: darthmaim Date: Wed, 20 Mar 2019 10:44:00 +0100 Subject: [PATCH] Add schema version support --- src/GW2Api.php | 29 +++++++++++++++++ src/V2/Endpoint.php | 25 ++++++++++++++ src/V2/IEndpoint.php | 16 +++++++++ src/V2/Schema/SchemaHandler.php | 27 +++++++++++++++ tests/SchemaTest.php | 58 +++++++++++++++++++++++++++++++++ tests/TestCase.php | 4 +++ 6 files changed, 159 insertions(+) create mode 100644 src/V2/Schema/SchemaHandler.php create mode 100644 tests/SchemaTest.php diff --git a/src/GW2Api.php b/src/GW2Api.php index 01f119a..fcbbbfc 100644 --- a/src/GW2Api.php +++ b/src/GW2Api.php @@ -53,8 +53,12 @@ use GW2Treasures\GW2Api\V2\IEndpoint; use GW2Treasures\GW2Api\V2\Localization\LocalizationHandler; use GW2Treasures\GW2Api\V2\Pagination\PaginationHandler; +use GW2Treasures\GW2Api\V2\Schema\SchemaHandler; class GW2Api { + /** @const string */ + const DEFAULT_SCHEMA = '2019-03-20T00:00:00Z'; + /** @var string $apiUrl */ protected $apiUrl = 'https://api.guildwars2.com/'; @@ -67,6 +71,9 @@ class GW2Api { /** @var array $handlers */ protected $handlers = []; + /** @var string $schema */ + protected $schema = self::DEFAULT_SCHEMA; + function __construct( array $options = [] ) { $this->options = $this->getOptions( $options ); @@ -149,6 +156,7 @@ protected function registerDefaultHandlers() { $this->registerHandler(LocalizationHandler::class); $this->registerHandler(PaginationHandler::class); $this->registerHandler(RestrictedGuildHandler::class); + $this->registerHandler(SchemaHandler::class); } public function getClient() { @@ -330,4 +338,25 @@ public function attachRegisteredHandlers( IEndpoint $endpoint ) { } } } + + /** + * Set the schema version to use for this api instance. + * + * @param string $schema + * @return $this + */ + public function schema( $schema ) { + $this->schema = $schema; + + return $this; + } + + /** + * Get the schema version used by this api instance. + * + * @return String + */ + public function getSchema() { + return $this->schema; + } } diff --git a/src/V2/Endpoint.php b/src/V2/Endpoint.php index c3a5c53..d84d21b 100644 --- a/src/V2/Endpoint.php +++ b/src/V2/Endpoint.php @@ -20,6 +20,9 @@ abstract class Endpoint implements IEndpoint { /** @var ApiHandler[] */ protected $handlers = []; + /** @var string schema */ + protected $schema = null; + /** * @param GW2Api $api */ @@ -43,6 +46,28 @@ protected function getApi() { return $this->api; } + /** + * Set the schema version to use when requesting this endpoint. + * + * @param string $schema + * @return $this + */ + public function schema( $schema ) { + $this->schema = $schema; + + return $this; + } + + /** + * Get the schema used to request this endpoint. + * Falls back to the global schema set in the GW2API instance. + * + * @return String + */ + public function getSchema() { + return $this->schema !== null ? $this->schema : $this->getApi()->getSchema(); + } + /** * Creates a new Request to this Endpoint. * diff --git a/src/V2/IEndpoint.php b/src/V2/IEndpoint.php index 70fc487..78cc72f 100644 --- a/src/V2/IEndpoint.php +++ b/src/V2/IEndpoint.php @@ -16,4 +16,20 @@ public function attach( ApiHandler $handler ); * @return string */ public function url(); + + /** + * Set the schema version to use when requesting this endpoint. + * + * @param string $schema + * @return $this + */ + public function schema( $schema ); + + /** + * Get the schema used to request this endpoint. + * Falls back to the global schema set in the GW2API instance. + * + * @return String + */ + public function getSchema(); } diff --git a/src/V2/Schema/SchemaHandler.php b/src/V2/Schema/SchemaHandler.php new file mode 100644 index 0000000..b9eeaa1 --- /dev/null +++ b/src/V2/Schema/SchemaHandler.php @@ -0,0 +1,27 @@ +getEndpoint()->getSchema(); + + return $request->withHeader( 'X-Schema-Version', $schema ); + } +} diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php new file mode 100644 index 0000000..3eceb31 --- /dev/null +++ b/tests/SchemaTest.php @@ -0,0 +1,58 @@ +api() ); + } + + public function testDefaultSchema() { + $this->mockResponse('[]'); + + $endpoint = $this->getEndpoint(); + + $this->assertEquals(EXPECTED_DEFAULT_SCHEMA, $endpoint->getSchema()); + + $endpoint->test(); + + $request = $this->getLastRequest(); + $this->assertHasHeader($request, 'X-Schema-Version', EXPECTED_DEFAULT_SCHEMA); + } + + public function testCustomSchema() { + $this->mockResponse('[]'); + + $endpoint = $this->getEndpoint()->schema(EXPECTED_CUSTOM_SCHEMA); + + $this->assertEquals(EXPECTED_CUSTOM_SCHEMA, $endpoint->getSchema()); + + $endpoint->test(); + + $request = $this->getLastRequest(); + $this->assertHasHeader($request, 'X-Schema-Version', EXPECTED_CUSTOM_SCHEMA); + } + + public function testGlobalSchema() { + $this->mockResponse('[]'); + + $endpoint = $this->getEndpoint(); + $this->assertEquals(EXPECTED_DEFAULT_SCHEMA, $endpoint->getSchema()); + + $this->api()->schema(EXPECTED_CUSTOM_SCHEMA); + + $this->assertEquals(EXPECTED_CUSTOM_SCHEMA, $endpoint->getSchema()); + + $endpoint->test(); + + $request = $this->getLastRequest(); + $this->assertHasHeader($request, 'X-Schema-Version', EXPECTED_CUSTOM_SCHEMA); + + // clean up schema for other tests + $this->resetApi(); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index f22e264..899c9d4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -39,6 +39,10 @@ protected function api() { return $this->api; } + protected function resetApi() { + unset($this->api); + } + /** * @param Response|RequestException|string $response * @param string $language