1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Image\Tests\Phpunit; |
4
|
|
|
|
5
|
|
|
use Brendt\Image\Config\DefaultConfigurator; |
6
|
|
|
use Brendt\Image\Config\ResponsiveFactoryConfigurator; |
7
|
|
|
use Brendt\Image\ResponsiveFactory; |
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
|
10
|
|
|
class ResponsiveFactoryTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var ResponsiveFactoryConfigurator |
15
|
|
|
*/ |
16
|
|
|
private $configurator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Filesystem |
20
|
|
|
*/ |
21
|
|
|
private $fs; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $publicPath = './tests/public'; |
27
|
|
|
|
28
|
|
|
public function __construct() { |
29
|
|
|
parent::__construct(); |
30
|
|
|
|
31
|
|
|
$this->fs = new Filesystem(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function __destruct() { |
35
|
|
|
if ($this->fs->exists($this->publicPath)) { |
36
|
|
|
$this->fs->remove($this->publicPath); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function setUp() { |
41
|
|
|
if ($this->fs->exists($this->publicPath)) { |
42
|
|
|
$this->fs->remove($this->publicPath); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->configurator = new DefaultConfigurator([ |
46
|
|
|
'publicPath' => $this->publicPath, |
47
|
|
|
'engine' => 'gd', |
48
|
|
|
'stepModifier' => 0.2, |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function test_create() { |
53
|
|
|
$factory = new ResponsiveFactory($this->configurator); |
54
|
|
|
$image = $factory->create('img/image.jpeg'); |
55
|
|
|
|
56
|
|
|
$this->assertTrue($this->fs->exists("{$this->publicPath}/img/image-384.jpeg")); |
57
|
|
|
$this->assertTrue($this->fs->exists("{$this->publicPath}/img/image.jpeg")); |
58
|
|
|
|
59
|
|
|
$this->assertNotEmpty($image->srcset()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function test_create_sets_correct_src() { |
63
|
|
|
$factory = new ResponsiveFactory($this->configurator); |
64
|
|
|
$url = 'img/image.jpeg'; |
65
|
|
|
$image = $factory->create($url); |
66
|
|
|
|
67
|
|
|
$this->assertEquals("/{$url}", $image->src()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function test_create_doesnt_render_full_width_srcset() { |
71
|
|
|
$factory = new ResponsiveFactory($this->configurator); |
72
|
|
|
$url = 'img/image.jpeg'; |
73
|
|
|
$publicPath = $this->configurator->getConfig()['publicPath']; |
74
|
|
|
$factory->create($url); |
75
|
|
|
|
76
|
|
|
$this->assertFalse($this->fs->exists("{$publicPath}/image-1920.jpeg")); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function test_create_sets_correct_srcset() { |
80
|
|
|
$factory = new ResponsiveFactory($this->configurator); |
81
|
|
|
$url = 'img/image.jpeg'; |
82
|
|
|
$image = $factory->create($url); |
83
|
|
|
|
84
|
|
|
$srcset = $image->srcset(); |
85
|
|
|
|
86
|
|
|
$this->assertNotEmpty($srcset); |
87
|
|
|
$this->assertContains('/img/image-384.jpeg', $srcset); |
88
|
|
|
$this->assertContains('/img/image-1152.jpeg', $srcset); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function test_create_sets_default_srcset() { |
92
|
|
|
$factory = new ResponsiveFactory($this->configurator); |
93
|
|
|
$url = 'img/image.jpeg'; |
94
|
|
|
$image = $factory->create($url); |
95
|
|
|
|
96
|
|
|
$srcset = $image->srcset(); |
97
|
|
|
|
98
|
|
|
$this->assertContains('/img/image.jpeg 1920w', $srcset); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|