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.

How to use PHPUnit (CIUnit) with CodeIgniter 2.1.0

January 16, 2012 8:38pm

Subscribe [10]
  • #1 / Jan 16, 2012 8:38pm

    kenjis's avatar

    kenjis

    118 posts

    I wrote a tutorial about how to install and use PHPUnit/CIUnit with CodeIgniter 2.1.0.

    http://d.hatena.ne.jp/Kenji_s/20120117/1326763908

  • #2 / Jan 17, 2012 8:06pm

    kenjis's avatar

    kenjis

    118 posts

    I added:

    Database Testing of CodeIgniter Application with PHPUnit (CIUnit)
    http://d.hatena.ne.jp/Kenji_s/20120118/1326848578

  • #3 / Jan 18, 2012 10:51am

    ShipSailingHome

    3 posts

    Kenji,

    I’m pretty new to PHPUnit. I’ve followed the instructions at this URL http://d.hatena.ne.jp/Kenji_s/20120117/1326763908—and I’m getting an error.

    I didn’t download a fresh copy of CodeIgniter, like in your tutorial, because I already have my project built.

    I’m hoping you can advise me. I keep getting this error:

    controllers  fixtures  generate  generate.php  getops.php  helpers  libs  models  phpunit.xml  system
    bob@dev1:~/zbb-dev/zbb/trunk/tests$ phpunit
    [CIUnit] PHP Error: Warning - require_once(PHPUnit/Autoload.php): failed to open stream: No such file or directory File Path: CIUnit/bootstrap_phpunit.php (line: 259)
    
    Fatal error: require_once(): Failed opening required 'PHPUnit/Autoload.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/bob/zbb-dev/zbb/trunk/application/third_party/CIUnit/bootstrap_phpunit.php on line 259
    bob@dev1:~/zbb-dev/zbb/trunk/tests$

    Thinking I had the PHPUnit files in the wrong place, I made a symlink in my CI web root:

    bob@dev1:~/zbb-dev/zbb$ ls
    dbunit.bat  dbunit.php  default.zip  kenjis-my-ciunit-27a0665997a8  PHPUnit-3.4.15  phpunit.bat  phpunit.php  PHPUnit.tar  test  trunk
    
    bob@dev1:~/zbb-dev/zbb$ cd trunk/
    
    bob@dev1:~/zbb-dev/zbb/trunk$ ls
    application  css  default_schema.sql  downloads  images  index.php  js  license.txt  system  tests
    
    bob@dev1:~/zbb-dev/zbb/trunk$ ln -s PHPUnit ../PHPUnit-3.4.15/
    bob@dev1:~/zbb-dev/zbb/trunk$ cd tests/
    
    bob@dev1:~/zbb-dev/zbb/trunk/tests$ phpunit
    [CIUnit] PHP Error: Warning - require_once(PHPUnit/Autoload.php): failed to open stream: No such file or directory File Path: CIUnit/bootstrap_phpunit.php (line: 259)
    
    Fatal error: require_once(): Failed opening required 'PHPUnit/Autoload.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/bob/zbb-dev/zbb/trunk/application/third_party/CIUnit/bootstrap_phpunit.php on line 259
    bob@dev1:~/zbb-dev/zbb/trunk/tests$

    But I’m still having the same problem.

    Maybe my PHPUnit install is just bad?

  • #4 / Jan 18, 2012 11:12am

    ShipSailingHome

    3 posts

    It looks like I’m using an older version of PHPUnit that doesn’t include Autoload.php . It has Framework.php instead.

    That’s disappointing because the newer version of PHPUnit requires a newer version of PHP—and, I can’t upgrade PHP on this server.

  • #5 / Jan 18, 2012 12:04pm

    ShipSailingHome

    3 posts

    I couldn’t upgrade to a newer version of PHP on that server, but I got access to a server that I could upgrade PHP on 😊

    I got it working! Woohoo!

  • #6 / Jan 22, 2012 9:18pm

    jessejimz's avatar

    jessejimz

    5 posts

    Hey, Keinji—I think I know your cousin in MX (Kenji S.).

    In any event, I’m on a Mac OS X + XAMPP + Eclipse Helios + CodeIgniter v2.0.3.

    I got everything running well via command terminal and really digg CIUnit. I’m so happy you spent valuable time creating this.

    Man, I cannot tell you how many hours I have spent tryting to get Makegood plugin for Eclipse working with CIUnit on my Mac. I keep getting this “CIUnit_TestCase_Selenium class is not available. Fix…” error that I can’t get rid of.

    My checklist of things I ensured:
    * I Have PHP 5.3.x installed
    * I have latest PHPUnit 3.7 installed via PEAR
    * I have PHPUnit Selenium installed via PEAR
    * I have the PEAR path in my Eclipse project’s include path (PEAR is up to date)
    * Installed latest Makegood plugin for Eclipse (works fine with PHPunit)
    * I properly set up Makegood settings for CIUnit
    * My environment variables are set in my .profile and have the XAMPP bins there (it’s why I can actually run CIUnit from command line)

    Have you gotten any reports about this pesky problem with Makegood? I know it has to do with dependency resolution by Makegood/Eclipse. Any help much appreciated.

  • #7 / Jan 22, 2012 10:07pm

    kenjis's avatar

    kenjis

    118 posts

    Man, I cannot tell you how many hours I have spent tryting to get Makegood plugin for Eclipse working with CIUnit on my Mac. I keep getting this “CIUnit_TestCase_Selenium class is not available. Fix…” error that I can’t get rid of.

    My checklist of things I ensured:
    * I Have PHP 5.3.x installed
    * I have latest PHPUnit 3.7 installed via PEAR
    * I have PHPUnit Selenium installed via PEAR
    * I have the PEAR path in my Eclipse project’s include path (PEAR is up to date)
    * Installed latest Makegood plugin for Eclipse (works fine with PHPunit)
    * I properly set up Makegood settings for CIUnit
    * My environment variables are set in my .profile and have the XAMPP bins there (it’s why I can actually run CIUnit from command line)

    Have you gotten any reports about this pesky problem with Makegood? I know it has to do with dependency resolution by Makegood/Eclipse. Any help much appreciated.

    Your checks seem to be good.

    But, what is “CIUnit_TestCase_Selenium class” ?
    I think there is no such class.

    I have not installed PHPUnit Selenium, but MakeGood works fine.

     

  • #8 / Jan 23, 2012 7:06pm

    philpalmieri's avatar

    philpalmieri

    34 posts

    Kenji, you rock! this is awesome and works great, thank you.

    I added a webtest folder, and selenium and it works perfectly

  • #9 / Feb 08, 2012 6:42am

    rubdottocom

    3 posts

    Hi!

    Whoah Kenji, thanks for the amazing work!

    I’m close to make it work, I have a problem with a simple test of a controller

    This is my controller

    <?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Home extends MY_Controller {
     
        function __construct()
        {
            parent::__construct();
      $this->load->helper('json'); 
        }
     public function view($page = 'home')
     {
      log_message('debug', 'Page: '. $page);
      if ( ! file_exists('application/views/pages/'.$page.'.php'))
      {   
       show_404(); // Whoops, we don't have a page for that!
      }
      
      $data['sections'] = $this->get_sections_master();
      
      $this->load->view('templates/header', $data);
      $this->load->view('templates/sidebar', $data);
      $this->load->view('pages/'.$page, $data);
      $this->load->view('templates/footer', $data);
       
     }
    ... Other stuff

    And this my test:

    <?php
    
    /**
     * @group Controller
     */
    
    class SomeControllerTest extends CIUnit_TestCase
    {
     public function setUp()
     {
      // Set the tested controller
      $this->CI = set_controller('Home');
     }
     
     public function testWelcomeController()
     {
      // Call the controllers method
      //$this->CI->index();
      $this->CI->view('home');
      
      // Fetch the buffered output
      $out = output();
      
      // Check if the content is OK
      $this->assertSame(0, preg_match('/(error|notice)/i', $out));
     }
    }

    When I run phpunit I receive 404 Message because the

    if ( ! file_exists('application/views/pages/'.$page.'.php'))

    But the file exists and if I try to open in a browser works fine, I miss something? :-(

  • #10 / Feb 08, 2012 7:07am

    kenjis's avatar

    kenjis

    118 posts

    Hi!

    Whoah Kenji, thanks for the amazing work!

    I’m close to make it work, I have a problem with a simple test of a controller

    This is my controller

    ...snip…
      if ( ! file_exists('application/views/pages/'.$page.'.php'))
    ...snip…

    ...snip…

    When I run phpunit I receive 404 Message because the

    if ( ! file_exists('application/views/pages/'.$page.'.php'))

    But the file exists and if I try to open in a browser works fine, I miss something? :-(

    Probably because you are pointing your view file as relative path.
    Please change the path as absolute path.

    For example:
    APPPATH.‘views/pages/’.$page.’.php’

     

  • #11 / Feb 08, 2012 8:13am

    rubdottocom

    3 posts

    Hi!

    Whoah Kenji, thanks for the amazing work!

    I’m close to make it work, I have a problem with a simple test of a controller

    This is my controller

    ...snip…
      if ( ! file_exists('application/views/pages/'.$page.'.php'))
    ...snip…

    ...snip…

    When I run phpunit I receive 404 Message because the

    if ( ! file_exists('application/views/pages/'.$page.'.php'))

    But the file exists and if I try to open in a browser works fine, I miss something? :-(

    Probably because you are pointing your view file as relative path.
    Please change the path as absolute path.

    For example:
    APPPATH.‘views/pages/’.$page.’.php’

    Works perfect :- ) I’m pretty newbie with CI and php ^^u

  • #12 / Feb 13, 2012 12:20pm

    rubdottocom

    3 posts

    Hi!

    I have another (probably) newbie question ^^

    I’m trying to test models with a test database using fixtures, but I have tables with Foreign Keys and I receive errors about I can’t truncate tables referenced in a foreign key

    I read about it and it seems a standard restriction, I could delete all rows but I can’t truncate a table with FK’s unless I disable the FK first.

    Any advice? :(

  • #13 / Mar 08, 2012 9:26pm

    kenjis's avatar

    kenjis

    118 posts

    The way to truncate the tables in CIUnit is not good.
    Any contributions is welcome. 😊

  • #14 / May 05, 2012 9:48pm

    royalgiant

    3 posts

    PHP Warning:  require_once(PHP/CodeCoverage/Autoload.php): failed to open stream: No such file or directory in /usr/lib/php/PHPUnit/Autoload.php on line 46
    PHP Fatal error:  require_once(): Failed opening required 'PHP/CodeCoverage/Autoload.php' (include_path='.:') in /usr/lib/php/PHPUnit/Autoload.php on line 46

    I keep getting this error. Does anyone know how to fix it? It happens when i run phpunit

     

  • #15 / May 25, 2012 9:25am

    Lechuss

    13 posts

    public function testRepeat()
        {
            $yell = "Hello, any one out there?";
            $returned = $this->CI->repeat($yell);
            $this->assertEquals($returned, $yell);
        }

    ..PHP Fatal error:  Call to undefined method Welcome::repeat() in /opt/bitnami/frameworks/codeigniter/tests/controllers/WelcomeControllerTest.php on line 20

     

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

ExpressionEngine News!

#eecms, #events, #releases