Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing a CodeIgniter 2.1 controller with PHPUnit

I'm taking over a project and am trying to write some tests for CodeIgniter to run on the command line with continuous integration and there seem to be very little documentation online on testing with CodeIgniter and getting tests to run for controllers seem particularly difficult.

There's some previous answers suggesting to use FooStack, but FooStack is for CodeIgniter 1.x as best as I can gather.

I'm at the point not where I'm considering running selenium over the whole site. Has anyone managed to test controllers with CodeIgniter?

Related answers:

  • How to test controllers with CodeIgniter?
like image 963
Kit Sunde Avatar asked Jan 10 '23 17:01

Kit Sunde


2 Answers

Using my-ciunit.

  1. Grab a copy of https://bitbucket.org/kenjis/my-ciunit
  2. Install PHPUnit 3.7.x
  3. Run the installation instructions.
  4. Presumably you didn't name the dadabase on the command line so go to application/config/testing.database.php and name your database with _test
  5. Create your database.
  6. Go to the tests folder and delete all the folders except controllers. All these files are examples and obviously you won't have those fixtures.
  7. You only need the SomeControllerTest.php as a starting point. Edit it to hit a controller you actually have. I wanted to just add a basic access test.
class HomepageControllerTest extends CIUnit_TestCase {
  public function setUp() {
    $this->CI = set_controller('homepage');
  }

  public function testIndexController() {
    $this->CI->index();
    $out = output();
    $this->assertNotEmpty($out);
  }
}
like image 56
Kit Sunde Avatar answered Jan 20 '23 07:01

Kit Sunde


I wrote two articles on testing Codeigniter both Unit and Acceptance tests. I use PHPUnit and Selenium for that.

Here are the links:

  • Unit tests: http://taiar.com.br/2013/11/08/testing-codeigniter-applications-with-phpunit/
  • Acceptance tests (Selenium): http://taiar.com.br/2014/04/21/acceptance-tests-on-codeigniter-with-phpunit-and-selenium/

I'm planning to write an article on tests at all (with testing theory) but my two first articles should help!

like image 21
taiar Avatar answered Jan 20 '23 05:01

taiar