1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the IrishDan\ResponsiveImageBundle package. |
4
|
|
|
* |
5
|
|
|
* (c) Daniel Byrne <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE file that was distributed with this source |
8
|
|
|
* code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace IrishDan\ResponsiveImageBundle\Tests\ImageProcessing; |
12
|
|
|
|
13
|
|
|
use IrishDan\ResponsiveImageBundle\ImageProcessing\FocusCropDataCalculator; |
14
|
|
|
use IrishDan\ResponsiveImageBundle\Tests\ResponsiveImageTestCase; |
15
|
|
|
|
16
|
|
|
class FocusCropDataCalculatorTest extends ResponsiveImageTestCase |
17
|
|
|
{ |
18
|
|
|
public function testGetFocusCropDataIsValid() |
19
|
|
|
{ |
20
|
|
|
$testData = [ |
21
|
|
|
[ |
22
|
|
|
'crop_coordinates' => [10, 10, 100, 100], |
23
|
|
|
'focus_coordinates' => [10, 10, 90, 90], |
24
|
|
|
], |
25
|
|
|
[ |
26
|
|
|
'crop_coordinates' => [283, 397, 991, 1289], |
27
|
|
|
'focus_coordinates' => [491, 514, 659, 709] |
28
|
|
|
], |
29
|
|
|
[ |
30
|
|
|
'crop_coordinates' => [227, 291, 2826, 2114], |
31
|
|
|
'focus_coordinates' => [1209, 634, 1743, 1195], |
32
|
|
|
], |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
$testStyles = [ |
36
|
|
|
[30, 60], |
37
|
|
|
[40, 50], |
38
|
|
|
[170, 240], |
39
|
|
|
[240, 170], |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
foreach ($testData as $imageCoordinates) { |
43
|
|
|
foreach ($testStyles as $style) { |
44
|
|
|
$focusOffsetFinder = new FocusCropDataCalculator( |
45
|
|
|
$imageCoordinates['crop_coordinates'], |
46
|
|
|
$imageCoordinates['focus_coordinates'], |
47
|
|
|
$style[0], |
48
|
|
|
$style[1] |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$focusCropData = $focusOffsetFinder->getFocusCropData(); |
52
|
|
|
|
53
|
|
|
$this->assertArrayHasKey('width', $focusCropData); |
54
|
|
|
$this->assertArrayHasKey('height', $focusCropData); |
55
|
|
|
$this->assertArrayHasKey('x', $focusCropData); |
56
|
|
|
$this->assertArrayHasKey('y', $focusCropData); |
57
|
|
|
|
58
|
|
|
$width = round($imageCoordinates['crop_coordinates'][2]) - round($imageCoordinates['crop_coordinates'][0]); // x2 - x1 |
59
|
|
|
$height = round($imageCoordinates['crop_coordinates'][3]) - round($imageCoordinates['crop_coordinates'][1]); // y2 - y1 |
60
|
|
|
|
61
|
|
|
$message = 'Too wide: ' . $focusCropData['width'] . '<=' . $width; |
62
|
|
|
$this->assertTrue($focusCropData['width'] <= $width, $message); |
63
|
|
|
|
64
|
|
|
$message = 'Too high: ' . (int)round($focusCropData['height']) . ' <= ' . $height; |
65
|
|
|
$this->assertTrue((int)round($focusCropData['height']) <= (int)$height, $message); |
66
|
|
|
|
67
|
|
|
$this->assertTrue($focusCropData['x'] >= $imageCoordinates['crop_coordinates'][0]); // x1 |
68
|
|
|
$this->assertTrue($focusCropData['y'] >= $imageCoordinates['crop_coordinates'][1]); // y1 |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
View Code Duplication |
public function testGetFocusCropDataWithVerticalStyle() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$focusOffsetFinder = new FocusCropDataCalculator( |
76
|
|
|
[10, 10, 90, 90], |
77
|
|
|
[40, 40, 60, 60], |
78
|
|
|
30, |
79
|
|
|
60 |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$focusCropData = $focusOffsetFinder->getFocusCropData(); |
83
|
|
|
|
84
|
|
|
// for vertical style |
85
|
|
|
$this->assertTrue($focusCropData['width'] < $focusCropData['height']); |
86
|
|
|
$this->assertEquals($focusCropData['height'], 80); |
87
|
|
|
$this->assertTrue($focusCropData['x'] > 10); |
88
|
|
|
$this->assertTrue($focusCropData['y'] == 10); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
View Code Duplication |
public function testGetFocusCropDataHorizontalStyle() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$focusOffsetFinder = new FocusCropDataCalculator( |
94
|
|
|
[10, 10, 90, 90], |
95
|
|
|
[40, 40, 60, 60], |
96
|
|
|
60, |
97
|
|
|
30 |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$focusCropData = $focusOffsetFinder->getFocusCropData(); |
101
|
|
|
|
102
|
|
|
// for horizontal style |
103
|
|
|
$this->assertTrue($focusCropData['width'] > $focusCropData['height']); |
104
|
|
|
$this->assertEquals($focusCropData['width'], 80); |
105
|
|
|
$this->assertTrue($focusCropData['x'] == 10); |
106
|
|
|
$this->assertTrue($focusCropData['y'] > 10); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
View Code Duplication |
public function testGetFocusCropDataStyleAnCropMatch() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$focusOffsetFinder = new FocusCropDataCalculator( |
112
|
|
|
[10, 10, 90, 90], |
113
|
|
|
[40, 40, 60, 60], |
114
|
|
|
60, |
115
|
|
|
60 |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
$focusCropData = $focusOffsetFinder->getFocusCropData(); |
119
|
|
|
|
120
|
|
|
// for horizontal style |
121
|
|
|
$this->assertTrue($focusCropData['width'] == $focusCropData['height']); |
122
|
|
|
$this->assertEquals($focusCropData['width'], 80); |
123
|
|
|
$this->assertTrue($focusCropData['x'] == 10); |
124
|
|
|
$this->assertTrue($focusCropData['y'] == 10); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.