Skip to content
Snippets Groups Projects
Commit edac712e authored by Patrick Barroca's avatar Patrick Barroca
Browse files

Show action in json

parent d9c9e1de
Branches
No related merge requests found
<?php
class IndexController extends Zend_Controller_Action {
protected $_model;
public function init() {
$this->_helper
->getHelper('contextSwitch')
......@@ -8,6 +10,11 @@ class IndexController extends Zend_Controller_Action {
->initContext();
}
public function preDispatch() {
$this->_model = Restful_Model::newFor($this->_getParam('model'));
}
public function catalogAction() {
$this->view->models = ['Model_Post'];
......@@ -16,28 +23,26 @@ class IndexController extends Zend_Controller_Action {
public function indexAction() {
if ($this->_request->isPost())
return $this->_forward('create', null, null, $this->_getAllParams());
return $this->_forward('create');
$this->_helper->json(Restful_Model::newFor($this->_getParam('model'))->index(), true);
$this->_helper->json($this->_model->index(), true);
}
public function showAction() {
if ($this->_request->isPut())
return $this->_forward('update', null, null, $this->_getAllParams());
return $this->_forward('update');
if ($this->_request->isDelete())
return $this->_forward('delete', null, null, $this->_getAllParams());
return $this->_forward('delete');
$this->_helper->json(Restful_Model::newFor($this->_getParam('model'))
->show((int) $this->_getParam('id')),
$this->_helper->json($this->_model->show((int) $this->_getParam('id')),
true);
}
public function editAction() {
$this->_helper->json(Restful_Model::newFor($this->_getParam('model'))
->show((int) $this->_getParam('id')),
$this->_helper->json($this->_model->show((int) $this->_getParam('id')),
true);
}
......
......@@ -30,9 +30,9 @@ class Restful_Model {
public function show($id) {
return $this->canShow()
? call_user_func_array([$this->_storm_model, 'find'], [$id])
: null;
return ($this->canShow()
&& $model = call_user_func_array([$this->_storm_model, 'find'], [$id]))
? $model->toArray() : new StdClass;
}
......
storm @ 7e275ba4
Subproject commit f403adf40d0c2a4904541aba1c07e0f984d4fea9
Subproject commit 7e275ba41bbd6317a17ed695f003bdec6e7fa83f
......@@ -87,8 +87,27 @@ class IndexControllerNotInCatalogShowTest extends RestfulControllerTestCase {
/** @test */
public function shouldReturnNull() {
$this->assertEquals('null', $this->getResponse()->getBody());
public function shouldReturnEmptyObject() {
$this->assertEquals('{}', $this->getResponse()->getBody());
}
}
class IndexControllerInCatalogShowTest extends RestfulControllerTestCase {
public function setUp() {
parent::setUp();
$this->fixture('Model_Testing', ['id' => 2]);
Restful_Model_Configuration::setConfig(
new Zend_Config(['models' => ['Model_Testing' => ['show' => 1]]]));
$this->dispatch('/Model_Testing/2', true);
}
/** @test */
public function responseShouldContainsId2() {
$this->assertEquals('{"id":2}', $this->_response->getBody());
}
}
......@@ -109,8 +128,8 @@ class IndexControllerNotInCatalogEditTest extends RestfulControllerTestCase {
/** @test */
public function shouldReturnNull() {
$this->assertEquals('null', $this->getResponse()->getBody());
public function shouldReturnEmptyObject() {
$this->assertEquals('{}', $this->getResponse()->getBody());
}
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment