Skip to content

Commit

Permalink
Add support for Statamic 5 and Laravel 11 (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni authored May 20, 2024
1 parent 43ef8e0 commit fb905d4
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 64 deletions.
30 changes: 19 additions & 11 deletions .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
name: Run Tests

on: [push, pull_request, workflow_dispatch]
on: ['push', 'pull_request']

jobs:
phpunit:
runs-on: ubuntu-latest
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
php: [8.2]
laravel: [9.*]
os: [ubuntu-latest]
php: [8.2, 8.3]
laravel: [10.*, 11.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 9.*
testbench: ^7.1
- laravel: 10.*
testbench: 8.*
- laravel: 11.*
testbench: 9.*

name: PHP ${{ matrix.php }} – Laravel ${{ matrix.laravel }} - ${{ matrix.stability }}
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none

- name: Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: Run tests
- name: Execute tests
run: vendor/bin/phpunit
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
],
"require": {
"php": "^8.2",
"laravel/framework": "^9.0 || ^10.0",
"laravel/framework": "^10.0 || ^11.0",
"spatie/browsershot": "^4.0",
"spatie/image": "^3.4",
"spatie/laravel-ray": "^1.32",
"spatie/schema-org": "^3.14",
"statamic/cms": "^4.27",
"statamic/cms": "^5.3",
"whitecube/lingua": "^1.1"
},
"require-dev": {
"nunomaduro/collision": "^6.1",
"orchestra/testbench": "7.1",
"phpunit/phpunit": "^9.5"
"nunomaduro/collision": "^7.0 || ^8.0",
"orchestra/testbench": "^8.0 || ^9.0",
"phpunit/phpunit": "^10.0"
},
"autoload": {
"psr-4": {
Expand Down
53 changes: 22 additions & 31 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
printerClass="NunoMaduro\Collision\Adapters\Phpunit\Printer">
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="base64:mIRM5Fpb6DtuRIejBwkqpt3c/ovlFZsSpo66efwo3g0="/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="MAIL_MAILER" value="array"/>
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="base64:mIRM5Fpb6DtuRIejBwkqpt3c/ovlFZsSpo66efwo3g0="/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="MAIL_MAILER" value="array"/>
</php>
<source>
<include>
<directory suffix=".php">./app</directory>
</include>
</source>
</phpunit>
2 changes: 1 addition & 1 deletion src/Data/SeoDefaultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function fileData(): array
'title' => $this->title(),
];

if (! Site::hasMultiple()) {
if (! Site::multiEnabled()) {
$data['data'] = Arr::removeNullValues(
$this->inDefaultSite()->data()->all()
);
Expand Down
6 changes: 3 additions & 3 deletions src/Data/SeoVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function path(): string
{
return vsprintf('%s/%s%s.yaml', [
Stache::store('seo')->store($this->type())->directory(),
Site::hasMultiple() ? $this->locale().'/' : '',
Site::multiEnabled() ? $this->locale().'/' : '',
$this->handle(),
]);
}
Expand All @@ -100,7 +100,7 @@ protected function cpUrl($route)
{
$params = [$this->handle()];

if (Site::hasMultiple()) {
if (Site::multiEnabled()) {
$params['site'] = $this->locale();
}

Expand Down Expand Up @@ -162,7 +162,7 @@ public function fileData(): array
// We only want to keep values of fields that exist in the blueprint.
$data = $this->data()->only($this->blueprintFields())->all();

if (Site::hasMultiple() && $this->hasOrigin()) {
if (Site::multiEnabled() && $this->hasOrigin()) {
$data['origin'] = $this->origin()->locale();
}

Expand Down
3 changes: 2 additions & 1 deletion src/Fields/BaseFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Aerni\AdvancedSeo\Contracts\Fields;
use Aerni\AdvancedSeo\Data\DefaultsData;
use Aerni\AdvancedSeo\Support\Helpers;
use Illuminate\Support\Arr;

abstract class BaseFields implements Fields
{
Expand All @@ -26,7 +27,7 @@ public function get(): array
{
return [
[
'fields' => array_flatten($this->sections(), 1),
'fields' => Arr::flatten($this->sections(), 1),
],
];
}
Expand Down
3 changes: 2 additions & 1 deletion src/Fields/ContentDefaultsFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Aerni\AdvancedSeo\Features\SocialImagesGenerator;
use Aerni\AdvancedSeo\Models\Defaults;
use Aerni\AdvancedSeo\Models\SocialImageTheme;
use Illuminate\Support\Str;

class ContentDefaultsFields extends BaseFields
{
Expand Down Expand Up @@ -300,7 +301,7 @@ protected function canonicalUrl(): array
'display' => $this->trans('seo_canonical_type.display'),
'instructions' => $this->trans('seo_canonical_type.default_instructions'),
'options' => [
'current' => $this->trans('seo_canonical_type.current', ['type' => ucfirst(str_singular($this->typePlaceholder()))]),
'current' => $this->trans('seo_canonical_type.current', ['type' => ucfirst(Str::singular($this->typePlaceholder()))]),
'other' => $this->trans('seo_canonical_type.other'),
'custom' => $this->trans('seo_canonical_type.custom'),
],
Expand Down
3 changes: 2 additions & 1 deletion src/Fields/OnPageSeoFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Aerni\AdvancedSeo\Features\Sitemap;
use Aerni\AdvancedSeo\Features\SocialImagesGenerator;
use Aerni\AdvancedSeo\Models\SocialImageTheme;
use Illuminate\Support\Str;

class OnPageSeoFields extends BaseFields
{
Expand Down Expand Up @@ -401,7 +402,7 @@ protected function canonicalUrl(): array
'field' => [
'type' => 'button_group',
'options' => [
'current' => $this->trans('seo_canonical_type.current', ['type' => ucfirst(str_singular($this->typePlaceholder()))]),
'current' => $this->trans('seo_canonical_type.current', ['type' => ucfirst(Str::singular($this->typePlaceholder()))]),
'other' => $this->trans('seo_canonical_type.other'),
'custom' => $this->trans('seo_canonical_type.custom'),
],
Expand Down
6 changes: 4 additions & 2 deletions src/GraphQL/Fields/ContentDefaultsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Aerni\AdvancedSeo\GraphQL\Types\ContentDefaultsType;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Rebing\GraphQL\Support\Field;
use Statamic\Facades\GraphQL;

Expand Down Expand Up @@ -37,7 +39,7 @@ public function type(): Type

protected function resolve($root, $args, $context, ResolveInfo $info): ?SeoVariables
{
$set = Seo::find(str_plural($info->fieldName), $args['handle']);
$set = Seo::find(Str::plural($info->fieldName), $args['handle']);

if (! $set) {
return null;
Expand All @@ -47,7 +49,7 @@ protected function resolve($root, $args, $context, ResolveInfo $info): ?SeoVaria
return null;
}

return array_has($args, 'site')
return Arr::has($args, 'site')
? $set->in($args['site'])
: $set->inDefaultSite();
}
Expand Down
6 changes: 4 additions & 2 deletions src/GraphQL/Types/SiteDefaultsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Aerni\AdvancedSeo\Data\SeoVariables;
use Aerni\AdvancedSeo\Facades\Seo;
use GraphQL\Type\Definition\ResolveInfo;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Rebing\GraphQL\Support\Type;
use Statamic\Facades\GraphQL;

Expand Down Expand Up @@ -46,7 +48,7 @@ public function fields(): array
private function resolver(): callable
{
return function ($root, $args, $context, ResolveInfo $info): ?SeoVariables {
$set = Seo::find('site', snake_case($info->fieldName));
$set = Seo::find('site', Str::snake($info->fieldName));

if (! $set) {
return null;
Expand All @@ -56,7 +58,7 @@ private function resolver(): callable
return null;
}

return array_has($root, 'site')
return Arr::has($root, 'site')
? $set->in($root['site'])
: $set->inDefaultSite();
};
Expand Down
5 changes: 3 additions & 2 deletions src/Http/Controllers/Cp/SeoDefaultsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Statamic\CP\Breadcrumbs;
use Statamic\Exceptions\NotFoundHttpException;
use Statamic\Facades\Site;
Expand Down Expand Up @@ -173,7 +174,7 @@ protected function defaults(): Collection
protected function flashDefaultsUnavailable(): void
{
session()->now('error', __('There are no :type defaults available for the selected site.', [
'type' => str_singular($this->type()),
'type' => Str::singular($this->type()),
]));

throw new NotFoundHttpException();
Expand All @@ -184,7 +185,7 @@ protected function redirectToIndex(SeoDefaultSet $set, string $site): RedirectRe
return redirect(cp_route("advanced-seo.{$set->type()}.index"))
->with('error', __('The :set :type is not available in the selected site.', [
'set' => $set->title(),
'type' => str_singular($this->type()),
'type' => Str::singular($this->type()),
]));
}

Expand Down
9 changes: 5 additions & 4 deletions src/Stache/SeoDefaultsStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Aerni\AdvancedSeo\Data\SeoDefaultSet;
use Aerni\AdvancedSeo\Data\SeoVariables;
use Aerni\AdvancedSeo\Facades\Seo;
use Illuminate\Support\Str;
use Statamic\Facades\File;
use Statamic\Facades\Path;
use Statamic\Facades\Site;
Expand All @@ -28,7 +29,7 @@ public function makeItemFromFile($path, $contents): SeoDefaultSet
{
$data = YAML::file($path)->parse($contents);

return Site::hasMultiple()
return Site::multiEnabled()
? $this->makeMultiSiteDefaultFromFile($path)
: $this->makeSingleSiteDefaultFromFile($path, $data);
}
Expand Down Expand Up @@ -91,7 +92,7 @@ protected function makeVariables(SeoDefaultSet $set, string $site): ?SeoVariable

protected function extractAttributesFromPath(string $path): array
{
$relative = str_after($path, $this->parent->directory());
$relative = Str::after($path, $this->parent->directory());
$type = pathinfo($relative, PATHINFO_DIRNAME);
$handle = pathinfo($relative, PATHINFO_FILENAME);

Expand All @@ -102,7 +103,7 @@ public function save($set): void
{
parent::save($set);

if (Site::hasMultiple()) {
if (Site::multiEnabled()) {
Site::all()->each(function ($site) use ($set) {
$site = $site->handle();
$set->existsIn($site) ? $set->in($site)->writeFile() : $set->makeLocalization($site)->deleteFile();
Expand All @@ -114,7 +115,7 @@ public function delete($set): void
{
parent::delete($set);

if (Site::hasMultiple()) {
if (Site::multiEnabled()) {
$set->localizations()->each(function ($localization) {
$localization->deleteFile();
});
Expand Down

0 comments on commit fb905d4

Please sign in to comment.