Skip to content

Commit

Permalink
v1.x: chore(ci): Use GitHub Actions instead of Travis (#9)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
meyfa authored Feb 18, 2022
1 parent 936c6b4 commit b0abcc2
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 40 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.DS_Store
vendor/
.idea
.phpunit.result.cache
composer.lock
19 changes: 0 additions & 19 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
File renamed without changes.
File renamed without changes.
38 changes: 27 additions & 11 deletions src/GDImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/GDAssertTraitTest53.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php

use PHPUnit\Framework\TestCase;

Expand Down
16 changes: 11 additions & 5 deletions tests/GDImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
29 changes: 29 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file provides functions from older PHP versions that are no longer present in newer versions.
* This is needed because we are on PHPUnit 4.8 which was built long before some things were deprecated.
*
* Load this file before anything else is run, preferably through php.ini auto_prepend_file.
*/

if (!function_exists('each')) {
function each(&$array)
{
if (!is_array($array) && !is_object($array)) {
return null;
}
$key = key($array);
if ($key === null) {
return false;
}
$value = $array[$key];
next($array);
return array(
0 => $key,
'key' => $key,
1 => $value,
'value' => $value,
);
}
}

0 comments on commit b0abcc2

Please sign in to comment.