XAF
XAF

Reputation: 1472

Cannot run phpunit test on codeigniter, CI not found error

I am running phpunit test on codeigniter, I have a admin_test.php file with this piece of code.

class Admin_test extends PHPUnit_Framework_TestCase{
    private $CI;

    public function __construct()
    {
        // create instance variable
        $this->CI = &get_instance();


    }


    public function testRenderReturnsHelloWorld(){
        $this->CI->load->controller('admin');
        $data = $this->admin->render();
        $expected = 'Hello World';
        $this->assertEquals($expected, $data);
    }
}

And when I run the phpunit test I get this error.

PHP Fatal error:  Call to undefined function get_instance() in C:\Users\farhana\Desktop\ci\tests\application\controllers\admin_test.php on line 16
PHP Stack trace:
PHP   1. {main}() 

I used composer to get phpunit test so following other tutorials wont work since composer is loading bootstrap.php file

Here is my phpunit.xml file

<?xml version="1.0" encoding="UTF-8" ?>
<phpunit bootstrap="./vendor/autoload.php"
         color = "true"
         convertErrorsToExpections="true"
         convertNoticesToExceptions="true"
         convertWarningsToExpections="true"
         syntaxCheck="false"
         >
    <testsuites>
        <testsuite name="Unit Tests">
            <directory>./tests/application/controllers/admin_test.php</directory>
        </testsuite>
    </testsuites>
</phpunit>

Upvotes: 4

Views: 1731

Answers (3)

Kobus Myburgh
Kobus Myburgh

Reputation: 1202

Your line reads:

$this->CI = &get_instance();

But it must be:

$this->CI =& get_instance();

Upvotes: 0

DanTheMann
DanTheMann

Reputation: 175

For anyone else struggling with this, try rougin/spark-plug

Upvotes: 1

xmike
xmike

Reputation: 1048

I do not have great familiarity with CI but had a chance to work with a codebase that used it. The thing is that function &get_isntance() is defined in system/core/CodeIgniter.php file. So (it seems) you could include it to be able to use the function (you could create some special bootstrap file that would have includes for this file and vendor/autoload.php).

I empasized on 'seems' by intent because actually that file has some prerequisitions for some constants to be already set (and some other stuff done - as in index file). Also besides defining that get_instance function it has a lot of side-effects -- it includes other files, instantiates objects (which also do their own jobs), registers error and exception handlers, handles 404 situations, creates conroller and its method to run, runs it, calls hooks and does benchmarking in between of all of those. Basically most of these actions are irrelevant to the testing process, espeically when talking of unit testing. So I think it is not really good idea to include that file (althoug that might work -- I did not check).

That leaves one with a choice -- either examine all CI bootrstraping code and extract all stuff that is suitable and in need for doing unit testing and running them. Or drop the idea of unit testing on CI-based code and try something less deep and more integrative: something as functional/acceptance testing offered by Codeception (or search for some other similair tool).

Personally, I think that first option is not worth of making such efforts by quite obvious reasons. So if you have to do CI and want to do some testing it would be more productive not to dive into its internals and concentrate on outer boundaries of the code to test.

(Can't say I've made a full-covering and comprehensive search on doing real unit testing of CI-based code, but it seems (even considering only brief search on SO-topics on the subject) it's quite a problem. So if you manage to solve any issues it would be interesting to see the ways you've done that.)

Upvotes: 0

Related Questions