1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Image\Tests\Phpunit\Scaler; |
4
|
|
|
|
5
|
|
|
use Brendt\Image\Config\DefaultConfigurator; |
6
|
|
|
use Brendt\Image\ResponsiveImage; |
7
|
|
|
use Brendt\Image\Scaler\WidthScaler; |
8
|
|
|
use Intervention\Image\ImageManager; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
11
|
|
|
use Symfony\Component\Finder\Finder; |
12
|
|
|
|
13
|
|
|
class WidthScalerTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Filesystem |
18
|
|
|
*/ |
19
|
|
|
private $fs; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $publicPath = './tests/public'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var WidthScaler |
28
|
|
|
*/ |
29
|
|
|
private $scaler; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ImageManager |
33
|
|
|
*/ |
34
|
|
|
private $engine; |
35
|
|
|
|
36
|
|
View Code Duplication |
public function __construct() { |
|
|
|
|
37
|
|
|
parent::__construct(); |
38
|
|
|
|
39
|
|
|
$this->fs = new Filesystem(); |
40
|
|
|
$this->engine = new ImageManager([ |
41
|
|
|
'driver' => 'gd', |
42
|
|
|
]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function __destruct() { |
46
|
|
|
if ($this->fs->exists($this->publicPath)) { |
47
|
|
|
$this->fs->remove($this->publicPath); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function setUp() { |
52
|
|
|
$this->scaler = new WidthScaler(new DefaultConfigurator([ |
53
|
|
|
'scaler' => 'width', |
54
|
|
|
'stepModifier' => 0.8, |
55
|
|
|
'publicPath' => $this->publicPath |
56
|
|
|
])); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
public function test_scale_down() { |
|
|
|
|
60
|
|
|
$sourceFile = $this->createSourceFile(); |
61
|
|
|
$imageObject = $this->createImageObject(); |
62
|
|
|
|
63
|
|
|
$sizes = $this->scaler->scale($sourceFile, $imageObject); |
64
|
|
|
|
65
|
|
|
$this->assertTrue(count($sizes) > 1); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
public function test_scale_down_with_max_width() { |
|
|
|
|
69
|
|
|
$sourceFile = $this->createSourceFile(); |
70
|
|
|
$imageObject = $this->createImageObject(); |
71
|
|
|
|
72
|
|
|
$this->scaler->setMaxWidth(1000); |
73
|
|
|
$sizes = $this->scaler->scale($sourceFile, $imageObject); |
74
|
|
|
|
75
|
|
|
foreach ($sizes as $width => $height) { |
76
|
|
|
$this->assertTrue($width < 1000); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public function test_scale_down_with_include_source_disabled() { |
|
|
|
|
81
|
|
|
$sourceFile = $this->createSourceFile(); |
82
|
|
|
$imageObject = $this->createImageObject(); |
83
|
|
|
|
84
|
|
|
$this->scaler->setIncludeSource(false); |
85
|
|
|
$sizes = $this->scaler->scale($sourceFile, $imageObject); |
86
|
|
|
|
87
|
|
|
$this->assertFalse(array_key_exists(1920, $sizes)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function createSourceFile() { |
91
|
|
|
$sourceFiles = Finder::create()->files()->in('./tests/img')->name('image.jpeg')->getIterator(); |
92
|
|
|
$sourceFiles->rewind(); |
93
|
|
|
|
94
|
|
|
return $sourceFiles->current(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function createImageObject() { |
98
|
|
|
$imageObject = $this->engine->make('./tests/img/image.jpeg'); |
99
|
|
|
|
100
|
|
|
return $imageObject; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
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.