Skip to content

Commit 39e11d0

Browse files
author
Andrey Helldar
committed
Updated tests
1 parent 1a024cf commit 39e11d0

File tree

7 files changed

+66
-83
lines changed

7 files changed

+66
-83
lines changed

tests/Commands/InstallTest.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,24 @@
22

33
namespace Tests\Commands;
44

5-
use Illuminate\Support\Facades\Schema;
65
use Tests\TestCase;
76

87
final class InstallTest extends TestCase
98
{
109
public function testRepositoryNotFound()
1110
{
12-
$this->assertFalse(
13-
Schema::hasTable($this->table)
14-
);
11+
$this->assertDatabaseDoesntTable($this->table);
1512

1613
$this->artisan('migrate:actions:status')
1714
->expectsOutput('Actions table not found.');
1815
}
1916

2017
public function testRepository()
2118
{
22-
$this->assertFalse(
23-
Schema::hasTable($this->table)
24-
);
19+
$this->assertDatabaseDoesntTable($this->table);
2520

2621
$this->artisan('migrate:actions:install')->run();
2722

28-
$this->assertTrue(
29-
Schema::hasTable($this->table)
30-
);
23+
$this->assertDatabaseHasTable($this->table);
3124
}
3225
}

tests/Commands/MigrateTest.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,33 @@
22

33
namespace Tests\Commands;
44

5-
use Illuminate\Support\Facades\Schema;
65
use Tests\TestCase;
76

87
final class MigrateTest extends TestCase
98
{
109
public function testMigrationCommand()
1110
{
12-
$this->assertFalse(
13-
Schema::hasTable($this->table)
14-
);
11+
$this->assertDatabaseDoesntTable($this->table);
1512

1613
$this->artisan('migrate:actions:install')->run();
1714

18-
$this->assertTrue(
19-
Schema::hasTable($this->table)
20-
);
21-
15+
$this->assertDatabaseHasTable($this->table);
2216
$this->assertDatabaseCount($this->table, 0);
2317

2418
$this->artisan('make:migration:action', ['name' => 'TestMigration'])->run();
2519
$this->artisan('migrate:actions')->run();
2620

2721
$this->assertDatabaseCount($this->table, 1);
28-
29-
$this->assertTrue(
30-
$this->table()->whereRaw('migration like \'%test_migration\'')->exists()
31-
);
22+
$this->assertDatabaseHasLike($this->table, 'migration', 'test_migration');
3223
}
3324

3425
public function testMigrationNotFound()
3526
{
36-
$this->assertFalse(
37-
Schema::hasTable($this->table)
38-
);
27+
$this->assertDatabaseDoesntTable($this->table);
3928

4029
$this->artisan('migrate:actions:install')->run();
4130

42-
$this->assertTrue(
43-
Schema::hasTable($this->table)
44-
);
31+
$this->assertDatabaseHasTable($this->table);
4532

4633
$this->artisan('migrate:actions:status')
4734
->expectsOutput('No actions found');

tests/Commands/RefreshTest.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@
22

33
namespace Tests\Commands;
44

5-
use Illuminate\Support\Facades\Schema;
65
use Tests\TestCase;
76

87
final class RefreshTest extends TestCase
98
{
109
public function testRefreshCommand()
1110
{
12-
$this->assertFalse(
13-
Schema::hasTable($this->table)
14-
);
11+
$this->assertDatabaseDoesntTable($this->table);
1512

1613
$this->artisan('migrate:actions:install')->run();
1714

18-
$this->assertTrue(
19-
Schema::hasTable($this->table)
20-
);
21-
15+
$this->assertDatabaseHasTable($this->table);
2216
$this->assertDatabaseCount($this->table, 0);
2317

2418
$this->artisan('make:migration:action', ['name' => 'Refresh'])->run();
@@ -29,9 +23,6 @@ public function testRefreshCommand()
2923
$this->artisan('migrate:actions:refresh')->run();
3024

3125
$this->assertDatabaseCount($this->table, 1);
32-
33-
$this->assertTrue(
34-
$this->table()->whereRaw('migration like \'%refresh\'')->exists()
35-
);
26+
$this->assertDatabaseHasLike($this->table, 'migration', 'refresh');
3627
}
3728
}

tests/Commands/ResetTest.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,28 @@
22

33
namespace Tests\Commands;
44

5-
use Illuminate\Support\Facades\Schema;
65
use Tests\TestCase;
76

87
final class ResetTest extends TestCase
98
{
109
public function testResetCommand()
1110
{
12-
$this->assertFalse(
13-
Schema::hasTable($this->table)
14-
);
11+
$this->assertDatabaseDoesntTable($this->table);
1512

1613
$this->artisan('migrate:actions:install')->run();
1714

18-
$this->assertTrue(
19-
Schema::hasTable($this->table)
20-
);
21-
15+
$this->assertDatabaseHasTable($this->table);
2216
$this->assertDatabaseCount($this->table, 0);
2317

2418
$this->artisan('make:migration:action', ['name' => 'Reset'])->run();
2519
$this->artisan('migrate:actions')->run();
2620

2721
$this->assertDatabaseCount($this->table, 1);
28-
29-
$this->assertTrue(
30-
$this->table()->whereRaw('migration like \'%reset\'')->exists()
31-
);
22+
$this->assertDatabaseHasLike($this->table, 'migration', 'reset');
3223

3324
$this->artisan('migrate:actions:reset')->run();
3425

3526
$this->assertDatabaseCount($this->table, 0);
36-
37-
$this->assertTrue(
38-
$this->table()->whereRaw('migration like \'%reset\'')->doesntExist()
39-
);
27+
$this->assertDatabaseDoesntLike($this->table, 'migration', 'reset');
4028
}
4129
}

tests/Commands/RollbackTest.php

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@
22

33
namespace Tests\Commands;
44

5-
use Illuminate\Support\Facades\Schema;
65
use Tests\TestCase;
76

87
final class RollbackTest extends TestCase
98
{
109
public function testRollbackCommand()
1110
{
12-
$this->assertFalse(
13-
Schema::hasTable($this->table)
14-
);
11+
$this->assertDatabaseDoesntTable($this->table);
1512

1613
$this->artisan('migrate:actions:install')->run();
1714

18-
$this->assertTrue(
19-
Schema::hasTable($this->table)
20-
);
21-
15+
$this->assertDatabaseHasTable($this->table);
2216
$this->assertDatabaseCount($this->table, 0);
2317

2418
$this->artisan('make:migration:action', ['name' => 'RollbackOne'])->run();
@@ -27,13 +21,9 @@ public function testRollbackCommand()
2721

2822
$this->assertDatabaseCount($this->table, 2);
2923

30-
$this->assertTrue(
31-
$this->table()->whereRaw('migration like \'%rollback_one\'')->exists()
32-
);
33-
34-
$this->assertTrue(
35-
$this->table()->whereRaw('migration like \'%rollback_two\'')->exists()
36-
);
24+
$this->assertDatabaseHasLike($this->table, 'migration', 'rollback_one');
25+
$this->assertDatabaseHasLike($this->table, 'migration', 'rollback_two');
26+
$this->assertDatabaseDoesntLike($this->table, 'migration', 'rollback_tree');
3727

3828
$this->artisan('migrate:actions:rollback')->run();
3929

@@ -48,8 +38,8 @@ public function testRollbackCommand()
4838

4939
$this->assertDatabaseCount($this->table, 3);
5040

51-
$this->assertTrue(
52-
$this->table()->whereRaw('migration like \'%rollback_tree\'')->exists()
53-
);
41+
$this->assertDatabaseHasLike($this->table, 'migration', 'rollback_one');
42+
$this->assertDatabaseHasLike($this->table, 'migration', 'rollback_two');
43+
$this->assertDatabaseHasLike($this->table, 'migration', 'rollback_tree');
5444
}
5545
}

tests/Commands/StatusTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@
22

33
namespace Tests\Commands;
44

5-
use Illuminate\Support\Facades\Schema;
65
use Tests\TestCase;
76

87
final class StatusTest extends TestCase
98
{
109
public function testStatusCommand()
1110
{
12-
$this->assertFalse(
13-
Schema::hasTable($this->table)
14-
);
11+
$this->assertDatabaseDoesntTable($this->table);
1512

1613
$this->artisan('migrate:actions:install')->run();
1714

18-
$this->assertTrue(
19-
Schema::hasTable($this->table)
20-
);
21-
15+
$this->assertDatabaseHasTable($this->table);
2216
$this->assertDatabaseCount($this->table, 0);
2317

2418
if ($this->is6x()) {

tests/Concerns/AssertDatabase.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,54 @@
33
namespace Tests\Concerns;
44

55
use Illuminate\Support\Facades\DB;
6+
use Illuminate\Support\Facades\Schema;
67

78
/** @mixin \Tests\TestCase */
89
trait AssertDatabase
910
{
10-
protected function assertDatabaseCount($table, int $count, $connection = null)
11+
protected function assertDatabaseCount($table, int $count, $connection = null): void
1112
{
1213
$actual = DB::connection($connection)->table($table)->count();
1314

1415
$this->assertEquals($count, $actual);
1516
}
17+
18+
protected function assertDatabaseHasTable(string $table): void
19+
{
20+
$this->assertTrue(
21+
$this->hasTable($table)
22+
);
23+
}
24+
25+
protected function assertDatabaseDoesntTable(string $table): void
26+
{
27+
$this->assertFalse(
28+
$this->hasTable($table)
29+
);
30+
}
31+
32+
protected function assertDatabaseHasLike(string $table, string $column, $value, $connection = null): void
33+
{
34+
$exists = DB::connection($connection)
35+
->table($table)
36+
->whereRaw("{$column} like '%$value%'")
37+
->exists();
38+
39+
$this->assertTrue($exists);
40+
}
41+
42+
protected function assertDatabaseDoesntLike(string $table, string $column, $value, $connection = null): void
43+
{
44+
$exists = DB::connection($connection)
45+
->table($table)
46+
->whereRaw("{$column} like '%$value%'")
47+
->doesntExist();
48+
49+
$this->assertTrue($exists);
50+
}
51+
52+
protected function hasTable(string $table): bool
53+
{
54+
return Schema::hasTable($table);
55+
}
1656
}

0 commit comments

Comments
 (0)