2

PHPUnit is giving me an error message whenever I try to include a file. For example the follwing code gives me the error:

<?php

include_once "PHPUnit/Autoload.php";
require_once "controller/ProductController.php";

class SecurityTests extends PHPUnit_Framework_TestCase
{
    public function testSmth() {
        $this->assertTrue(1==1);
    }
}

?>

But if I remove the fourth line (require_once "controller/ProductController.php";), it runs fine.

The error I get is:

Warning: require_once(PHPUnit/Framework/Error.php): failed to open stream: No such file or directory in E:\wamp\bin\php\php5.4.3\pear\PHPUnit\Util\ErrorHandler.php on line 48

EDIT: My include_path in the php.ini file is:

include_path = ".;E:\wamp\bin\php\php5.4.3\pear\;E:\wamp\www\renting\"

What is strange:

<?php
echo get_include_path(); **// Will echo .;E:\wamp\bin\php\php5.4.3\pear\;E:\wamp\www\renting\** 
require_once 'PHPUnit/Autoload.php';
require_once 'controller/AccountController.php';
echo get_include_path(); **// Will echo E:/wamp/www/renting/**

And also:

<?php

require_once 'controller/AccountController.php';
echo get_include_path(); **// Will echo E:/wamp/www/renting/**
require_once 'PHPUnit/Autoload.php';

echo get_include_path(); **// Will not even echo.**

This is very strange to me.

Any ideas?

cgf
  • 3,369
  • 7
  • 45
  • 65
  • What does your directory structure look like and from where are you running the test? – Niklas Modess Mar 05 '13 at 12:38
  • I am using PhpStorm for running the tests, but if I run it from the test folder with command line, I get the same results. – cgf Mar 05 '13 at 22:49
  • If I remove the included file, I don't get any sort of error, the tests run okay. – cgf Mar 05 '13 at 22:50

3 Answers3

2

I had a similar issue, PHPUnit could not locate files in parent directories. It only located files being children to the current directory of the class importing them, e,g.

file structure:

var/
    www/
        /api
            index.php
            conf.php
        /config
           dbconfig.php

index.php

require_once 'conf.php'; #works fine
require_once '../config/dbconfig.php; #ERROR

PHPUnit has problems finding the relative path when trying to go to parent folders. After reading this answer. The solution was to insert dirname(dirname(__FILE__)), dirname(__FILE__) which returns the path of the current directory, by applying dirname to the path, it returns the parent path.

index.php

require_once 'conf.php'; #works fine
require_once dirname(dirname(__FILE__)) . '../config/dbconfig.php'; #works fine.
Emir Husic
  • 717
  • 5
  • 16
1

Its a know issue.

Verify your PHUnit installed below packeges:

pear channel-discover pear.phpunit.de
pear channel-discover components.ez.no
pear install phpunit/PHP_CodeCoverage

try sudo if you don't have permissions.

Some links which helps you :

https://bugs.launchpad.net/ubuntu/+source/phpunit/+bug/701544

fatal error 'File/Iterator/Autoload.php' not found when running phpunit

phpunit require_once() error

Community
  • 1
  • 1
Vishal Bharti
  • 185
  • 1
  • 9
  • Well, it seems that my files aren't actually missing and there is nothing for me to download. The problem is that it doesn't manage to include the files that it needs even though they are there. I do not get it. – cgf Mar 05 '13 at 01:37
0

Ok, it took me days to figure this one out. I hope nobody will ever go through what I went.

I had in my php.ini:

include_path = ".;E:\wamp\bin\php\php5.4.3\pear\;E:\wamp\www\renting\"

But because I used to not use multiple php include paths in my ini file, I had used

set_include_path("E:/wamp/www/renting/");

in some of the files, even if they weren't the ones that I was trying to include.

Anyway, it might sound silly and confusing, but if you find yourself in a situation where PHPUnit does not load properly for some reason, think of the possibility of your code changing the include path somehow.

cgf
  • 3,369
  • 7
  • 45
  • 65