0

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?

Bradley Trager
  • 3,570
  • 3
  • 26
  • 33
  • Have you tried this? http://stackoverflow.com/questions/13649408/integrating-codeigniter-2-with-phpspec2 – Vassilis Barzokas Dec 15 '13 at 12:12
  • Yes, thank you. That does work well for loading the dependencies, but when I remove the namespace, phpspec is not able to locate my spec. And if I don't remove the namespace, my app doesn't run. – Bradley Trager Dec 15 '13 at 16:02
  • If you're removing the namespace in your controller, then it's not going to be available at that namespace anymore. Did you try removing the controllers\ portion of your namespace inside Calculator and just declare it as namespace Calculator? Then modifying phpspec's namespace expectation to match? – Zarathuztra Dec 15 '13 at 17:03
  • @Zarazthuztra, that sounds promising. I would be very appreciative if you could post an answer with an example of what you mean. Specifically "modifying phpspec's namespace expectation. Thank you. – Bradley Trager Dec 16 '13 at 00:50

1 Answers1

1

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?

Zarathuztra
  • 3,215
  • 1
  • 20
  • 34
  • When I add back the namespace after referencing CI_Controller it once again cannot find CI_CONTROLLER. – Bradley Trager Dec 16 '13 at 11:05
  • Update: I figured out I can add the "controllers" namespace and use "extends \CI_Controller" (the backslash means to use the global namespace) to make the pass test, but that breaks my app. So my real problem figuring out how to use CI with namespaces which is not supported. see: http://stackoverflow.com/questions/3700626/namespace-in-php-codeigniter-framework and http://philsturgeon.co.uk/blog/2012/12/5-things-codeigniter-cannot-do-without-a-rewrite. The other option which I would prefer for now is to get phpspec working without the namespaces. – Bradley Trager Dec 16 '13 at 14:37
  • @BradleyTrager hmmmm that should have worked. I don't work with namespaces much (yet, I'm a bad bad man) but I'll keep investigating for you. just bare with me. Hopefully I'll have something for you by tonight, if that works? – Zarathuztra Dec 16 '13 at 15:22
  • Anytime is absolutely fine, thank you. I appreciate your help. – Bradley Trager Dec 16 '13 at 15:27