Summer Sale! All accounts are 50% off this week.

GJV's avatar
Level 1

PHPUnit Testing

I have to create phpunit tests for a laravel project. As of now, I have to test the repository classes which contain database queries. The project has no tests folder and no phpunit.xml file so I've just copied those files from laravel's github. The project is using oracle database and it is deployed on AWS. When running the feature test, which bootstraps the laravel app, do I need to be connected to the project's database?

0 likes
15 replies
tykus's avatar

do I need to be connected to the project's database

No, connecting to the project's database will have the side-effect of adding, modifying and/or deleting records in the project's database; which would be inconvenient in local development, but chaotic in production! You should have a separate test database which will allow you to work independently of the real data.

You can use Database Seeders and/or Model Factories to populate the database with the data necessary to run each test example. The RefreshDatabase trait will automatically handle any migrations/transactions necessary to ensure each test runs independently; ensuring data from previous test example(s) do not affect the currently running test.

1 like
GJV's avatar
Level 1

Thank you. I have already configured the phpunit.xml to use sqlite for testing but when running a test that extends Tests\Testcase would result to this error: Yajra\Pdo\Oci8\Exceptions\Oci8Exception: ORA-12170: TNS:Connect timeout occurred

GJV's avatar
Level 1

So I'm confused why am I getting an oracle error when I have already set the testing db to sqlite. dump(getenv('DB_CONNECTION')); would even show "sqlite"

tykus's avatar

@GJV can you share the phpunit.xml file where you have configured the SQLite database?

GJV's avatar
Level 1

@tykus I've also checked the config/database.php and the sqlite connection is already there.

tykus's avatar

@GJV okay; that should be enough to use the SQLite database.

Is there anywhere in your application code that expressly uses the Oracle connection; e.g. your Model sets a $connection property; or a query that fully qualifies the table name using the Oracle connection?

GJV's avatar
Level 1

@tykus upon checking, yes there is. the Model has a $connection variable which was set to an oracle connection in its constructor.

tykus's avatar

@GJV well, there's your problem with the attempted connection to your Oracle database. Is the explicit $connection needed?

GJV's avatar
Level 1

@tykus I guess it is needed. I don't have the rights to change anything in the code though. All I have to do is to create the tests. Is there a workaround for this?

tykus's avatar

@GJV you could replace the config for the specified connection in the Test setup, so that it points to another connection.

Sinnbeck's avatar

@GJV Ask whoever wrote that to allow you to move it to a method instead

public function getConnectionName()
    {
        return app()->environment('testing') ? 'testing' : 'oracle';
    }
GJV's avatar
Level 1

@tykus thank you for your inputs. I think I was able to get rid of the error relating to oracle. I now have another error with migrations.

tykus's avatar

@GJV no worries, mark the thread solved if you’re all set.

The migration issue might be Oracle-specific DDL being run against SQLite; you might need an Oracle test database?

GJV's avatar
Level 1

I'm doing a database integration / functional test . I am testing an update function that uses a Model which has a defined db connection that is different from the default connection. How can I rollback changes for model's connection? I have tried using DatabaseTransaction trait and created an array of connection like this: https://github.com/laravel/framework/issues/10873#issuecomment-289348849. I have also tried this: https://laracasts.com/discuss/channels/laravel/transactions-with-multiple-database-connections. But they both have the same result, the data wont be updated.

Please or to participate in this conversation.