fmalk / codeigniter-phpunit

Hack to make CodeIgniter work with PHPUnit.
234 stars 61 forks source link

Tests not working with form_validation library #43

Open eldan88 opened 8 years ago

eldan88 commented 8 years ago

Hi,

I am trying to test a controller that has the form_validation library. When I run phpunit it gives me the following error message

) MyTestController::testHome Error: Call to a member function helper() on null

/Users/eldanv/Dropbox/web-content/ci/system/libraries/Form_validation.php:147

When I comment out the line number the error goes away but I can't leave it un commmented .

How can I fix this issue?

Below is my controller code and test code


class Welcome extends CI_Controller {

    public function __construct()
    {

        parent::__construct();
        $this->load->library(["form_validation"]);

        //$this->load->helper(["html"]);

    }

    public function testCode(){

        return "hi";

    }

}

Test code


require_once('CITestCase.php');

class MyTestController extends CITestCase
{

    public function testHome()
    {
        $this->requireController('Welcome');
        $CI = new Welcome();
        $CI->testCode();
    }

}
cnadig-cc commented 8 years ago

+1 I'm facing the same problem

eldan88 commented 8 years ago

This seems to work well...

https://github.com/kenjis/ci-phpunit-test

cnadig-cc commented 8 years ago

@eldan88 I ran into problems using ci-phpunit-test too.

fmalk commented 8 years ago

Does it happen to every helper, or just this form_validation? It is one thing for the issue to be with this project, and another to be with a helper library...

cnadig-cc commented 8 years ago

I've not tried with other helpers. I'm using the testing code as is, downloaded from GitHub. I've not added any test cases of my own. when I run phpunit in my project folder, it fails with this error: ~/workspace/cargo/carrier-suite(develop ✗) phpunit PHPUnit 4.8.21 by Sebastian Bergmann and contributors.

...PHP Fatal error: Call to a member function helper() on a non-object in /Users/cnadig-cc/workspace/cargo/carrier-suite/system/libraries/Form_validation.php on line 147 PHP Stack trace: PHP 1. {main}() /usr/local/bin/phpunit:0 PHP 2. PHPUnit_TextUI_Command::main() /usr/local/bin/phpunit:545 PHP 3. PHPUnit_TextUI_Command->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:100 PHP 4. PHPUnit_TextUI_TestRunner->doRun() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:149 PHP 5. PHPUnit_Framework_TestSuite->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/TestRunner.php:440 PHP 6. PHPUnit_Framework_TestSuite->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php:747 PHP 7. PHPUnit_Framework_TestCase->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php:747 PHP 8. PHPUnit_Framework_TestResult->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:724 PHP 9. PHPUnit_Framework_TestCase->runBare() phar:///usr/local/bin/phpunit/phpunit/Framework/TestResult.php:612 PHP 10. PHPUnit_Framework_TestCase->runTest() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:768 PHP 11. ReflectionMethod->invokeArgs() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:909 PHP 12. ControllersTest->testLoadController() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:909 PHP 13. CI_Controller->__construct() /Users/cnadig-cc/workspace/cargo/carrier-suite/application/tests/core/ControllersTest.php:33 PHP 14. load_class() /Users/cnadig-cc/workspace/cargo/carrier-suite/system/core/Controller.php:75 PHP 15. CI_Form_validation->__construct() /Users/cnadig-cc/workspace/cargo/carrier-suite/system/core/Common.php:196

fmalk commented 8 years ago

@cnadig-cc so, what you're doing is: you downloaded vanilla CI, latest version, then downloaded this project, installed as instructed but you can't run any tests. Is that your sequence? If it is, I'll try to replicate it and see what I can find. It could be from a new CI version incompatibility I'm not aware of.

cnadig-cc commented 8 years ago
  1. Yes. I downloaded vanilla CI (3.0.4), added the test cases as is from GitHub.com, set $autoload['libraries']=array('form_validation') ; , and I get this error.
  2. If I remove 'form_validation' from autoload, it runs fine.
  3. Also, In my actual project, I have other libraries that are autoloaded. But when I run phpunit, I get errors like : PHP Fatal error: Class 'CI_Tracks_Auth' not found in /Library/WebServer/Documents/cargo/testci/system/core/Common.php on line 196 The library class name is "TracksAuth". I don't know why it adds the "CI" prefix and looks for CI_Tracks_Auth
cnadig-cc commented 8 years ago

What's described here is what is happening when I try to auto load libraries.