Passed
Push — master ( 260455...4746f9 )
by Brent
04:30
created

ResponsiveFactoryTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 91
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __destruct() 0 5 2
A setUp() 0 11 2
A test_create() 0 9 1
A test_create_sets_correct_src() 0 7 1
A test_create_doesnt_render_full_width_srcset() 0 8 1
A test_create_sets_correct_srcset() 0 11 1
A test_create_sets_default_srcset() 0 9 1
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