fmalk / codeigniter-phpunit

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

Weird case #32

Closed sylouuu closed 9 years ago

sylouuu commented 9 years ago

Hello,

I updated to the latest CI version and I tried with your files.

I had no output, no error after running phpunit.

By doing step-by-step debug, I found that I have to comment this line: https://github.com/fmalk/codeigniter-phpunit/blob/master/system/core/CodeIgniter.php#L514

And my helper email test works.

But my front app shows me a blank page...

Have you more info about this please?

PS: Seems OK with,

    if (!defined('PHPUNIT_TEST')) {
        call_user_func_array(array(&$CI, $method), $params);
    }

Bests

fmalk commented 9 years ago

First thought: do you have a default controller/action? That line 514 is CI's main "router", it calls whatever method of your controller it is supposed to call, based on URL. Since CLI has no such thing, it tries to call your default route. CI is supposed to call this line even with PHPUnit.

sylouuu commented 9 years ago

Hi @fmalk,

Yes I have,

$route['default_controller'] = 'home';

Home.php

<?php
    if (!defined('BASEPATH')) {
        exit('No direct script access allowed');
    }

    class Home extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }

        public function index()
        {
            redirect(base_url() .'ca/campaign');
        }
    }
fmalk commented 9 years ago

@sylouuu sorry I was away this weekend. So your default route calls for a redirect, which might not be testable, and could very well break your CLI run, since both methods used by index() can potentially break (base_url() and redirect(), you have no URL info). Try changing your default route and see if it changes anything, or try changing that index() into an empty method. Remember, CI will call your default route once, because that's what bootstrapping without an URL does, and then your tests are "free" to instantiate your controllers at will. But that default route has to go smoothly.

sylouuu commented 9 years ago

Hi @fmalk,

Why I didn't think about that?

public function index()
{
    if (is_cli() === false) {
        redirect(base_url() .'ca/campaign');
    }
}

You have right, it's better now, I removed my fix in the core file.

Thanks

fmalk commented 9 years ago

@sylouuu no problem, glad I could help. I'll try to incorporate this issue in the docs.