Completed
Push — master ( 5b2b8e...821ba7 )
by Joschi
02:04
created

SourceSizeListTest::testSourceSizeList()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * responsive-images-css
5
 *
6
 * @category   Jkphl
7
 * @package    Jkphl\Respimgcss
8
 * @subpackage Jkphl\Respimgcss\Tests\Infrastructure
9
 * @author     Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright  Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\Respimgcss\Tests\Infrastructure;
38
39
use Jkphl\Respimgcss\Application\Contract\CalculatorServiceFactoryInterface;
40
use Jkphl\Respimgcss\Application\Factory\LengthFactory;
41
use Jkphl\Respimgcss\Application\Factory\SourceSizeFactory;
42
use Jkphl\Respimgcss\Application\Model\ImageCandidateSet;
43
use Jkphl\Respimgcss\Domain\Contract\AbsoluteLengthInterface;
44
use Jkphl\Respimgcss\Domain\Model\ImageCandidateMatch;
45
use Jkphl\Respimgcss\Domain\Model\WidthImageCandidate;
46
use Jkphl\Respimgcss\Infrastructure\SourceSizeList;
47
use Jkphl\Respimgcss\Infrastructure\ViewportCalculatorServiceFactory;
48
use Jkphl\Respimgcss\Tests\AbstractTestBase;
49
50
/**
51
 * Source size list test
52
 *
53
 * @package    Jkphl\Respimgcss
54
 * @subpackage Jkphl\Respimgcss\Tests\Infrastructure
55
 */
56
class SourceSizeListTest extends AbstractTestBase
57
{
58
    /**
59
     * Test the source size list
60
     */
61
    public function testSourceSizeList()
62
    {
63
        $sourceSizeFactory = new SourceSizeFactory(new ViewportCalculatorServiceFactory(), 16);
64
        $sourceSize1       = $sourceSizeFactory->createFromSourceSizeStr(
65
            '((min-width: 200px) and (min-resolution: 2)) 80vw'
66
        );
67
        $sourceSize2       = $sourceSizeFactory->createFromSourceSizeStr('(min-width: 800px) 50vw');
68
        $sourceSize3       = $sourceSizeFactory->createFromSourceSizeStr('100vw');
69
        $sourceSize4       = $sourceSizeFactory->createFromSourceSizeStr('(min-width: 400px) 80vw');
70
        $sourceSizeList    = new SourceSizeList(
71
            [$sourceSize1, $sourceSize2, $sourceSize3, $sourceSize4],
72
            $sourceSizeFactory
73
        );
74
        $this->assertInstanceOf(SourceSizeList::class, $sourceSizeList);
75
        $this->assertEquals(4, count($sourceSizeList));
76
        $this->assertEquals($sourceSize2, $sourceSizeList[0]);
77
        $this->assertEquals($sourceSize4, $sourceSizeList[1]);
78
        $this->assertEquals($sourceSize1, $sourceSizeList[2]);
79
        $this->assertEquals($sourceSize3, $sourceSizeList[3]);
80
81
        $this->matchImageCandidates(
82
            $sourceSizeList,
83
            [
84
                $sourceSizeFactory->createAbsoluteLength(0),
85
                $sourceSizeFactory->createAbsoluteLength(400),
86
                $sourceSizeFactory->createAbsoluteLength(800),
87
            ]
88
        );
89
    }
90
91
    /**
92
     * Find image candidates for breakpoints
93
     *
94
     * @param SourceSizeList $sourceSizeList         Source sizes list
95
     * @param AbsoluteLengthInterface[] $breakpoints Breakpoints
96
     */
97
    protected function matchImageCandidates(SourceSizeList $sourceSizeList, array $breakpoints)
98
    {
99
        $imageCandidateSet = new ImageCandidateSet(new WidthImageCandidate('small.jpg', 400));
100
        $this->assertInstanceOf(ImageCandidateSet::class, $imageCandidateSet);
101
102
        $imageCandidateSet[] = new WidthImageCandidate('medium.jpg', 800);
103
        $imageCandidateSet[] = new WidthImageCandidate('large.jpg', 1200);
104
        $imageCandidateSet[] = new WidthImageCandidate('extralarge.jpg', 1600);
105
106
        $imageCandidateResultFiles = ['small.jpg', 'medium.jpg', 'extralarge.jpg'];
107
108
        foreach ([1] as $density) {
109
            /** @var AbsoluteLengthInterface $breakpoint */
110
            foreach ($breakpoints as $breakpoint) {
111
                $imageCandidateMatch = $sourceSizeList->findImageCandidate($imageCandidateSet, $breakpoint, $density);
112
                $this->assertInstanceOf(ImageCandidateMatch::class, $imageCandidateMatch);
113
                $this->assertEquals(
114
                    $imageCandidateMatch->getImageCandidate()->getFile(),
115
                    array_shift($imageCandidateResultFiles)
116
                );
117
            }
118
        }
119
    }
120
121
    /**
122
     * Test the source size list with invalid source size
123
     *
124
     * @expectedException \Jkphl\Respimgcss\Ports\InvalidArgumentException
125
     * @expectedExceptionCode 1523047851
126
     */
127
    public function testSourceSizeListInvalid()
128
    {
129
        $lengthFactory = new LengthFactory($this->createMock(CalculatorServiceFactoryInterface::class), 16);
130
        new SourceSizeList(['test'], $lengthFactory);
131
    }
132
}