Skip to content
Snippets Groups Projects
Commit 6e4dcc28 authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

Working catalog action with availables models / methods / properties

parent 5627e5f9
Branches
No related merge requests found
LICENSE 0 → 100644
Restful-STORM is under the MIT License (MIT)
Copyright (c) 2010-2011 Agence Française Informatique http://www.afi-sa.fr
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
\ No newline at end of file
......@@ -40,7 +40,7 @@ class IndexController extends Zend_Controller_Action {
public function catalogAction() {
$this->view->models = [];
$this->view->models = Restful_Model_Configuration::getAll();
}
......
<h1>Catalog</h1>
<ul>
<?php foreach($this->models as $model) { ?>
<li><a href="<?php echo $this->url(['model' => $model], 'restindex', true);?>">
<?php echo $this->escape($model); ?></a></li>
<?php } ?>
</ul>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Catalog</title>
</head>
<body>
<h1>Catalog</h1>
<?php foreach($this->models as $model => $config) { ?>
<h2><?php echo $this->escape($model);?></h2>
<h3>Méthodes</h3>
<ul>
<?php foreach($config->getMethods() as $cap => $url) { ?>
<li><?php echo $this->escape($url)?> (<?php echo $this->escape($cap);?>)</li>
<?php } ?>
</ul>
<h3>Champs</h3>
<pre><?php echo json_encode($config->getFields()); ?></pre>
<?php } ?>
</body>
</html>
......@@ -58,6 +58,7 @@ class Restful_Model {
public function show($id) {
xdebug_break();
if (!$this->canShow()
|| !$model = call_user_func_array([$this->_storm_model, 'find'], [$id]))
return new stdclass();
......
......@@ -61,6 +61,17 @@ class Restful_Model_Configuration {
}
public static function getAll() {
if (!self::$_config->models)
return [];
self::$_loaded_instances = [];
foreach(self::$_config->models as $model => $config)
self::$_loaded_instances[$model] = new self($model, $config);
return self::$_loaded_instances;
}
public function __construct($model, $conf) {
$this->_model = $model;
if (!$conf)
......@@ -83,6 +94,29 @@ class Restful_Model_Configuration {
}
public function getMethods() {
$router = Zend_Controller_Front::getInstance()->getRouter();
$methods = [];
if ($this->canIndex())
$methods['index'] = 'GET ' . $router->assemble(['model' => $this->_model], 'restindex', true, false);
if ($this->canShow())
$methods['show'] = 'GET ' . $router->assemble(['model' => $this->_model, 'id' => '[:id]'], 'restshow', true, false);
if ($this->canCreate())
$methods['create'] = 'POST ' . $router->assemble(['model' => $this->_model], 'restindex', true, false);
if ($this->canUpdate())
$methods['update'] = 'PUT ' . $router->assemble(['model' => $this->_model, 'id' => '[:id]'], 'restshow', true, false);
if ($this->canDelete())
$methods['delete'] = 'DELETE ' . $router->assemble(['model' => $this->_model, 'id' => '[:id]'], 'restshow', true, false);
return $methods;
}
public function __call($name, $args) {
if ('can' == substr($name, 0, 3)
&& ($capability = strtolower(substr($name, 3)))
......
......@@ -181,6 +181,59 @@ class IndexControllerNotInCatalogEditTest extends RestfulControllerTestCase {
}
class IndeControllerCatalogTest extends RestfulControllerTestCase {
public function setUp() {
parent::setUp();
Model_Testing::beVolatile();
Restful_Model_Configuration::setConfig(
new Zend_Config(['models' => ['Model_Testing' => ['caps' => 'index;show;create;update;delete']]]));
$this->dispatch('/catalog', true);
}
/** @test */
public function titleShouldBeCatalog() {
$this->assertXPathContentContains('//h1', 'Catalog');
}
/** @test */
public function testingModelShouldBePresent() {
$this->assertXPathContentContains('//h2', 'Model_Testing');
}
/** @test */
public function indexUrlShouldBePresent() {
$this->assertXPathContentContains('//li', 'GET /Model_Testing');
}
/** @test */
public function showUrlShouldBePresent() {
$this->assertXPathContentContains('//li', 'GET /Model_Testing/[:id]');
}
/** @test */
public function createUrlShouldBePresent() {
$this->assertXPathContentContains('//li', 'POST /Model_Testing');
}
/** @test */
public function updateUrlShouldBePresent() {
$this->assertXPathContentContains('//li', 'PUT /Model_Testing/[:id]');
}
/** @test */
public function deleteUrlShouldBePresent() {
$this->assertXPathContentContains('//li', 'DELETE /Model_Testing/[:id]');
}
}
class Model_Testing extends Storm_Model_Abstract {
......
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