I have the following problem while inserting phpunit inside ci/cd pipeline. The idea behind phpunit that I am using test database with fixtures, and I am deleting content inside tables between every single test, which absolutely works. Now since I am want to reproduce the same process inside ci/cd pipeline, I get this error message, that it is not possible to connect to the database.
$ php bin/console doctrine:database:drop --force --if-exists --env=test --connection=default
99In ExceptionConverter.php line 103:
100
101 An exception occurred in the driver: SQLSTATE[HY000] [2002] php_network_get
102 addresses: getaddrinfo for db failed: Name or service not known
103
104In Exception.php line 28:
105
106 SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for db failed:
107 Name or service not known
108
109In Driver.php line 28:
110
111 SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for db failed:
112 Name or service not known
113
114In Driver.php line 28:
115
116 PDO::__construct(): php_network_getaddresses: getaddrinfo for db failed: Na
117 me or service not known
My .env.test
# define your env variables for the test env here
KERNEL_CLASS='App\Kernel'
APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots
# .env.test.local
DATABASE_URL="mysql://db:db@db:3306/testdb?serverVersion=mariadb-10.3.11&charset=utf8mb4"
CI/CD Pipeline (Gitlab)
phpunit:
stage: test
dependencies:
- composer
services:
- name: mariadb:10.3.11
alias: mysql
variables:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: testdb
MYSQL_USER: root
MYSQL_PASSWORD: root
DATABASE_URL: mysql://root:root@db:3306/testdb?serverVersion=mariadb-10.3.11&charset=utf8mb4
before_script:
- php bin/console doctrine:database:drop --force --if-exists --env=test --connection=default
- php bin/console cache:clear --env=test
- php bin/console doctrine:database:create --env=test
- php bin/console doctrine:migrations:migrate --env=test
- php bin/console doctrine:fixtures:load
script:
- mkdir $TEST_DIR
- vendor/bin/phpunit tests/ServiceTest --coverage-text --log-junit $TEST_DIR/junit.xml --coverage-clover $TEST_DIR/clover.xml
cache:
key: $CACHE_KEY
paths:
- ./vendor/
Snippet about testing that I did so far
class CartServiceTest extends WebTestCase
{
public EntityManager $entityManager;
public KernelBrowser $client;
public Cart $cart;
public User $user;
public function setUp(): void
{
$this->client = static::createClient();
$this->entityManager = $this->client->getContainer()
->get('doctrine')
->getManager();
$userFixture = new UserFixtures();
$user = $userFixture->load($this->entityManager);
$productFixture = new ProductFixtures();
$product = $productFixture->load($this->entityManager);
$this->loginUser();
}
public function tearDown(): void
{
self::ensureKernelShutdown();
$purger = new ORMPurger($this->entityManager);
$purger->purge();
}
/**
* Login User for Testing
*
* @return void
*/
public function loginUser(): void
{
//Login User
$userRepository = static::getContainer()->get(UserRepository::class);
$this->user = $userRepository->findOneBy(['email' => 'testmail@gmail.com']);
$this->client->loginUser($this->user);
}
//************************//
//****** TESTING ******* //
//**********************//
/**
* Test getCart()
*
* @return void
*/
public function testgetCart(): void
{
//Create Cart (Method is going to be tested especially)
$createCart = static::getContainer()->get(CartService::class)->getOrCreateCart();
//Get Cart by User ID
$cart = static::getContainer()->get(CartService::class)->getCart();
//Customer
$this->assertIsObject($this->user);
$this->assertIsInt($this->user->getId());
$this->assertEquals('testmail@gmail.com', $this->user->getEmail());
$this->assertEquals('Test', $this->user->getFirstName());
$this->assertEquals('User', $this->user->getLastName());
$this->assertIsString($this->user->getFirstName());
$this->assertIsString($this->user->getLastName());
//Cart
$this->assertIsInt($cart->getId());
$this->assertIsObject($cart);
$this->assertObjectHasAttribute('id', $cart);
$this->assertObjectHasAttribute('customer', $cart);
$this->assertObjectHasAttribute('totalprice', $cart);
}
The project is running on ddev(docker) machine, with Symfony 5.4