Skip to content

Commit

Permalink
100% Code Coverage + Model Action Fixes
Browse files Browse the repository at this point in the history
Action can be set on Model publicly, and retrieved publicly (in case
you forget what action you might have just triggered on the Model).
  • Loading branch information
MichaelJ2324 committed Apr 28, 2017
1 parent ca10221 commit 807897e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 37 deletions.
12 changes: 8 additions & 4 deletions src/Auth/Abstracts/AbstractAuthController.php
Expand Up @@ -138,7 +138,7 @@ public function getActionEndpoint($action)
if (isset($this->endpoints[$action])) {
return $this->endpoints[$action];
}
return null;
return NULL;
}

/**
Expand All @@ -147,9 +147,9 @@ public function getActionEndpoint($action)
public function isAuthenticated()
{
if (!empty($this->token)) {
return true;
return TRUE;
}
return false;
return FALSE;
}

/**
Expand All @@ -162,9 +162,11 @@ public function authenticate()
$Endpoint = $this->configureEndpoint($Endpoint,self::ACTION_AUTH);
$response = $Endpoint->execute()->getResponse();
if ($response->getStatus() == '200') {
//@codeCoverageIgnoreStart
$this->setToken($response->getBody());
return true;
return TRUE;
}
//@codeCoverageIgnoreEnd
}
return FALSE;
}
Expand All @@ -179,9 +181,11 @@ public function logout()
$Endpoint = $this->configureEndpoint($Endpoint,self::ACTION_LOGOUT);
$response = $Endpoint->execute()->getResponse();
if ($response->getStatus() == '200') {
//@codeCoverageIgnoreStart
$this->clearToken();
return TRUE;
}
//@codeCoverageIgnoreEnd
}
return FALSE;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Auth/Abstracts/AbstractOAuth2Controller.php
Expand Up @@ -106,12 +106,14 @@ public function refresh()
$Endpoint = $this->configureEndpoint($Endpoint, self::ACTION_OAUTH_REFRESH);
$response = $Endpoint->execute()->getResponse();
if ($response->getStatus() == '200') {
//@codeCoverageIgnoreStart
$this->setToken($response->getBody());
return true;
return TRUE;
}
//@codeCoverageIgnoreEnd
}
}
return false;
return FALSE;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Endpoint/Abstracts/AbstractCollectionEndpoint.php
Expand Up @@ -186,8 +186,10 @@ public function getEndPointUrl($full = FALSE) {
protected function configureResponse(ResponseInterface $Response) {
$Response = parent::configureResponse($Response);
if ($Response->getStatus() == '200'){
//@codeCoverageIgnoreStart
$this->updateCollection();
}
//@codeCoverageIgnoreEnd
return $Response;
}

Expand Down
70 changes: 57 additions & 13 deletions src/Endpoint/Abstracts/AbstractModelEndpoint.php
Expand Up @@ -3,6 +3,7 @@
namespace MRussell\REST\Endpoint\Abstracts;

use MRussell\Http\Request\Curl;
use MRussell\Http\Request\RequestInterface;
use MRussell\Http\Response\ResponseInterface;
use MRussell\REST\Endpoint\Data\AbstractEndpointData;
use MRussell\REST\Endpoint\Data\DataInterface;
Expand Down Expand Up @@ -61,7 +62,7 @@ abstract class AbstractModelEndpoint extends AbstractSmartEndpoint implements Mo
* Current action being executed
* @var string
*/
protected $action = 'retrieve';
protected $action = self::MODEL_ACTION_RETRIEVE;

//Static
/**
Expand All @@ -86,8 +87,8 @@ public function __construct(array $options = array(), array $properties = array(
public function __call($name, $arguments) {
if (array_key_exists($name,$this->actions)){
$this->action = $name;
$this->configureAction($this->action,$arguments);
return $this->execute();
$this->setCurrentAction($this->action);
return $this->execute($arguments);
}
throw new UnknownModelAction(array(get_class($this),$name));
}
Expand Down Expand Up @@ -191,7 +192,7 @@ public function set($key, $value) {
* @throws \MRussell\REST\Exception\Endpoint\InvalidRequest
*/
public function retrieve($id = NULL) {
$this->action = self::MODEL_ACTION_RETRIEVE;
$this->setCurrentAction(self::MODEL_ACTION_RETRIEVE);
$idKey = $this->modelIdKey();
if ($id !== NULL){
if (isset($this->model[$idKey])){
Expand All @@ -203,7 +204,6 @@ public function retrieve($id = NULL) {
throw new MissingModelId(array($this->action,get_class($this)));
}
}
$this->configureAction($this->action);
return $this->execute();
}

Expand All @@ -213,24 +213,59 @@ public function retrieve($id = NULL) {
*/
public function save() {
if (isset($this->model[$this->modelIdKey()])){
$this->action = self::MODEL_ACTION_UPDATE;
$this->setCurrentAction(self::MODEL_ACTION_UPDATE);
} else {
$this->action = self::MODEL_ACTION_CREATE;
$this->setCurrentAction(self::MODEL_ACTION_CREATE);
}
$this->configureAction($this->action);
return $this->execute();
}

/**
* @inheritdoc
*/
public function delete(){
$this->action = self::MODEL_ACTION_DELETE;
$this->configureAction($this->action);
$this->setCurrentAction(self::MODEL_ACTION_DELETE);
return $this->execute();
}

/**
* Set the current action taking place on the Model
* @param string $action
* @return $this
*/
public function setCurrentAction($action){
$action = (string) $action;
if (array_key_exists($action,$this->actions)){
$this->action = $action;
}
return $this;
}

/**
* Get the current action taking place on the Model
*/
public function getCurrentAction(){
return $this->action;
}

//Endpoint Overrides
/**
* Configure the Action before configuring the Request
* @param null $data
* @return $this
* @throws \MRussell\REST\Exception\Endpoint\InvalidRequest
* @codeCoverageIgnore
*/
public function execute($data = NULL)
{
$actionArgs = $data;
if (!is_array($actionArgs)){
$actionArgs = array();
}
$this->configureAction($this->action,$actionArgs);
return parent::execute(); // TODO: Change the autogenerated stub
}

/**
* Update any properties or data based on the current action
* - Called before Execute on dynamic
Expand Down Expand Up @@ -268,8 +303,11 @@ protected function configureData($data)
protected function configureResponse(ResponseInterface $Response) {
$Response = parent::configureResponse($Response);
if ($Response->getStatus() == '200'){
//@codeCoverageIgnoreStart
$this->updateModel();

}
//@codeCoverageIgnoreEnd
return $Response;
}

Expand Down Expand Up @@ -297,9 +335,15 @@ protected function updateModel(){
*/
protected function configureURL(array $options)
{
$idKey = $this->modelIdKey();
$id = $this->get($idKey);
$options[self::MODEL_ID_VAR] = (empty($id)?'':$id);
switch($this->getCurrentAction()){
case self::MODEL_ACTION_CREATE:
$options[self::MODEL_ID_VAR] = '';
break;
default:
$idKey = $this->modelIdKey();
$id = $this->get($idKey);
$options[self::MODEL_ID_VAR] = (empty($id)?'':$id);
}
return parent::configureURL($options);
}

Expand Down
43 changes: 25 additions & 18 deletions tests/Endpoint/AbstractModelEndpointTest.php
Expand Up @@ -148,6 +148,22 @@ public function testDataAccess(){
$this->assertEquals(array(),$Model->asArray());
}

/**
* @covers ::setCurrentAction
* @covers ::getCurrentAction
*/
public function testCurrentAction(){
$Model = new ModelEndpoint();
$this->assertEquals($Model,$Model->setCurrentAction(ModelEndpoint::MODEL_ACTION_CREATE));
$this->assertEquals(ModelEndpoint::MODEL_ACTION_CREATE,$Model->getCurrentAction());
$this->assertEquals($Model,$Model->setCurrentAction(ModelEndpoint::MODEL_ACTION_UPDATE));
$this->assertEquals(ModelEndpoint::MODEL_ACTION_UPDATE,$Model->getCurrentAction());
$this->assertEquals($Model,$Model->setCurrentAction(ModelEndpoint::MODEL_ACTION_DELETE));
$this->assertEquals(ModelEndpoint::MODEL_ACTION_DELETE,$Model->getCurrentAction());
$this->assertEquals($Model,$Model->setCurrentAction('foo'));
$this->assertEquals(ModelEndpoint::MODEL_ACTION_DELETE,$Model->getCurrentAction());
}

/**
* @covers ::configureAction
* @covers ::retrieve
Expand All @@ -162,10 +178,7 @@ public function testRetrieve(){
$this->assertEquals('localhost/api/v1/model/1234',$Model->getRequest()->getURL());
$this->assertEquals('1234',$Model['id']);

$Class = new \ReflectionClass(static::$_REFLECTED_CLASS);
$action = $Class->getProperty('action');
$action->setAccessible(TRUE);
$this->assertEquals('retrieve',$action->getValue($Model));
$this->assertEquals(ModelEndpoint::MODEL_ACTION_RETRIEVE,$Model->getCurrentAction());

$Model['id'] = '5678';
$this->assertEquals($Model,$Model->retrieve());
Expand Down Expand Up @@ -205,11 +218,9 @@ public function testSave(){
$Model->setProperty('url','model/$id');
$Model->set('foo','bar');
$Class = new \ReflectionClass(static::$_REFLECTED_CLASS);
$action = $Class->getProperty('action');
$action->setAccessible(TRUE);

$this->assertEquals($Model,$Model->save());
$this->assertEquals('create',$action->getValue($Model));
$this->assertEquals('create',$Model->getCurrentAction());
$this->assertEquals('localhost/api/v1/model',$Model->getRequest()->getURL());
$this->assertEquals(JSON::HTTP_POST,$Model->getRequest()->getMethod());
$this->assertEquals(array(
Expand All @@ -218,7 +229,7 @@ public function testSave(){

$Model->set('id','1234');
$this->assertEquals($Model,$Model->save());
$this->assertEquals('update',$action->getValue($Model));
$this->assertEquals('update',$Model->getCurrentAction());
$this->assertEquals('localhost/api/v1/model/1234',$Model->getRequest()->getURL());
$this->assertEquals(JSON::HTTP_PUT,$Model->getRequest()->getMethod());
$this->assertEquals(array(
Expand All @@ -237,12 +248,9 @@ public function testDelete(){
$Model->setBaseUrl('localhost/api/v1/');
$Model->setProperty('url','model/$id');
$Model->set('id','1234');
$Class = new \ReflectionClass(static::$_REFLECTED_CLASS);
$action = $Class->getProperty('action');
$action->setAccessible(TRUE);

$this->assertEquals($Model,$Model->delete());
$this->assertEquals('delete',$action->getValue($Model));
$this->assertEquals(ModelEndpoint::MODEL_ACTION_DELETE,$Model->getCurrentAction());
$this->assertEquals('localhost/api/v1/model/1234',$Model->getRequest()->getURL());
$this->assertEquals(JSON::HTTP_DELETE,$Model->getRequest()->getMethod());
}
Expand All @@ -262,9 +270,8 @@ public function testConfigureResponse(){
$status = $ReflectedResponse->getProperty('status');
$status->setAccessible(TRUE);
$status->setValue($Response,'200');
$action = $ReflectedModel->getProperty('action');
$action->setAccessible(TRUE);
$action->setValue($Model,ModelEndpoint::MODEL_ACTION_CREATE);

$Model->setCurrentAction(ModelEndpoint::MODEL_ACTION_CREATE);
$method = $ReflectedModel->getMethod('configureResponse');
$method->setAccessible(TRUE);
$Model->setResponse($Response);
Expand All @@ -289,12 +296,12 @@ public function testConfigureResponse(){
'name' => 'foo',
'foo' => 'bar'
),$Model->asArray());
$action->setValue($Model,ModelEndpoint::MODEL_ACTION_DELETE);
$Model->setCurrentAction(ModelEndpoint::MODEL_ACTION_DELETE);
$updateModel->invoke($Model);
$this->assertEquals(array(),$Model->asArray());
$this->assertEmpty($Model->get('id'));

$action->setValue($Model,ModelEndpoint::MODEL_ACTION_UPDATE);
$Model->setCurrentAction(ModelEndpoint::MODEL_ACTION_UPDATE);
$updateModel->invoke($Model);
$this->assertEquals(array(
'id' => '1234',
Expand All @@ -303,7 +310,7 @@ public function testConfigureResponse(){
),$Model->asArray());

$Model->clear();
$action->setValue($Model,ModelEndpoint::MODEL_ACTION_RETRIEVE);
$Model->setCurrentAction(ModelEndpoint::MODEL_ACTION_RETRIEVE);
$updateModel->invoke($Model);
$this->assertEquals(array(
'id' => '1234',
Expand Down

0 comments on commit 807897e

Please sign in to comment.