1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Image\Scaler; |
4
|
|
|
|
5
|
|
|
use Brendt\Image\Config\DefaultConfigurator; |
6
|
|
|
use Intervention\Image\ImageManager; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
use Symfony\Component\Finder\Finder; |
10
|
|
|
|
11
|
|
|
class SizesScalerTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Filesystem |
16
|
|
|
*/ |
17
|
|
|
private $fs; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $publicPath = './tests/public'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ImageManager |
26
|
|
|
*/ |
27
|
|
|
private $engine; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
public function __construct() { |
31
|
|
|
parent::__construct(); |
32
|
|
|
|
33
|
|
|
$this->fs = new Filesystem(); |
34
|
|
|
$this->engine = new ImageManager([ |
35
|
|
|
'driver' => 'gd', |
36
|
|
|
]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function __destruct() { |
40
|
|
|
if ($this->fs->exists($this->publicPath)) { |
41
|
|
|
$this->fs->remove($this->publicPath); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
public function test_scale_down() { |
|
|
|
|
46
|
|
|
$scaler = new SizesScaler(new DefaultConfigurator([ |
47
|
|
|
'publicPath' => $this->publicPath, |
48
|
|
|
'includeSource' => false, |
49
|
|
|
])); |
50
|
|
|
$scaler->setSizes([500, 10000, 1920]); |
51
|
|
|
|
52
|
|
|
$sourceFile = $this->createSourceFile(); |
53
|
|
|
$imageObject = $this->createImageObject(); |
54
|
|
|
|
55
|
|
|
$sizes = $scaler->scale($sourceFile, $imageObject); |
56
|
|
|
|
57
|
|
|
$this->assertCount(2, $sizes); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
public function test_scale_down_with_include_source() { |
|
|
|
|
61
|
|
|
$scaler = new SizesScaler(new DefaultConfigurator([ |
62
|
|
|
'publicPath' => $this->publicPath, |
63
|
|
|
'includeSource' => true, |
64
|
|
|
])); |
65
|
|
|
$scaler->setSizes([500, 800]); |
66
|
|
|
|
67
|
|
|
$sourceFile = $this->createSourceFile(); |
68
|
|
|
$imageObject = $this->createImageObject(); |
69
|
|
|
|
70
|
|
|
$sizes = $scaler->scale($sourceFile, $imageObject); |
71
|
|
|
|
72
|
|
|
$this->assertCount(3, $sizes); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
private function createImageObject() { |
77
|
|
|
$imageObject = $this->engine->make('./tests/img/image.jpeg'); |
78
|
|
|
|
79
|
|
|
return $imageObject; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function createSourceFile() { |
83
|
|
|
$sourceFiles = Finder::create()->files()->in('./tests/img')->name('image.jpeg')->getIterator(); |
84
|
|
|
$sourceFiles->rewind(); |
85
|
|
|
|
86
|
|
|
return $sourceFiles->current(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.