Skip to content

Commit d105b3a

Browse files
committed
add exclude param and tests
1 parent 83b33ca commit d105b3a

6 files changed

Lines changed: 116 additions & 23 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Statamic\Console\Commands\Concerns;
4+
5+
use Statamic\Facades\Stache;
6+
7+
trait HasExcludes
8+
{
9+
protected function addExcludes(?string $excludes = null): void
10+
{
11+
if (empty($excludes)) {
12+
return;
13+
}
14+
15+
foreach (explode(',', $excludes) as $key) {
16+
Stache::exclude($key);
17+
}
18+
}
19+
}

src/Console/Commands/StacheClear.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,22 @@
33
namespace Statamic\Console\Commands;
44

55
use Illuminate\Console\Command;
6+
use Statamic\Console\Commands\Concerns\HasExcludes;
67
use Statamic\Console\RunsInPlease;
78
use Statamic\Facades\Stache;
89

910
class StacheClear extends Command
1011
{
11-
use RunsInPlease;
12-
13-
/**
14-
* The name and signature of the console command.
15-
*
16-
* @var string
17-
*/
18-
protected $signature = 'statamic:stache:clear';
19-
20-
/**
21-
* The console command description.
22-
*
23-
* @var string
24-
*/
12+
use HasExcludes, RunsInPlease;
13+
14+
protected $signature = 'statamic:stache:clear {--exclude=}';
15+
2516
protected $description = 'Clear the "Stache" cache';
2617

27-
/**
28-
* Execute the console command.
29-
*
30-
* @return void
31-
*/
3218
public function handle()
3319
{
20+
$this->addExcludes($this->option('exclude'));
21+
3422
Stache::clear();
3523

3624
$this->components->info('You have trimmed the Stache. It looks dashing.');

src/Console/Commands/StacheRefresh.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@
33
namespace Statamic\Console\Commands;
44

55
use Illuminate\Console\Command;
6+
use Statamic\Console\Commands\Concerns\HasExcludes;
67
use Statamic\Console\RunsInPlease;
78
use Statamic\Facades\Stache;
89

910
use function Laravel\Prompts\spin;
1011

1112
class StacheRefresh extends Command
1213
{
13-
use RunsInPlease;
14+
use HasExcludes, RunsInPlease;
15+
16+
protected $signature = 'statamic:stache:refresh {--exclude=}';
1417

15-
protected $signature = 'statamic:stache:refresh';
1618
protected $description = 'Clear and rebuild the "Stache" cache';
1719

1820
public function handle()
1921
{
22+
$this->addExcludes($this->option('exclude'));
23+
2024
spin(callback: fn () => Stache::clear(), message: 'Clearing the Stache...');
2125
spin(callback: fn () => Stache::warm(), message: 'Warming the Stache...');
2226

src/Console/Commands/StacheWarm.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@
33
namespace Statamic\Console\Commands;
44

55
use Illuminate\Console\Command;
6+
use Statamic\Console\Commands\Concerns\HasExcludes;
67
use Statamic\Console\RunsInPlease;
78
use Statamic\Facades\Stache;
89

910
use function Laravel\Prompts\spin;
1011

1112
class StacheWarm extends Command
1213
{
13-
use RunsInPlease;
14+
use HasExcludes, RunsInPlease;
15+
16+
protected $signature = 'statamic:stache:warm {--exclude=}';
1417

15-
protected $signature = 'statamic:stache:warm';
1618
protected $description = 'Build the "Stache" cache';
1719

1820
public function handle()
1921
{
22+
$this->addExcludes($this->option('exclude'));
23+
2024
spin(callback: fn () => Stache::warm(), message: 'Warming the Stache...');
2125

2226
$this->components->info('You have poured oil over the Stache and polished it until it shines. It is warm and ready');
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Tests\Console\Commands;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use Statamic\Console\Commands\StacheClear;
7+
use Statamic\Facades\Stache;
8+
use Tests\TestCase;
9+
10+
class StacheClearTest extends TestCase
11+
{
12+
#[Test]
13+
public function it_doesnt_add_any_exclusion_if_no_parameter()
14+
{
15+
Stache::shouldReceive('exclude')->never()
16+
->shouldReceive('clear')->once();
17+
18+
$this->artisan(StacheClear::class);
19+
}
20+
21+
#[Test]
22+
public function it_adds_exclude()
23+
{
24+
Stache::shouldReceive('exclude')->once()->with('foo')->andReturn()
25+
->shouldReceive('clear')->once()->andReturn();
26+
27+
$this->artisan(StacheClear::class, ['--exclude' => 'foo']);
28+
}
29+
30+
#[Test]
31+
public function it_adds_multiple_excludes()
32+
{
33+
Stache::shouldReceive('exclude')->once()->with('foo')->andReturn()
34+
->shouldReceive('exclude')->once()->with('bar')->andReturn()
35+
->shouldReceive('clear')->once()->andReturn();
36+
37+
$this->artisan(StacheClear::class, ['--exclude' => 'foo,bar']);
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Tests\Console\Commands;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use Statamic\Console\Commands\StacheWarm;
7+
use Statamic\Facades\Stache;
8+
use Tests\TestCase;
9+
10+
class StacheWarmTest extends TestCase
11+
{
12+
#[Test]
13+
public function it_doesnt_add_any_exclusion_if_no_parameter()
14+
{
15+
Stache::shouldReceive('exclude')->never()
16+
->shouldReceive('warm')->once();
17+
18+
$this->artisan(StacheWarm::class);
19+
}
20+
21+
#[Test]
22+
public function it_adds_exclude()
23+
{
24+
Stache::shouldReceive('exclude')->once()->with('foo')->andReturn()
25+
->shouldReceive('warm')->once()->andReturn();
26+
27+
$this->artisan(StacheWarm::class, ['--exclude' => 'foo']);
28+
}
29+
30+
#[Test]
31+
public function it_adds_multiple_excludes()
32+
{
33+
Stache::shouldReceive('exclude')->once()->with('foo')->andReturn()
34+
->shouldReceive('exclude')->once()->with('bar')->andReturn()
35+
->shouldReceive('warm')->once()->andReturn();
36+
37+
$this->artisan(StacheWarm::class, ['--exclude' => 'foo,bar']);
38+
}
39+
}

0 commit comments

Comments
 (0)