Hi all,
Despite CI’s obvious greatness some may find it lacks easy Unit Testing (no sight of them in 1.6, besides that puny Test class). I am currently cooking up a small wrapper lib to use phpUnit 3.2, yaml (db)fixtures with Spyc, and yet to come some sort of test scaffolding.
At the current stage a test case looks like this:
include_once dirname(__FILE__).'/../CIUnit.php';
set_controller('main'); //one of my app controllers
class testMainController extends CIUnit_TestCase{
function setUp(){
// load fixtures into test database
// eg.: application/tests/fixtures/thomas_posts_fixt.yml
// with the convention that there is a db table 'thomas_posts'
$this->dbfixt('thomas_posts', 'thomas_media', 'thomas_posts_media');
}
//let's test the first page
public function testDisplay(){
//$this->CI always refers to your current controller
$this->CI->index();
//fetch the controllers output
$outputted = output();
//and peak at the variables assigned the view displayed in $this->CI->index();
$assigned_vars = viewvars();
//test view for php errors
$this->assertFalse(preg_match('/(error|notice)/i', $outputted);
//correctly assigned values?
//compare to fixtures
$this->assertEquals($this->thomas_posts_fixt['first'], $assigned_vars['posts'][0]);
}
//lets test a model too!
function testSomeModel(){
$this->CI->load->model('Post_model');
//retrieve a post
$post = $this->CI->Post_model->post(1);
//and check its text field
$this->assertEquals('desc of first entry', $post['text']);
$post = $this->CI->Post_model->post(-22);
$this->assertFalse($post);
//or compare it directly to the fixture
$this->assertEquals($this->thomas_posts_fixt['first'], $post);
}
}So, anyone interested in this? I really would like to know as the code is good enough for internal use but would need some lovin’ to make it available to others…
Besides, are there any syntax requests you find useful?, or other needs that should be catered (like eg. it should play nice with your simpletest testcases, css selectors, etc.)
Cheers rafsoaken