Completed
Push — master ( d21468...5b2b8e )
by Joschi
02:16
created

SourceSizeListTest::matchImageCandidates()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 17
rs 9.4285
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\Factory\SourceSizeFactory;
40
use Jkphl\Respimgcss\Application\Model\ImageCandidateSet;
41
use Jkphl\Respimgcss\Domain\Contract\AbsoluteLengthInterface;
42
use Jkphl\Respimgcss\Domain\Model\ImageCandidateMatch;
43
use Jkphl\Respimgcss\Domain\Model\WidthImageCandidate;
44
use Jkphl\Respimgcss\Infrastructure\SourceSizeList;
45
use Jkphl\Respimgcss\Infrastructure\ViewportCalculatorServiceFactory;
46
use Jkphl\Respimgcss\Tests\AbstractTestBase;
47
48
/**
49
 * Source size list test
50
 *
51
 * @package    Jkphl\Respimgcss
52
 * @subpackage Jkphl\Respimgcss\Tests\Infrastructure
53
 */
54
class SourceSizeListTest extends AbstractTestBase
55
{
56
    /**
57
     * Test the source size list
58
     */
59
    public function testSourceSizeList()
60
    {
61
        $sourceSizeFactory = new SourceSizeFactory(new ViewportCalculatorServiceFactory(), 16);
62
        $sourceSize1       = $sourceSizeFactory->createFromSourceSizeStr('((min-width: 200px) and (min-resolution: 2)) 80vw');
63
        $sourceSize2       = $sourceSizeFactory->createFromSourceSizeStr('(min-width: 400px) 80vw');
64
        $sourceSize3       = $sourceSizeFactory->createFromSourceSizeStr('(min-width: 800px) 50vw');
65
        $sourceSize4       = $sourceSizeFactory->createFromSourceSizeStr('100vw');
66
        $sourceSizeList    = new SourceSizeList([$sourceSize1, $sourceSize2, $sourceSize3, $sourceSize4]);
67
        $this->assertInstanceOf(SourceSizeList::class, $sourceSizeList);
68
        $this->assertEquals(4, count($sourceSizeList));
69
        $this->assertEquals($sourceSize1, $sourceSizeList[2]);
70
        $this->assertEquals($sourceSize2, $sourceSizeList[1]);
71
        $this->assertEquals($sourceSize3, $sourceSizeList[0]);
72
        $this->assertEquals($sourceSize4, $sourceSizeList[3]);
73
74
        $this->matchImageCandidates(
75
            $sourceSizeList,
76
            [
77
                $sourceSizeFactory->createAbsoluteLength(0),
78
                $sourceSizeFactory->createAbsoluteLength(400),
79
                $sourceSizeFactory->createAbsoluteLength(800),
80
            ]
81
        );
82
    }
83
84
    /**
85
     * Find image candidates for breakpoints
86
     *
87
     * @param SourceSizeList $sourceSizeList         Source sizes list
88
     * @param AbsoluteLengthInterface[] $breakpoints Breakpoints
89
     */
90
    protected function matchImageCandidates(SourceSizeList $sourceSizeList, array $breakpoints)
91
    {
92
        $imageCandidateSet = new ImageCandidateSet(new WidthImageCandidate('small.jpg', 400));
93
        $this->assertInstanceOf(ImageCandidateSet::class, $imageCandidateSet);
94
95
        $imageCandidateSet[] = new WidthImageCandidate('medium.jpg', 800);
96
        $imageCandidateSet[] = new WidthImageCandidate('large.jpg', 1200);
97
        $imageCandidateSet[] = new WidthImageCandidate('extralarge.jpg', 1600);
98
99
        foreach ([1] as $density) {
100
            /** @var AbsoluteLengthInterface $breakpoint */
101
            foreach ($breakpoints as $breakpoint) {
102
                $imageCandidateMatch = $sourceSizeList->findImageCandidate($imageCandidateSet, $breakpoint, $density);
103
//                echo $breakpoint->getValue().': ';
104
//                print_r($imageCandidateMatch->getImageCandidate());
105
//                echo PHP_EOL;
106
                $this->assertInstanceOf(ImageCandidateMatch::class, $imageCandidateMatch);
107
            }
108
        }
109
    }
110
111
    /**
112
     * Test the source size list with invalid source size
113
     *
114
     * @expectedException \Jkphl\Respimgcss\Ports\InvalidArgumentException
115
     * @expectedExceptionCode 1523047851
116
     */
117
    public function testSourceSizeListInvalid()
118
    {
119
        new SourceSizeList(['test']);
120
    }
121
}