Completed
Push — master ( 05b722...3814de )
by dan
01:59
created

  A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 15
nc 1
nop 0
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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