fmalk / codeigniter-phpunit

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

Error: get_instance() is not defined - CI 2.2 #14

Closed fmalk closed 10 years ago

fmalk commented 10 years ago

On behalf of Gilad, who sent me this email, I'm posting it here as an issue

Hey,

First of all I’d like to say thanks for the codeigniter-phpunit that you did.

I’m using CI 2.2.0 and I saw in the Changelog that it should work and you make less hacks in the core files and ect. But this project still has 3 hack to the system files and not 1, and also I have problem running it on my CI 2.2.0 version.

Any change you can help me with it ? Right now I’m trying your examples and get error that the get_instance() is not defined (I’m also using HMVC but have tried testing modules yet)

P.s. I don’t know if it matters but I’m trying to run the PHPUNIT from my IDE ( PHPSTORM ), but I’m able to run a simple phpunit tests.

Appreciate any help you can give me.

Thanks, Gilad.

fmalk commented 10 years ago
  1. The project is tested on CI 2.2, so it should work after you replace the core files. The goal is to minimize core hacks, but as of now they are mandatory.
  2. The Travis build which tests this project runs exactly on CI 2.2, has one build tested on HHVM, and you can see there is a line with that get_instance call. So the error must come from your environment.
  3. What I was able to gather is that get_instance is only available after routing a Controller, and after the pre_controller hook. Usually, the default controller in your config is loaded while bootstraping, and then you call whatever controller you want to test.
fmalk commented 10 years ago

As of running PHPUnit on PhpStorm, I use Aptana 3, and that's an auxiliary script I use to run tests inside it (I'm on a WAMP dev server):

#!C:\localhost\wamp\php\php546x130204135542\php.exe
<?php
/* PHPUnit
 */
define('PHPUnit_MAIN_METHOD', 'PHPUnit_TextUI_Command::main');

if (strpos('C:\localhost\wamp\php\php546x130204135542\php.exe', '@php_bin') === 0) {
    require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPUnit' . DIRECTORY_SEPARATOR . 'Autoload.php';
} else {
    require 'C:\localhost\wamp\php\php546x130204135542\pear' . DIRECTORY_SEPARATOR . 'PHPUnit' . DIRECTORY_SEPARATOR . 'Autoload.php';
}

PHPUnit_TextUI_Command::main();

Save this as phpunit.php and have your IDE run this script, it'll serve as an $> phpunit from the command line.

ghost commented 10 years ago

hello and thanks for you replay and opening this issue,

i'll start with 3. i have my default controller set up and working, but my guess is that PHPStorm is just running this test file and not going through the bootsraping process because i can't even use the CI DEFINE Constants. when i'm running the test without any CI function it looks fine but the CI stuff doesn't work.

the steps that i did was:

  1. installing PHPUnit on my remote server (my workstation is windows but my DEV Server is a remote REDHAT server) 2 patched the phpstorm v7.1 to work with is PHPUnit (they fixed it only on v8)
  2. tested exmaple test worked fine.
  3. copied the phpunit.xml next to my index.php
  4. copied the tests folder into my application folder.
  5. copied the 3 hacked files to my system/core folder.
  6. tested your exmaple test from the application/tests folder without success.

about that script that you use in the Aptana 3.. where should i put it and what is its purpose ?

feryardiant commented 10 years ago

Hi @GiladGr! I'm not sure if I can help, because PHPStorm is not my primary IDE and I don't really know how to work with it. but let me try:

  1. I have PHPStorm 7.1.3 installed om my system (I'm using ubuntu 14.04 64bit)
  2. I load all of my global composer vendor as PHPStorm External libraries
  3. I setup new project with fresh-install of CI 2.2.0 and codeigniter-phpunit latest as well
  4. Open up PHPStorm terminal window and run phpunit from it: phpstorm-phpunit bigger image
  5. But i'm not be able to run unit test via PHPStorm build-in runner Shift+Alt+F10 (maybe there's something wrong with my configuration) screenshot from 2014-07-30 21 29 11

EDIT: I found this video on youtube and I hope it's useful http://www.youtube.com/watch?v=wPVXgTl6f7w

ghost commented 10 years ago

i haven't tried the PHPSTORM console but i was able to run it successfully using putty, my guess is that is the same, using the PHPSTORM and CI i had problems but i'm not sure it's the same as yours, in your case it says "Could not open input file: phpunit" what is this file ? it's the script you use on the aptana ? you added it to the run-configuration?

i've watched that video and many others but no help from that..

ghost commented 10 years ago

if so can you please add pictures of you run-configuration and i'll try to match them to mine ?

feryardiant commented 10 years ago

This is my project settings, screenshot from 2014-07-31 01 04 51 I've try to use 3 difference option there, but no luck at all..

and as you said above:

2 patched the phpstorm v7.1 to work with is PHPUnit (they fixed it only on v8)

I found this.

Then I tryed this hack, but still no luck. :sweat_smile: screenshot from 2014-07-31 01 30 16

And finally I try to revert back my php.jar and downgrade phpunit to 3.7 (using local composer installation) and ... :sweat: phpunit 3.7

feryardiant commented 10 years ago

Wooo!! I get it!!

  1. If you're using composer, make sure you use your composer autoload.php file as your custom loader on your project setting like this: (I'm using global composer installation) phpstorm-settings
  2. Setup your Run/Debug configuration, under Test Runner choose defined in configuration file as Test Scope: then check use alternative configuration file and choose your <your-project-dir>/phpunit.xml, like this: phpstorn-testconfig
  3. Patch your php.jar file using this solution. In my case, it located in ~/.local/share/php-storm/plugins/php/lib/ (it should be based on your phpstorm installation directory, maybe)
  4. I hope I didn't miss anything here, now you should run Shift+Alt+F10 and see: ci-test
  5. make some coffee

:smile:

ghost commented 10 years ago

i'll try your solution tomorrow, i already patched the PHPSTORM but i think i might need help with the first two steps becasue i tried all kind of set ups with no luck but i'll double check your solution again and let you know.

maybe it has something to do with that my dev server is on a remote redhat machine and my IDE is on my local windows machine (project is on a network folder on the Redhat) so when i need to write the configuration and bootstrap paths it looks like it asks for the windows paths, but the files are on the remote Redhat machine, anyway i tried both paths without luck, guess i mixed some stuff.

and no i haven't tried using the compser autoload, i just checked the "path to phpunit.phar" but it worked only with a file path in windows.

ghost commented 10 years ago

as i mentioned i'm working on a remote machine, when using Putty i can see that the phpunit is installed from anywhere, but when using the PHPStorm now i get the following error : can't find PHPUnit in the include path: capture

fmalk commented 10 years ago

@GiladGr by now I take the issue has been resolved?

ghost commented 10 years ago

actually no.. couldn't figure it out and it took to much time so i left it for now. i'll get back to it soon and let you know if i'll have any success with it.

thanks for your help anyway.