I have a problem for a week, my tests can not connect with the database.
Indeed I have passed my environment under docker, my database connects perfectly but not the tests. Do you know why?
My errors :
[critical] Uncaught PHP Exception Doctrine\DBAL\Exception\ConnectionException: "An exception occurred in the driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for database failed: Name or service not known" at /opt/project/vendor/doctrine/dbal/src/Driver/API/MySQL/ExceptionConverter.php line 103
Here are my files:
docker-compose.yml
version: '3.7'
services:
database:
image: 'mysql:5.7'
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: app
ports:
- '3306:3306'
volumes:
- db-data:/var/lib/mysql/data:rw
app:
image: peedro07/app:latest
ports:
- "8080:80"
environment:
DATABASE_URL: mysql://root:root#database:3306/app
volumes:
db-data:
My Dockerfile which is build with the docker build command . -f chemin/Dockerfile -t peedro07/app
FROM php:8.1-apache
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod +x /usr/local/bin/install-php-extensions && \
install-php-extensions pdo_mysql intl
RUN curl -sSk https://getcomposer.org/installer | php -- --disable-tls && \
mv composer.phar /usr/local/bin/composer
COPY . /var/www/
COPY ./docker/php/apache.conf /etc/apache2/sites-available/000-default.conf
RUN cd /var/www && \
composer install
WORKDIR /var/www/
#ENTRYPOINT ["bash", "./docker/docker.sh"]
EXPOSE 80
My phpunit.xml.dist
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="tests/bootstrap.php"
convertDeprecationsToExceptions="false"
>
<php>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
<ini name="display_errors" value="1"/>
<ini name="error_reporting" value="-1"/>
<server name="APP_ENV" value="test" force="true"/>
<server name="SHELL_VERBOSITY" value="-1"/>
<server name="SYMFONY_PHPUNIT_REMOVE" value=""/>
<server name="SYMFONY_PHPUNIT_VERSION" value="9.5"/>
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>
<!-- Run `composer require symfony/panther` before enabling this extension -->
<!--
<extensions>
<extension class="Symfony\Component\Panther\ServerExtension" />
</extensions>
-->
<extensions>
<extension class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension"/>
</extensions>
</phpunit>
In my .env :
DATABASE_URL="mysql://root:root#database:3306/app"
In my env.test
KERNEL_CLASS='App\Kernel'
APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots
DATABASE_URL="mysql://root:root#database:3306/app"
Symfony is usually adding a suffix to the database name in the test environment.
You can verify that with the php bin/console debug:config doctrine --env=test command and look for the doctrine.dbal.connections.dbname_suffix config.
Or you can look in one of these files depending on which version of Symfony you are using.
Older:
config/packages/test/doctrine.yaml
doctrine:
dbal:
# "TEST_TOKEN" is typically set by ParaTest
dbname: 'main_test%env(default::TEST_TOKEN)%'
Newer:
config/packages/doctrine.yaml
when#test:
doctrine:
dbal:
# "TEST_TOKEN" is typically set by ParaTest
dbname_suffix: '_test%env(default::TEST_TOKEN)%'
This config is useful to make sure you are not testing on "real data" aka corrupting the prod database.
And the token might be useful should you have parallel testing.
What I would usually do is:
Create the tests database with php bin/console doctrine:database:create --env=test
Make sure my tests load fixtures to have a predictable dataset between each test
Run my tests
Thank you for your response. It was simply a docker problem. I had to add a volume to my service app. I then killed my containers, and restarted docker.
After reading several documentations, I also noticed that the mysql 5.7 image had some configuration problems, so I switched to the mariadb image which works perfectly with mysql (even if I find this behavior a bit weird ...)
Thanks to all :)
My new docker-compose.yml :
database:
image: 'mariadb'
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: app
command: ["mysqld", "--ignore-db-dir=lost+found", "--explicit_defaults_for_timestamp"]
restart: always
ports:
- '3306:3306'
volumes:
- db-data:/var/lib/mysql/data:rw
app:
image: peedro07/app:latest
restart: always
ports:
- "8080:80"
environment:
DATABASE_URL: mysql://root:root#database:3306/app
volumes:
- app-data:/var/www
I want to run my unit tests through my yml file, but they fail because they must be run on Mysql
I think something is missing.
here is yaml file:
steps:
- uses: shivammathur/setup-php#15c43e89cdef867065b0213be354c2841860869e
with:
php-version: '7.4|8.0'
- uses: actions/checkout#v2
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Optimize Project
run: php artisan optimize:clear
- name: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: mysql
DB_HOST: 127.0.0.1
DB_PORT: 3306
DB_DATABASE: database_name
DB_USERNAME: root
DB_PASSWORD:
run: |
php artisan migrate
php artisan test
and here is phpunit.xml file:
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<!-- <server name="DB_CONNECTION" value="sqlite"/> -->
<!-- <server name="DB_DATABASE" value=":memory:"/> -->
<server name="MAIL_MAILER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="TELESCOPE_ENABLED" value="false"/>
This example shows an example of adding MySQL into into your GitHub action, so the database is available:
Your GitHub actions yaml needs a services section:
services:
# mysql-service Label used to access the service container
mysql:
# Docker Hub image (also with version)
image: mysql:5.7
env:
## Accessing to Github secrets, where you can store your configuration
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test_db
## map the "external" 3306 port with the "internal" 3306
ports:
- 3306:3306
# Set health checks to wait until mysql database has started (it takes some seconds to start)
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3
The env you can set with your GH Actions command that needs database access can include:
env:
DB_CONNECTION: mysql
DB_DATABASE: db_test
DB_USER: root
DB_PASSWORD: root
You may want to check out Chipper CI, which does a lot of this work for you for testing Laravel apps!
I have the following phpunit test:
namespace Test\Database\Integration\Repositories;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Repositories\RoverRepository;
use App\Model\Rover;
use App\Model\Grid;
class RoverRepositoryTest extends TestCase
{
use RefreshDatabase;
/**
* Undocumented variable
*
* #var RoverRepository|null
*/
private $repository=null;
/**
* Undocumented variable
*
* #var Grid|null;
*/
private $grid=null;
public function setUp(): void
{
parent::setUp();
$this->runDatabaseMigrations();
$grid=factory(Grid::class)->create([
'width'=>5,
'height'=>5
]);
$rover=factory(Rover::class, 5)->create([
'grid_id' => $grid->id,
'grid_pos_x' => rand(0, $grid->width),
'grid_pos_y' => rand(0, $grid->height),
]);
$this->grid=$grid;
//How do I run Migrations and generate the db?
$this->repository = new RoverRepository();
}
public function tearDown(): void
{
parent::tearDown();
//How do I nuke my Database?
}
/**
* Testing Base Search
*
* #return void
*/
public function testBasicSearch(): void
{
$this->assertTrue(true);
}
}
And my phpunit.xml is set as:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Database">
<directory suffix="Test.php">./tests/Database</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="MAIL_DRIVER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DATABASE_URL" value="localhost"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
But for some reason when I launch my test with the command:
1) Test\Database\Integration\Repositories\RoverRepositoryTest::testBasicSearch
Illuminate\Database\QueryException: SQLSTATE[08006] [7] FATAL: password authentication failed for user "laravel" (SQL: select tablename from pg_catalog.pg_tables where schemaname = 'public')
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:624
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:333
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Schema/PostgresBuilder.php:108
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Schema/PostgresBuilder.php:35
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/FreshCommand.php:79
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/FreshCommand.php:46
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:32
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:90
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:34
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:576
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php:183
/var/www/html/vendor/symfony/console/Command/Command.php:255
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php:170
/var/www/html/vendor/symfony/console/Application.php:921
/var/www/html/vendor/symfony/console/Application.php:273
/var/www/html/vendor/symfony/console/Application.php:149
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Application.php:90
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Application.php:182
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:275
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/PendingCommand.php:136
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/PendingCommand.php:220
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php:56
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/RefreshDatabase.php:55
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/RefreshDatabase.php:18
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:105
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:72
/var/www/html/tests/Database/Integration/Repositories/RoverRepositoryTest.php:31
Caused by
PDOException: SQLSTATE[08006] [7] FATAL: password authentication failed for user "laravel"
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:46
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connectors/PostgresConnector.php:33
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php:182
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:918
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:943
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:399
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:325
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:657
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:624
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:333
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Schema/PostgresBuilder.php:108
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Schema/PostgresBuilder.php:35
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/FreshCommand.php:79
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/FreshCommand.php:46
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:32
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:90
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:34
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:576
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php:183
/var/www/html/vendor/symfony/console/Command/Command.php:255
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php:170
/var/www/html/vendor/symfony/console/Application.php:921
/var/www/html/vendor/symfony/console/Application.php:273
/var/www/html/vendor/symfony/console/Application.php:149
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Application.php:90
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Application.php:182
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:275
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/PendingCommand.php:136
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/PendingCommand.php:220
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php:56
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/RefreshDatabase.php:55
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/RefreshDatabase.php:18
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:105
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:72
/var/www/html/tests/Database/Integration/Repositories/RoverRepositoryTest.php:31
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
For the production setup I use the following .env file for thw database:
DB_CONNECTION=pgsql
DB_HOST=postgresql
DB_PORT=5432
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=IDontTellYouThisIsNotAnActuallPassword
SO I have theese 2 questions:
Why my settings for sqlite are being ingored?
How do I enforce to use the inmemory sqlite when testing?
Edit 1
I made a file named .env.testing:
DB_CONNECTION=sqlite
DATABASE_URL=localhost
DB_DATABASE=:memory:
And changed my phpunit.xml into:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Database">
<directory suffix="Test.php">./tests/Database</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="MAIL_DRIVER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
</php>
</phpunit>
Also the settings for sqlite in laravel's are:
'sqlite' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
Edit 2
I tried as suggested:
namespace Test\Database\Integration\Repositories;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use App\Repositories\RoverRepository;
use App\Model\Rover;
use App\Model\Grid;
class RoverRepositoryTest extends TestCase
{
use RefreshDatabase;
use DatabaseMigrations;
/**
* Undocumented variable
*
* #var RoverRepository|null
*/
private $repository=null;
/**
* Undocumented variable
*
* #var Grid|null;
*/
private $grid=null;
public function setUp(): void
{
parent::setUp();
$this->runDatabaseMigrations();
$grid=factory(Grid::class)->create([
'width'=>5,
'height'=>5
]);
$rover=factory(Rover::class, 5)->create([
'grid_id' => $grid->id,
'grid_pos_x' => rand(0, $grid->width),
'grid_pos_y' => rand(0, $grid->height),
]);
$this->grid=$grid;
//How do I run Migrations and generate the db?
$this->repository = new RoverRepository();
}
public function tearDown(): void
{
parent::tearDown();
//How do I nuke my Database?
}
/**
* Testing Base Search
*
* #return void
*/
public function testBasicSearch(): void
{
$this->assertTrue(true);
}
}
And then I run the following commands:
php artisan config:clear
php artisan config:clear --env=testing
php artisan config:clear --env=development
php artisan config:cache
Still the error continues.
Edit 3
After lots of testing I empty the .env file. Then I run the command:
php artisan config:cache
And the error has changed into:
PHPUnit 7.5.13 by Sebastian Bergmann and contributors.
E 1 / 1 (100%)
Time: 111 ms, Memory: 18.00 MB
There was 1 error:
1) Test\Database\Integration\Repositories\RoverRepositoryTest::testBasicSearch
Mockery\Exception\BadMethodCallException: Received Mockery_1_Illuminate_Console_OutputStyle::askQuestion(), but no expectations were specified
/var/www/html/vendor/symfony/console/Style/SymfonyStyle.php:222
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php:332
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/ConfirmableTrait.php:31
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/FreshCommand.php:34
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:32
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:90
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:34
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:576
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php:183
/var/www/html/vendor/symfony/console/Command/Command.php:255
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php:170
/var/www/html/vendor/symfony/console/Application.php:921
/var/www/html/vendor/symfony/console/Application.php:273
/var/www/html/vendor/symfony/console/Application.php:149
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Application.php:90
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Application.php:182
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:275
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/PendingCommand.php:136
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/PendingCommand.php:220
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php:56
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/RefreshDatabase.php:55
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/RefreshDatabase.php:18
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:105
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:72
Also if I keep only the sqlite settings and restore into the original .env I get the error:
PHPUnit 7.5.13 by Sebastian Bergmann and contributors.
E 1 / 1 (100%)
Time: 115 ms, Memory: 18.00 MB
There was 1 error:
1) Test\Database\Integration\Repositories\RoverRepositoryTest::testBasicSearch
InvalidArgumentException: Database [pgsql] not configured.
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:152
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:115
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:86
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/FreshCommand.php:77
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/FreshCommand.php:46
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:32
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:90
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:34
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:576
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php:183
/var/www/html/vendor/symfony/console/Command/Command.php:255
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php:170
/var/www/html/vendor/symfony/console/Application.php:921
/var/www/html/vendor/symfony/console/Application.php:273
/var/www/html/vendor/symfony/console/Application.php:149
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Application.php:90
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Application.php:182
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:275
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/PendingCommand.php:136
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/PendingCommand.php:220
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php:56
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/DatabaseMigrations.php:16
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:109
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:72
Meaning that the .env.testing is being completely ignored if .env is provided.
When implementing the SQLite database for the testing, you do not need to specify the database URL since it is running in the memory.
Remove the line from phpunit.xml
<env name="DATABASE_URL" value="localhost"/>
Your config might be caching. Run the command
php artisan config:clear
Now re-run the test.
Remove the code from the setUp() function
$this->runDatabaseMigrations();
To migrate the database in the SQLite, you can use
use Illuminate\Foundation\Testing\DatabaseMigrations;
Edit: For config/database.php
For using SQLite, you need to configure in the database.php.
//config/database.php
...
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path('database/database.sqlite'),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
...
it seems like your tests are still using the sql database.
when doing tests using an SQLite database, I suggest that you verify these things:
your database.sqlite must be created in the database folder and don't forget to adjust the file rights.
you must include these 2 traits:
// this trait is used to wrap every query in a transaction where this one will be deleted at the end of the test
use DatabaseTransactions;
// this one allow Laravel to prepare the database
use DatabaseMigrations;
if it's still not working, i suggest that you stop your server, execute the current artisan command php artisan config:clear and restart it.
EDIT
i just saw that your phpunit.xml contains some error, delete all the env tags and add only these:
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="MAIL_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
creating a .env.testing is not needed
Make sure your .env file has the same password as your database.
I faced the same problem.
I searched and see this topic.
and I wanna put the answer for future search.
I tried many things and after a while, I understand on my new linux OS, I didn't install the sqlite driver.
sudo apt-get install php-sqlite
on windows, just uncomment the SQLite extension in php.ini by removing the simicolon ; before that line.
so the problem solved simply.
Do you use any enviromental variable with the VERY SAME NAME with the one specified in .env? Sometimes theese can cause a huge conflict with the ones specified in .env ovveriding them.
In your case it may be this case either via useing Homestead or docker with docker-compose, in theese cases is good idea to set a unique prefix identifying the use of it. For example insteat of using names such as DB_CONNECTION name it DOCKER_DB_CONNECTION hence no conflict at all.
Also in case of a docker-compose this is a bad idea:
version: '3.1'
services:
develop:
image: ddesyllas/php-dev:dev-n-build
volumes:
- ".:/var/www/html"
- "./.laravel.env:/var/www/html/.env"
links:
- memcache
environment:
DB_CONNECTION: pgsql
DB_HOST : postgresql
DB_PORT : 5432
DB_DATABASE: ${DOCKER_POSTGRES_DB}
DB_USERNAME: ${DOCKER_POSTGRES_USER}
DB_PASSWORD: ${DOCKER_POSTGRES_PASSWORD}
nginx:
image: nginx:alpine
ports:
- 7880:7880
links:
- "develop:develop"
volumes:
- ".:/var/www/html"
- "./docker/nginx.conf:/etc/nginx/nginx.conf:ro"
postgresql:
image: postgres:alpine
volumes:
- './docker/misc_volumes/postgresql:/var/lib/postgresql/data'
environment:
POSTGRES_USER: ${DOCKER_POSTGRES_USER}
POSTGRES_DB: ${DOCKER_POSTGRES_DB}
POSTGRES_PASSWORD: ${DOCKER_POSTGRES_PASSWORD}
memcache:
image: memcached:alpine
Instead use enviromental variable names as specified in the following docker-compose.yml is a good idea:
version: '3.1'
services:
develop:
image: ddesyllas/php-dev:dev-n-build
volumes:
- ".:/var/www/html"
- "./.laravel.env:/var/www/html/.env"
links:
- memcache
environment:
DOCKER_DB_CONNECTION: pgsql
DOCKER_DB_HOST : postgresql
DOCKER_DB_PORT : 5432
DOCKER_DB_DATABASE: ${DOCKER_POSTGRES_DB}
DOCKER_DB_USERNAME: ${DOCKER_POSTGRES_USER}
DOCKER_DB_PASSWORD: ${DOCKER_POSTGRES_PASSWORD}
nginx:
image: nginx:alpine
ports:
- 7880:7880
links:
- "develop:develop"
volumes:
- ".:/var/www/html"
- "./docker/nginx.conf:/etc/nginx/nginx.conf:ro"
postgresql:
image: postgres:alpine
volumes:
- './docker/misc_volumes/postgresql:/var/lib/postgresql/data'
environment:
POSTGRES_USER: ${DOCKER_POSTGRES_USER}
POSTGRES_DB: ${DOCKER_POSTGRES_DB}
POSTGRES_PASSWORD: ${DOCKER_POSTGRES_PASSWORD}
memcache:
image: memcached:alpine
Do you noticed that I renamed the enviromental variables like this:
DB_CONNECTION => DOCKER_DB_CONNECTION
DB_HOST => DOCKER_DB_HOST
DB_PORT => DOCKER_DB_PORT
DB_DATABASE => DOCKER_DB_DATABASE
DB_USERNAME => DOCKER_DB_USERNAME
DB_PASSWORD => DOCKER_DB_PASSWORD
So i am creating a new symfony 4 project but i cant get mysql to connect to my docker mysql. what part am i doing wrong?
Ive tried change mysql versions, composing it down and updating different mysql root and password and is not connecting.
here is my docker-compose.yml for mysql.
mysql:
image: mysql:8
container_name: sf4_mysql
volumes:
- .docker/data/db:/var/lib/mysql
command:
- "--default-authentication-plugin=mysql_native_password"
- "--lower_case_table_names=1"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
Here is mysql Url in my .env
DATABASE_URL=mysql://sf4:sf4#sf4_mysql:3306/sf4
every-time i do ./bin/console doctrine:database:create, connection is refused.
just trying to create a database.
controller
class HospitalAdminController extends AbstractController
{
/**
* #Route("/admin/hospital/new")
*/
public function new(EntityManagerInterface $em)
{
$hospital = new Hospital();
$hospital->setName('Example Hospital')
->setPhone(8175831483)
->setAddress('123 Avenue');
$em->persist($hospital);
$em->flush();
return new Response(sprintf(
'Hiya! New Hospital id: #%d phone:%s address%s',
$hospital->getId(),
$hospital->getPhone(),
$hospital->getAddress()
));
}
You need to expose mysql port. default is 3306.
see port publishing
and more examples specific to mysql-8 on docker
For checking if mysql is running it is good to run inside terminal its client so you minimize potential error source to the database itself not including potential errors in php.
Laravel v5.5.14
PHPUnit v6.4.1
I have a Laravel application with a number of feature tests. My base TestCase uses the RefreshDatabase trait for triggering a DB refresh before each test, and the env variables in my phpunit.xml looks like so;
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
</php>
The intention being to leave the development mysql DB connection alone and run the tests using sqlite in memory. Despite these settings (and clearing cached config etc) the DB refresh always refreshes the mysql connection DB.
I've looked into the functionality within RefreshDatabase and debugged the refreshDatabase method, which contians;
$this->usingInMemoryDatabase()
? $this->refreshInMemoryDatabase()
: $this->refreshTestDatabase();
And usingInMemoryDatabase always returns false. Looking at the content of that method, when retrieving the DB_CONNECTION from config it always returns mysql, even though it should be being overridden by the env variable above.
What is going on here?
Run php artisan config:clear before running the tests to clear the cache to make sure the config variables are not cached from a wrong environment. If this doesn't help run PHPUnit using your IDE (e.g PHPStorm) and specifically select your project phpunit.xml