Skip to content

Commit

Permalink
Merge pull request #4 from justbetter/feature/wip-features
Browse files Browse the repository at this point in the history
Added phpunit tests & added package banner
  • Loading branch information
BobWez98 authored Oct 22, 2024
2 parents bea7ee7 + 866119a commit aaef323
Show file tree
Hide file tree
Showing 17 changed files with 318 additions and 26 deletions.
23 changes: 18 additions & 5 deletions .github/workflows/analyse.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,35 @@ name: analyse
on: ['push', 'pull_request']

jobs:
analyse:
runs-on: ubuntu-latest
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.3]
laravel: [11.*]
stability: [prefer-stable]
include:
- laravel: 11.*
testbench: 9.*

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

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

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

- name: Install dependencies
run: |
composer require "laravel/framework:10.*" "orchestra/testbench:8.*" --no-interaction --no-update
composer update --prefer-stable --prefer-dist --no-interaction
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: Analyse
run: composer analyse
38 changes: 38 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: coverage

on: ['push', 'pull_request']

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.3]
laravel: [11.*]
stability: [prefer-stable]
include:
- laravel: 11.*
testbench: 9.*

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

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

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

- name: Install dependencies
run: |
composer config allow-plugins.pestphp/pest-plugin true
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: Execute tests
run: XDEBUG_MODE=coverage php vendor/bin/pest --coverage --min=85
40 changes: 40 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: tests

on: [ 'push', 'pull_request' ]

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

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

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

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

- 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: Execute tests
run: composer test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ composer.lock
node_modules
vendor
.php_cs.cache
.phpunit.result.cache
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<a href="https://github.com/justbetter/statamic-glide-directive" title="JustBetter">
<img src="./art/banner.png" alt="Banner">
</a>

# Statamic glide directive

> Blade directive to use glide with images
Expand Down
Binary file added art/banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 19 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,33 @@
"description": "Addon that adds a glide blade directive",
"type": "package",
"require": {
"php": "^8.2|^8.3",
"php": "^8.1|^8.2|^8.3",
"laravel/framework": "^11.0",
"statamic/cms": "^5.0"
},
"require-dev": {
"laravel/pint": "^1.7",
"larastan/larastan": "^2.5",
"phpstan/phpstan-mockery": "^1.1",
"orchestra/testbench": "^8.0|^9.0"
"phpunit/phpunit": "^10.1",
"orchestra/testbench": "^8.0|^9.0",
"pestphp/pest": "^2.0"
},
"autoload": {
"psr-4": {
"JustBetter\\GlideDirective\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"JustBetter\\GlideDirective\\Tests\\": "tests"
}
},
"config": {
"sort-packages": true,
"allow-plugins": {
"pixelfear/composer-dist-plugin": true
"pixelfear/composer-dist-plugin": true,
"pestphp/pest-plugin": true
}
},
"extra": {
Expand All @@ -48,9 +56,15 @@
}
],
"scripts": {
"test": "phpunit",
"analyse": "phpstan",
"fix-style": "pint",
"style": "pint --test"
"style": "pint --test",
"quality": [
"@test",
"@analyse",
"@style"
],
"fix-style": "pint"
},
"minimum-stability": "dev"
}
4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ includes:
parameters:
paths:
- src
- tests
level: 8
checkMissingIterableValueType: false
ignoreErrors:
- identifier: missingType.iterableValue
14 changes: 14 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Tests">
<directory>./tests/*</directory>
</testsuite>
</testsuites>
<coverage/>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
9 changes: 4 additions & 5 deletions src/Jobs/GenerateGlideImageJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@

namespace JustBetter\GlideDirective\Jobs;

use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Statamic\Assets\Asset;
use Illuminate\Bus\Batchable;
use Statamic\Statamic;

class GenerateGlideImageJob implements ShouldQueue
{
use Batchable;
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use Batchable;

public function __construct(
public Asset $asset,
public string $preset = '',
public string $fit = '',
public ?string $format = null,
) {
}
) {}

public function handle(): void
{
Expand All @@ -34,7 +33,7 @@ public function handle(): void
'preset' => $this->preset,
'src' => $this->asset->url(),
'format' => $this->format,
'fit' => $this->fit
'fit' => $this->fit,
]
)->fetch();
}
Expand Down
21 changes: 11 additions & 10 deletions src/Responsive.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace JustBetter\GlideDirective;

use JustBetter\GlideDirective\Jobs\GenerateGlideImageJob;
use Statamic\Contracts\Imaging\ImageManipulator;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use JustBetter\GlideDirective\Jobs\GenerateGlideImageJob;
use Statamic\Assets\Asset;
use Statamic\Contracts\Imaging\ImageManipulator;
use Statamic\Facades\Glide;
use Statamic\Facades\Image;
use Statamic\Facades\URL;
use Statamic\Assets\Asset;
use Statamic\Fields\Value;
use Statamic\Support\Str;
use Statamic\Statamic;
use Statamic\Support\Str;

class Responsive
{
Expand Down Expand Up @@ -81,7 +81,7 @@ public static function getPresets(Asset $asset): array
}

if (self::canUseMimeTypeSource()) {
if($glideUrl = self::getGlideUrl($asset, $fit ?? $data['fit'], $preset)) {
if ($glideUrl = self::getGlideUrl($asset, $fit ?? $data['fit'], $preset)) {
$presets[$asset->mimeType()] .= $glideUrl.' '.$size;

if ($preset !== 'placeholder') {
Expand All @@ -99,15 +99,15 @@ public static function getPresets(Asset $asset): array
$index++;
}

if (!$webpSourceFound && !$mimeTypeSourceFound) {
if (! $webpSourceFound && ! $mimeTypeSourceFound) {
$presets = ['placeholder' => $asset->url()];
}

if (! isset($presets['placeholder'])) {
$presets['placeholder'] = Statamic::tag('glide:data_url')->params([
'preset' => collect($configPresets)->keys()->first(),
'src' => $asset->url(),
'fit' => 'crop_focal'
'fit' => 'crop_focal',
])->fetch();
}

Expand All @@ -121,7 +121,7 @@ protected static function getGlideUrl(Asset $asset, string $preset, string $fit,
'preset' => $preset,
'src' => $asset->url(),
'format' => $format,
'fit' => $fit
'fit' => $fit,
])->fetch();
}

Expand All @@ -133,10 +133,11 @@ protected static function getGlideUrl(Asset $asset, string $preset, string $fit,

$params = $manipulator->getParams();

$manipulationCacheKey = 'asset::' . $asset->id() . '::' . md5(json_encode($params) ? json_encode($params) : '');
$manipulationCacheKey = 'asset::'.$asset->id().'::'.md5(json_encode($params) ? json_encode($params) : '');

if ($cachedUrl = Glide::cacheStore()->get($manipulationCacheKey)) {
$url = Str::ensureLeft(config('statamic.assets.image_manipulation.route'), '/') . '/' . $cachedUrl;
$url = Str::ensureLeft(config('statamic.assets.image_manipulation.route'), '/').'/'.$cachedUrl;

return URL::encode($url);
}

Expand Down
3 changes: 3 additions & 0 deletions tests/Assets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!.gitignore
!test.png
Binary file added tests/Assets/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit aaef323

Please sign in to comment.