Skip to content

Commit a6c9731

Browse files
committed
Add tests for withoutOverlapping and before- and afterCallbacks
1 parent f4a43b0 commit a6c9731

File tree

6 files changed

+95
-28
lines changed

6 files changed

+95
-28
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"require-dev": {
1717
"mockery/mockery": "^1.2",
1818
"orchestra/testbench": "^3.5 || ^3.6 || ^3.7 || ^3.8 || ^4.0 || ^5.0",
19-
"symfony/console": "^4.4|^5.0"
19+
"symfony/console": "^4.4|^5.0",
20+
"timacdonald/log-fake": "^1.6"
2021
},
2122
"autoload": {
2223
"psr-4": {

tests/Support/Kernel.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Tests\Support;
4+
5+
use Illuminate\Console\Scheduling\Schedule;
6+
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
7+
8+
class Kernel extends ConsoleKernel
9+
{
10+
protected $commands = [
11+
TestCommand::class,
12+
TestCommand2::class,
13+
];
14+
15+
protected function schedule(Schedule $schedule)
16+
{
17+
$schedule->command('test:command')->withoutOverlapping();
18+
$schedule->command('test:command2')->before(function () {
19+
logger()->info('log after');
20+
})->after(function () {
21+
logger()->warning('log before');
22+
});
23+
}
24+
}

tests/Support/TestCommand.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,9 @@
66

77
class TestCommand extends Command
88
{
9-
/**
10-
* The name and signature of the console command.
11-
*
12-
* @var string
13-
*/
149
protected $signature = 'test:command';
15-
16-
/**
17-
* The console command description.
18-
*
19-
* @var string
20-
*/
2110
protected $description = 'Do some testy stuff';
2211

23-
/**
24-
* Create a new command instance.
25-
*
26-
* @return void
27-
*/
28-
public function __construct()
29-
{
30-
parent::__construct();
31-
}
32-
33-
/**
34-
* Execute the console command.
35-
*
36-
* @return mixed
37-
*/
3812
public function handle()
3913
{
4014
logger('did something testy');

tests/Support/TestCommand2.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Tests\Support;
4+
5+
use Illuminate\Console\Command;
6+
7+
class TestCommand2 extends Command
8+
{
9+
protected $signature = 'test:command2';
10+
11+
protected $description = 'Do some testy stuff';
12+
13+
public function handle()
14+
{
15+
logger('did something testy');
16+
}
17+
}

tests/TaskHandlerTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests;
44

55
use Firebase\JWT\JWT;
6+
use Illuminate\Console\Application as ConsoleApplication;
67
use Illuminate\Console\Scheduling\Schedule;
78
use Illuminate\Container\Container;
89
use Illuminate\Contracts\Console\Kernel;
@@ -13,6 +14,7 @@
1314
use Stackkit\LaravelGoogleCloudScheduler\OpenIdVerificator;
1415
use Stackkit\LaravelGoogleCloudScheduler\TaskHandler;
1516
use Throwable;
17+
use TiMacDonald\Log\LogFake;
1618

1719
class TaskHandlerTest extends TestCase
1820
{
@@ -136,4 +138,46 @@ public function the_aud_claim_must_be_the_same_as_the_app_id()
136138

137139
$this->taskHandler->handle();
138140
}
141+
142+
/** @test */
143+
public function it_prevents_overlapping_if_the_command_is_scheduled_without_overlapping()
144+
{
145+
$this->fakeCommand->shouldReceive('capture')->andReturn('test:command');
146+
$this->openId->shouldReceive('guardAgainstInvalidOpenIdToken')->andReturnNull();
147+
$this->openId->shouldReceive('decodeToken')->andReturnNull();
148+
149+
cache()->clear();
150+
151+
Log::shouldReceive('debug')->twice();
152+
153+
$this->taskHandler->handle();
154+
155+
$expression = '* * * * *';
156+
$command = ConsoleApplication::formatCommandString('test:command');
157+
$mutex = 'framework'.DIRECTORY_SEPARATOR.'schedule-'.sha1($expression.$command);
158+
159+
cache()->add($mutex, true);
160+
161+
$this->taskHandler->handle();
162+
163+
cache()->delete($mutex);
164+
165+
$this->taskHandler->handle();
166+
}
167+
168+
/** @test */
169+
public function it_runs_the_before_and_after_callbacks()
170+
{
171+
$this->fakeCommand->shouldReceive('capture')->andReturn('test:command2');
172+
$this->openId->shouldReceive('guardAgainstInvalidOpenIdToken')->andReturnNull();
173+
$this->openId->shouldReceive('decodeToken')->andReturnNull();
174+
175+
Log::swap(new LogFake());
176+
177+
$this->taskHandler->handle();
178+
179+
Log::assertLoggedMessage('info', 'log after');
180+
Log::assertLoggedMessage('warning', 'log before');
181+
Log::assertLoggedMessage('debug', 'did something testy');
182+
}
139183
}

tests/TestCase.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace Tests;
44

5+
use Illuminate\Console\Scheduling\Schedule;
6+
use Tests\Support\Kernel;
7+
use Tests\Support\TestCommand;
8+
59
class TestCase extends \Orchestra\Testbench\TestCase
610
{
711
/**
@@ -29,6 +33,9 @@ protected function getPackageProviders($app)
2933
*/
3034
protected function getEnvironmentSetUp($app)
3135
{
32-
//
36+
$app->singleton(
37+
\Illuminate\Contracts\Console\Kernel::class,
38+
Kernel::class
39+
);
3340
}
3441
}

0 commit comments

Comments
 (0)