0

I try to implement some unit tests with phpUnit, on a project that uses Eloquent. The installation is made with composer for both frameworks. But when I try to implement a simple example test, it fails loading database.php

the example test

include("database.php");
class ArticleControllerTest extends TestCase
{


    public function testAccueil()
    {
        //récupère la date du jour
        $a = \app\model\Article::first();

        $this->assertInternalType("string", $a->titreGeneral);

    }
}

Database.php

<?php
require 'vendor/autoload.php';

use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Events\Dispatcher;

$capsule = new Capsule;

$capsule->addConnection(array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'spectacles',
    'username'  => 'root',
    'password'  => 'root',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => ''
));
$capsule->setEventDispatcher(new Dispatcher(new Container));
$capsule->setAsGlobal();

$capsule->bootEloquent();

error message with these script (the database is in the right folder)

PHP Warning:  include(../../database.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/spectacles/TestUnits/controllerTest/ArticleControllerTest.php on line 6

Warning: include(../../database.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/spectacles/TestUnits/controllerTest/ArticleControllerTest.php on line 6

PHP Warning:  include(): Failed opening '../../database.php' for inclusion (include_path='.:') in /Applications/MAMP/htdocs/spectacles/TestUnits/controllerTest/ArticleControllerTest.php on line 6

Warning: include(): Failed opening '../../database.php' for inclusion (include_path='.:') in /Applications/MAMP/htdocs/spectacles/TestUnits/controllerTest/ArticleControllerTest.php on line 6

And error messages when I try to put the code in database.php directly in the example test :

PDOException: SQLSTATE[HY000] [2002] No such file or directory

I don't think the problem comes prom the paths (they are the right paths). Did I miss something ?

Amanite Laurine
  • 1,149
  • 1
  • 11
  • 22
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Sep 07 '16 at 19:57

1 Answers1

0

PDOException: SQLSTATE[HY000] [2002] No such file or directory means the database could not connect.

The other errors are clearly telling you that the script could not include the database.php file.

The paths are not correct. You are not properly understanding how include() works. Read http://php.net/manual/en/function.include.php.

In a nutshell, the following code expects the database.php file to exist in the same directory as the file itself, or on the include path.

include("database.php");
class ArticleControllerTest extends TestCase
{
...

These may also help:

Community
  • 1
  • 1
Gerard Roche
  • 6,162
  • 4
  • 43
  • 69
  • There is more details on this problem and on how to avoid it here : http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew Sep 07 '16 at 19:57
  • Actually I gifured out the problem. I didn"t understand why the include wasn't working, cause database.php was in the same directory. Fanally I don't know why but PHPUnit searches the element included from the root of the project folder. And I solved the other problem adding this line 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock' in the database.php – Amanite Laurine Sep 08 '16 at 06:18