|
4 | 4 |
|
5 | 5 | namespace Tests\Commands;
|
6 | 6 |
|
7 |
| -use DragonCode\LaravelActions\Constants\Names; |
| 7 | +use DragonCode\LaravelActions\Console\Command; |
| 8 | +use DragonCode\LaravelActions\Constants\Options; |
| 9 | +use DragonCode\LaravelActions\Services\Mutex; |
| 10 | +use Illuminate\Container\Container; |
| 11 | +use Mockery as m; |
| 12 | +use Symfony\Component\Console\Input\ArrayInput; |
| 13 | +use Symfony\Component\Console\Output\NullOutput; |
8 | 14 | use Tests\TestCase;
|
9 | 15 |
|
10 | 16 | class MutexTest extends TestCase
|
11 | 17 | {
|
12 |
| - public function testMigrationCommand() |
| 18 | + protected Command $command; |
| 19 | + |
| 20 | + protected Mutex $mutex; |
| 21 | + |
| 22 | + public function testCanRunIsolatedCommandIfNotBlocked() |
| 23 | + { |
| 24 | + $this->mutex->shouldReceive('create') |
| 25 | + ->andReturn(true) |
| 26 | + ->once(); |
| 27 | + |
| 28 | + $this->mutex->shouldReceive('forget') |
| 29 | + ->never() |
| 30 | + ->once(); |
| 31 | + |
| 32 | + $this->runCommand(); |
| 33 | + |
| 34 | + $this->assertSame(1, $this->command->ran); |
| 35 | + } |
| 36 | + |
| 37 | + public function testCannotRunIsolatedCommandIfBlocked() |
13 | 38 | {
|
14 |
| - $this->assertDatabaseDoesntTable($this->table); |
| 39 | + $this->mutex->shouldReceive('create') |
| 40 | + ->andReturn(false) |
| 41 | + ->once(); |
15 | 42 |
|
16 |
| - $this->artisan(Names::INSTALL)->assertExitCode(0); |
| 43 | + $this->mutex->shouldReceive('forget') |
| 44 | + ->never() |
| 45 | + ->once(); |
17 | 46 |
|
18 |
| - $this->assertDatabaseHasTable($this->table); |
19 |
| - $this->assertDatabaseCount($this->table, 0); |
| 47 | + $this->runCommand(); |
20 | 48 |
|
21 |
| - $this->artisan(Names::MAKE, ['name' => 'TestMigration'])->assertExitCode(0); |
22 |
| - $this->artisan(Names::MIGRATE)->assertExitCode(0); |
| 49 | + $this->assertSame(0, $this->command->ran); |
| 50 | + } |
| 51 | + |
| 52 | + public function testCanRunCommandAgainAfterOtherCommandFinished() |
| 53 | + { |
| 54 | + $this->mutex->shouldReceive('create') |
| 55 | + ->andReturn(true) |
| 56 | + ->twice(); |
| 57 | + |
| 58 | + $this->mutex->shouldReceive('forget') |
| 59 | + ->never() |
| 60 | + ->twice(); |
| 61 | + |
| 62 | + $this->runCommand(); |
| 63 | + $this->runCommand(); |
| 64 | + |
| 65 | + $this->assertEquals(2, $this->command->ran); |
| 66 | + } |
| 67 | + |
| 68 | + public function testCanRunCommandAgainNonAutomated() |
| 69 | + { |
| 70 | + $this->mutex->shouldNotHaveBeenCalled(); |
| 71 | + |
| 72 | + $this->runCommand(false); |
| 73 | + |
| 74 | + $this->assertEquals(1, $this->command->ran); |
| 75 | + } |
| 76 | + |
| 77 | + protected function setUp(): void |
| 78 | + { |
| 79 | + $this->command = new class extends Command |
| 80 | + { |
| 81 | + public int $ran = 0; |
| 82 | + |
| 83 | + public function handle(): int |
| 84 | + { |
| 85 | + $this->ran++; |
| 86 | + |
| 87 | + return self::SUCCESS; |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + $this->mutex = m::mock(Mutex::class); |
| 92 | + |
| 93 | + $container = Container::getInstance(); |
| 94 | + $container->instance(Mutex::class, $this->mutex); |
| 95 | + $this->command->setLaravel($container); |
| 96 | + } |
| 97 | + |
| 98 | + protected function runCommand($withIsolated = true) |
| 99 | + { |
| 100 | + $input = new ArrayInput(['--' . Options::ISOLATED => $withIsolated]); |
| 101 | + $output = new NullOutput; |
23 | 102 |
|
24 |
| - $this->assertDatabaseCount($this->table, 1); |
25 |
| - $this->assertDatabaseMigrationHas($this->table, 'test_migration'); |
| 103 | + $this->command->run($input, $output); |
26 | 104 | }
|
27 | 105 | }
|
0 commit comments