-
Notifications
You must be signed in to change notification settings - Fork 2k
Description
PHP Version
8.2
CodeIgniter4 Version
4.4.2
CodeIgniter4 Installation Method
Composer (using codeigniter4/appstarter)
Which operating systems have you tested for this bug?
Windows
Which server did you use?
apache
Database
MariaDB 10.4.18
What happened?
When I try to run unit tests on models declared in the App namespace, the models only reference the default database and not the test database.
And when I try to comment the default database, I get the following error:
CodeIgniter\Database\Exceptions\DatabaseException: Unable to connect to the database.
Main connection [MySQLi]: Access denied for user ''@'localhost' (using password: NO)
Same thing happens even when the environment is set as TESTING, i.e. CI_ENVIRONMENT = testing in the .env file.
Steps to Reproduce
-
Install Shield to a fresh CodeIgniter installation:
composer require codeigniter4/shield. This is just to confirm that migrations run in the appropriate (test) database. -
Create a model (eg. SampleModel.php) via command line:
php spark make:model sample --suffix --table sample_table -
Create a migration for the model:
php spark make:migration create_sample_tableand add fields for theup()method like so:
$this->forge->addField([
'id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
'name' => ['type' => 'varchar', 'constraint' => 300],
'description' => ['type' => 'text', 'null' => true],
'created_at' => ['type' => 'datetime'],
'updated_at' => ['type' => 'datetime', 'null' => true],
'deleted_at' => ['type' => 'datetime', 'null' => true],
]);
$this->forge->addPrimaryKey('id');
$this->forge->createTable('sample_table');Don't forget to drop the table in the down() method: $this->forge->dropTable('sample_table');
- Create a test for the model: test/app/Models/SampleModelTest.php with the following code:
declare(strict_types=1);
namespace Tests\App\Models;
use App\Models\SampleModel;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
final class SampleModelTest extends CIUnitTestCase
{
use DatabaseTestTrait;
protected $migrate = true;
protected $migrateOnce = true;
protected $namespace = null;
protected $refresh = true;
public function testSampleModelCanSaveData() : void
{
$model = model(SampleModel::class);
$result = $model->save([
'name' => 'asample',
'description' => 'Lorem ipsum dolor'
]);
$this->assertTrue($result);
$this->seeInDatabase('sample_table', [
'name' => 'asample'
]);
}
}- Create your default and test databases and add the records in your
.envfile:
# database.default.hostname = localhost
# database.default.database = ci4_app
# database.default.username = root
# database.default.password =
# database.default.DBDriver = MySQLi
# database.default.DBPrefix =
# database.default.port = 3306
database.tests.hostname = localhost
database.tests.database = ci4_test
database.tests.username = root
database.tests.password =
database.tests.DBDriver = MySQLi
database.tests.DBPrefix =
database.tests.port = 3306- Comment the default database and run the test:
vendor\bin\phpunit
Expected Output
I expected that the test would run without any error, as models in the App namespace are not supposed to be limited to the default database.
Anything else?
Commenting the $DBGroup property of the model class:
// protected $DBGroup = 'default';`seemed to resolve the issue, as pointed out by @datamweb which makes me to wonder why the 'default' table was hardcoded in the first place.