views:

21

answers:

2

This one's got me stumped. I've been working with PHPUnit for a couple of months now, so I'm not that green...but I look forward to being pointed in the direction of the obvious mistake I'm making! The initialisation process outlined below works fine if I run the "app" from a browser - but PHPUnit is choking...can any one put me out of my misery?

I'm trying to test a homebrew MVC, for study purposes. It follows a typical ZF layout. Here's the index page:

include './../library/SKL/Application.php';
$SKL_Application = new SKL_Application();
$SKL_Application->initialise('./../application/configs/config.ini');

Here's the application class (early days...)

include 'bootstrap.php';

class SKL_Application {

    /**
     * initialises the application
     */
    public function initialise($file) {
        $this->processBootstrap();
        //purely to test PHPUnit is working as expected
        return true;
    }

   /**
    * iterates over bootstrap class and executes 
    * all methods prefixed with "_init"
    */
    private function processBootstrap() {
        $Bootstrap = new Bootstrap();
        $bootstrap_methods = get_class_methods($Bootstrap);

        foreach ($bootstrap_methods as $method) {
            if(substr($method,0,5) == '_init'){
                $bootstrap->$method();
            }
        }
        return true;
    }
}

Here's the test:

require_once dirname(__FILE__).'/../../../public/bootstrap.php';
require_once dirname(__FILE__).'/../../../library/SKL/Application.php';


class SKL_ApplicationTest extends PHPUnit_Framework_TestCase {
   protected $object;


   protected function setUp() {
       $this->object = new SKL_Application();
   }

   /**
    * Tears down the fixture, for example, closes a network connection.
    * This method is called after a test is executed.
    */
   protected function tearDown() {
   }


   public function testInitialise() {
       $this->assertType('boolean',$this->object->initialise());

   }

}

But I keep stumbling at the first hurdle!!

PHP Warning:  include(bootstrap.php): failed to open stream: 
No such file or directory in path\to\files\SKL\Application.php on line 9

any ideas?

+1  A: 

Use include_once or better yet require_once instead of include to include the bootstrap.php in the Application class file. Despite being already loaded include loads it again but since it's obviously not on the include path you get the error.

Raoul Duke
I figured it out - I had to append the required file locations to the include path. The application itself is just pulling the files in from relative paths, but that wasn't working in PHPUnit - so I just set them up in a separate file and include the file in the tests.
sunwukung
A: 

Thanks to Raoul Duke for giving me a push in the right direction, here's where I got to so far

1 - add the root of the application to the include path

2 - make all inclusion paths relative to the root of the application

3 - include a file in your unit tests that performs the same function, but compensates for the relative location when it is included. I just used realpath() on the directory location of the files.

The problem I have now is that the darn thing won't see any additional files I'm trying to pass it.

So, I'm trying to test a configuration class, that will parse a variety of filetypes dynamically. The directory structure is like this:

Application_ConfigTest.php
config.ini

The first test:

public function testParseFile() {
    $this->assertType('array',$this->object->parseFile('config.ini'));
}

The error:

failed to open stream: No such file or directory

WTF? It's IN the same directory as the test class...

I solved this by providing an absolute (i.e. file structure) path to the configuration file.Can anyone explain to me how PHPUnit resolves it's paths, or is it because the test class itself is included elsewhere, rendering relative paths meaningless?

sunwukung