Skip to content

Commit

Permalink
Model Endpoint Unit Tests + Model Endpoint Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelJ2324 committed Apr 2, 2017
1 parent 3980a82 commit ca10221
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/Endpoint/Abstracts/AbstractModelEndpoint.php
Expand Up @@ -7,7 +7,9 @@
use MRussell\REST\Endpoint\Data\AbstractEndpointData;
use MRussell\REST\Endpoint\Data\DataInterface;
use MRussell\REST\Endpoint\Interfaces\ModelInterface;
use MRussell\REST\Endpoint\JSON\ModelEndpoint;
use MRussell\REST\Exception\Endpoint\EndpointException;
use MRussell\REST\Exception\Endpoint\MissingModelId;
use MRussell\REST\Exception\Endpoint\UnknownModelAction;

/**
Expand Down Expand Up @@ -173,14 +175,14 @@ public function update(array $model){
* @inheritdoc
*/
public function get($key) {
return $this->model[$key];
return $this->offsetGet($key);
}

/**
* @inheritdoc
*/
public function set($key, $value) {
$this->model[$key] = $value;
$this->offsetSet($key,$value);
return $this;
}

Expand All @@ -189,20 +191,20 @@ public function set($key, $value) {
* @throws \MRussell\REST\Exception\Endpoint\InvalidRequest
*/
public function retrieve($id = NULL) {
$this->action = self::MODEL_ACTION_RETRIEVE;
$idKey = $this->modelIdKey();
if ($id !== NULL){
if (isset($this->model[$idKey])){
$this->reset();
$this->set($idKey,$id);
}
$this->set($idKey,$id);
} else {
if (!isset($this->model[$idKey])){
throw new EndpointException("Cannot retrieve Model without an ID");
throw new MissingModelId(array($this->action,get_class($this)));
}
}
$this->action = self::MODEL_ACTION_RETRIEVE;
$this->configureAction($this->action);
$this->execute();
return $this->execute();
}

/**
Expand All @@ -225,7 +227,7 @@ public function save() {
public function delete(){
$this->action = self::MODEL_ACTION_DELETE;
$this->configureAction($this->action);
$this->execute();
return $this->execute();
}

//Endpoint Overrides
Expand Down Expand Up @@ -295,12 +297,9 @@ protected function updateModel(){
*/
protected function configureURL(array $options)
{
$id = '';
$modelIdKey = static::modelIdKey();
if (isset($this->model[$modelIdKey])){
$id = $this->model[$modelIdKey];
}
$options[self::MODEL_ID_VAR] = $id;
$idKey = $this->modelIdKey();
$id = $this->get($idKey);
$options[self::MODEL_ID_VAR] = (empty($id)?'':$id);
return parent::configureURL($options);
}

Expand Down
9 changes: 9 additions & 0 deletions src/Exception/Endpoint/MissingModelId.php
@@ -0,0 +1,9 @@
<?php

namespace MRussell\REST\Exception\Endpoint;


class MissingModelId extends EndpointException
{
protected $message = 'Model ID missing for current action [%s] on Endpoint: %s';
}
165 changes: 165 additions & 0 deletions tests/Endpoint/AbstractModelEndpointTest.php
Expand Up @@ -4,6 +4,7 @@

use MRussell\Http\Request\Curl;
use MRussell\Http\Request\JSON;
use MRussell\REST\Exception\Endpoint\MissingModelId;
use MRussell\REST\Exception\Endpoint\UnknownModelAction;
use MRussell\REST\Tests\Stubs\Endpoint\ModelEndpoint;
use MRussell\REST\Tests\Stubs\Endpoint\ModelEndpointWithActions;
Expand Down Expand Up @@ -146,4 +147,168 @@ public function testDataAccess(){
$this->assertEquals($Model,$Model->reset());
$this->assertEquals(array(),$Model->asArray());
}

/**
* @covers ::configureAction
* @covers ::retrieve
* @covers ::configureURL
*/
public function testRetrieve(){
$Model = new ModelEndpoint();
$Model->setBaseUrl('localhost/api/v1/');
$Model->setProperty('url','model/$id');
$Model->setRequest(new JSON());
$this->assertEquals($Model,$Model->retrieve('1234'));
$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));

$Model['id'] = '5678';
$this->assertEquals($Model,$Model->retrieve());
$this->assertEquals('localhost/api/v1/model/5678',$Model->getRequest()->getURL());
$this->assertEquals(JSON::HTTP_GET,$Model->getRequest()->getMethod());
$this->assertEquals('5678',$Model->get('id'));

$this->assertEquals($Model,$Model->retrieve('0000'));
$this->assertEquals('localhost/api/v1/model/0000',$Model->getRequest()->getURL());
$this->assertEquals(JSON::HTTP_GET,$Model->getRequest()->getMethod());
$this->assertEquals('0000',$Model->get('id'));
}

/**
* @covers ::retrieve
* @expectedException MRussell\REST\Exception\Endpoint\MissingModelId
* @expectedExceptionMessageRegExp /Model ID missing for current action/
*/
public function testMissingModelId(){
$Model = new ModelEndpoint();
$Model->setBaseUrl('localhost/api/v1/');
$Model->setProperty('url','model/$id');
$Model->setRequest(new JSON());
$Model->retrieve();
}

/**
* @covers ::save
* @covers ::configureAction
* @covers ::configureURL
* @covers ::configureData
*/
public function testSave(){
$Model = new ModelEndpoint();
$Model->setRequest(new JSON());
$Model->setBaseUrl('localhost/api/v1/');
$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('localhost/api/v1/model',$Model->getRequest()->getURL());
$this->assertEquals(JSON::HTTP_POST,$Model->getRequest()->getMethod());
$this->assertEquals(array(
'foo' => 'bar'
),$Model->getRequest()->getBody());

$Model->set('id','1234');
$this->assertEquals($Model,$Model->save());
$this->assertEquals('update',$action->getValue($Model));
$this->assertEquals('localhost/api/v1/model/1234',$Model->getRequest()->getURL());
$this->assertEquals(JSON::HTTP_PUT,$Model->getRequest()->getMethod());
$this->assertEquals(array(
'id' => '1234',
'foo' => 'bar'
),$Model->getRequest()->getBody());
}

/**
* @covers ::delete
* @covers ::configureAction
*/
public function testDelete(){
$Model = new ModelEndpoint();
$Model->setRequest(new JSON());
$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('localhost/api/v1/model/1234',$Model->getRequest()->getURL());
$this->assertEquals(JSON::HTTP_DELETE,$Model->getRequest()->getMethod());
}

/**
* @covers ::configureResponse
* @covers ::updateModel
*/
public function testConfigureResponse(){
$Model = new \MRussell\REST\Endpoint\JSON\ModelEndpoint();
$Model->setBaseUrl('localhost/api/v1/');
$Model->setProperty('url','model/$id');
$Response = $Model->getResponse();

$ReflectedResponse = new \ReflectionClass('MRussell\Http\Response\JSON');
$ReflectedModel = new \ReflectionClass('MRussell\REST\Endpoint\JSON\ModelEndpoint');
$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);
$method = $ReflectedModel->getMethod('configureResponse');
$method->setAccessible(TRUE);
$Model->setResponse($Response);
$this->assertEquals($Response,$method->invoke($Model,$Response));
$this->assertNotEmpty($Response->getRequest());

$status->setValue($Response,'200');
$body = $ReflectedResponse->getProperty('body');
$body->setAccessible(TRUE);
$body->setValue($Response,json_encode(array(
'id' => '1234',
'name' => 'foo',
'foo' => 'bar'
)
));
$Model->setResponse($Response);
$updateModel = $ReflectedModel->getMethod('updateModel');
$updateModel->setAccessible(TRUE);
$updateModel->invoke($Model);
$this->assertEquals(array(
'id' => '1234',
'name' => 'foo',
'foo' => 'bar'
),$Model->asArray());
$action->setValue($Model,ModelEndpoint::MODEL_ACTION_DELETE);
$updateModel->invoke($Model);
$this->assertEquals(array(),$Model->asArray());
$this->assertEmpty($Model->get('id'));

$action->setValue($Model,ModelEndpoint::MODEL_ACTION_UPDATE);
$updateModel->invoke($Model);
$this->assertEquals(array(
'id' => '1234',
'name' => 'foo',
'foo' => 'bar'
),$Model->asArray());

$Model->clear();
$action->setValue($Model,ModelEndpoint::MODEL_ACTION_RETRIEVE);
$updateModel->invoke($Model);
$this->assertEquals(array(
'id' => '1234',
'name' => 'foo',
'foo' => 'bar'
),$Model->asArray());
}
}
1 change: 1 addition & 0 deletions tests/Endpoint/AbstractSmartEndpointTest.php
Expand Up @@ -134,6 +134,7 @@ public function testSetData(){
$method = $Class->getMethod('configureData');
$method->setAccessible(TRUE);
$this->assertEquals(array('foo' => 'bar'),$method->invoke($Endpoint,$Endpoint->getData()));
$this->assertEquals(NULL,$method->invoke($Endpoint,NULL));
}

/**
Expand Down

0 comments on commit ca10221

Please sign in to comment.