Skip to content

Commit d889ed5

Browse files
committed
initial commit
0 parents  commit d889ed5

12 files changed

+397
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
composer.lock
2+
vendor

.travis.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: php
2+
3+
php:
4+
- 7.0
5+
- 7.1
6+
- 7.2
7+
8+
env:
9+
matrix:
10+
- COMPOSER_FLAGS="--prefer-lowest"
11+
- COMPOSER_FLAGS=""
12+
13+
before_script:
14+
- travis_retry composer self-update
15+
- travis_wait composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
16+
17+
script:
18+
- vendor/bin/phpunit --testdox

composer.json

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "arondeparon/schedulewhen",
3+
"description": ":A command that enabled you see what the scheduler will do at a given moment in time.",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Aron Rotteveel",
8+
"email": "[email protected]",
9+
"homepage": "https://github.com/arondeparon"
10+
}
11+
],
12+
"homepage": "https://github.com/arondeparon/schedulewhen",
13+
"keywords": ["Laravel", "ScheduleWhen"],
14+
"require": {
15+
"illuminate/support": "~5"
16+
},
17+
"require-dev": {
18+
"phpunit/phpunit": "~7.0",
19+
"mockery/mockery": "^1.1",
20+
"orchestra/testbench": "~3.0",
21+
"sempro/phpunit-pretty-print": "^1.0"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"ArondeParon\\ScheduleWhen\\": "src/"
26+
}
27+
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"ArondeParon\\ScheduleWhen\\Tests\\": "tests"
31+
}
32+
},
33+
"extra": {
34+
"laravel": {
35+
"providers": [
36+
"ArondeParon\\ScheduleWhen\\ScheduleWhenServiceProvider"
37+
],
38+
"aliases": {
39+
"ScheduleWhen": "ArondeParon\\ScheduleWhen\\Facades\\ScheduleWhen"
40+
}
41+
}
42+
}
43+
}

license.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Aron Rotteveel <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

phpunit.xml.dist

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Package">
14+
<directory suffix=".php">./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory>src/</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

readme.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# ScheduleWhen
2+
3+
[![Latest Version on Packagist][ico-version]][link-packagist]
4+
[![Total Downloads][ico-downloads]][link-downloads]
5+
[![Build Status][ico-travis]][link-travis]
6+
[![StyleCI][ico-styleci]][link-styleci]
7+
8+
A Laravel Console Command that tells you the scheduler would do at a given moment in time.
9+
10+
## Installation
11+
12+
Via Composer
13+
14+
``` bash
15+
$ composer require arondeparon/schedulewhen
16+
```
17+
18+
## Usage
19+
20+
``` php
21+
php artisan schedule:when "2019-01-01 08:05"
22+
```
23+
24+
## Testing
25+
26+
``` bash
27+
$ composer test
28+
```
29+
30+
## License
31+
32+
MIT license. Please see the [license file](license.md) for more information.
33+
34+
[ico-version]: https://img.shields.io/packagist/v/arondeparon/schedulewhen.svg?style=flat-square
35+
[ico-downloads]: https://img.shields.io/packagist/dt/arondeparon/schedulewhen.svg?style=flat-square
36+
[ico-travis]: https://img.shields.io/travis/arondeparon/schedulewhen/master.svg?style=flat-square
37+
[ico-styleci]: https://styleci.io/repos/12345678/shield
38+
39+
[link-packagist]: https://packagist.org/packages/arondeparon/schedulewhen
40+
[link-downloads]: https://packagist.org/packages/arondeparon/schedulewhen
41+
[link-travis]: https://travis-ci.org/arondeparon/schedulewhen
42+
[link-styleci]: https://styleci.io/repos/12345678
43+
[link-author]: https://github.com/arondeparon
44+
[link-contributors]: ../../contributors

src/Commands/ScheduleWhenCommand.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace ArondeParon\ScheduleWhen\Commands;
4+
5+
use ArondeParon\ScheduleWhen\ScheduleWhen;
6+
use Carbon\Carbon;
7+
use Illuminate\Console\Command;
8+
9+
class ScheduleWhenCommand extends Command
10+
{
11+
protected $name = 'schedule:when';
12+
13+
protected $signature = 'schedule:when {dateTime=now}';
14+
15+
protected $description = 'List the commands that will run at a certain time';
16+
17+
public function handle()
18+
{
19+
try {
20+
$moment = Carbon::parse($this->argument('dateTime'));
21+
} catch (\Exception $e) {
22+
$this->error("Could not parse {$this->argument('dateTime')} into a valid Carbon object.");
23+
return 1;
24+
}
25+
26+
/** @var ScheduleWhen $matcher */
27+
$matcher = app(ScheduleWhen::class);
28+
$dueEvents = $matcher->handle($moment);
29+
30+
if (count($dueEvents) === 0) {
31+
$this->info("No commands will run at {$this->argument('dateTime')}.");
32+
return 0;
33+
}
34+
35+
$this->info("The following commands will run at {$moment->toDateTimeString()}:");
36+
37+
$this->table([
38+
'expression',
39+
'command',
40+
], $dueEvents);
41+
42+
return 0;
43+
}
44+
}

src/ScheduleWhen.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace ArondeParon\ScheduleWhen;
4+
5+
use Carbon\CarbonInterface;
6+
use Cron\CronExpression;
7+
use Illuminate\Console\Scheduling\Schedule;
8+
use Illuminate\Contracts\Console\Kernel;
9+
10+
class ScheduleWhen
11+
{
12+
/**
13+
* @var Schedule
14+
*/
15+
private $schedule;
16+
17+
/**
18+
* @var Kernel
19+
*/
20+
private $kernel;
21+
22+
public function __construct(Schedule $schedule, Kernel $kernel)
23+
{
24+
$this->schedule = $schedule;
25+
$this->kernel = $kernel;
26+
}
27+
28+
public function handle(CarbonInterface $moment): array
29+
{
30+
$dueEvents = [];
31+
32+
foreach ($this->schedule->events() as $event) {
33+
if (CronExpression::factory($event->getExpression())->isDue($moment->toDateTimeString())) {
34+
$dueEvents[] = [
35+
'expression' => $event->getExpression(),
36+
'command' => $event->command,
37+
];
38+
}
39+
}
40+
41+
return $dueEvents;
42+
}
43+
}

src/ScheduleWhenServiceProvider.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace ArondeParon\ScheduleWhen;
4+
5+
use ArondeParon\ScheduleWhen\Commands\ScheduleWhenCommand;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class ScheduleWhenServiceProvider extends ServiceProvider
9+
{
10+
/**
11+
* Perform post-registration booting of services.
12+
*
13+
* @return void
14+
*/
15+
public function boot()
16+
{
17+
if ($this->app->runningInConsole()) {
18+
$this->bootForConsole();
19+
}
20+
}
21+
22+
/**
23+
* Console-specific booting.
24+
*
25+
* @return void
26+
*/
27+
protected function bootForConsole()
28+
{
29+
$this->commands([
30+
ScheduleWhenCommand::class
31+
]);
32+
}
33+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace ArondeParon\ScheduleWhen\Tests\Command;
4+
5+
use ArondeParon\ScheduleWhen\ScheduleWhen;
6+
use ArondeParon\ScheduleWhen\Tests\TestCase;
7+
use ArondeParon\ScheduleWhen\Commands\ScheduleWhenCommand;
8+
9+
class ScheduleWhenCommandTest extends TestCase
10+
{
11+
public function test_it_requires_a_valid_date()
12+
{
13+
$this->mock(ScheduleWhen::class)
14+
->shouldNotReceive('handle');
15+
16+
$this->artisan(ScheduleWhenCommand::class, ['dateTime' => 'Banana Pancakes'])->assertExitCode(1);
17+
}
18+
19+
public function test_it_will_default_to_now_if_no_argument()
20+
{
21+
$this->mock(ScheduleWhen::class)
22+
->shouldReceive('handle');
23+
24+
$this->artisan(ScheduleWhenCommand::class)->assertExitCode(0);
25+
}
26+
27+
public function test_it_will_call_the_service_if_date_is_valid()
28+
{
29+
$this->mock(ScheduleWhen::class)
30+
->shouldReceive('handle');
31+
32+
$this->artisan(ScheduleWhenCommand::class)->assertExitCode(0);
33+
}
34+
}

tests/ScheduleWhenTest.php

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
4+
namespace ArondeParon\ScheduleWhen\Tests;
5+
6+
use ArondeParon\ScheduleWhen\ScheduleWhen;
7+
use Carbon\Carbon;
8+
use Illuminate\Console\Command;
9+
use Illuminate\Console\Scheduling\Schedule;
10+
11+
class ScheduleWhenTest extends TestCase
12+
{
13+
/** @var Schedule */
14+
protected $schedule;
15+
16+
/** @var ScheduleWhen */
17+
protected $scheduleWhen;
18+
19+
public function test_it_will_return_an_empty_array_if_none_match()
20+
{
21+
$results = $this->scheduleWhen->handle(Carbon::now());
22+
23+
$this->assertCount(0, $results);
24+
}
25+
26+
public function test_it_will_return_an_array_of_matched_items()
27+
{
28+
$mockedCommand = $this->mock(Command::class);
29+
$anotherCommand = $this->mock(Command::class);
30+
31+
Carbon::setTestNow(Carbon::now()->setTimeFromTimeString('08:00'));
32+
33+
$this->schedule->command($mockedCommand)
34+
->daily()
35+
->at('08:00');
36+
37+
$this->schedule->command($anotherCommand)
38+
->hourly();
39+
40+
$results = $this->scheduleWhen->handle(Carbon::now());
41+
42+
$this->assertCount(2, $results);
43+
}
44+
45+
public function test_it_will_not_return_items_that_occur_on_other_intervals()
46+
{
47+
$mockedCommand = $this->mock(Command::class);
48+
$anotherCommand = $this->mock(Command::class);
49+
$someOtherCommand = $this->mock(Command::class);
50+
51+
Carbon::setTestNow(Carbon::now()->setTimeFromTimeString('08:00'));
52+
53+
$this->schedule->command($mockedCommand)
54+
->daily()
55+
->at('08:00');
56+
57+
$this->schedule->command($anotherCommand)
58+
->hourly();
59+
60+
$this->schedule->command($someOtherCommand)
61+
->dailyAt('08:01');
62+
63+
$results = $this->scheduleWhen->handle(Carbon::now());
64+
65+
$this->assertCount(2, $results);
66+
}
67+
68+
public function setUp(): void
69+
{
70+
parent::setUp();
71+
72+
$this->schedule = app(Schedule::class);
73+
$this->scheduleWhen = app(ScheduleWhen::class);
74+
}
75+
}

0 commit comments

Comments
 (0)