Skip to content

Commit

Permalink
rename and update demo items
Browse files Browse the repository at this point in the history
  • Loading branch information
ivolovikov committed Oct 16, 2012
1 parent c239328 commit e91331c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 47 deletions.
16 changes: 8 additions & 8 deletions demo/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @link https://github.com/paysio/yii-rest-api
* @copyright Copyright (c) 2012 Pays I/O Ltd. (http://pays.io)
* @license http://www.opensource.org/licenses/mit-license.php MIT license
* @package REST_Service_DEMO
* @package REST_Service_Demo
*/

YiiBase::setPathOfAlias('rest', realpath(__DIR__ . '/../extensions/yii-rest-api/library/rest'));
Expand All @@ -24,19 +24,19 @@
'components' => array(
'restService' => array(
'class' => '\rest\Service',
'enable' => isset($_REQUEST['_rest']),
'enable' => isset($_SERVER['REQUEST_URI']) && (strpos($_SERVER['REQUEST_URI'], '/api/') !== false), //for example
),

'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'baseUrl' => '',
'rules' => array(
array('rest/index', 'pattern' => 'api/rest', 'verb' => 'GET'),
array('rest/create', 'pattern' => 'api/rest', 'verb' => 'POST', 'parsingOnly' => true),
array('rest/view', 'pattern' => 'api/rest/<id>', 'verb' => 'GET'),
array('rest/update', 'pattern' => 'api/rest/<id>', 'verb' => 'PUT', 'parsingOnly' => true),
array('rest/delete', 'pattern' => 'api/rest/<id>', 'verb' => 'DELETE', 'parsingOnly' => true),
'rules' => array(
array('restUser/index', 'pattern' => 'api/users', 'verb' => 'GET', 'parsingOnly' => true),
array('restUser/create', 'pattern' => 'api/users', 'verb' => 'POST', 'parsingOnly' => true),
array('restUser/view', 'pattern' => 'api/users/<id>', 'verb' => 'GET', 'parsingOnly' => true),
array('restUser/update', 'pattern' => 'api/users/<id>', 'verb' => 'PUT', 'parsingOnly' => true),
array('restUser/delete', 'pattern' => 'api/users/<id>', 'verb' => 'DELETE', 'parsingOnly' => true),
)
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @link https://github.com/paysio/yii-rest-api
* @copyright Copyright (c) 2012 Pays I/O Ltd. (http://pays.io)
* @license http://www.opensource.org/licenses/mit-license.php MIT license
* @package REST_Service_DEMO
* @package REST_Service_Demo
*/

/**
Expand All @@ -17,13 +17,8 @@
* @method bool isRestService()
* @method \rest\Service getRestService()
*/
class RestController extends Controller
class RestUserController extends Controller
{
public function init()
{
Yii::app()->restService->enable();
}

/**
* @return array
*/
Expand All @@ -36,38 +31,38 @@ public function behaviors()

public function actionIndex()
{
$model = new RestMockModel();
$model = new RestUser();
$data = array(
'count' => 100,
'data' => array($model, $model, $model)
);
$this->render('empty', $data, false, array('count', 'data'));
$this->render('empty', $data);
}

public function actionView()
{
$model = $this->loadModel();
$this->render('empty', array('model' => $model), false, array('model'));
$this->render('empty', array('model' => $model));
}

public function actionCreate()
{
$model = new RestMockModel();
$model = new RestUser();

if ($this->isPost() && ($data = $_POST)) {
$model->attributes = $data;
if ($model->validate()) {
$this->redirect(array('view', 'id' => $model), true, 201);
}
}
$this->render('empty', array('model' => $model), false, array('model'));
$this->render('empty', array('model' => $model));
}

public function actionUpdate()
{
$model = $this->loadModel();
$data = array(
'version' => Yii::app()->request->getPut('version'),
'email' => Yii::app()->request->getPut('email'),
'name' => Yii::app()->request->getPut('name'),
);

Expand All @@ -77,7 +72,7 @@ public function actionUpdate()
$this->redirect(array('view', 'id' => $model));
}
}
$this->render('empty', array('model' => $model), false, array('model'));
$this->render('empty', array('model' => $model));
}

public function actionDelete()
Expand All @@ -91,13 +86,13 @@ public function actionDelete()
}

/**
* @return RestMockModel
* @return RestUser
* @throws CHttpException
*/
public function loadModel()
{
$id = isset($_GET['id']) ? $_GET['id'] : null;
$object = new RestMockModel();
$object = new RestUser();
if ($id != $object->id) {
throw new CHttpException(404, Yii::t('app', 'Object not found'));
}
Expand All @@ -118,9 +113,14 @@ public function loadModel()
* @see renderPartial
* @see getLayoutFile
*/
public function render($view, $data = null, $return = false, array $fields = array())
public function render($view, $data = null, $return = false, array $fields = array('count', 'model', 'data'))
{
if (($behavior = $this->asa('restAPI')) && $behavior->getEnabled()) {
if (isset($data['model']) && $this->isRestService() &&
count(array_intersect(array_keys($data), $fields)) == 1) {
$data = $data['model'];
$fields = null;
}
return $this->renderRest($view, $data, $return, $fields);
} else {
return parent::render($view, $data, $return);
Expand Down
18 changes: 9 additions & 9 deletions demo/models/RestMockModel.php → demo/models/RestUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
* @link https://github.com/paysio/yii-rest-api
* @copyright Copyright (c) 2012 Pays I/O Ltd. (http://pays.io)
* @license http://www.opensource.org/licenses/mit-license.php MIT license
* @package REST_Service_DEMO
* @package REST_Service_Demo
*/

/**
* @method array getRenderAttributes(bool $recursive = true)
* @method string getObjectId()
*/
class RestMockModel extends CModel
class RestUser extends CModel
{
public $id = 'TEST_ID';

public $version = 0.1;
public $email = '[email protected]';

public $name = 'Yii REST API';
public $name = 'Test REST User';

public $hidden;
public $password = 'hidden_password';

public function __construct()
{
Expand All @@ -32,7 +32,7 @@ public function __construct()
*/
public function attributeNames()
{
return array('id', 'version', 'name', 'hidden');
return array('id', 'email', 'name', 'password');
}

/**
Expand All @@ -41,10 +41,10 @@ public function attributeNames()
public function rules()
{
return array(
array('version', 'numerical'),
array('name', 'length', 'max' => 244),
array('email', 'email'),
array('name, password', 'length', 'max' => 244),

array('id,version,name', 'safe', 'on' => 'render'),
array('id, email, name', 'safe', 'on' => 'render'),
);
}

Expand Down
1 change: 1 addition & 0 deletions tests/restTest/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ protected function _request($url, array $options, array $headers = array())
$defaultOptions = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => 1,
CURLOPT_USERPWD => 'demo:demo',
);

if (isset($options[CURLOPT_POSTFIELDS])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,36 @@

namespace restTest;

class RestControllerTest extends AbstractTest
class RestUserTest extends AbstractTest
{
public function testCreate()
{
$result = $this->post('/api/rest', array('version' => 0.2));
$result = $this->post('/api/users', array('email' => '[email protected]'));
$model = json_decode($result['body']);

$this->assertEquals($model->id, 'TEST_ID');
$this->assertEquals($model->version, 0.2);
$this->assertEquals($model->email, '[email protected]');

$this->assertEquals($result['code'], 201);

$this->assertContains('api/rest/TEST_ID', $result['location']);
$this->assertNotEmpty($result['location']);
}

public function testView()
{
$result = $this->get('/api/rest/TEST_ID');
$result = $this->get('/api/users/TEST_ID');
$model = json_decode($result['body']);
$model = $model->model;

$this->assertEquals($model->id, 'TEST_ID');
$this->assertEquals($model->version, 0.1);
$this->assertEquals($model->email, '[email protected]');
$this->assertEquals($model->name, 'Test REST User');

$this->assertEquals($result['code'], 200);
}

public function testIndex()
{
$result = $this->get('/api/rest');
$result = $this->get('/api/users');
$data = json_decode($result['body']);

$this->assertEquals($data->count, 100);
Expand All @@ -51,30 +51,34 @@ public function testIndex()

public function testUpdate()
{
$result = $this->put('/api/rest/TEST_ID', array('version' => '0.3'));
$result = $this->put('/api/users/TEST_ID', array(
'email' => '[email protected]',
'name' => 'Updated Name'
));
$model = json_decode($result['body']);

$this->assertEquals($model->id, 'TEST_ID');
$this->assertEquals($model->version, 0.3);
$this->assertEquals($model->email, '[email protected]');
$this->assertEquals($model->name, 'Updated Name');

$this->assertEquals($result['code'], 200);
}

public function testError()
{
$result = $this->put('/api/rest/TEST_ID', array('version' => 'wrong_version'));
$result = $this->put('/api/users/TEST_ID', array('email' => 'wrong_email'));
$model = json_decode($result['body']);

$this->assertEquals($model->error->type, 'invalid_param_error');
$this->assertCount(1, $model->error->params);
$this->assertEquals($model->error->params[0]->name, 'version');
$this->assertEquals($model->error->params[0]->name, 'email');

$this->assertEquals($result['code'], 400);
}

public function testDelete()
{
$result = $this->delete('/api/rest/TEST_ID');
$result = $this->delete('/api/users/TEST_ID');
$model = json_decode($result['body']);

$this->assertEquals($model->id, 'TEST_ID');
Expand Down

0 comments on commit e91331c

Please sign in to comment.