From f6567ebd1b0f6592bda2dde6c853c16199711fee Mon Sep 17 00:00:00 2001 From: Mike Russell Date: Wed, 22 Mar 2017 15:03:30 -0400 Subject: [PATCH] Endpoint Provider Tests --- src/Endpoint/Abstracts/AbstractEndpoint.php | 4 - .../Abstracts/AbstractSmartEndpoint.php | 1 - src/Endpoint/JSON/SmartEndpoint.php | 4 +- .../Provider/AbstractEndpointProvider.php | 29 ++--- .../Endpoint/InvalidRegistration.php | 5 +- tests/Auth/AbstractAuthControllerTest.php | 2 +- tests/Client/AbstractClientTest.php | 3 +- .../{Data => }/AbstractEndpointDataTest.php | 15 ++- .../Endpoint/AbstractEndpointProviderTest.php | 113 ++++++++++++++++++ tests/Stubs/Endpoint/AuthEndpoint.php | 4 +- tests/Stubs/Endpoint/EndpointProvider.php | 6 +- .../Endpoint/EndpointProviderWithDefaults.php | 29 +++++ 12 files changed, 182 insertions(+), 33 deletions(-) rename tests/Endpoint/{Data => }/AbstractEndpointDataTest.php (92%) create mode 100644 tests/Endpoint/AbstractEndpointProviderTest.php create mode 100644 tests/Stubs/Endpoint/EndpointProviderWithDefaults.php diff --git a/src/Endpoint/Abstracts/AbstractEndpoint.php b/src/Endpoint/Abstracts/AbstractEndpoint.php index ed5d3bb..966fc45 100644 --- a/src/Endpoint/Abstracts/AbstractEndpoint.php +++ b/src/Endpoint/Abstracts/AbstractEndpoint.php @@ -4,13 +4,9 @@ use MRussell\Http\Request\Curl; use MRussell\REST\Auth\AuthControllerInterface; -use MRussell\REST\Endpoint\Data\AbstractEndpointData; -use MRussell\REST\Endpoint\Data\DataInterface; use MRussell\REST\Endpoint\Interfaces\EndpointInterface; use MRussell\REST\Exception\Endpoint\InvalidRequest; -use MRussell\REST\Exception\Endpoint\InvalidURLEndpointException; use MRussell\REST\Exception\Endpoint\InvalidData; -use MRussell\REST\Exception\Endpoint\InvalidOptions; use MRussell\Http\Response\ResponseInterface; use MRussell\Http\Request\RequestInterface; diff --git a/src/Endpoint/Abstracts/AbstractSmartEndpoint.php b/src/Endpoint/Abstracts/AbstractSmartEndpoint.php index fad60e6..b9ccb4d 100644 --- a/src/Endpoint/Abstracts/AbstractSmartEndpoint.php +++ b/src/Endpoint/Abstracts/AbstractSmartEndpoint.php @@ -3,7 +3,6 @@ namespace MRussell\REST\Endpoint\Abstracts; use MRussell\REST\Endpoint\Data\AbstractEndpointData; -use MRussell\REST\Endpoint\Data\EndpointData; use MRussell\REST\Exception\Endpoint\EndpointException; abstract class AbstractSmartEndpoint extends AbstractEndpoint diff --git a/src/Endpoint/JSON/SmartEndpoint.php b/src/Endpoint/JSON/SmartEndpoint.php index 7a02edb..995f306 100644 --- a/src/Endpoint/JSON/SmartEndpoint.php +++ b/src/Endpoint/JSON/SmartEndpoint.php @@ -4,9 +4,9 @@ use MRussell\Http\Request\JSON as JSONRequest; use MRussell\Http\Response\JSON as JSONResponse; -use MRussell\REST\Endpoint\Abstracts\AbstractEndpoint; +use MRussell\REST\Endpoint\Abstracts\AbstractSmartEndpoint; -class SmartEndpoint extends AbstractEndpoint +class SmartEndpoint extends AbstractSmartEndpoint { protected static $_DATA_CLASS = 'MRussell\\REST\\Endpoint\\Data\\EndpointData'; diff --git a/src/Endpoint/Provider/AbstractEndpointProvider.php b/src/Endpoint/Provider/AbstractEndpointProvider.php index 5f713b4..a9729cf 100644 --- a/src/Endpoint/Provider/AbstractEndpointProvider.php +++ b/src/Endpoint/Provider/AbstractEndpointProvider.php @@ -2,10 +2,8 @@ namespace MRussell\REST\Endpoint\Provider; -use MRussell\REST\Auth\AuthControllerInterface; -use MRussell\REST\Endpoint\ControllerInterface; use MRussell\REST\Endpoint\Interfaces\EndpointInterface; -use MRussell\REST\Exception\Endpoint\InvalidEndpointRegistration; +use MRussell\REST\Exception\Endpoint\InvalidRegistration; use MRussell\REST\Exception\Endpoint\UnknownEndpoint; abstract class AbstractEndpointProvider implements EndpointProviderInterface @@ -20,7 +18,7 @@ abstract class AbstractEndpointProvider implements EndpointProviderInterface /** * @var array */ - protected $registry; + protected $registry = array(); public function __construct() { foreach(static::$_DEFAULT_ENDPOINTS as $name => $epData){ @@ -33,20 +31,23 @@ public function __construct() { /** * @inheritdoc - * @throws InvalidEndpointRegistration + * @throws InvalidRegistration */ public function registerEndpoint($name, $className, array $properties = array()) { - $implements = class_implements($className); - if (is_array($implements) && isset($implements['MRussell\\REST\\Endpoint\\Interfaces\\EndpointInterface'])) { - $this->registry[$name] = array( - 'class' => $className, - 'properties' => $properties - ); - } else { - throw new InvalidEndpointRegistration($className); + try{ + $implements = class_implements($className); + if (is_array($implements) && isset($implements['MRussell\REST\Endpoint\Interfaces\EndpointInterface'])) { + $this->registry[$name] = array( + 'class' => $className, + 'properties' => $properties + ); + return $this; + } + } catch(\Exception $ex){ + //Class Implements failed to Load Class completely } - return $this; + throw new InvalidRegistration($className); } /** diff --git a/src/Exception/Endpoint/InvalidRegistration.php b/src/Exception/Endpoint/InvalidRegistration.php index 1c10fdc..a1b1b1b 100644 --- a/src/Exception/Endpoint/InvalidRegistration.php +++ b/src/Exception/Endpoint/InvalidRegistration.php @@ -2,8 +2,9 @@ namespace MRussell\REST\Exception\Endpoint; -class InvalidRegistration extends EndpointException { +class InvalidRegistration extends EndpointException +{ - protected $message = 'Endpoint Object [%s] must extend MRussell\REST\Endpoint\EndpointInterface'; + protected $message = 'Endpoint Object [%s] must extend MRussell\REST\Endpoint\Interfaces\EndpointInterface'; } \ No newline at end of file diff --git a/tests/Auth/AbstractAuthControllerTest.php b/tests/Auth/AbstractAuthControllerTest.php index 2796b80..01328ec 100644 --- a/tests/Auth/AbstractAuthControllerTest.php +++ b/tests/Auth/AbstractAuthControllerTest.php @@ -202,7 +202,7 @@ public function testConfigureData(){ $method = $Class->getMethod('configureEndpoint'); $method->setAccessible(TRUE); $this->assertEquals($AuthEndpoint,$method->invoke($Auth,$AuthEndpoint,AuthController::ACTION_AUTH)); - $this->assertEquals($this->credentials,$AuthEndpoint->getData()); + $this->assertEquals($this->credentials,$AuthEndpoint->getData()->asArray()); $this->assertEquals($LogoutEndpoint,$method->invoke($Auth,$LogoutEndpoint,AuthController::ACTION_LOGOUT)); $this->assertEquals(array(),$LogoutEndpoint->getData()); diff --git a/tests/Client/AbstractClientTest.php b/tests/Client/AbstractClientTest.php index ce34c7b..2781db4 100644 --- a/tests/Client/AbstractClientTest.php +++ b/tests/Client/AbstractClientTest.php @@ -5,6 +5,7 @@ use MRussell\REST\Tests\Stubs\Auth\AuthController; use MRussell\REST\Tests\Stubs\Client\Client; use MRussell\REST\Tests\Stubs\Endpoint\EndpointProvider; +use MRussell\REST\Tests\Stubs\Endpoint\EndpointProviderWithDefaults; /** @@ -67,7 +68,7 @@ public function testSetAuth(){ */ public function testSetEndpointProvider(Client $Client){ $this->Client = $Client; - $EndpointProvider = new EndpointProvider(); + $EndpointProvider = new EndpointProviderWithDefaults(); $this->assertEquals($this->Client,$this->Client->setEndpointProvider($EndpointProvider)); $this->assertEquals($EndpointProvider,$this->Client->getEndpointProvider()); return $this->Client; diff --git a/tests/Endpoint/Data/AbstractEndpointDataTest.php b/tests/Endpoint/AbstractEndpointDataTest.php similarity index 92% rename from tests/Endpoint/Data/AbstractEndpointDataTest.php rename to tests/Endpoint/AbstractEndpointDataTest.php index 9b1b586..19999e3 100644 --- a/tests/Endpoint/Data/AbstractEndpointDataTest.php +++ b/tests/Endpoint/AbstractEndpointDataTest.php @@ -1,6 +1,6 @@ asArray(TRUE); } + + /** + * @expectedException MRussell\REST\Exception\Endpoint\InvalidData + * @expectedExceptionMessageRegExp /Missing or Invalid data on Endpoint Data\. Errors: (Missing \[[A-z0-9,].*\]|Invalid \[[A-z0-9,].*\])/ + */ + public function testInvalidAndMissingData(){ + $Data = new StubData(); + $properties = $Data->getProperties(); + $properties['required']['bar'] = NULL; + $Data->setProperties($properties); + $Data['foo'] = 1234; + $Data->asArray(TRUE); + } } diff --git a/tests/Endpoint/AbstractEndpointProviderTest.php b/tests/Endpoint/AbstractEndpointProviderTest.php new file mode 100644 index 0000000..c34a526 --- /dev/null +++ b/tests/Endpoint/AbstractEndpointProviderTest.php @@ -0,0 +1,113 @@ +getProperty('registry'); + $property->setAccessible(TRUE); + $this->assertEquals(array(),$property->getValue($Provider)); + + $Provider = new EndpointProviderWithDefaults(); + $this->assertNotEmpty($property->getValue($Provider)); + } + + /** + * @covers ::registerEndpoint + * @return EndpointProviderInterface + */ + public function testRegisterEndpoint(){ + $Provider = new EndpointProvider(); + $this->assertEquals($Provider,$Provider->registerEndpoint('auth','MRussell\REST\Tests\Stubs\Endpoint\AuthEndpoint')); + $this->assertEquals($Provider,$Provider->registerEndpoint('foo','MRussell\REST\Endpoint\JSON\Endpoint',array( + 'url' => 'foo', + 'httpMethod' => JSON::HTTP_GET + ))); + return $Provider; + } + + /** + * @depends testRegisterEndpoint + * @param EndpointProviderInterface $Provider + * @covers ::registerEndpoint + * @expectedException MRussell\REST\Exception\Endpoint\InvalidRegistration + */ + public function testInvalidRegistration(EndpointProviderInterface $Provider){ + $Provider->registerEndpoint("baz","baz"); + } + + /** + * @depends testRegisterEndpoint + * @covers ::hasEndpoint + * @covers ::getEndpoint + * @covers ::buildEndpoint + * @param EndpointProviderInterface $Provider + */ + public function testGetEndpoint(EndpointProviderInterface $Provider){ + $this->assertEquals(FALSE, $Provider->hasEndpoint('test')); + $this->assertEquals(TRUE, $Provider->hasEndpoint('foo')); + $this->assertEquals(TRUE, $Provider->hasEndpoint('auth')); + $Auth = new AuthEndpoint(); + $this->assertEquals($Auth, $Provider->getEndpoint('auth')); + $FooEP = $Provider->getEndpoint('foo'); + $this->assertNotEmpty($FooEP); + $this->assertEquals('foo',$FooEP->getEndPointUrl()); + $this->assertEquals(array( + 'url' => 'foo', + 'httpMethod' => JSON::HTTP_GET, + 'auth' => FALSE + ),$FooEP->getProperties()); + } + + /** + * @depends testRegisterEndpoint + * @param EndpointProviderInterface $Provider + * @covers ::getEndpoint + * @expectedException MRussell\REST\Exception\Endpoint\UnknownEndpoint + */ + public function testUnknownEndpoint(EndpointProviderInterface $Provider){ + $Provider->getEndpoint('test'); + } + +} diff --git a/tests/Stubs/Endpoint/AuthEndpoint.php b/tests/Stubs/Endpoint/AuthEndpoint.php index 78089cb..01889a4 100644 --- a/tests/Stubs/Endpoint/AuthEndpoint.php +++ b/tests/Stubs/Endpoint/AuthEndpoint.php @@ -3,9 +3,9 @@ namespace MRussell\REST\Tests\Stubs\Endpoint; use MRussell\Http\Request\JSON; -use MRussell\REST\Endpoint\JSON\Endpoint; +use MRussell\REST\Endpoint\JSON\SmartEndpoint; -class AuthEndpoint extends Endpoint +class AuthEndpoint extends SmartEndpoint { protected static $_ENDPOINT_URL = 'authenticate'; diff --git a/tests/Stubs/Endpoint/EndpointProvider.php b/tests/Stubs/Endpoint/EndpointProvider.php index 6e226b4..c546733 100644 --- a/tests/Stubs/Endpoint/EndpointProvider.php +++ b/tests/Stubs/Endpoint/EndpointProvider.php @@ -6,9 +6,5 @@ class EndpointProvider extends AbstractEndpointProvider { - protected static $_DEFAULT_ENDPOINTS = array( - 'auth' => array( - 'class' => 'MRussell\\REST\\Tests\\Stubs\\Endpoint\\AuthEndpoint' - ) - ); + } \ No newline at end of file diff --git a/tests/Stubs/Endpoint/EndpointProviderWithDefaults.php b/tests/Stubs/Endpoint/EndpointProviderWithDefaults.php new file mode 100644 index 0000000..fbd438b --- /dev/null +++ b/tests/Stubs/Endpoint/EndpointProviderWithDefaults.php @@ -0,0 +1,29 @@ + array( + 'class' => 'MRussell\REST\Tests\Stubs\Endpoint\AuthEndpoint' + ), + 'refresh' => array( + 'class' => 'MRussell\REST\Tests\Stubs\Endpoint\RefreshEndpoint' + ), + 'logout' => array( + 'class' => 'MRussell\REST\Tests\Stubs\Endpoint\LogoutEndpoint' + ), + 'ping' => array( + 'class' => 'MRussell\REST\Endpoint\JSON\Endpoint', + 'properties' => array( + 'url' => 'ping', + 'httpMethod' => JSON::HTTP_GET + ) + ) + ); +} \ No newline at end of file