Questions regarding unit testing on custom modules with phpunit in AsgardCMS v.2.0
-
So the thing is I can't figure out how what I'm missing when I try to create tests for a new custom module I've created and called News. This module has been created with the Asgard's builtin module scaffolder.
My experience with unit testing is quite limited, I've been able to do some basic tests, but perhaps I feel a bit overwhelmed when the application grows. I have time for the project I'm working on, and I'd really like to run unit testing in order to improve stability as well as the rest of benefits that has this kind of development.
Said that, let's explain what's going on:
All suite of tests passes, except when it's the turn of my
News
module, where I get this error:Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: news__news_translations (SQL: select * from "news__news_translations" where "news__news_translations"."news_id" is null and "news__news_translations"."news_id" is not null)
I've used the builtin
BasePageTest
from thePage
module as a base to do mine (I'd use theBlog
module tests, but seems they're slighty different of the rest of the tests on v2) , so the code of myNewsBaseTest
is as following:<?php namespace Modules\News\Tests; use Illuminate\Contracts\Console\Kernel; use Illuminate\Database\Eloquent\Model; use Illuminate\View\ViewServiceProvider; use Mcamara\LaravelLocalization\Facades\LaravelLocalization; use Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider; use Modules\Core\Providers\CoreServiceProvider; use Modules\News\Providers\NewsServiceProvider; use Modules\News\Repositories\NewsRepository; use Nwidart\Modules\LaravelModulesServiceProvider; use Orchestra\Testbench\TestCase; abstract class BaseNewsTest extends TestCase { protected $news; public function setUp() { parent::setUp(); $this->resetDatabase(); $this->news = app(NewsRepository::class); } protected function getPackageProviders($app) { return [ LaravelModulesServiceProvider::class, CoreServiceProvider::class, ViewServiceProvider::class, NewsServiceProvider::class, LaravelLocalizationServiceProvider::class, ]; } protected function getPackageAliases($app) { return [ 'Eloquent' => Model::class, 'LaravelLocalization' => LaravelLocalization::class, ]; } protected function getEnvironmentSetUp($app) { $app['path.base'] = __DIR__ . '/..'; $app['config']->set('database.default', 'sqlite'); $app['config']->set('database.connections.sqlite', array( 'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '', )); $app['config']->set('translatable.locales', ['en', 'fr']); } private function resetDatabase() { // Relative to the testbench app folder: vendors/orchestra/testbench/src/fixture $migrationsPath = 'Database/Migrations'; $artisan = $this->app->make(Kernel::class); // Makes sure the migrations table is created $artisan->call('migrate', [ '--database' => 'sqlite', ]); // We empty all tables $artisan->call('migrate:reset', [ '--database' => 'sqlite', ]); // Migrate $artisan->call('migrate', [ '--database' => 'sqlite', ]); $artisan->call('migrate', [ '--database' => 'sqlite', '--path' => 'Modules/News/Database/Migrations', ]); } }
I'm sure I'm missing something, as to migrate all the tables in my module (both
news__news
andnews__news_translations
), but I assume (perhaps wrongly) that this piece of code at the end:$artisan->call('migrate', [ '--database' => 'sqlite', '--path' => 'Modules/News/Database/Migrations', ]);
Is this piece of code above doing the thing I'm expecting?
What I'm missing?
-
I would edit some typos on the post, but Akismet is flagging it as spam... wtf...?
-
OK, got it, thanks to @nWidart .
The test complained about tables that haven't been created. So, the function
resetDatabase()
was doing nothing.
In order to put the functionresetDatabase()
to work, you have to tell the module's service provider where to load the migrations from.Thus, this gets solved by adding:
$this->loadMigrationsFrom(__DIR__.'/../Database/Migrations');
into your module service provider'sboot()
method.