Skip to content

Commit

Permalink
Endpoint Provider Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelJ2324 committed Mar 22, 2017
1 parent 57a36dd commit f6567eb
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 33 deletions.
4 changes: 0 additions & 4 deletions src/Endpoint/Abstracts/AbstractEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 0 additions & 1 deletion src/Endpoint/Abstracts/AbstractSmartEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Endpoint/JSON/SmartEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
29 changes: 15 additions & 14 deletions src/Endpoint/Provider/AbstractEndpointProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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){
Expand All @@ -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);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Exception/Endpoint/InvalidRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

}
2 changes: 1 addition & 1 deletion tests/Auth/AbstractAuthControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
3 changes: 2 additions & 1 deletion tests/Client/AbstractClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MRussell\REST\Tests\Endpoint\Data;
namespace MRussell\REST\Tests\Endpoint;

use MRussell\REST\Endpoint\Data\EndpointData as StockData;
use MRussell\REST\Tests\Stubs\Endpoint\EndpointData as StubData;
Expand Down Expand Up @@ -211,4 +211,17 @@ public function testInvalidData(){
$Data['foo'] = 1234;
$Data->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);
}
}
113 changes: 113 additions & 0 deletions tests/Endpoint/AbstractEndpointProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace MRussell\REST\Tests\Endpoint;

use MRussell\Http\Request\JSON;
use MRussell\REST\Endpoint\Provider\EndpointProviderInterface;
use MRussell\REST\Tests\Stubs\Endpoint\AuthEndpoint;
use MRussell\REST\Tests\Stubs\Endpoint\EndpointProvider;
use MRussell\REST\Tests\Stubs\Endpoint\EndpointProviderWithDefaults;


/**
* Class AbstractEndpointProviderTest
* @package MRussell\REST\Tests\Endpoint
* @coversDefaultClass MRussell\REST\Endpoint\Provider\AbstractEndpointProvider
* @group AbstractEndpointProviderTest
*/
class AbstractEndpointProviderTest extends \PHPUnit_Framework_TestCase
{

public static function setUpBeforeClass()
{
//Add Setup for static properties here
}

public static function tearDownAfterClass()
{
//Add Tear Down for static properties here
}

public function setUp()
{
parent::setUp();
}

public function tearDown()
{
parent::tearDown();
}

/**
* @covers ::__construct
* @covers ::registerEndpoint
*/
public function testConstructor(){
$Provider = new EndpointProvider();
$Class = new \ReflectionClass('MRussell\REST\Tests\Stubs\Endpoint\EndpointProvider');
$property = $Class->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');
}

}
4 changes: 2 additions & 2 deletions tests/Stubs/Endpoint/AuthEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
6 changes: 1 addition & 5 deletions tests/Stubs/Endpoint/EndpointProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@

class EndpointProvider extends AbstractEndpointProvider
{
protected static $_DEFAULT_ENDPOINTS = array(
'auth' => array(
'class' => 'MRussell\\REST\\Tests\\Stubs\\Endpoint\\AuthEndpoint'
)
);

}
29 changes: 29 additions & 0 deletions tests/Stubs/Endpoint/EndpointProviderWithDefaults.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace MRussell\REST\Tests\Stubs\Endpoint;


use MRussell\Http\Request\JSON;
use MRussell\REST\Endpoint\Provider\AbstractEndpointProvider;

class EndpointProviderWithDefaults extends AbstractEndpointProvider
{
protected static $_DEFAULT_ENDPOINTS = array(
'auth' => 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
)
)
);
}

0 comments on commit f6567eb

Please sign in to comment.