user9022931
user9022931

Reputation:

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 :

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

Upvotes: 4

Views: 2515

Answers (1)

dominikweber
dominikweber

Reputation: 632

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.

Upvotes: 3

Related Questions