Skip to content

Commit 4359f87

Browse files
committed
Expand DB test case to accept multiple migrations & seeds
1 parent 31635a1 commit 4359f87

3 files changed

Lines changed: 110 additions & 20 deletions

File tree

system/Test/CIDatabaseTestCase.php

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
*/
5353
class CIDatabaseTestCase extends CIUnitTestCase
5454
{
55-
5655
/**
5756
* Should the db be refreshed before
5857
* each test?
@@ -62,25 +61,28 @@ class CIDatabaseTestCase extends CIUnitTestCase
6261
protected $refresh = true;
6362

6463
/**
65-
* The name of the fixture used for all tests
66-
* within this test case.
64+
* The seed file(s) used for all tests within this test case.
65+
* Should be fully-namespaced or relative to $basePath
6766
*
68-
* @var string
67+
* @var string|array
6968
*/
7069
protected $seed = '';
7170

7271
/**
73-
* The path to where we can find the seeds directory.
72+
* The path to the seeds directory.
7473
* Allows overriding the default application directories.
7574
*
7675
* @var string
7776
*/
78-
protected $basePath = TESTPATH . '_support/Database';
77+
protected $basePath = SUPPORTPATH . 'Database';
7978

8079
/**
81-
* The namespace to help us find the migration classes.
80+
* The namespace(s) to help us find the migration classes.
81+
* Empty is equivalent to running `spark migrate -all`.
82+
* Note that running "all" runs migrations in date order,
83+
* but specifying namespaces runs them in namespace order (then date)
8284
*
83-
* @var string
85+
* @var string|array|null
8486
*/
8587
protected $namespace = 'Tests\Support';
8688

@@ -163,19 +165,10 @@ protected function setUp(): void
163165
{
164166
parent::setUp();
165167

166-
// Add namespaces we need for testing
167-
Services::autoloader()->addNamespace('Tests\Support\DatabaseTestMigrations', TESTPATH . '_support/DatabaseTestMigrations');
168-
169168
$this->loadDependencies();
170169

171170
if ($this->refresh === true)
172171
{
173-
if (! empty($this->namespace))
174-
{
175-
$this->migrations->setNamespace($this->namespace);
176-
}
177-
$this->migrations->regress(0, 'tests');
178-
179172
// Delete all of the tables to ensure we're at a clean start.
180173
$tables = $this->db->listTables();
181174

@@ -187,14 +180,31 @@ protected function setUp(): void
187180
{
188181
if ($table === $this->db->DBPrefix . 'migrations')
189182
{
183+
$this->db->table($table)->truncate();
190184
continue;
191185
}
192186

193187
$forge->dropTable($table, true);
194188
}
195189
}
196190

197-
$this->migrations->latest('tests');
191+
// If no namespace was specified then migrate all
192+
if (empty($this->namespace))
193+
{
194+
$this->migrations->setNamespace(null);
195+
$this->migrations->latest('tests');
196+
}
197+
198+
// Run migrations for each specified namespace
199+
else
200+
{
201+
$namespaces = is_array($this->namespace) ? $this->namespace : [$this->namespace];
202+
foreach ($namespaces as $namespace)
203+
{
204+
$this->migrations->setNamespace($namespace);
205+
$this->migrations->latest('tests');
206+
}
207+
}
198208
}
199209

200210
if (! empty($this->seed))
@@ -204,7 +214,11 @@ protected function setUp(): void
204214
$this->seeder->setPath(rtrim($this->basePath, '/') . '/Seeds');
205215
}
206216

207-
$this->seed($this->seed);
217+
$seeds = is_array($this->seed) ? $this->seed : [$this->seed];
218+
foreach ($seeds as $seed)
219+
{
220+
$this->seed($seed);
221+
}
208222
}
209223
}
210224

@@ -239,7 +253,6 @@ public function seed(string $name)
239253
return $this->seeder->call($name);
240254
}
241255

242-
//--------------------------------------------------------------------
243256
//--------------------------------------------------------------------
244257
// Database Test Helpers
245258
//--------------------------------------------------------------------
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php namespace Tests\Support\Database\Seeds;
2+
3+
class AnotherSeeder extends \CodeIgniter\Database\Seeder
4+
{
5+
public function run()
6+
{
7+
$row = [
8+
'name' => 'Jerome Lohan',
9+
'email' => 'jlo@lohanenterprises.com',
10+
'country' => 'UK',
11+
];
12+
13+
$this->db->table('user')->insert($row);
14+
}
15+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php namespace CodeIgniter\Database;
2+
3+
use Config\Services;
4+
use CodeIgniter\Test\CIDatabaseTestCase;
5+
6+
class DatabaseTestCaseTest extends CIDatabaseTestCase
7+
{
8+
protected $loaded = false;
9+
10+
/**
11+
* Should the db be refreshed before
12+
* each test?
13+
*
14+
* @var boolean
15+
*/
16+
protected $refresh = true;
17+
18+
/**
19+
* The seed file(s) used for all tests within this test case.
20+
* Should be fully-namespaced or relative to $basePath
21+
*
22+
* @var string|array
23+
*/
24+
protected $seed = [
25+
'Tests\Support\Database\Seeds\CITestSeeder',
26+
'Tests\Support\Database\Seeds\AnotherSeeder'
27+
];
28+
29+
/**
30+
* The namespace(s) to help us find the migration classes.
31+
* Empty is equivalent to running `spark migrate -all`.
32+
* Note that running "all" runs migrations in date order,
33+
* but specifying namespaces runs them in namespace order (then date)
34+
*
35+
* @var string|array|null
36+
*/
37+
protected $namespace = [
38+
'Tests\Support',
39+
'Tests\Support\MigrationTestMigrations',
40+
];
41+
42+
public function setUp(): void
43+
{
44+
if (! $this->loaded)
45+
{
46+
Services::autoloader()->addNamespace('Tests\Support\MigrationTestMigrations', SUPPORTPATH . 'MigrationTestMigrations');
47+
$this->loaded = true;
48+
}
49+
50+
parent::setUp();
51+
}
52+
53+
public function testMultipleSeeders()
54+
{
55+
$this->seeInDatabase('user', ['name' => 'Jerome Lohan']);
56+
}
57+
58+
public function testMultipleMigrationNamespaces()
59+
{
60+
$this->seeInDatabase('foo', ['key' => 'foobar']);
61+
}
62+
}

0 commit comments

Comments
 (0)