EllisLab text mark
Advanced Search
1 of 3
1
   
I’d really like to get PHPUnit working with CI
Posted: 28 April 2011 05:23 AM
Avatar
Joined: 2009-08-30
10 posts

Hi Guys,

I haven’t really been posting here much but i’ve been using CI a bit over the last year or two.

I’m working on a project at the moment that I would love to be able to test with PHPUnit. I’m aware of the problems surrounding this and would really like to overcome them.

I know you probably can’t test the controllers, which is fine by me. I really just want to test my libraries and models.

So, has anybody successfully gotten PHPUnit to test models, I think libraries are probably ok?

I have searched the forum etc and read about the solutions for CI 1.7.2 but there doesn’t seem to be much available for 2.0?

Would anybody be interested in working with me to develop a nice bridge between CI 2.0 and PHPunit to make testing easier.. Ideally I want to be able to script my tests with various chunks of test data..

Thanks for any input guys.

techguy

 
Posted: 28 April 2011 09:46 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2006-06-01
225 posts

I know the Reactor team is working on Unit Tests for CI using PHPUnit.  I believe the latest code in BitBucket contains everything needed to use it, though I haven’t tried the code yet.

 Signature 

Bonfire - Faster CodeIgniter development | Ocular Template Library

 
Posted: 28 April 2011 10:04 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2009-08-30
10 posts

Cool, I just had a look and spotted the unit test branch.

I wonder where i’ll find docs on it, I’m not used to BitBucket.

Thanks for that.

 
Posted: 28 April 2011 10:38 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2006-06-01
225 posts

As for docs… good question smile You might stalk @gaker or @pkriete on Twitter to see if they can give you any pointers. smile

 Signature 

Bonfire - Faster CodeIgniter development | Ocular Template Library

 
Posted: 29 April 2011 12:52 PM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2009-08-30
10 posts

Cool, thanks.

I just tweeted @gaker, he says they are testing the core now, so it should be out soon.

Great stuff.

Thanks again smile

 
Posted: 29 April 2011 12:55 PM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2006-06-01
225 posts

No problem! I’m just excited the core is actually going to have good test coverage. Will make keeping things stable much easier for the Reactor team.

 Signature 

Bonfire - Faster CodeIgniter development | Ocular Template Library

 
Posted: 02 May 2011 03:20 PM   [ # 6 ]   [ Rating: 0 ]
Joined: 2011-04-24
2 posts

Hi,

I’m working at a project using CI and I’m testing controllers with PHPUnit.
I’m using 1.7.3 since it needs to work with PHP 4, but updating to 2.x is straightforward.
It requires a little tweaking of the bootstrapping sequence and some fixing extending a couple of core classes. I have flagged one of the issues in Bug Tracker.

I’d like to share my approach. You might find a better way of doing things and maybe our work might be of interest to others in the CI community.

Let me know if you’d like me to expand on this.

Regards

Carlo

 
Posted: 03 May 2011 03:51 AM   [ # 7 ]   [ Rating: 0 ]
Joined: 2011-03-01
2 posts

Hello Carlo, I would like to know more about this, can you please help me on this. I am using CI 2.0, how can I bridge the PHPUnit and FooStack or is there any other way doing it.. please help.

thanks,

 
Posted: 03 May 2011 06:28 PM   [ # 8 ]   [ Rating: 0 ]
Joined: 2011-04-24
2 posts

Hi Zair,
I’m not using FooStack. I plug in PHPUnit directly.
The way I go about this is by providing an alternative entry point that uses the same bootstrapping sequence provided by core/CodeIgniter.php, but stops before routing kicks in.

This requires:
- extracting the variable definitions in the index page into a config/settings.php file
- providing a bootstrap.php file included in test classes

I also use a sub class of PHPUnit_Framework_TestCase ( CITestCaseExtension ) to provide some basic common functionality to the test classes, including some rudimentary functions to set up fixtures.

The test classes are organized in an application/tests directory that mirrors the controllers’ so that by pointing the phpunit script to that directory all the tests wherein are run together.

We have:
index.php
application
  |__ config
  |    |__ settings.php
  |__ controllers
  |    |__ myController.php
  |__ tests
      |__ bootstrap.php
      |__ CITestCaseExtension.php
      |__ myControllerTest.php

In this case changing to application and running:
phpunit —include-path /application tests

phpunit automatically runs myControllerTest instantiating myController

I’m attaching sample files to clarify.

 
Posted: 28 June 2011 09:28 PM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2009-07-29
60 posts

Any updated news on this?

 Signature 

website: http://meglea.com
twitter: http://twitter.com/megleaweb

 
Posted: 11 July 2011 06:03 AM   [ # 10 ]   [ Rating: 0 ]
Joined: 2008-09-06
28 posts

Yeah i am curious too smile

 
Posted: 13 July 2011 09:32 PM   [ # 11 ]   [ Rating: 0 ]
Joined: 2008-07-13
45 posts

I am also interested in using PHPUnit in my project. smile

 
Posted: 18 July 2011 09:18 PM   [ # 12 ]   [ Rating: 0 ]
Avatar
Joined: 2007-12-02
135 posts

Hi guys,

CIUnit worked and passed all sample tests within with CodeIgniter 2.0.

https://bitbucket.org/kenjis/my-ciunit

 
Posted: 19 July 2011 06:25 AM   [ # 13 ]   [ Rating: 0 ]
Joined: 2008-09-06
28 posts

Seems like it can’t handle CSRF protection. He can’t find the function csrf_verify().

And I am getting errors on my autoloaded models:

[CIUnit] PHP Error: Notice - Undefined property: Welcome::$config_model File Path: core/MY_Controller.php (line: 40)
PHP Fatal error:  Call to a member function get_config() on a non-object in /application/core/MY_Controller.php on line 40

Fatal error: Call to a member function get_config() on a non-object in /application/core/MY_Controller.php on line 40

 
Posted: 19 July 2011 11:14 AM   [ # 14 ]   [ Rating: 0 ]
Joined: 2011-07-19
1 posts
CarloGl - 03 May 2011 10:28 PM

Hi Zair,
I’m not using FooStack. I plug in PHPUnit directly.
The way I go about this is by providing an alternative entry point that uses the same bootstrapping sequence provided by core/CodeIgniter.php, but stops before routing kicks in.

This requires:
- extracting the variable definitions in the index page into a config/settings.php file
- providing a bootstrap.php file included in test classes

I also use a sub class of PHPUnit_Framework_TestCase ( CITestCaseExtension ) to provide some basic common functionality to the test classes, including some rudimentary functions to set up fixtures.

The test classes are organized in an application/tests directory that mirrors the controllers’ so that by pointing the phpunit script to that directory all the tests wherein are run together.

I was able to use PHPUnit with CI 2.0 using something like that. I don’t use fixtures but mocks. It works as I want.

Uncompress the attached files in the application directory and should work (code is quite dirty, I finished it today tongue laugh )

 
Posted: 20 July 2011 04:40 AM   [ # 15 ]   [ Rating: 0 ]
Avatar
Joined: 2007-12-02
135 posts
P.T. - 19 July 2011 10:25 AM

And I am getting errors on my autoloaded models:

[CIUnit] PHP Error: Notice - Undefined property: Welcome::$config_model File Path: core/MY_Controller.php (line: 40)
PHP Fatal error:  Call to a member function get_config() on a non-object in /application/core/MY_Controller.php on line 40

Fatal error: Call to a member function get_config() on a non-object in /application/core/MY_Controller.php on line 40

I commited the fix.
https://bitbucket.org/kenjis/my-ciunit/changeset/ef8c637ad9b9

 
1 of 3
1