Using Phpunit/ Why phpunit removes all data in my database - laravel

I'm trying to test my functions but everytime I try to run phpunit, it will delete all the records in my tables. I already setup my phpunit.xml file, to just in my test database, I just followed everything in the docs, do I have to setup a sqlite inmy machine, since I don't have that ? or what ? I only have laragon installed and phpmyadmin.
phpunit.xml file
<?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="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>

Be sure to use the DatabaseMigrations trait in your test classes. Eg.
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\RefreshDatabase;
class SomeTest extends TestCase
{
use DatabaseMigrations;
use RegreshDatabase;
/**
* A basic test example.
*
* #return void
*/
public function testSomething()
{
//some code
}
}
This migrates your data on setup, and then registers a direction to roll back the migrations when the application is torn down at the end of the test.

Related

Laravel DatabaseMigration not running migrations

I'm trying to create a very simple test, where it calls a service function that executes an ::all function in a model.
EDIT: As mentioned in a comment, I put it as a feature test, but still same issue:
namespace Tests\Feature;
use App\Models\FlashCard;
use App\Services\FlashCardService;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleTest extends TestCase
{
use DatabaseMigrations;
/**
* A basic test example.
*
* #return void
*/
public function test_the_application_returns_a_successful_response()
{
$flashCardService = new FlashCardService();
$flashCardService::getAll();
}
}
The getAll() function all it does is:
public static function getAll(): Collection {
return FlashCard::all(['id', 'question', 'answer', 'status']);
}
The problem is that I get an error that the is no such table
General error: 1 no such table: flash_cards so the DatabaseMigrations is not running.
My phpunit.xml looks like this
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
<php>
<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"/>
</php>
</phpunit>
Any ideas?

Apply phpunit.xml setting for speific classes only

I have the following setting in my phpunit.xml:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="Application Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
How can i tell to phpunit to use :memory: for certain test class and not for all classes ?
Unfortunately, PHPUnit is now unable to use custom environments for only some test suites. So you can't use the following code:
...
<testsuite name="Application Test Suite">
<directory suffix="Test.php">./tests</directory>
<php>
<env name="DB_DATABASE" value="example_db"/>
</php>
</testsuite>
...
So instead of setting the env DB_DATABASE via the phpunit.xml file, you can set it via the new .env file. Suppose you create a .env.first file that contains:
DB_DATABASE=first_db
Then in the phpunit.xml file, you can create a new <testsuite> which you will later target to use with the .env file you have created. As an example:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="Foo">
<directory prefix="FirstDB" suffix="Test.php">./tests</directory> <!-- Will match all files in `./tests` whose filename contains a FirstDB prefix and a Test.php suffix -->
</testsuite>
<testsuite name="Bar">
<directory prefix="SecondDB" suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value="second_db"/>
</php>
</phpunit>
Then for the next step you can run it with:
php artisan test --env=first --testsuite=Foo
php artisan test --testsuite=Bar

Got "Access denied for user" when set database memory in laravel test

I'm trying to implement testing for my laravel project.
This is my phpunit.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
<php>
<server name="APP_ENV" value="testing"/>
<server name="APP_KEY" value="base64:in7fPzfWpFUCvr48DCZvkNf5qzqI/6SqSLpJysihepE=" />
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="DB_CONNECTION" value="mysql"/>
<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"/>
</php>
</phpunit>
And I want to test database connect first, I've used RefreshDatabase trait to AutoRedirectTest (auto created when I creating project)
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Log;
use Tests\TestCase;
class AutoRedirectTest extends TestCase
{
use RefreshDatabase;
/**
* A basic test example.
*
* #return void
*/
public function test_home_redirect()
{
$response = $this->get('/');
$response->assertStatus(301);
$response->assertHeader('Location', '/nova');
}
}
Then when I run php artisan test, it throw error when testing AutoRedirectTest
SQLSTATE[HY000] [1044] Access denied for user 'apartment_management'#'%' to database ':memory:' (SQL: select * from information_schema.tables where table_schema = :memory: and table_name = migrations and table_type = 'BASE TABLE')
I've also tried to create new test file and add RefreshDatabase. I got the same error. But when I replace :memory: to my real database name. I've not got error above anymore.
<server name="DB_DATABASE" value="my_real_database_name"/>
So did I config something wrong?
I'm using laravel 8.61.0 with mysql 5.7.22 configured in docker.

ModelFactory: Error: Call to a member function connection() on null

While trying to populate database using ModelFactory I am coming across this bit of error, I have been looking up since the last six hours, can not seem to fix it.
My Test runs this:
$books = factory('App\Book', 2)->create();
and the error message is this:
There was 1 error:
1) Tests\App\Http\Controllers\BooksControllerTest::testIndexReturnsCollection
Error: Call to a member function connection() on null
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\Model.php:1234
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\Model.php:1200
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\Model.php:1030
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\Model.php:945
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\Model.php:983
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\FactoryBuilder.php:203
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\support\Collection.php:407
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\FactoryBuilder.php:207
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\FactoryBuilder.php:185
D:\Code\Server\htdocs\rlfp.localhost.com\tests\app\Http\Controllers\BooksControllerTest.php:35
On my bootstrap/app.php I have on line 26 and 28, like so:
$app->withFacades();
$app->withEloquent();
While my phpunit.xml is like so:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="bootstrap/app.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_DATABASE" value="rlp_rlfpsfactory"/>
</php>
</phpunit>
Do I need to put anything else in the php part of phpunit xml file?
After spending one whole day with no luck, thanks to #ChrisSprague I downgraded Lumen to 5.6 and it worked.
Steps:
In composer.json file change
"laravel/lumen-framework": "5.7.*"
to
"laravel/lumen-framework": "5.6.*"
Delete your vendor folder
Do composer install
Run the test again!
If it still does not work, make sure you have correct test database details in your phpunit.xml file
Mine looks like:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="bootstrap/app.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="DB_DATABASE" value="rlp_rlfpsfactory"/> <!-- this is testing database -->
<env name="DB_USERNAME" value="root"/>
<env name="DB_PASSWORD" value=""/>
</php>
</phpunit>

PHPUnit configuring and running a group test [Laravel 5.6]

I have this phpunit.xml with #somegroup:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="bootstrap/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Feature Tests">
<directory suffix="Test.php">/tests/Feature</directory>
</testsuite>
<testsuite name="Unit Tests">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<groups>
<include>
<group>somegroup</group>
</include>
</groups>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
</php>
</phpunit>
Then I have this method with #group tag:
/**
* #group somegroup
*/
public function testStatus()
{
$response = $this->get('/');
$response->assertStatus(200);
}
And then I run test group tag in command line, like this:
vendor\bin\phpunit --group somegroup
And I get this:
No tests executed!
Test command without #group tag works:
vendor\bin\phpunit
What am I doing wrong?
Is it Laravel that does not support #group?

Resources