Skip to content

Commit cccd891

Browse files
authored
Merge pull request #3 from nicklayb/fixSortedTables
Fixed table sort
2 parents 5036d22 + 8e9e528 commit cccd891

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/Import.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,21 @@ public function hashPassword($password)
258258
*/
259259
public function getSortedSourceTables()
260260
{
261-
$tables = $this->getSourceTables();
262261
$filteredTables = collect([]);
263262
$holds = collect([]);
264-
foreach ($tables as $table) {
265-
if ($this->hasLastTable($table)) {
266-
$holds->push($table);
263+
264+
foreach ($this->getSourceTables() as $table) {
265+
$index = $this->hasLastTable($table);
266+
if ($index >= 0) {
267+
$holds->put($index, $table);
267268
} elseif (!$this->hasIgnoreTable($table)) {
268269
$filteredTables->push($table);
269270
}
270271
}
272+
$arrayHolds = $holds->toArray();
273+
ksort($arrayHolds);
274+
$holds = collect($arrayHolds);
275+
271276
return $filteredTables->merge($holds);
272277
}
273278

@@ -282,13 +287,14 @@ public function hasIgnoreTable($table)
282287
}
283288

284289
/**
285-
* Check if a specified table should be last
290+
* Gets the index of a table in the last tables array
286291
*
287-
* @return bool
292+
* @return int
288293
*/
289294
public function hasLastTable($table)
290295
{
291-
return in_array($table, $this->lastTables);
296+
$index = array_search($table, $this->lastTables);
297+
return ($index !== false) ? $index : -1;
292298
}
293299

294300
/**

tests/ImportTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class BasicImport extends Nicklayb\LaravelDbImport\Import
1111
class ExtendedImport extends Nicklayb\LaravelDbImport\Import
1212
{
1313
protected $ignoreTables = [ 'migrations' ];
14-
protected $lastTables = [ 'relation_one', 'relation_two' ];
14+
protected $lastTables = [ 'relation_two','relation_one' ];
1515
protected $selects = [
1616
'users' => [
1717
'id', 'firstname', 'lastname'
@@ -89,12 +89,12 @@ public function testHasIgnoreTableInexistant()
8989

9090
public function testHasLastTable()
9191
{
92-
$this->assertTrue($this->extendedImport->hasLastTable('relation_one'));
92+
$this->assertEquals($this->extendedImport->hasLastTable('relation_one'), 1);
9393
}
9494

9595
public function testHasLastTableInexistant()
9696
{
97-
$this->assertFalse($this->extendedImport->hasLastTable('products'));
97+
$this->assertEquals($this->extendedImport->hasLastTable('products'), -1);
9898
}
9999

100100
public function testHasPasswordReset()
@@ -236,10 +236,12 @@ public function testGetSortedSourceTables()
236236
'products',
237237
'roles',
238238
'orders',
239-
'relation_one',
240-
'relation_two'
239+
'relation_two',
240+
'relation_one'
241241
];
242-
243-
$this->assertEquals($expected, $this->extendedImport->getSortedSourceTables()->toArray());
242+
$tables = $this->extendedImport->getSortedSourceTables()->toArray();
243+
for ($i = 0; $i < count($tables) ; $i++) {
244+
$this->assertEquals($expected[$i], $tables[$i]);
245+
}
244246
}
245247
}

0 commit comments

Comments
 (0)