From a9b3b9187148d3dbe7819c81adde263c793171fb Mon Sep 17 00:00:00 2001 From: Marty Friedel <1491079+martyf@users.noreply.github.com> Date: Fri, 10 May 2024 10:32:59 +0930 Subject: [PATCH] Support for Statamic 5 --- README.md | 2 +- composer.json | 2 +- src/Support/StatamicLoggerReader.php | 50 ++++----- tests/Support/StatamicLoggerReaderTest.php | 1 + tests/TestCase.php | 125 +++++++++------------ 5 files changed, 78 insertions(+), 102 deletions(-) diff --git a/README.md b/README.md index 84ce5b8..031010b 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ -![Statamic 4+](https://img.shields.io/badge/Statamic-4+-FF269E?style=for-the-badge&link=https://statamic.com) +![Statamic 5.0](https://img.shields.io/badge/Statamic-5.0-FF269E?style=for-the-badge&link=https://statamic.com) [![Logger for Statamic on Packagist](https://img.shields.io/packagist/v/mitydigital/statamic-logger?style=for-the-badge)](https://packagist.org/packages/mitydigital/statamic-logger/stats) --- diff --git a/composer.json b/composer.json index c3c6aea..23c4c1b 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ ], "require": { "php": "^8.2", - "statamic/cms": "^4.0" + "statamic/cms": "^5.0" }, "extra": { "statamic": { diff --git a/src/Support/StatamicLoggerReader.php b/src/Support/StatamicLoggerReader.php index 72bf993..14e935c 100644 --- a/src/Support/StatamicLoggerReader.php +++ b/src/Support/StatamicLoggerReader.php @@ -44,31 +44,6 @@ public function getDates(): Collection return $logs->sortKeysDesc(); } - protected function read(string $date): bool - { - // get the configured path and filename - $path = \MityDigital\StatamicLogger\Facades\StatamicLogger::getStoragePath(); - $filename = \MityDigital\StatamicLogger\Facades\StatamicLogger::getStorageFilename(); - - // build the path - $fullPath = storage_path($path.DIRECTORY_SEPARATOR.$filename.'-'.$date.'.log'); - - // if the file doesn't exist, return false - if (! file_exists($fullPath)) { - return false; - } - - // load the file - $this->file = new SplFileObject($fullPath); - $this->file->seek($this->file->getSize()); - - // set the total number of records - $this->total = $this->file->key(); - - // loaded and total found, return true - return true; - } - public function paginate(string $date, int $page, int $perPage): array|Collection { // set the page and per page values @@ -104,6 +79,31 @@ public function paginate(string $date, int $page, int $perPage): array|Collectio return $lines; } + protected function read(string $date): bool + { + // get the configured path and filename + $path = \MityDigital\StatamicLogger\Facades\StatamicLogger::getStoragePath(); + $filename = \MityDigital\StatamicLogger\Facades\StatamicLogger::getStorageFilename(); + + // build the path + $fullPath = storage_path($path.DIRECTORY_SEPARATOR.$filename.'-'.$date.'.log'); + + // if the file doesn't exist, return false + if (! file_exists($fullPath)) { + return false; + } + + // load the file + $this->file = new SplFileObject($fullPath); + $this->file->seek($this->file->getSize()); + + // set the total number of records + $this->total = $this->file->key(); + + // loaded and total found, return true + return true; + } + public function getPage(): int { return $this->page; diff --git a/tests/Support/StatamicLoggerReaderTest.php b/tests/Support/StatamicLoggerReaderTest.php index 17aa847..ee927fd 100644 --- a/tests/Support/StatamicLoggerReaderTest.php +++ b/tests/Support/StatamicLoggerReaderTest.php @@ -142,6 +142,7 @@ it('returns the correct page after pagination', function () { // paginate, then getPage $this->reader->paginate($this->today, 2, 10); + expect($this->reader->getPage())->toBe(2); $this->reader->paginate($this->today, 4, 10); diff --git a/tests/TestCase.php b/tests/TestCase.php index 40f5265..5b6269a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,57 +2,61 @@ namespace MityDigital\StatamicLogger\Tests; -use Facades\Statamic\Version; use Illuminate\Encryption\Encrypter; use Illuminate\Support\Facades\File; use MityDigital\StatamicLogger\ServiceProvider; -use Orchestra\Testbench\TestCase as OrchestraTestCase; -use Statamic\Console\Processes\Composer; -use Statamic\Extend\Manifest; -use Statamic\Providers\StatamicServiceProvider; +use Statamic\Facades\Site; use Statamic\Statamic; +use Statamic\Testing\AddonTestCase; -abstract class TestCase extends OrchestraTestCase +abstract class TestCase extends AddonTestCase { protected $shouldFakeVersion = true; - protected function setUp(): void + protected string $addonServiceProvider = ServiceProvider::class; + + protected function getEnvironmentSetUp($app) { - parent::setUp(); + parent::getEnvironmentSetUp($app); - $this->withoutVite(); + // assets and asset containers + $app['config']->set('statamic.stache.stores.asset-containers.directory', + $this->getTempDirectory('/content/assets')); - if ($this->shouldFakeVersion) { - Version::shouldReceive('get') - ->andReturn(Composer::create(__DIR__.'/../')->installedVersion(Statamic::PACKAGE)); - } - } + // collections, entries and collection trees + $app['config']->set('statamic.stache.stores.collections.directory', + $this->getTempDirectory('/content/collections')); + $app['config']->set('statamic.stache.stores.collection-trees.directory', + $this->getTempDirectory('/content/collection-trees')); + $app['config']->set('statamic.stache.stores.entries.directory', + $this->getTempDirectory('/content/collections')); - protected function getPackageProviders($app) - { - return [ - StatamicServiceProvider::class, - ServiceProvider::class, - ]; - } + // taxonomies and terms + $app['config']->set('statamic.stache.stores.taxonomies.directory', + $this->getTempDirectory('/content/taxonomies')); + $app['config']->set('statamic.stache.stores.terms.directory', + $this->getTempDirectory('/content/terms')); - protected function getPackageAliases($app) - { - return [ - 'Statamic' => Statamic::class, - ]; + // navigation and nav-trees + $app['config']->set('statamic.stache.stores.navigation.directory', + $this->getTempDirectory('/content/nav')); + $app['config']->set('statamic.stache.stores.nav-trees.directory', + $this->getTempDirectory('/content/nav')); + + // globals + $app['config']->set('statamic.stache.stores.globals.directory', + $this->getTempDirectory('/content/globals')); + $app['config']->set('statamic.stache.stores.global-variables.directory', + $this->getTempDirectory('/content/globals')); + + // users + $app['config']->set('statamic.stache.stores.users.directory', + $this->getTempDirectory('/content/users')); } - protected function getEnvironmentSetUp($app) + public function getTempDirectory($suffix = ''): string { - parent::getEnvironmentSetUp($app); - - $app->make(Manifest::class)->manifest = [ - 'mitydigital/statamic-logger' => [ - 'id' => 'mitydigital/statamic-logger', - 'namespace' => 'MityDigital\\StatamicLogger', - ], - ]; + return __DIR__.'/TestSupport/'.($suffix == '' ? '' : '/'.$suffix); } protected function resolveApplicationConfiguration($app) @@ -64,7 +68,6 @@ protected function resolveApplicationConfiguration($app) 'cp', 'forms', 'static_caching', - 'sites', 'stache', 'system', 'users', @@ -73,7 +76,7 @@ protected function resolveApplicationConfiguration($app) foreach ($configs as $config) { $app['config']->set( "statamic.$config", - require(__DIR__."/../vendor/statamic/cms/config/{$config}.php") + require (__DIR__."/../vendor/statamic/cms/config/{$config}.php") ); } @@ -91,39 +94,16 @@ protected function resolveApplicationConfiguration($app) $app['config']->set('auth.providers.users.driver', 'statamic'); $app['config']->set('statamic.users.repository', 'file'); - // assets and asset containers - $app['config']->set('statamic.stache.stores.asset-containers.directory', - $this->getTempDirectory('/content/assets')); - - // collections, entries and collection trees - $app['config']->set('statamic.stache.stores.collections.directory', - $this->getTempDirectory('/content/collections')); - $app['config']->set('statamic.stache.stores.collection-trees.directory', - $this->getTempDirectory('/content/collection-trees')); - $app['config']->set('statamic.stache.stores.entries.directory', - $this->getTempDirectory('/content/collections')); - - // taxonomies and terms - $app['config']->set('statamic.stache.stores.taxonomies.directory', - $this->getTempDirectory('/content/taxonomies')); - $app['config']->set('statamic.stache.stores.terms.directory', - $this->getTempDirectory('/content/terms')); - - // navigation and nav-trees - $app['config']->set('statamic.stache.stores.navigation.directory', - $this->getTempDirectory('/content/nav')); - $app['config']->set('statamic.stache.stores.nav-trees.directory', - $this->getTempDirectory('/content/nav')); - - // globals - $app['config']->set('statamic.stache.stores.globals.directory', - $this->getTempDirectory('/content/globals')); - $app['config']->set('statamic.stache.stores.global-variables.directory', - $this->getTempDirectory('/content/globals')); - - // users - $app['config']->set('statamic.stache.stores.users.directory', - $this->getTempDirectory('/content/users')); + Statamic::booted(function () { + // configure to be an AU site + Site::setSites([ + 'default' => [ + 'name' => config('app.name'), + 'locale' => 'en_AU', + 'url' => '/', + ], + ]); + }); } protected function tearDown(): void @@ -132,9 +112,4 @@ protected function tearDown(): void parent::tearDown(); } - - public function getTempDirectory($suffix = ''): string - { - return __DIR__.'/TestSupport/'.($suffix == '' ? '' : '/'.$suffix); - } }