Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Addon Testing Changes #9871

Merged
merged 7 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Console/Commands/stubs/addon/TestCase.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace {{ namespace }}\Tests;

use {{ namespace }}\ServiceProvider;
use Statamic\Extend\AddonTestCase;
use Statamic\Testing\AddonTestCase;

abstract class TestCase extends AddonTestCase
{
Expand Down
25 changes: 24 additions & 1 deletion src/Extend/AddonTestCase.php → src/Testing/AddonTestCase.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

namespace Statamic\Extend;
namespace Statamic\Testing;

use Facades\Statamic\Version;
use Illuminate\Support\Str;
use Orchestra\Testbench\TestCase as OrchestraTestCase;
use ReflectionClass;
use Statamic\Console\Processes\Composer;
use Statamic\Extend\Manifest;
use Statamic\Providers\StatamicServiceProvider;
use Statamic\Statamic;
use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk;

abstract class AddonTestCase extends OrchestraTestCase
{
Expand All @@ -20,13 +23,33 @@ protected function setUp(): void
$this->withoutMix();
$this->withoutVite();

$uses = array_flip(class_uses_recursive(static::class));

if (isset($uses[PreventsSavingStacheItemsToDisk::class])) {
$reflection = new ReflectionClass($this);
$this->fakeStacheDirectory = Str::before(dirname($reflection->getFileName()), '/tests').'/tests/__fixtures__/dev-null';

$this->preventSavingStacheItemsToDisk();
}

Version::shouldReceive('get')->zeroOrMoreTimes()->andReturn(Composer::create(__DIR__.'/../')->installedVersion(Statamic::PACKAGE));
$this->addToAssertionCount(-1);

\Statamic\Facades\CP\Nav::shouldReceive('build')->zeroOrMoreTimes()->andReturn(collect());
$this->addToAssertionCount(-1); // Dont want to assert this
}

public function tearDown(): void
{
$uses = array_flip(class_uses_recursive(static::class));

if (isset($uses[PreventSavingStacheItemsToDisk::class])) {
$this->deleteFakeStacheDirectory();
}

parent::tearDown();
}

protected function getPackageProviders($app)
{
$serviceProviders = [
Expand Down
29 changes: 29 additions & 0 deletions src/Testing/Concerns/PreventsSavingStacheItemsToDisk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Statamic\Testing\Concerns;

use Statamic\Facades\Path;
use Statamic\Facades\Stache;
use Statamic\Support\Str;

trait PreventsSavingStacheItemsToDisk
{
protected function preventSavingStacheItemsToDisk(): void
{
$this->fakeStacheDirectory = Path::tidy($this->fakeStacheDirectory);

Stache::stores()->each(function ($store) {
$dir = Path::tidy(Str::before($this->fakeStacheDirectory, '/dev-null'));
$relative = Str::after(Str::after($store->directory(), $dir), '/');
$store->directory($this->fakeStacheDirectory.'/'.$relative);
});
}

protected function deleteFakeStacheDirectory(): void
{
app('files')->deleteDirectory($this->fakeStacheDirectory);

mkdir($this->fakeStacheDirectory);
touch($this->fakeStacheDirectory.'/.gitkeep');
}
}
25 changes: 2 additions & 23 deletions tests/PreventSavingStacheItemsToDisk.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,9 @@

namespace Tests;

use Statamic\Facades\Path;
use Statamic\Facades\Stache;
use Statamic\Support\Str;
use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk as BasePreventsSavingStacheItemsToDisk;

trait PreventSavingStacheItemsToDisk
{
protected $fakeStacheDirectory = __DIR__.'/__fixtures__/dev-null';

protected function preventSavingStacheItemsToDisk()
{
$this->fakeStacheDirectory = Path::tidy($this->fakeStacheDirectory);

Stache::stores()->each(function ($store) {
$dir = Path::tidy(__DIR__.'/__fixtures__');
$relative = Str::after(Str::after($store->directory(), $dir), '/');
$store->directory($this->fakeStacheDirectory.'/'.$relative);
});
}

protected function deleteFakeStacheDirectory()
{
app('files')->deleteDirectory($this->fakeStacheDirectory);

mkdir($this->fakeStacheDirectory);
touch($this->fakeStacheDirectory.'/.gitkeep');
}
use BasePreventsSavingStacheItemsToDisk;
}
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase

protected $shouldFakeVersion = true;
protected $shouldPreventNavBeingBuilt = true;
protected $fakeStacheDirectory = __DIR__.'/__fixtures__/dev-null';

protected function setUp(): void
{
Expand Down
Loading