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\FileSizeScaler; |
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 FileSizeScalerTest 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 FileSizeScaler |
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 FileSizeScaler(new DefaultConfigurator([ |
53
|
|
|
'publicPath' => $this->publicPath, |
54
|
|
|
])); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
public function test_scale_down() { |
|
|
|
|
58
|
|
|
$sourceFile = $this->createSourceFile(); |
59
|
|
|
$imageObject = $this->createImageObject(); |
60
|
|
|
|
61
|
|
|
$sizes = $this->scaler->scale($sourceFile, $imageObject); |
62
|
|
|
|
63
|
|
|
$this->assertTrue(count($sizes) > 1); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
public function test_scale_down_with_max_width() { |
|
|
|
|
67
|
|
|
$sourceFile = $this->createSourceFile(); |
68
|
|
|
$imageObject = $this->createImageObject(); |
69
|
|
|
|
70
|
|
|
$this->scaler->setMaxWidth(1000); |
71
|
|
|
$sizes = $this->scaler->scale($sourceFile, $imageObject); |
72
|
|
|
|
73
|
|
|
foreach ($sizes as $width => $height) { |
74
|
|
|
$this->assertTrue($width < 1000); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
public function test_scale_down_with_max_filesize() { |
|
|
|
|
79
|
|
|
$sourceFile = $this->createSourceFile(); |
80
|
|
|
$imageObject = $this->createImageObject(); |
81
|
|
|
|
82
|
|
|
$this->scaler->setMaxFileSize(100000); |
83
|
|
|
$sizes = $this->scaler->scale($sourceFile, $imageObject); |
84
|
|
|
|
85
|
|
|
$this->assertCount(3, $sizes); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// TODO: test algorithm |
89
|
|
|
|
90
|
|
|
private function createImageObject() { |
91
|
|
|
$imageObject = $this->engine->make('./tests/img/image.jpeg'); |
92
|
|
|
|
93
|
|
|
return $imageObject; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function createSourceFile() { |
97
|
|
|
$sourceFiles = Finder::create()->files()->in('./tests/img')->name('image.jpeg')->getIterator(); |
98
|
|
|
$sourceFiles->rewind(); |
99
|
|
|
|
100
|
|
|
return $sourceFiles->current(); |
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.