22
33namespace AssertGD ;
44
5+ use AssertGD \DiffCalculator \RgbaChannels ;
6+
57/**
68 * Use this trait in a test case class to gain access to the image similarity
79 * assertions.
810 */
911trait GDAssertTrait
1012{
13+ /**
14+ * @var DiffCalculator The difference calculator to compare the images with.
15+ */
16+ protected $ diffCalculator ;
17+
1118 /**
1219 * Asserts that the difference between $expected and $actual is AT MOST
1320 * $threshold. $expected and $actual can be GD image resources or paths to
@@ -22,14 +29,15 @@ trait GDAssertTrait
2229 * @param string|resource $actual The actual image.
2330 * @param string $message The failure message.
2431 * @param float $threshold Error threshold between 0 and 1.
32+ * @param DiffCalculator|null $diffCalculator The difference calculator to use.
2533 *
2634 * @return void
2735 *
2836 * @throws PHPUnit\Framework\AssertionFailedError
2937 */
30- public function assertSimilarGD ($ expected , $ actual , $ message = '' , $ threshold = 0 )
38+ public function assertSimilarGD ($ expected , $ actual , $ message = '' , $ threshold = 0 , $ diffCalculator = null )
3139 {
32- $ constraint = $ this ->isSimilarGD ($ expected , $ threshold );
40+ $ constraint = $ this ->isSimilarGD ($ expected , $ threshold, $ diffCalculator );
3341 $ this ->assertThat ($ actual , $ constraint , $ message );
3442 }
3543
@@ -44,15 +52,16 @@ public function assertSimilarGD($expected, $actual, $message = '', $threshold =
4452 * @param string|resource $actual The actual image.
4553 * @param string $message The failure message.
4654 * @param float $threshold Error threshold between 0 and 1.
55+ * @param DiffCalculator|null $diffCalculator The difference calculator to use.
4756 *
4857 * @return void
4958 *
5059 * @throws PHPUnit\Framework\AssertionFailedError
5160 */
52- public function assertNotSimilarGD ($ expected , $ actual , $ message = '' , $ threshold = 0 )
61+ public function assertNotSimilarGD ($ expected , $ actual , $ message = '' , $ threshold = 0 , $ diffCalculator = null )
5362 {
5463 $ constraint = $ this ->logicalNot (
55- $ this ->isSimilarGD ($ expected , $ threshold )
64+ $ this ->isSimilarGD ($ expected , $ threshold, $ diffCalculator )
5665 );
5766 $ this ->assertThat ($ actual , $ constraint , $ message );
5867 }
@@ -66,11 +75,28 @@ public function assertNotSimilarGD($expected, $actual, $message = '', $threshold
6675 *
6776 * @param string|resource $expected The expected image.
6877 * @param float $threshold Error threshold between 0 and 1.
78+ * @param DiffCalculator|null $diffCalculator The difference calculator to use.
6979 *
7080 * @return GDSimilarityConstraint The constraint.
7181 */
72- public function isSimilarGD ($ expected , $ threshold = 0 )
82+ public function isSimilarGD ($ expected , $ threshold = 0 , $ diffCalculator = null )
7383 {
74- return new GDSimilarityConstraint ($ expected , $ threshold );
84+ return new GDSimilarityConstraint ($ expected , $ threshold , $ diffCalculator ?? $ this ->diffCalculator ?? new RgbaChannels ());
85+ }
86+
87+ /**
88+ * Sets the difference calculator to use for image comparisons in this test case.
89+ *
90+ * @var DiffCalculator $diffCalculator
91+ */
92+ public function setDiffCalculator ($ diffCalculator ): void
93+ {
94+ if (!($ diffCalculator instanceof DiffCalculator)) {
95+ throw new \InvalidArgumentException (
96+ 'The difference calculator must implement the `AssertGD\DiffCalculator` interface '
97+ );
98+ }
99+
100+ $ this ->diffCalculator = $ diffCalculator ;
75101 }
76102}
0 commit comments