fmalk / codeigniter-phpunit

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

Can CI custom libraries be tested? #34

Closed jagandecapri closed 9 years ago

jagandecapri commented 9 years ago

I have a custom library, Item.php, written as such:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Item {
        public function dummy()
        {
          return 1;
        }
}

ItemTest.php is as such:

<?php
require('CITestCase.php');

class ItemTest extends CITestCase {

  protected $item;

  public function setUp()
  {
    $this->CI->load->library('item');
  }

  public function testItem()
  {
    $this->assertEquals($this->CI->item->dummy(), 1);
  }
}
/* End of file ItemTest.php */

However, running this test shows error Call to a member function dummy() on a non-objectFilename:

Any help would be greated appreciated. Thanks.

feryardiant commented 9 years ago

if you install it properly, you shouldn't get any error. I've try it and nothing happen

ciunit - 34

jagandecapri commented 9 years ago

Hi @feryardiant ,

I found the problem, I was loading a controller with same name with my library class.

controllers/Item.php libraries/Item.php

CI could not have have same name for controller, models and libraries as I found out from this link http://stackoverflow.com/questions/7156123/why-are-code-igniters-models-controllers-libraries-not-seperated and by testing myself.

Beginner mistake I think. Anyway thanks. ;)

feryardiant commented 9 years ago

Anytime dude :smile:

stephane-wis commented 9 years ago

Hello, I have a problem with my library Cockpit and phpunit (which ran well before adding the Library). "Class 'CI_Cockpit' not found" is the error phpunit produces, right after a class_load() call. What did I missed?

fmalk commented 9 years ago

@stephane-wis please open another issue, with more information. Which CI version? Where is your CI_Cockpit file located?