How to use php spec with code igniter?

680 views Asked by At

I have a code igniter project and I installed phpspec. My problem is that when I run a test on my class, it throws an error that it cannot find that class's base class (which is automatically loaded by the code igniter framework) namely "Class 'CI_Controller' not found".

I tried manually including the path to the base class and removing the line namespace controllers; which seems to fix that problem. But now my phpspec test is failing saying that class controllers\Calculator does not exist.

Here is how I have it set up:

My class in src > controllers > Calculator.php:

<?php
//I had to remove the following line for my app to work:
//namespace controllers;

class Calculator extends CI_Controller {...}

My test in spec > controllers > CalculatorSpec.php:

<?php

namespace spec\controllers;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

//I had to add this line to fix my first problem
include_once '/Users/bradleytrager/Desktop/Workspace/code-ignitor-calculator/system/core/Controller.php';

class CalculatorSpec extends ObjectBehavior
{
    function it_is_initializable()
    {
        $this->shouldHaveType('controllers\Calculator');
    }
}

Can anyone help me to get this to work?

1

There are 1 answers

4
Zarathuztra On

Inside your controller test, you've told your matcher to expect something in namespace:

controllers\Calculator;

But the problem is that Calculator is in the namespace of Calculator given your controller class above. How to fix this? Just add that namespace to your controller

namespace controllers;

Basically it looks like you removed it to fix one thing, then once you fixed it you forgot to put it back in. You haven't said what happens if you include your CI_Controller class AND use namespace controllers. Calculator is in the global space unless you define it to be in something else like above, yet you're referencing it inside a different space out of global in

$this->shouldHaveType('controllers\Calculator');

So now, what happens when you add the namespace back to your Calculator controller?