Skip to content

Commit 1fd6cca

Browse files
Added Mutex tests
1 parent 008f7e3 commit 1fd6cca

File tree

1 file changed

+88
-10
lines changed

1 file changed

+88
-10
lines changed

tests/Commands/MutexTest.php

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,102 @@
44

55
namespace Tests\Commands;
66

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;
814
use Tests\TestCase;
915

1016
class MutexTest extends TestCase
1117
{
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()
1338
{
14-
$this->assertDatabaseDoesntTable($this->table);
39+
$this->mutex->shouldReceive('create')
40+
->andReturn(false)
41+
->once();
1542

16-
$this->artisan(Names::INSTALL)->assertExitCode(0);
43+
$this->mutex->shouldReceive('forget')
44+
->never()
45+
->once();
1746

18-
$this->assertDatabaseHasTable($this->table);
19-
$this->assertDatabaseCount($this->table, 0);
47+
$this->runCommand();
2048

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;
23102

24-
$this->assertDatabaseCount($this->table, 1);
25-
$this->assertDatabaseMigrationHas($this->table, 'test_migration');
103+
$this->command->run($input, $output);
26104
}
27105
}

0 commit comments

Comments
 (0)