0

I would like to test my php application in phpunit. My problem is the require_once doesn't find the file what I would like to test. I get this error:

Warning:
require_once(C:\MyProject\phpunit-tesztek\include/../include/form.php):
failed to open stream: No such file or directory in
C:\MyProject\phpunit-tesztek\include\FormTest.php on line 4

So it search the form.php file in include/../include/form.php what is wrong.

I used this code:

require_once 'PHPUnit/Autoload.php';
require_once(__DIR__.'/../include/form.php');

the Test file is in C:\MyProject\phpunit-tesztek\include\FormTest.php

and the file what I want to test is in: C:\MyProject\include\form.php

What is the problem?

2
  • 1
    The problem is that the path you want to required ("C:\MyProject\include\form.php") does not match the path you specify: "C:\MyProject\phpunit-tesztek\include/../include/form.php". It should become clear from the error message, can you see it? See as well the answer by Steven Scott. Commented Oct 3, 2013 at 17:09
  • 1
    Please share why you were not able to select an answer so far. Is there anything not clear how to solve this? Please let us know. Commented Oct 5, 2013 at 20:43

3 Answers 3

4

The problem is the relative path. The __DIR__ uses the directory the file is in without a trailing separator. You are then changing the path with a relative path (/../) to go up one directory.

See the manual entry -> PHP Manual for __DIR__

__DIR__ will return C:\MyProject\phpunit-tesztek\include

You are looking to use the C:\MyProject\include\ path

__DIR__ . '/../../include/Form.php';

I normally use the dirname(__FILE__) myself, as it gets the current directory of the source code file (which is absolute) allowing my relative path to move from that location.

Sign up to request clarification or add additional context in comments.

2 Comments

dirname(__FILE__) is the outdated way to write exactly __DIR__ which was introduced in PHP 5.3 and not available earlier. It btw. was introduced to spare dirname() calls. Just FYI :)
@hakre Thanks for the FYI. I have references to the old, as 5.2 was still in use in areas, and for some of the team, it is a little clearer that you are getting the directory of what is passed. While DIR is the script's directory, you can use dirname() on another variable to retrieve its path as well.\
0

Try removing ../

require_once(DIR.'include/form.php');

Comments

0

Use magic constant DIR :

require_once(__DIR__ . '/../include/form.php');

1 Comment

sorry, seems like SO text parser issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.