Skip to content

Commit

Permalink
Add some integration tests for Watch controller.
Browse files Browse the repository at this point in the history
Make the controllers non-shared services as having them shared makes
mocking $app much harder.
  • Loading branch information
markstory committed Aug 19, 2013
1 parent f2c22d3 commit 818a42c
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Xhgui/ServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ protected function _services()
*/
protected function _controllers()
{
$this['watchController'] = $this->share(function ($c) {
$this['watchController'] = function ($c) {
return new Xhgui_Controller_Watch($c['app'], $c['watchFunctions']);
});
};

$this['runController'] = $this->share(function ($c) {
$this['runController'] = function ($c) {
return new Xhgui_Controller_Run($c['app'], $c['profiles'], $c['watchFunctions']);
});
};

$this['customController'] = $this->share(function ($c) {
$this['customController'] = function ($c) {
return new Xhgui_Controller_Custom($c['app'], $c['profiles']);
});
};
}

}
94 changes: 94 additions & 0 deletions tests/Controller/WatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
use Slim\Environment;

class Controller_WatchTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
parent::setUp();
Environment::mock(array(
'SCRIPT_NAME' => 'index.php',
'PATH_INFO' => '/watch'
));
$di = Xhgui_ServiceContainer::instance();
unset($di['app']);

$di['app'] = $di->share(function ($c) {
return $this->getMock(
'Slim\Slim',
array('redirect', 'render', 'urlFor'),
array($c['config'])
);
});
$this->watches = $di['watchController'];
$this->app = $di['app'];
$this->watchFunctions = $di['watchFunctions'];
$this->watchFunctions->truncate();
}

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

$this->watches->get();
}

public function testPostAdd()
{
$_POST = array(
'watch' => array(
array('name' => 'strlen'),
array('name' => 'strpos')
)
);
$this->app->expects($this->once())
->method('urlFor')
->with('watch.list');

$this->app->expects($this->once())
->method('redirect');

$this->watches->post();
$result = $this->watchFunctions->getAll();

$this->assertCount(2, $result);
$this->assertEquals('strlen', $result[0]['name']);
$this->assertEquals('strpos', $result[1]['name']);
}

public function testPostModify()
{
$this->watchFunctions->save(array('name' => 'strlen'));
$saved = $this->watchFunctions->getAll();

$_POST = array(
'watch' => array(
array('name' => 'strpos', '_id' => $saved[0]['_id'])
)
);
$this->watches->post();
$result = $this->watchFunctions->getAll();

$this->assertCount(1, $result);
$this->assertEquals('strpos', $result[0]['name']);
}

public function testPostDelete()
{
$this->watchFunctions->save(array('name' => 'strlen'));
$saved = $this->watchFunctions->getAll();

$_POST = array(
'watch' => array(
array('removed' => 1, 'name' => 'strpos', '_id' => $saved[0]['_id'])
)
);
$this->watches->post();
$result = $this->watchFunctions->getAll();

$this->assertCount(0, $result);
}

}

0 comments on commit 818a42c

Please sign in to comment.