Skip to content

Commit

Permalink
Add sanity checks for artisan command with feedback as to what needs …
Browse files Browse the repository at this point in the history
…to be fixed
  • Loading branch information
mikebronner committed Dec 31, 2017
1 parent 50ed138 commit 06cea55
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [0.2.15] - 30 Dec 2017
### Added
- sanity checks for artisan command with feedback as to what needs to be fixed.

## [0.2.14] - 30 Dec 2017
### Added
- ability to flush cache for a given model via Artisan command.
Expand Down
19 changes: 18 additions & 1 deletion src/Console/Commands/Flush.php
@@ -1,6 +1,7 @@
<?php namespace GeneaLabs\LaravelModelCaching\Console\Commands;

use Illuminate\Console\Command;
use GeneaLabs\LaravelModelCaching\CachedModel;

class Flush extends Command
{
Expand All @@ -10,8 +11,24 @@ class Flush extends Command
public function handle()
{
$option = $this->option('model');

if (! $option) {
$this->error("You must specify a model to flush a model's cache:");
$this->line("modelCache:flush --model=App\\Model");

return 1;
}

$model = new $option;

if (! $model instanceof CachedModel) {
$this->error("'{$option}' is not an instance of CachedModel.");
$this->line("Only CachedModel instances can be flushed.");

return 1;
}

$model->flushCache();
$this->info("Cache for model '{$option}' flushed.");
$this->info("✔︎ Cache for model '{$option}' has been flushed.");
}
}
18 changes: 18 additions & 0 deletions tests/Unit/Console/Commands/FlushTest.php
Expand Up @@ -5,6 +5,7 @@
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
use GeneaLabs\LaravelModelCaching\Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

Expand Down Expand Up @@ -82,4 +83,21 @@ public function testGivenModelWithRelationshipIsFlushed()
$this->assertEmpty($flushedResults);
$this->assertEquals($result, 0);
}

public function testNonCachedModelsCannotBeFlushed()
{
$result = $this->artisan(
'modelCache:flush',
['--model' => UncachedAuthor::class]
);

$this->assertEquals($result, 1);
}

public function testModelOptionIsSpecified()
{
$result = $this->artisan('modelCache:flush', []);

$this->assertEquals($result, 1);
}
}

0 comments on commit 06cea55

Please sign in to comment.