Skip to content

Commit 06cea55

Browse files
committed
Add sanity checks for artisan command with feedback as to what needs to be fixed
1 parent 50ed138 commit 06cea55

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.2.15] - 30 Dec 2017
8+
### Added
9+
- sanity checks for artisan command with feedback as to what needs to be fixed.
10+
711
## [0.2.14] - 30 Dec 2017
812
### Added
913
- ability to flush cache for a given model via Artisan command.

src/Console/Commands/Flush.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Console\Commands;
22

33
use Illuminate\Console\Command;
4+
use GeneaLabs\LaravelModelCaching\CachedModel;
45

56
class Flush extends Command
67
{
@@ -10,8 +11,24 @@ class Flush extends Command
1011
public function handle()
1112
{
1213
$option = $this->option('model');
14+
15+
if (! $option) {
16+
$this->error("You must specify a model to flush a model's cache:");
17+
$this->line("modelCache:flush --model=App\\Model");
18+
19+
return 1;
20+
}
21+
1322
$model = new $option;
23+
24+
if (! $model instanceof CachedModel) {
25+
$this->error("'{$option}' is not an instance of CachedModel.");
26+
$this->line("Only CachedModel instances can be flushed.");
27+
28+
return 1;
29+
}
30+
1431
$model->flushCache();
15-
$this->info("Cache for model '{$option}' flushed.");
32+
$this->info("✔︎ Cache for model '{$option}' has been flushed.");
1633
}
1734
}

tests/Unit/Console/Commands/FlushTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
66
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
77
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
8+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
89
use GeneaLabs\LaravelModelCaching\Tests\TestCase;
910
use Illuminate\Foundation\Testing\RefreshDatabase;
1011

@@ -82,4 +83,21 @@ public function testGivenModelWithRelationshipIsFlushed()
8283
$this->assertEmpty($flushedResults);
8384
$this->assertEquals($result, 0);
8485
}
86+
87+
public function testNonCachedModelsCannotBeFlushed()
88+
{
89+
$result = $this->artisan(
90+
'modelCache:flush',
91+
['--model' => UncachedAuthor::class]
92+
);
93+
94+
$this->assertEquals($result, 1);
95+
}
96+
97+
public function testModelOptionIsSpecified()
98+
{
99+
$result = $this->artisan('modelCache:flush', []);
100+
101+
$this->assertEquals($result, 1);
102+
}
85103
}

0 commit comments

Comments
 (0)