Michael Stokes
Michael Stokes

Reputation: 401

PHPUnit error "Failed to open stream: No such file or directory"

I just installed PHPUnit Testing and for some reason it won't read my file. I'm sure it's in the correct path, but why PHPUnit can't find it?

Here is my sample code:

functions.php

<?php 

 function my_addition($arg1, $arg2){
        return $arg1 + $arg2;

 ?>

Here is the code and the file to test:

functions_test.php

<?php 


use PHPUnit\Framework\TestCase;




class functions_test extends TestCase {


public function testmy_addition() {
    include("./data/functions.php");
    $result = my_addition(1,1);
    $this->assertEquals(2, $result);

}


}


?>

What can I do to get it to work and make it pass?

Upvotes: 5

Views: 6355

Answers (1)

Katie
Katie

Reputation: 2693

You should be able to use __DIR__ . "/data/functions.php" in your include statement
(that's two underscores before and after).

I did a quick test in my test environment and had no problems.

The __DIR__ magic constant gives you the full path to the test file you are running. From the PHP website:

The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory.

Upvotes: 6

Related Questions