Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 512
Michael Stokes Avatar asked Jun 27 '16 17:06

Michael Stokes


1 Answers

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.

like image 64
Katie Avatar answered Oct 11 '22 01:10

Katie