From b0abcc21feae6af0f81008b620dbec4703f452eb Mon Sep 17 00:00:00 2001 From: Fabian Meyer <3982806+meyfa@users.noreply.github.com> Date: Fri, 18 Feb 2022 12:16:36 +0100 Subject: [PATCH] v1.x: chore(ci): Use GitHub Actions instead of Travis (#9) * v1.x: chore(ci): Use GitHub Actions instead of Travis * 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. * v1.x: fix(ci): Reference matrix.php-versions correctly * v1.x: fix(ci): Add test bootstrap for PHPUnit 4.8 on PHP 8 support * 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. * v1.x: fix(tests): Skip resource freeing test on PHP 8 --- .github/workflows/main.yml | 45 +++++++++++++++++++++++++++++++++ .gitignore | 3 +++ .travis.yml | 19 -------------- LICENSE | 2 +- README.md | 14 +++++++--- .phpunit.xml => phpunit.xml | 0 .phpunit53.xml => phpunit53.xml | 0 src/GDImage.php | 38 ++++++++++++++++++++-------- tests/GDAssertTraitTest53.php | 2 +- tests/GDImageTest.php | 16 ++++++++---- tests/bootstrap.php | 29 +++++++++++++++++++++ 11 files changed, 128 insertions(+), 40 deletions(-) create mode 100644 .github/workflows/main.yml delete mode 100644 .travis.yml rename .phpunit.xml => phpunit.xml (100%) rename .phpunit53.xml => phpunit53.xml (100%) create mode 100644 tests/bootstrap.php diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..db31420 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,45 @@ +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 + ini-values: auto_prepend_file="${{github.workspace}}/tests/bootstrap.php" + + - 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-versions == '5.3' }} + run: vendor/bin/phpunit --configuration phpunit53.xml + + - name: Run test suite + if: ${{ matrix.php-versions != '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..b534be2 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,14 @@ # 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. +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 @@ -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/.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 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; } /** 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 @@ - 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()); } } 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, + ); + } +}