Skip to content

Commit 1d65f3a

Browse files
lonnieezellMGatner
authored andcommitted
Got testing to recognize the new migration folder layout, and fixed a few tests. Close but not quite everything passing just yet.
1 parent 404fbc4 commit 1d65f3a

6 files changed

Lines changed: 74 additions & 66 deletions

File tree

system/Autoloader/FileLocator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,19 @@ public function locateFile(string $file, string $folder = null, string $ext = 'p
126126
$filename = implode('/', $segments);
127127
break;
128128
}
129-
129+
130130
// if no namespaces matched then quit
131131
if (empty($paths))
132132
{
133133
return false;
134134
}
135-
135+
136136
// Check each path in the namespace
137137
foreach ($paths as $path)
138138
{
139139
// Ensure trailing slash
140140
$path = rtrim($path, '/') . '/';
141-
141+
142142
// If we have a folder name, then the calling function
143143
// expects this file to be within that folder, like 'Views',
144144
// or 'libraries'.
@@ -153,7 +153,7 @@ public function locateFile(string $file, string $folder = null, string $ext = 'p
153153
return $path;
154154
}
155155
}
156-
156+
157157
return false;
158158
}
159159

system/Database/MigrationRunner.php

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ public function __construct(BaseConfig $config, $db = null)
167167
* Locate and run all new migrations
168168
*
169169
* @param string|null $group
170-
*
171170
*/
172171
public function progress(string $group = null)
173172
{
@@ -210,7 +209,8 @@ public function progress(string $group = null)
210209
$this->addHistory($migration, $batch);
211210
}
212211
// If a migration failed then try to back out what was done
213-
else {
212+
else
213+
{
214214
$this->regress(-1);
215215

216216
$message = lang('Migrations.generalFault');
@@ -234,13 +234,13 @@ public function progress(string $group = null)
234234
*
235235
* Calls each migration step required to get to the provided batch
236236
*
237-
* @param int $targetBatch Target batch number, or negative for a relative batch, 0 for all
237+
* @param integer $targetBatch Target batch number, or negative for a relative batch, 0 for all
238238
* @param string|null $group
239239
*
240240
* @return mixed Current batch number on success, FALSE on failure or no migrations are found
241241
* @throws ConfigException
242242
*/
243-
public function regress(int $targetBatch, string $group = null)
243+
public function regress(int $targetBatch = 0, string $group = null)
244244
{
245245
if (! $this->enabled)
246246
{
@@ -285,7 +285,7 @@ public function regress(int $targetBatch, string $group = null)
285285

286286
// Get all migrations
287287
$this->namespace = null;
288-
$allMigrations = $this->findMigrations();
288+
$allMigrations = $this->findMigrations();
289289

290290
// Gather migrations down through each batch until reaching the target
291291
$migrations = [];
@@ -317,9 +317,9 @@ public function regress(int $targetBatch, string $group = null)
317317
}
318318

319319
// Add the history and put it on the list
320-
$migration = $allMigrations[$uid];
320+
$migration = $allMigrations[$uid];
321321
$migration->history = $history;
322-
$migrations[] = $migration;
322+
$migrations[] = $migration;
323323
}
324324
}
325325

@@ -331,7 +331,8 @@ public function regress(int $targetBatch, string $group = null)
331331
$this->removeHistory($migration->history);
332332
}
333333
// If a migration failed then quit so as not to ruin the whole batch
334-
else {
334+
else
335+
{
335336
$message = lang('Migrations.generalFault');
336337

337338
if ($this->silent)
@@ -353,10 +354,9 @@ public function regress(int $targetBatch, string $group = null)
353354
* Method "up" or "down" determined by presence in history.
354355
* NOTE: This is not recommended and provided mostly for testing.
355356
*
356-
* @param string $path Full path to a valid migration file
357-
* @param string $path Namespace of the target migration
357+
* @param string $path Full path to a valid migration file
358+
* @param string $path Namespace of the target migration
358359
* @param string|null $group
359-
*
360360
*/
361361
public function force(string $path, string $namespace, string $group = null)
362362
{
@@ -392,16 +392,16 @@ public function force(string $path, string $namespace, string $group = null)
392392
$this->setNamespace($migration->namespace);
393393
foreach ($this->getHistory($this->group) as $history)
394394
{
395-
if ($this->getObjectUid($history) == $migration->uid)
395+
if ($this->getObjectUid($history) === $migration->uid)
396396
{
397-
$method = 'down';
397+
$method = 'down';
398398
$migration->history = $history;
399399
break;
400400
}
401401
}
402402

403403
// up
404-
if ($method == 'up')
404+
if ($method === 'up')
405405
{
406406
// Start a new batch
407407
$batch = $this->getLastBatch() + 1;
@@ -464,7 +464,7 @@ public function findMigrations(): array
464464
/**
465465
* Retrieves a list of available migration scripts for one namespace
466466
*
467-
* @param string $namespace The namespace to search for migrations
467+
* @param string $namespace The namespace to search for migrations
468468
*
469469
* @return array List of unsorted migrations from the namespace
470470
*/
@@ -508,8 +508,8 @@ public function findNamespaceMigrations(string $namespace): array
508508
/**
509509
* Create a migration object from a file path.
510510
*
511-
* @param string $path The path to the file
512-
* @param string $path The namespace of the target migration
511+
* @param string $path The path to the file
512+
* @param string $path The namespace of the target migration
513513
*
514514
* @return object|false Returns the migration object, or false on failure
515515
*/
@@ -551,7 +551,7 @@ protected function migrationFromFile(string $path, string $namespace)
551551
* Set namespace.
552552
* Allows other scripts to modify on the fly as needed.
553553
*
554-
* @param ?string $namespace or null for "all"
554+
* @param string $namespace or null for "all"
555555
*
556556
* @return MigrationRunner
557557
*/
@@ -710,7 +710,7 @@ public function clearHistory()
710710
* @param object $migration
711711
* @param integer $batch
712712
*
713-
* @return void
713+
* @return void
714714
*/
715715
protected function addHistory($migration, int $batch)
716716
{
@@ -742,7 +742,6 @@ protected function addHistory($migration, int $batch)
742742
*/
743743
protected function removeHistory($history)
744744
{
745-
746745
$this->db->table($this->table)->where('id', $history->id)->delete();
747746

748747
if (is_cli())
@@ -795,9 +794,9 @@ public function getBatchHistory(int $batch): array
795794
$this->ensureTable();
796795

797796
$query = $this->db->table($this->table)
798-
->where('batch', $batch)
799-
->orderBy('id', 'asc')
800-
->get();
797+
->where('batch', $batch)
798+
->orderBy('id', 'asc')
799+
->get();
801800

802801
return $query ? $query->getResultObject() : [];
803802
}
@@ -816,7 +815,7 @@ public function getBatches(): array
816815
$batches = $this->db->table($this->table)
817816
->select('batch')
818817
->distinct()
819-
->orderBy('batch' ,'asc')
818+
->orderBy('batch', 'asc')
820819
->get()
821820
->getResultArray();
822821

@@ -862,7 +861,7 @@ public function getBatchStart(int $batch): string
862861
if ($batch < 0)
863862
{
864863
$batches = $this->getBatches();
865-
$batch = $batches[count($batches) - 1 + $targetBatch] ?? 0;
864+
$batch = $batches[count($batches) - 1 + $targetBatch] ?? 0;
866865
}
867866

868867
$migration = $this->db->table($this->table)
@@ -891,7 +890,7 @@ public function getBatchEnd(int $batch): string
891890
if ($batch < 0)
892891
{
893892
$batches = $this->getBatches();
894-
$batch = $batches[count($batches) - 1 + $targetBatch] ?? 0;
893+
$batch = $batches[count($batches) - 1 + $targetBatch] ?? 0;
895894
}
896895

897896
$migration = $this->db->table($this->table)

system/Test/CIDatabaseTestCase.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
namespace CodeIgniter\Test;
3939

40+
use CodeIgniter\Config\Config;
41+
use Config\Autoload;
4042
use Config\Database;
4143
use Config\Migrations;
4244
use Config\Services;
@@ -160,6 +162,9 @@ protected function setUp()
160162
{
161163
parent::setUp();
162164

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

165170
if ($this->refresh === true)
@@ -172,20 +177,20 @@ protected function setUp()
172177
// Delete all of the tables to ensure we're at a clean start.
173178
$tables = $this->db->listTables();
174179

175-
if (is_array($tables))
176-
{
177-
$forge = Database::forge('tests');
178-
179-
foreach ($tables as $table)
180-
{
181-
if ($table === $this->db->DBPrefix . 'migrations')
182-
{
183-
continue;
184-
}
185-
186-
$forge->dropTable($table, true);
187-
}
188-
}
180+
// if (is_array($tables))
181+
// {
182+
// $forge = Database::forge('tests');
183+
//d($tables);
184+
// foreach ($tables as $table)
185+
// {
186+
// if ($table === $this->db->DBPrefix . 'migrations')
187+
// {
188+
// continue;
189+
// }
190+
//
191+
// $forge->dropTable($table, true);
192+
// }
193+
// }
189194

190195
$this->migrations->regress(0, 'tests');
191196
$this->migrations->progress('tests');

tests/_support/Config/MockServices.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ class MockServices extends BaseService
77
{
88

99
public $psr4 = [
10-
'Tests/Support' => TESTPATH . '_support/',
11-
'Tests/Support/DatabaseTestMigrations' => TESTPATH . '_support/DatabaseTestMigrations',
12-
'Tests/Support/MigrationTestMigrations' => TESTPATH . '_support/MigrationTestMigrations',
10+
'Tests/Support' => TESTPATH . '_support/',
1311
];
1412
public $classmap = [];
1513

tests/_support/MigrationTestMigrations/Database/Migrations/2018-01-24-102302_Another_migration.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public function up()
2020

2121
public function down()
2222
{
23-
$this->forge->dropColumn('foo', 'value');
23+
if ($this->db->tableExists('foo'))
24+
{
25+
$this->forge->dropColumn('foo', 'value');
26+
}
2427
}
2528
}

tests/system/Database/Migrations/MigrationRunnerTest.php

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use org\bovigo\vfs\vfsStream;
66
use CodeIgniter\Test\CIDatabaseTestCase;
77
use org\bovigo\vfs\visitor\vfsStreamStructureVisitor;
8+
use Config\Services;
89

910
/**
1011
* @group DatabaseLive
@@ -25,6 +26,8 @@ public function setUp()
2526
$this->start = $this->root->url() . '/';
2627
$this->config = new Migrations();
2728
$this->config->enabled = true;
29+
30+
Services::autoloader()->addNamespace('Tests\Support\MigrationTestMigrations', TESTPATH . '_support/MigrationTestMigrations');
2831
}
2932

3033
public function testLoadsDefaultDatabaseWhenNoneSpecified()
@@ -162,7 +165,7 @@ public function testFindMigrationsReturnsEmptyArrayWithNoneFound()
162165
$config->type = 'timestamp';
163166
$runner = new MigrationRunner($config);
164167

165-
$runner->setPath($this->start);
168+
// $runner->setPath($this->start);
166169

167170
$this->assertEquals([], $runner->findMigrations());
168171
}
@@ -175,22 +178,22 @@ public function testFindMigrationsSuccessTimestamp()
175178

176179
$runner = $runner->setNamespace('Tests\Support\MigrationTestMigrations');
177180

178-
$mig1 = (object)[
179-
'name' => 'Some_migration',
180-
'path' => TESTPATH . '_support/MigrationTestMigrations/Database/Migrations/2018-01-24-102301_Some_migration.php',
181-
'version' => '2018-01-24-102301',
182-
'class' => 'Tests\Support\MigrationTestMigrations\Database\Migrations\Migration_some_migration',
183-
'namespace' => 'Tests\Support\MigrationTestMigrations',
184-
];
181+
$mig1 = (object)[
182+
'name' => 'Some_migration',
183+
'path' => TESTPATH . '_support/MigrationTestMigrations/Database/Migrations/2018-01-24-102301_Some_migration.php',
184+
'version' => '2018-01-24-102301',
185+
'class' => 'Tests\Support\MigrationTestMigrations\Database\Migrations\Migration_some_migration',
186+
'namespace' => 'Tests\Support\MigrationTestMigrations',
187+
];
185188
$mig1->uid = $runner->getObjectUid($mig1);
186189

187-
$mig2 = (object)[
188-
'name' => 'Another_migration',
189-
'path' => TESTPATH . '_support/MigrationTestMigrations/Database/Migrations/2018-01-24-102302_Another_migration.php',
190-
'version' => '2018-01-24-102302',
191-
'class' => 'Tests\Support\MigrationTestMigrations\Database\Migrations\Migration_another_migration',
192-
'namespace' => 'Tests\Support\MigrationTestMigrations',
193-
];
190+
$mig2 = (object)[
191+
'name' => 'Another_migration',
192+
'path' => TESTPATH . '_support/MigrationTestMigrations/Database/Migrations/2018-01-24-102302_Another_migration.php',
193+
'version' => '2018-01-24-102302',
194+
'class' => 'Tests\Support\MigrationTestMigrations\Database\Migrations\Migration_another_migration',
195+
'namespace' => 'Tests\Support\MigrationTestMigrations',
196+
];
194197
$mig1->uid = $runner->getObjectUid($mig1);
195198

196199
$migrations = $runner->findMigrations();
@@ -243,7 +246,7 @@ public function testVersionReturnsUpDownSuccess()
243246
$this->assertEquals('2018-01-24-102302', $version);
244247
$this->seeInDatabase('foo', ['key' => 'foobar']);
245248

246-
$runner->version(0);
249+
$runner->regress(0);
247250
$version = $runner->getBatchEnd($runner->getLastBatch());
248251

249252
$this->assertEquals('0', $version);
@@ -259,7 +262,7 @@ public function testProgressSuccess()
259262

260263
$runner = $runner->setNamespace('Tests\Support\MigrationTestMigrations');
261264

262-
$runner->progess();
265+
$runner->progress();
263266
$version = $runner->getBatchEnd($runner->getLastBatch());
264267

265268
$this->assertEquals('2018-01-24-102302', $version);
@@ -299,7 +302,7 @@ public function testHistoryRecordsBatches()
299302

300303
$runner = $runner->setNamespace('Tests\Support\MigrationTestMigrations');
301304

302-
$runner->progess();
305+
$runner->progress();
303306
$version = $runner->getBatchEnd($runner->getLastBatch());
304307

305308
$this->assertEquals('2018-01-24-102301', $version);

0 commit comments

Comments
 (0)