Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit No such file or directory

I'm trying to test my RoomRepository with PHPUnit and Symfony 4. I installed symfony/phpunit-bridge using composer. I created a simple entity called Room with one id and name attributs and a repository method to get a Room by its id.

public function get(int $id): ?Room
{
   /** @var Room $room */
   $room = $this->findOneBy(['id' => $id]);

   return $room;
}

My test is quite simple as you can see :

public function testGet(): void
{
    /** @var RoomRepository $repository */
    $repository = $this->em->getRepository(Room::class);

    $room = $repository->get(1);

    $this->assertCount(1, $room);
}

I am new with test and I don't know if it's the right way to proceed. I followed the Symfony documentation.

So, when I execute the following command :

./vendor/bin/simple-phpunit

I am getting this error :

Doctrine\DBAL\Exception\ConnectionException: An exception occurred in driver: SQLSTATE[HY000] [2002] No such file or directory

I am pretty sure this is a commun mistake and very easy to fix...

Furthermore, I wrote other simple asserts that worked very well. I don't think it's about PHPUnit configuration.

Here some informations about my env :

  • PHP 7.1
  • Symfony4.0.5
  • PHPUnit 5.7.27
  • Docker with Laradock (containers : mysql, apache2, workspace)

Thanks guys for reading my post and have a nice day :)


1 Answers

I had the same problem – I just forgot to add the database url to the phpunit.xml.dist file. You have to add:

<phpunit ...>
  <php>
    ...
    <env name="DATABASE_URL" value="mysql://username:password@server:port/database" />
    ...
  </php>
  ...
</phpunit>

Of course with your own credentials instead of the placeholders.

like image 155
dominikweber Avatar answered Oct 25 '25 13:10

dominikweber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!