Skip to content

Commit

Permalink
Added tests (#30)
Browse files Browse the repository at this point in the history
Added tests and ran Pint
  • Loading branch information
kevinmeijer97 authored Nov 5, 2024
1 parent 233dcd9 commit 1b831e3
Show file tree
Hide file tree
Showing 32 changed files with 717 additions and 88 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/analyse.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: analyse

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
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: Analyse
run: composer analyse
29 changes: 29 additions & 0 deletions .github/workflows/changelog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: "Update Changelog"

on:
release:
types: [ published, edited, deleted ]

jobs:
generate:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3
with:
ref: ${{ github.event.release.target_commitish }}

- name: Generate changelog
uses: justbetter/generate-changelogs-action@main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
repository: ${{ github.repository }}

- name: Commit CHANGELOG
uses: stefanzweifel/git-auto-commit-action@v4
with:
branch: ${{ github.event.release.target_commitish }}
commit_message: Update CHANGELOG
file_pattern: CHANGELOG.md
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=100
32 changes: 32 additions & 0 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: style

on:
push:
branches:
- master
jobs:
style:
name: Style
runs-on: ubuntu-latest

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

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
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 install

- name: Style
run: composer fix-style

- name: Commit Changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Fix styling changes
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.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
27 changes: 19 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1|^8.2|^8.3",
"ext-fileinfo": "*",
"statamic/cms": "^4.0 || ^5.0",
"laravel/framework": "^9.50.0 || ^10.0 || ^11.0",
"statamic/cms": "^5.0",
"laravel/framework": "^10.0 || ^11.0",
"league/glide": "^2.2"
},
"require-dev": {
"laravel/pint": "^1.1",
"orchestra/testbench": "^7.0 || ^8.0",
"phpunit/phpunit": "^9.0 || ^10.0"
"laravel/pint": "^1.7",
"larastan/larastan": "^2.5",
"phpstan/phpstan-mockery": "^1.1",
"phpunit/phpunit": "^10.1",
"orchestra/testbench": "^8.0|^9.0",
"pestphp/pest": "^2.0"
},
"autoload": {
"psr-4": {
Expand All @@ -38,12 +41,20 @@
},
"scripts": {
"test": "phpunit",
"style": "pint --test"
"analyse": "phpstan",
"style": "pint --test",
"quality": [
"@test",
"@analyse",
"@style"
],
"fix-style": "pint"
},
"config": {
"sort-packages": true,
"allow-plugins": {
"pixelfear/composer-dist-plugin": true
"pixelfear/composer-dist-plugin": true,
"pestphp/pest-plugin": true
}
},
"extra": {
Expand Down
2 changes: 1 addition & 1 deletion config/image-optimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@

// You can exclude containers from optimization entirely here
'excluded_containers' => [],
];
];
11 changes: 11 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
includes:
- ./vendor/larastan/larastan/extension.neon
- ./vendor/phpstan/phpstan-mockery/extension.neon

parameters:
paths:
- src
- tests
level: 8
ignoreErrors:
- identifier: missingType.iterableValue
13 changes: 5 additions & 8 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<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 processUncoveredFiles="true">
<coverage/>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</phpunit>
</source>
</phpunit>
2 changes: 1 addition & 1 deletion routes/cp.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Route::prefix('statamic-image-optimize')
->name('statamic-image-optimize.')
->controller(ImageResizeController::class)
->group(function() {
->group(function () {
Route::get('/', 'index')->name('index');
Route::get('/resize-images/{forceAll?}', 'resizeImages')->name('resize-images');
Route::get('/resize-images-count/{batchId?}', 'resizeImagesJobCount')->name('resize-images-count');
Expand Down
23 changes: 13 additions & 10 deletions src/Actions/OptimizeAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,35 @@

use JustBetter\ImageOptimize\Jobs\ResizeImageJob;
use Statamic\Actions\Action;
use Statamic\Contracts\Assets\Asset;
use Statamic\Assets\Asset;

class OptimizeAssets extends Action
{
public static function title()
public static function title(): string
{
return __('image-optimize::messages.optimize');
}

public function visibleTo($item)
// @phpstan-ignore-next-line
public function visibleTo($item): bool
{
return $item instanceof Asset;
}

public function visibleToBulk($items)
// @phpstan-ignore-next-line
public function visibleToBulk($items): bool
{
return $this->visibleTo($items->first());
}

public function run($assets, $values)
// @phpstan-ignore-next-line
public function run($assets, $values): void
{
collect($assets)
->each(function ($asset) {
if ($asset instanceof Asset && $asset->isImage()) {
ResizeImageJob::dispatch($asset);
}
// @phpstan-ignore-next-line
collect($assets ?? [])
->filter(fn (mixed $asset): bool => $asset instanceof Asset)
->each(function (Asset $asset): void {
ResizeImageJob::dispatch($asset);
});
}
}
11 changes: 9 additions & 2 deletions src/Actions/ResizeImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@ class ResizeImage implements ResizesImage
{
public function resize(Asset $asset, ?int $width = null, ?int $height = null): void
{
if (! $asset->exists() ||
! $asset->isImage() ||
in_array($asset->containerHandle(), config('image-optimize.excluded_containers'))
) {
return;
}

$width ??= (int) config('image-optimize.default_resize_width');
$height ??= (int) config('image-optimize.default_resize_height');

// Prevents exceptions occurring when resizing non-compatible filetypes like SVG.
try {
$orientedImage = Image::make($asset->resolvedPath())->orientate();

$image = (new Size())->runMaxResize($orientedImage, $width, $height);
$image = (new Size)->runMaxResize($orientedImage, $width, $height);

$asset->disk()->filesystem()->put($asset->path(), $image->encode());

Expand All @@ -37,6 +44,6 @@ public function resize(Asset $asset, ?int $width = null, ?int $height = null): v

public static function bind(): void
{
app()->singleton(ResizesImage::class,static::class);
app()->singleton(ResizesImage::class, static::class);
}
}
Loading

0 comments on commit 1b831e3

Please sign in to comment.