ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Unit Testing with phpUnit (models+controllers+fixtures) - want it?

February 06, 2008 5:06pm

Subscribe [31]
  • #1 / Feb 06, 2008 5:06pm

    rafsoaken

    33 posts

    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

  • #2 / Feb 07, 2008 11:35am

    John Fuller

    779 posts

    Sure, why not polish it up and release it? 

    There is also a SimpleTest library which Ciscoheat released which works pretty well.  I don’t know how SimpleTest compares with phpUnit though.

  • #3 / Feb 14, 2008 2:32pm

    legolin

    1 posts

    rafsoaken,

    I just found your post because I’m interested enough in this sort of ability to start working on it myself.  I really like that you’re adding classic functional tests (testing the controllers and output); I’m currently most interested in the unit testing aspect and am trying to figure out how to wrap the models for testing…

    My thought was to create a custom include so I can still use the phpunit command from the command line; what’s your approach?

    Adam

  • #4 / Jun 22, 2008 11:01pm

    ccxmqq

    1 posts

    Can you gave me this test framework code~~

  • #5 / Jul 06, 2008 6:24pm

    rafsoaken

    33 posts

    Here we are, the first release CIUnit, unit testing with phpUnit for Codeigniter. We are at v0.10, hooray!
    Download it here: fooStack v0.10 for Codeigniter

    It’s called “fooStack”, consisting of a drop-in replacment of the Codeigniter.php file (actually it only defines the CIUnit version constant and per default extends the codeigniter Loader and Output class - use ‘extends fooLoader’ and ‘fooOutput’ to extend them), and all the classes needed (eg for parsing yaml and generating fixtures)

    Tests of CIUnit itself are of course included and should assure you that your setup of fooStack works just fine. 

    I included a short description on how to set it up in the fooStack.html file, please drop me a line or post here if you start using it.

    cheers
    rafsoaken

  • #6 / Jul 06, 2008 11:32pm

    Jamongkad

    67 posts

    Oh awesome up until recently I thought I was the only one who thought CI’s unit testing suite was lacking. You just spared me the effort of porting SimpleTest to CI. Thanks!

  • #7 / Aug 08, 2008 5:15am

    dsloan

    48 posts

    Certainly looks like a great project. However, I’m having a problem getting up and running with testing my application.

    See my post on the problem at http://ellislab.com/forums/viewthread/87635/

  • #8 / Aug 31, 2008 6:47pm

    rafsoaken

    33 posts

    The new version v0.12 of CIUnit is available, hooray!

    Some minor small bugs have been fixed, and CIUnit now is very stable for CodeIgniter versions 1.62, 1.63 and the upcoming 1.70 release (tested against trunk).
    Get this beauty here: fooStack/CIUnit v0.12 for Codeigniter

    Additionally the project is now also available from a public mercurial repository: fooStack/CIUnit bitbucket repository

    Happy testing!
    Clemens

  • #9 / Sep 26, 2008 8:02pm

    Thorsten

    2 posts

    nevermind 😊

  • #10 / Oct 01, 2008 12:28pm

    rafsoaken

    33 posts

    The repository (mercurial) is now to be found at: http://www.bitbucket.org/rafsoaken/ciunit/overview/

    Check it out !

  • #11 / Oct 01, 2008 1:26pm

    Thorsten

    2 posts

    yo raf,

    is the code thats in your repository any newer than then one thats in the zip at
    http://www.foostack.com/foostack/fooStack_v0.12.zip ?

    t

  • #12 / Oct 01, 2008 1:32pm

    rafsoaken

    33 posts

    the repository is always at the latest version of the code, the zip is like a snapshot. But at this moment it should be still the same.

  • #13 / Oct 22, 2008 7:28am

    anurat

    3 posts

    Hi all,

      Thank you for a making Code Igniter and fooStack.

      I have a problem using it though.

    I have followed the instructions from this link.
    http://www.foostack.com/foostack/
    Now I try to run a test but it produces nothing.
    Here is an example.

    C:\xampp\htdocs\equipment\tests>phpunit --verbose AllTests
    
    C:\xampp\htdocs\equipment\tests>phpunit --verbose AllTests
    
    C:\xampp\htdocs\equipment\tests>phpunit --verbose AllTests
    
    C:\xampp\htdocs\equipment\tests>

    I assume it should print out something. I tried to change
    some test cases but nothing happens so far.

    Please assist,
    Thank you so much,

    Anurat,

  • #14 / Oct 22, 2008 3:02pm

    rafsoaken

    33 posts

    Hi Anurat,
    I couldn’t reproduce your error - do you have some more information on what you are using exactly? Eg.: phpunit version, version of Codeigniter, and php version?
    greets
    clem

  • #15 / Oct 27, 2008 1:16am

    anurat

    3 posts

    Hi Clem,

      Thanks for your reply,

      I’m using XAMPP for windows version 1.6.6a
      It has PHP 5.2.5 and Apache 2.0
      I use Code Igniter 1.6.3 and PHPUnit 3.3.1

      I had this notice before I fixed it.

    C:\xampp\htdocs\equipment\tests>phpunit --verbose AllTests
    
    Strict Standards: Only variables should be passed by reference in C:\xampp\htdoc
    s\equipment\tests\CIUnit.php on line 66
    
    C:\xampp\htdocs\equipment\tests

      I also changed Common.php

    from
    include( 'config.php' );
    
    to
    include( 'libraries/fooStack/fooBase/config.php' );

      PHPUnit seems to work fine. I got some results when using with normal PHP classes.

    Thanks for your work,
    Looking forward to hearing from you,
    Anurat,

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases