2
2
3
3
namespace AssertGD ;
4
4
5
+ use AssertGD \DiffCalculator \RgbaChannels ;
6
+
5
7
/**
6
8
* Use this trait in a test case class to gain access to the image similarity
7
9
* assertions.
8
10
*/
9
11
trait GDAssertTrait
10
12
{
13
+ /**
14
+ * @var DiffCalculator The difference calculator to compare the images with.
15
+ */
16
+ protected $ diffCalculator ;
17
+
11
18
/**
12
19
* Asserts that the difference between $expected and $actual is AT MOST
13
20
* $threshold. $expected and $actual can be GD image resources or paths to
@@ -22,14 +29,15 @@ trait GDAssertTrait
22
29
* @param string|resource $actual The actual image.
23
30
* @param string $message The failure message.
24
31
* @param float $threshold Error threshold between 0 and 1.
32
+ * @param DiffCalculator|null $diffCalculator The difference calculator to use.
25
33
*
26
34
* @return void
27
35
*
28
36
* @throws PHPUnit\Framework\AssertionFailedError
29
37
*/
30
- public function assertSimilarGD ($ expected , $ actual , $ message = '' , $ threshold = 0 )
38
+ public function assertSimilarGD ($ expected , $ actual , $ message = '' , $ threshold = 0 , $ diffCalculator = null )
31
39
{
32
- $ constraint = $ this ->isSimilarGD ($ expected , $ threshold );
40
+ $ constraint = $ this ->isSimilarGD ($ expected , $ threshold, $ diffCalculator );
33
41
$ this ->assertThat ($ actual , $ constraint , $ message );
34
42
}
35
43
@@ -44,15 +52,16 @@ public function assertSimilarGD($expected, $actual, $message = '', $threshold =
44
52
* @param string|resource $actual The actual image.
45
53
* @param string $message The failure message.
46
54
* @param float $threshold Error threshold between 0 and 1.
55
+ * @param DiffCalculator|null $diffCalculator The difference calculator to use.
47
56
*
48
57
* @return void
49
58
*
50
59
* @throws PHPUnit\Framework\AssertionFailedError
51
60
*/
52
- public function assertNotSimilarGD ($ expected , $ actual , $ message = '' , $ threshold = 0 )
61
+ public function assertNotSimilarGD ($ expected , $ actual , $ message = '' , $ threshold = 0 , $ diffCalculator = null )
53
62
{
54
63
$ constraint = $ this ->logicalNot (
55
- $ this ->isSimilarGD ($ expected , $ threshold )
64
+ $ this ->isSimilarGD ($ expected , $ threshold, $ diffCalculator )
56
65
);
57
66
$ this ->assertThat ($ actual , $ constraint , $ message );
58
67
}
@@ -66,11 +75,28 @@ public function assertNotSimilarGD($expected, $actual, $message = '', $threshold
66
75
*
67
76
* @param string|resource $expected The expected image.
68
77
* @param float $threshold Error threshold between 0 and 1.
78
+ * @param DiffCalculator|null $diffCalculator The difference calculator to use.
69
79
*
70
80
* @return GDSimilarityConstraint The constraint.
71
81
*/
72
- public function isSimilarGD ($ expected , $ threshold = 0 )
82
+ public function isSimilarGD ($ expected , $ threshold = 0 , $ diffCalculator = null )
73
83
{
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 ;
75
101
}
76
102
}
0 commit comments