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]
  • #46 / Apr 08, 2009 9:57am

    rafsoaken

    33 posts

    Hi all,

    @spyro
    Did you try the latest repository version or zip version?

    @PHP Four
    Other than was is on the CIUnit web page and what you can find in the threads I do not know of anything yet - Requests like yours might be an incentive write one eg. in the wiki here..

    @everyone
    If you happen to have some simple test cases using CIUnit that you can share please do so! (For quick sharing just use the codeigniter wiki for example)

    @MDomansky
    Could you specify a bit more clearly what you are trying to test?

    cheers
    raf

  • #47 / Apr 13, 2009 4:54am

    Mackstar

    76 posts

    Hi,

    I have tried using this and it seems to work, although I am getting the following errors

    Severity: Warning
    Message:  Cannot modify header information - headers already sent by (output started at /opt/local/lib/php/PHPUnit/Util/Printer.php:168)
    Filename: libraries/Session.php
    Line Number: 662

    This is when running the default controller test

    phpunit ControllersAllTests from within the tests/controller dir, I also get it when running all tests from the tests directory when running all tests.

    I am using version 1.5 with CI 1.7.1

    Many thanks

    Richard

  • #48 / Apr 13, 2009 4:59am

    Mackstar

    76 posts

    Sorry I found that rafsoaken already answered my question… Although this seems a hacky way just to get a testing sweet to run…

    But maybe it will be worth it….?

  • #49 / Apr 13, 2009 5:20am

    Mackstar

    76 posts

    I ended up surpressing errors, which may defeat the point here, but maybe I can find some balance on that!??

    if(defined(‘CIUnit_Version’))
    {
    error_reporting(E_ERROR);
    }

  • #50 / Apr 13, 2009 5:52am

    Mackstar

    76 posts

    Possible problem with alt CodeIgniter.php, I am using an alt db and load it with

    $this->db1 = $this->load->database('no1', TRUE);

    I then call actions on this with commands such as

    $this->db1->insert('table', $data);

    This works fine with the original CodeIgniter.php but with the doctored one it gives me

    Fatal error: Call to a member function insert() on a non-object in /path_to_ccs_ci/system/application/models/model.php on line whatever

    errors, how can I get this to work as it is looking to be a sweet useful solution for my testing needs..

    Many thanks

    Richard

  • #51 / Apr 13, 2009 4:30pm

    rafsoaken

    33 posts

    Hi Richard,
    I looked at it and changed some small bit in the fooStack Loader so it gives you your alternative db connection back properly. Please try the latest repository version from here: http://www.bitbucket.org/rafsoaken/ciunit/overview/
    cheers
    raf

  • #52 / Apr 13, 2009 9:18pm

    Mackstar

    76 posts

    Raf,

    Thanks for this and your valuable contribution to the CI community. It worked great…!

    I also added the following line to my error_php.php file to get rid of the headers already sent error when testing controllers that use sessions…

    <?php if(!(strstr($message, "Cannot modify header information ")&&defined;('CIUnit_Version')&&strstr;($line, "662"))){ ?>
    
    <?php  } ?>

    Many thanks

    Richard

  • #53 / Apr 14, 2009 4:27am

    rafsoaken

    33 posts

    Thanks Richard,

    That’s also a nice trick to avoid those header errors. I’ll keep that in mind!

    cheers
    raf

  • #54 / Apr 16, 2009 11:54pm

    Mackstar

    76 posts

    Raf and Everyone,

    Even though I managed to hide the errors, I am unable to test controllers that use sessions to control a authenticated environment…

    Any ideas on this?

    Many thanks

    Richard

  • #55 / Apr 17, 2009 9:47am

    rafsoaken

    33 posts

    Hi Richard,

    I think that is a general PHP / Framework Issue - You generally can’t set headers when running PHP from the commandline in a meaningful way, because you do not communicate with any clients. One way to still get what you want is e.g. that you set your session variables differently when running in test mode, e.g: a la “if( defined(‘CIUnit_Version’)){}".

    View my answer to a similar question:
    http://ellislab.com/forums/viewreply/540182/
    Or something more general here:
    http://stackoverflow.com/questions/190292/phpunit-unit-testing-with-items-that-need-to-send-headers

    hope that helps
    cheers
    raf

  • #56 / Apr 17, 2009 10:46pm

    Mackstar

    76 posts

    Thanks again for the follow up Raf,

    I think you are right, I have been using php through the browser for so long and running php apps through the command line means there is stuff I need to think about…

    But I will probably go on to create my own session management meaning I can continue using your library, which has already become extremely helpful and is becoming a big part of my work-flow when using PHP.

    Thanks again

    Richard

  • #57 / Sep 17, 2009 9:05pm

    kenjis

    118 posts

    Hi, I’ve got an error when I run AllTests.php.
    CI 1.7.1, PHP 5.2.6, PHPUnit 3.3.16, Xdebug 2.0.5

    
    
    

    There is no Welcome.php, but welcome.php. I added code like this.
    But this patch may be too adhoc.

    --- system/application/tests/CIUnit.php    (リビジョン 4)
    +++ system/application/tests/CIUnit.php    (リビジョン 5)
    @@ -121,7 +121,14 @@
                 //it was not loaded before
                 if (!class_exists($controller_name))
                 {
    -                include_once(APPPATH . 'controllers/' . $controller . EXT);
    +                // check lower case controller file name
    +                if (is_file(APPPATH . 'controllers/' . $controller . EXT)) {
    +                    include_once(APPPATH . 'controllers/' . $controller . EXT);
    +                }
    +                else
    +                {
    +                    include_once(APPPATH . 'controllers/' . strtolower($controller) . EXT);
    +                }
                 }
                                                  
                 self::$current = $controller_name;
  • #58 / Sep 19, 2009 5:45pm

    voland

    4 posts

    If you want to test a redirect function then you can do this steps:
    1. create a mock_helper helper with function

    if (defined('CIUnit_Version')) {
        function redirect($uri = '', $method = 'location', $http_response_code = 302) {
            $CI =& get_instance();
            $CI->load->_ci_cached_vars['redirect_to'] = $uri;
        }
    }

    2. load mock helper BEFORE url helper
    3. in you test method you can check redirect in this way

    $this->CI->redir(); //call method with redirect
    $data = viewvars(); //get all view vars
    $this->assertSame('welcome/index', $data['redirect_to']); //check for redirect uri
  • #59 / Dec 05, 2009 2:56pm

    n0-0ne

    3 posts

    HI I’ve just downloaded CIUnit 0.17 to unit test my code igniter application.
    PHPUnit version 3.4.3
    CI version 1.7.2

    I’m now learning CI and built a really simple blog model class (doesn’t really do any thing yet)
    but when I try to test it I get the Fatal error that the class has already been declared

    $ phpunit --verbose AllTests.php
    
    Fatal error: Cannot redeclare class Blog_modelTest in /var/www/CodeIgniter/system/application/tests/models/blog_model_test.php on line 16
    
    Call Stack:
        0.0004      61944   1. {main}() /usr/bin/phpunit:0
        0.1269    4480512   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:52
        0.1269    4481244   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:147
        0.1290    4484292   4. PHPUnit_Runner_BaseTestRunner->getTest() /usr/share/php/PHPUnit/TextUI/Command.php:168
        0.1291    4484516   5. PHPUnit_Runner_BaseTestRunner->loadSuiteClass() /usr/share/php/PHPUnit/Runner/BaseTestRunner.php:112
        0.1291    4485004   6. PHPUnit_Runner_StandardTestSuiteLoader->load() /usr/share/php/PHPUnit/Runner/BaseTestRunner.php:170
        0.1294    4519536   7. PHPUnit_Util_Fileloader::checkAndLoad() /usr/share/php/PHPUnit/Runner/StandardTestSuiteLoader.php:102
        0.1294    4519796   8. PHPUnit_Util_Fileloader::load() /usr/share/php/PHPUnit/Util/Fileloader.php:91
        0.1298    4533212   9. include_once('/var/www/CodeIgniter/system/application/tests/AllTests.php') /usr/share/php/PHPUnit/Util/Fileloader.php:113
        0.1709    6438416  10. require_once('/var/www/CodeIgniter/system/application/tests/models/ModelsAllTests.php') /var/www/CodeIgniter/system/application/tests/AllTests.php:19

    the test code itself is also very simple -

    <?php
    include_once dirname(__FILE__).'/../CIUnit.php';
    
    class Blog_modelTest extends CIUnit_TestCase {
    
        public function setUp() {    
            $this->CI->load->model('Blog_model');
        }
    
        function testGet_entries()
        {    
             $this->assertEquals(10, count($this->blog->get_entries));
        }
    
    
    }

    any idea why this would happen ?

  • #60 / Jul 20, 2010 6:15am

    sidhartha

    10 posts

    if i run ‘phpunit AllTests.php’ in tests directory (system/application/tests) it works fine. but the time when i run’phpunit ModelsAllTests.php’ inside the subdirectory of test i.e(system/application/tests/models) it shows some error. inmy case it is

    PHP Warning:  require(/opt/lampp/htdocs/calculator/system/application/libraries/fooStack/fooBase/../../../../../../application/libraries/fooStack/fooBase/Common.php): failed to open stream: No such file or directory in /opt/lampp/htdocs/calculator/system/application/libraries/fooStack/fooBase/CodeIgniter.php on line 39
    PHP Fatal error:  require(): Failed opening required ‘/opt/lampp/htdocs/calculator/system/application/libraries/fooStack/fooBase/../../../../../../application/libraries/fooStack/fooBase/Common.php’ (include_path=’.:/opt/lampp/lib/php/PEAR:/root/PEAR’) in /opt/lampp/htdocs/calculator/system/application/libraries/fooStack/fooBase/CodeIgniter.php on line 39


    what r the changes i need to do to overcome this?


    i also want to know where should i write the test cases for a controller and a model respectively.

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

ExpressionEngine News!

#eecms, #events, #releases