From f8fb218b7fd8c22a501ba19a833c83a92c9576e1 Mon Sep 17 00:00:00 2001 From: Fabian Meyer <3982806+meyfa@users.noreply.github.com> Date: Fri, 18 Feb 2022 11:47:39 +0100 Subject: [PATCH 1/6] v1.x: chore(ci): Use GitHub Actions instead of Travis --- .github/workflows/main.yml | 44 +++++++++++++++++++++++++++++++++++ .gitignore | 3 +++ .travis.yml | 19 --------------- LICENSE | 2 +- README.md | 10 +++++++- tests/GDAssertTraitTest53.php | 2 +- 6 files changed, 58 insertions(+), 22 deletions(-) create mode 100644 .github/workflows/main.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..5eb30e6 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,44 @@ +name: CI + +on: + push: + branches: + - main + - v*.x + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + php-versions: ['5.3', '5.4', '5.5', '5.6', '7.2', '7.3', '7.4', '8'] + steps: + - uses: actions/checkout@v2 + + - name: Setup PHP ${{ matrix.php-versions }} + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + extensions: gd + coverage: none + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v2 + with: + path: vendor + key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-php- + + - name: Install dependencies + run: composer install --prefer-dist --no-progress + + - name: Run test suite (PHP 5.3) + if: ${{ matrix.php-version == '5.3' }} + run: vendor/bin/phpunit --configuration .phpunit53.xml + + - name: Run test suite + if: ${{ matrix.php-version != '5.3' }} + run: vendor/bin/phpunit diff --git a/.gitignore b/.gitignore index 31e3ac6..3cd8f99 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ +.DS_Store vendor/ .idea +.phpunit.result.cache +composer.lock diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 74e86e9..0000000 --- a/.travis.yml +++ /dev/null @@ -1,19 +0,0 @@ -language: php -php: - - 5.6 - - 7.0 - - 7.1 - - 7.2 -matrix: - include: - - php: 5.3 - dist: precise - - php: 5.4 - dist: trusty - - php: 5.5 - dist: trusty -install: - - composer install --no-interaction -script: - - if [[ ${TRAVIS_PHP_VERSION:0:3} != "5.3" ]]; then vendor/bin/phpunit --configuration .phpunit.xml; fi - - if [[ ${TRAVIS_PHP_VERSION:0:3} == "5.3" ]]; then vendor/bin/phpunit --configuration .phpunit53.xml; fi diff --git a/LICENSE b/LICENSE index 3f2ea3e..16a7973 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 Fabian Meyer +Copyright (c) 2018 - 2022 Fabian Meyer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index f49b929..783e03c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # AssertGD for PHPUnit -[![Build Status](https://travis-ci.com/meyfa/phpunit-assert-gd.svg?branch=master)](https://travis-ci.com/meyfa/phpunit-assert-gd) +[![CI](https://github.com/meyfa/phpunit-assert-gd/actions/workflows/main.yml/badge.svg?branch=v1.x)](https://github.com/meyfa/phpunit-assert-gd/actions/workflows/main.yml) Trying to assert images with PHPUnit? This project provides a constraint and the required assertions that allow you do to so. @@ -22,6 +22,14 @@ Add this package to your Composer dev-dependencies: composer require --dev meyfa/phpunit-assert-gd ``` +**Compatibility table** + +| AssertGD version | Supported PHP version | Supported PHPUnit version | +| :--------------- | :-------------------- | :------------------------ | +| 3.* | >= 7.3 | 9 | +| 2.* | >= 7.2 | 8 | +| 1.* | >= 5.3.3 | 4.8.36 - 6.5.0 | + ## Examples The assertions are available as a diff --git a/tests/GDAssertTraitTest53.php b/tests/GDAssertTraitTest53.php index f558658..76f858a 100644 --- a/tests/GDAssertTraitTest53.php +++ b/tests/GDAssertTraitTest53.php @@ -1,4 +1,4 @@ - Date: Fri, 18 Feb 2022 11:51:51 +0100 Subject: [PATCH 2/6] v1.x: fix(ci): Remove leading dot for phpunit XML files This will make it possible for PHPUnit to autodiscover those files and mirrors the repo state on the main branch. --- .github/workflows/main.yml | 2 +- .phpunit.xml => phpunit.xml | 0 .phpunit53.xml => phpunit53.xml | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename .phpunit.xml => phpunit.xml (100%) rename .phpunit53.xml => phpunit53.xml (100%) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5eb30e6..2470c82 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -37,7 +37,7 @@ jobs: - name: Run test suite (PHP 5.3) if: ${{ matrix.php-version == '5.3' }} - run: vendor/bin/phpunit --configuration .phpunit53.xml + run: vendor/bin/phpunit --configuration phpunit53.xml - name: Run test suite if: ${{ matrix.php-version != '5.3' }} diff --git a/.phpunit.xml b/phpunit.xml similarity index 100% rename from .phpunit.xml rename to phpunit.xml diff --git a/.phpunit53.xml b/phpunit53.xml similarity index 100% rename from .phpunit53.xml rename to phpunit53.xml From c92bc51e4b908f291d344c3f2e751a7a4b532ba9 Mon Sep 17 00:00:00 2001 From: Fabian Meyer <3982806+meyfa@users.noreply.github.com> Date: Fri, 18 Feb 2022 11:53:57 +0100 Subject: [PATCH 3/6] v1.x: fix(ci): Reference matrix.php-versions correctly --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2470c82..71ee9b8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -36,9 +36,9 @@ jobs: run: composer install --prefer-dist --no-progress - name: Run test suite (PHP 5.3) - if: ${{ matrix.php-version == '5.3' }} + if: ${{ matrix.php-versions == '5.3' }} run: vendor/bin/phpunit --configuration phpunit53.xml - name: Run test suite - if: ${{ matrix.php-version != '5.3' }} + if: ${{ matrix.php-versions != '5.3' }} run: vendor/bin/phpunit From a45f1302be41e38683e209192d9550184916de01 Mon Sep 17 00:00:00 2001 From: Fabian Meyer <3982806+meyfa@users.noreply.github.com> Date: Fri, 18 Feb 2022 11:56:52 +0100 Subject: [PATCH 4/6] v1.x: fix(ci): Add test bootstrap for PHPUnit 4.8 on PHP 8 support --- .github/workflows/main.yml | 1 + tests/bootstrap.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/bootstrap.php diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 71ee9b8..db31420 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,6 +22,7 @@ jobs: php-version: ${{ matrix.php-versions }} extensions: gd coverage: none + ini-values: auto_prepend_file="${{github.workspace}}/tests/bootstrap.php" - name: Cache Composer packages id: composer-cache diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..2594976 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,29 @@ + $key, + 'key' => $key, + 1 => $value, + 'value' => $value, + ); + } +} From d4906b190fdfecd70a1874adb359ac041de62a34 Mon Sep 17 00:00:00 2001 From: Fabian Meyer <3982806+meyfa@users.noreply.github.com> Date: Fri, 18 Feb 2022 12:11:05 +0100 Subject: [PATCH 5/6] v1.x: fix: Add missing PHP 8 support PHP 8 uses an image class instead of resources for GD images. See #3. We can simply adapt the behavior to be conditional, to hopefully gain support for all PHP versions from 5.3.3 to 8. --- README.md | 4 ++-- src/GDImage.php | 38 +++++++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 783e03c..b534be2 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,12 @@ [![CI](https://github.com/meyfa/phpunit-assert-gd/actions/workflows/main.yml/badge.svg?branch=v1.x)](https://github.com/meyfa/phpunit-assert-gd/actions/workflows/main.yml) Trying to assert images with PHPUnit? This project provides a constraint and the -required assertions that allow you do to so. +required assertions that allow you to do so. It supports comparing **files on disk** as well as **image resources** in memory. -**Compatibility note:** This library supports PHP versions 5.3.3 up to 7.2.2. It +**Compatibility note:** This library supports PHP versions 5.3.3 up to 8. It supports PHPUnit from version 4.8.36 to version 6.5.0. Since those PHPUnit versions are completely incompatible, extreme hacks have to be used that depend on the Composer autoloading order. Please file an issue if diff --git a/src/GDImage.php b/src/GDImage.php index e89b856..12f9123 100644 --- a/src/GDImage.php +++ b/src/GDImage.php @@ -14,36 +14,52 @@ class GDImage * Constructs a new instance. Accepts either an image resource or a file * path to load the image from. * - * @param string|resource $value The image resource or file path. + * If you provide an already-loaded image resource, it is YOUR job to + * destroy the image when you no longer need it. + * Resources loaded from a file will be destroyed by this class upon calling + * finish(). + * + * @param string|resource|\GdImage $value The image resource or file path. */ public function __construct($value) { - if (!is_resource($value)) { - $value = imagecreatefromstring(file_get_contents($value)); - $this->destroy = true; + // PHP < 8 uses resources, PHP >= 8 uses GdImage objects. + if (is_resource($value) || $value instanceof \GdImage) { + $this->res = $value; + return; } - $this->res = $value; + $this->res = imagecreatefromstring(file_get_contents($value)); + $this->destroy = true; } /** - * @return resource The underlying GD image resource. + * Disposes of this image by calling `finish()`. */ - public function getResource() + public function __destruct() { - return $this->res; + $this->finish(); } /** - * Frees the allocated resource if it was loaded in the constructor. Will - * not free the resource if it was passed already-loaded. + * Free any allocated resources. This should be called as soon as the image + * is no longer needed. * * @return void */ public function finish() { - if ($this->destroy) { + if ($this->destroy && isset($this->res)) { imagedestroy($this->res); } + $this->res = null; + } + + /** + * @return resource The underlying GD image resource. + */ + public function getResource() + { + return $this->res; } /** From 26931431cfa6e471976a460f5f646a0e6560712c Mon Sep 17 00:00:00 2001 From: Fabian Meyer <3982806+meyfa@users.noreply.github.com> Date: Fri, 18 Feb 2022 12:14:33 +0100 Subject: [PATCH 6/6] v1.x: fix(tests): Skip resource freeing test on PHP 8 --- tests/GDImageTest.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/GDImageTest.php b/tests/GDImageTest.php index d0b0985..deddbaf 100644 --- a/tests/GDImageTest.php +++ b/tests/GDImageTest.php @@ -59,11 +59,17 @@ public function testFinish() $obj = new GDImage('./tests/images/stripes-bw-10x10.png'); $img = $obj->getResource(); $obj->finish(); - try { - imagesx($img); - } catch (Exception $e) { - return; + // skip on PHP >= 8, where images are objects instead of resources + // and where manual destruction does nothing + if (is_resource($img)) { + // expect resource to be destroyed + try { + imagesx($img); + } catch (Exception $e) { + return; + } + $this->fail(); } - $this->fail(); + $this->assertNull($obj->getResource()); } }