Skip to content

Commit

Permalink
Add base controller class & update tests.
Browse files Browse the repository at this point in the history
For the more complex controllers like the run controllers using mocks is
not going to be easy/fun. Add some more 'framework' to make testing
easier.
  • Loading branch information
markstory committed Aug 31, 2013
1 parent 5869761 commit b696f8e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
23 changes: 23 additions & 0 deletions src/Xhgui/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

class Xhgui_Controller
{
protected $_templateVars = array();
protected $_template = null;

public function set($vars)
{
$this->_templateVars = array_merge($this->_templateVars, $vars);
}

public function templateVars()
{
return $this->_templateVars;
}

public function render()
{
$this->_app->render($this->_template, $this->_templateVars);
}

}
7 changes: 3 additions & 4 deletions src/Xhgui/Controller/Watch.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class Xhgui_Controller_Watch
class Xhgui_Controller_Watch extends Xhgui_Controller
{

protected $_app;
Expand All @@ -16,9 +16,8 @@ public function get()
{
$watched = $this->_watches->getAll();

$this->_app->render('watch/list.twig', array(
'watched' => $watched,
));
$this->_template = 'watch/list.twig';
$this->set(array('watched' => $watched));
}

public function post()
Expand Down
8 changes: 6 additions & 2 deletions src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@

// Watch function routes.
$app->get('/watch', function () use ($di) {
$di['watchController']->get();
$c = $di['watchController'];
$c->get();
$c->render();
})->name('watch.list');

$app->post('/watch', function () use ($di) {
$di['watchController']->post();
$c = $di['watchController'];
$c->post();
$c->render();
})->name('watch.save');


Expand Down
8 changes: 3 additions & 5 deletions tests/Controller/WatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function setUp()
$di['app'] = $di->share(function ($c) {
return $this->getMock(
'Slim\Slim',
array('redirect', 'render', 'urlFor'),
array('redirect', 'urlFor'),
array($c['config'])
);
});
Expand All @@ -28,11 +28,9 @@ public function setUp()

public function testGet()
{
$this->app->expects($this->once())
->method('render')
->with('watch/list.twig', array('watched' => array()));

$this->watches->get();
$result = $this->watches->templateVars();
$this->assertEquals(array(), $result['watched']);
}

public function testPostAdd()
Expand Down

0 comments on commit b696f8e

Please sign in to comment.