Passed
Push — master ( 4b5f24...20219f )
by Brent
02:15
created

test_create_sets_default_srcset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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