Niranjan Kumar
Niranjan Kumar

Reputation: 1

getting an error "Trying to get property of non-object" in phpunit 5.4

I am trying to execute the following script in linux(centos 6.6) command prompt, but i am getting an error Trying to get property of non-object. I am using php version 5.6 and phpunit 5.4. This is the source file ....

    protected function getTenants()
    {
        $this->db->select('*');
        $this->db->from('tenant');
        $this->db->where('status',1);
        $rs=$this->db->get();
        return $rs->result();
    }
    public function getInactiveTenants()
    {
        $this->db->select('*');
        $this->db->from('tenant');
        $this->db->where('status',0);
        $rs=$this->db->get();
        return $rs->result();
    }
    public function getTenant($id)
    {
        $this->db->select('*');
        $this->db->from('tenant');
        $this->db->where('tenant_id',$id);
        $rs=$this->db->get();
        return $rs->row();
    }
    public function updateTenant($options = array()){
        extract($options);
        //print_r($options);exit;
        $this->db->set ( 'mobile_num', $mobile_num );
        $this->db->set ( 'email', $email );
        $this->db->set ( 'name', $name );
        $this->db->set ( 'phone_num', $phone_num );
        $this->db->set ( 'description', $description );
        $this->db->set ( 'address', $address );
        $this->db->set ( 'city', $city );
        $this->db->set ( 'state', $state );
        $this->db->set ( 'country', $country );
        $this->db->set ( 'created_by', $this->phpsession->get('sess_user_id'));
        $this->db->where('tenant_id',$tenant_id);
        $this->db->update ( "tenant" );
        return 1;
    }

This is the file which i used to run to test the above file

class Tenant_modelTest extends PHPUnit_Framework_TestCase {
    public  $CI;


    public function testGetTenants() {
        $this->CI->load->model('tenant_model');
        $posts = $this->CI->tenant_model->getTenants();
        $this->assertEquals($rs->result(), $posts);
    }

    public function testGetInactiveTenants(){
        $this->CI->load->model('tenant_model');
                $results = $this->CI->tenant_model->getInactiveTenants();
                $this->assertEquals($rs->result(), $results);
    }

    public function testGetTenant(){
        $this->CI->load->model('tenant_model');
                $results = $this->CI->tenant_model->getTenants($id);
                $this->assertEquals($rs->result(), $results);
    }

    public function testUpdateTenant(){
        $this->CI->load->model('tenant_model');
                $results = $this->CI->tenant_model->updateTenants($options = array());
                $this->assertEquals(1, $results);

    }
}

error:

1) Tenant_modelTest::testGetTenants Trying to get property of non-object 
/var/www/html/cloudscheduler/application/tests/tenant_modelTest.php:12 
ERRORS! Tests: 4, Assertions: 0, Errors: 4

Can someone please help me on this...

Upvotes: 0

Views: 1109

Answers (1)

Mudshark
Mudshark

Reputation: 3253

You need to instantiate the $CI object:

function __construct()
{
    $this->CI =& get_instance();
}

Upvotes: 1

Related Questions