5

We've decided to use PHPUnit (with Jenkins) in our next project. We're considering different PHP frameworks, one of which is CodeIgniter. I see that a lot of people use My CIUNIT to "bridge" PHPUnit and Codeigniter. There is little to no explanation in the online documentation.

Why is it needed?

Other frameworks don't seem to need a "cool bridge" like this.

Community
  • 1
  • 1
Adam Lynch
  • 3,341
  • 5
  • 36
  • 66
  • 2
    Zend Framework has `Zend_Test_PHPUnit_ControllerTestCase` for the same reasons listed in Francis Avila's answer. As I wanted to test our controllers separate from the router and view scripts (a.k.a. *unit* testing), I built two base test case classes to do the setup and cleanup. It wasn't easy digging through all the ZF code to make sure I was doing everything necessary, but now our tests are smaller and run quickly. – David Harkness Jul 05 '12 at 23:59

1 Answers1

10

Reasons:

  1. Codeigniter's components are tightly coupled. You need some big basic parts running (the loader, the router, the CFG object) before you can use any other pieces.
  2. Codeigniter is not designed to run from a CLI. It has a great deal of bootstrapping code in its index.php front-controller, and it assumes a web server environment.

You do not absolutely require CIUNIT to unit-test CI. But you will have to do something. In my case, I wrote an alternative front controller to index.php which just loads the minimum necessary to get a CI superobject. I require_once it at the top of my test files, and $this->CI =& get_instance() in SetUp() methods. If I were being an absolutely pure unit-tester, however, I should be destroying and re-creating the CI object after every test in case some state got trapped in it. I'm not sure if CIUNIT does this for you.

Francis Avila
  • 31,233
  • 6
  • 58
  • 96
  • 1
    Great answer! A lot of times these "bridges" have to replicate the environment the app is running. The more loosely coupled things are, the more you can write your tests against the individual components and get rid of the environment emulation. That's my take on it, I think this is a great answer and up voted it! – frosty Jul 05 '12 at 14:03
  • 1
    Point 2 is not entirely accurate (though it used to be). I want to say it was version 2 that gave use much better cli integration. http://codeigniter.com/user_guide/general/cli.html – Greg Jul 05 '12 at 16:45