|
1 |
| -# phpunit-assert-gd |
| 1 | +# AssertGD for PHPUnit |
2 | 2 |
|
3 | 3 | [](https://travis-ci.org/meyfa/phpunit-assert-gd)
|
4 | 4 |
|
5 |
| -PHPUnit matcher/assertions for GD image resources |
| 5 | +Trying to assert images with PHPUnit? This project provides a constraint and the |
| 6 | +required assertions that allow you do to so. |
| 7 | + |
| 8 | +It supports comparing **files on disk** as well as **image resources** in |
| 9 | +memory. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +Add this package to your Composer dev-dependencies: |
| 14 | + |
| 15 | +``` |
| 16 | +composer require --dev meyfa/phpunit-assert-gd |
| 17 | +``` |
| 18 | + |
| 19 | +## Examples |
| 20 | + |
| 21 | +The assertions are available as a |
| 22 | +[trait](http://php.net/manual/en/language.oop5.traits.php), so you can easily |
| 23 | +`use` them in your test case class: |
| 24 | + |
| 25 | +```php |
| 26 | +<?php |
| 27 | +use AssertGD\GDAssertTrait; |
| 28 | + |
| 29 | +class ExampleTest extends PHPUnit_Framework_TestCase |
| 30 | +{ |
| 31 | + // this trait adds the assert methods to your test case |
| 32 | + use GDAssertTrait; |
| 33 | + |
| 34 | + public function testSomething() |
| 35 | + { |
| 36 | + $this->assertSimilarGD('./tests/expected.png', './tests/actual.png'); |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +**Note:** While this library should work with PHP down to at least v5.3.3, |
| 42 | +traits are a v5.4.0 feature. For versions lower than v5.4.0, you have to use |
| 43 | +this alternative syntax: |
| 44 | + |
| 45 | +```php |
| 46 | +<?php |
| 47 | +use AssertGD\GDSimilarityConstraint; |
| 48 | + |
| 49 | +class ExampleTest extends PHPUnit_Framework_TestCase |
| 50 | +{ |
| 51 | + public function testSomething() |
| 52 | + { |
| 53 | + $this->assertThat('./tests/actual.png', |
| 54 | + new GDSimilarityConstraint('./tests/expected.png')); |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +### Plain comparisons |
| 60 | + |
| 61 | +Use `assertSimilarGD` if you expect 2 images to be exactly equal. |
| 62 | +Use `assertNotSimilarGD` if you expect there to be differences. |
| 63 | + |
| 64 | +```php |
| 65 | +$this->assertSimilarGD('./tests/img.png', './tests/same.png'); |
| 66 | +$this->assertNotSimilarGD('./tests/img.png', './tests/other.png'); |
| 67 | +``` |
| 68 | + |
| 69 | +### Threshold values |
| 70 | + |
| 71 | +Provide a number between 0 and 1 to set the error threshold. For example, a |
| 72 | +value of 0.2 would allow for at most 20% difference. |
| 73 | + |
| 74 | +```php |
| 75 | +$this->assertSimilarGD('./tests/img.png', './tests/similar.png', '', 0.2); |
| 76 | +``` |
| 77 | + |
| 78 | +### Parameter types |
| 79 | + |
| 80 | +Instead of file paths, you can pass in GD image resources. This eliminates |
| 81 | +having to write something to disk prior to the comparison. |
| 82 | + |
| 83 | +```php |
| 84 | +$img = imagecreatetruecolor(10, 10); |
| 85 | +$this->assertSimilarGD('./tests/empty-10x10.png', $img); |
| 86 | +imagedestroy($img); |
| 87 | +``` |
| 88 | + |
| 89 | +### Manual constraint |
| 90 | + |
| 91 | +If you need to configure mock objects or do other, more complex matching calls, |
| 92 | +use `isSimilarGD` to obtain a constraint object (similar to what would be |
| 93 | +returned by `equalTo`, `isTrue`, etc.). |
| 94 | + |
| 95 | +```php |
| 96 | +$this->assertThat( |
| 97 | + './tests/actual.png', |
| 98 | + $this->isSimilarGD('./tests/expected.png') |
| 99 | +); |
| 100 | +``` |
0 commit comments