Passed
Push — master ( ea3f04...21da4e )
by Joschi
02:21
created

createImageCandidateSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
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\Domain
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\Domain;
38
39
use Jkphl\Respimgcss\Application\Factory\LengthFactory;
40
use Jkphl\Respimgcss\Domain\Contract\CssRulesetInterface;
41
use Jkphl\Respimgcss\Domain\Contract\SourceSizeImageCandidateMatch;
42
use Jkphl\Respimgcss\Domain\Contract\SourceSizeListInterface;
43
use Jkphl\Respimgcss\Domain\Model\Css\MediaCondition;
44
use Jkphl\Respimgcss\Domain\Model\Css\Ruleset;
45
use Jkphl\Respimgcss\Domain\Model\ImageCandidateSet;
46
use Jkphl\Respimgcss\Domain\Model\WidthImageCandidate;
47
use Jkphl\Respimgcss\Domain\Service\WidthCssRulesetCompilerService;
48
use Jkphl\Respimgcss\Infrastructure\ViewportCalculatorServiceFactory;
49
use Jkphl\Respimgcss\Tests\AbstractTestBase;
50
use Jkphl\Respimgcss\Tests\Domain\Mock\AbsoluteLength;
51
use PHPUnit\Framework\MockObject\Stub\ConsecutiveCalls;
52
53
/**
54
 * Width CSS ruleset compiler service tests
55
 *
56
 * @package    Jkphl\Respimgcss
57
 * @subpackage Jkphl\Respimgcss\Tests\Domain
58
 */
59
class WidthCssRulesetCompilerServiceTest extends AbstractTestBase
60
{
61
    /**
62
     * Test the width CSS ruleset compiler service using the image candidates
63
     */
64
    public function testWidthCssRulesetCompilerServiceImageCandidates()
65
    {
66
        $ruleset             = new Ruleset();
67
        $length              = new AbsoluteLength(500);
68
        $imageCandidateSet   = new ImageCandidateSet();
69
        $imageCandidateSet[] = new WidthImageCandidate('small.jpg', 400);
70
        $imageCandidateSet[] = new WidthImageCandidate('medium.jpg', 800);
71
        $lengthFactory       = new LengthFactory(new ViewportCalculatorServiceFactory(), 16);
72
        $compiler            = new WidthCssRulesetCompilerService(
73
            $ruleset,
74
            [$length],
75
            $imageCandidateSet,
76
            $lengthFactory
77
        );
78
        $this->assertInstanceOf(WidthCssRulesetCompilerService::class, $compiler);
79
80
        $cssRuleset = $compiler->compile(2);
81
        $this->assertInstanceOf(CssRulesetInterface::class, $cssRuleset);
82
    }
83
84
    /**
85
     * Test the width CSS ruleset compiler service using a source sizes list
86
     */
87
    public function testWidthCssRulesetCompilerServiceSourceSizes()
88
    {
89
        $ruleset           = new Ruleset();
90
        $imageCandidateSet = $this->createImageCandidateSet();
91
        $lengthFactory     = new LengthFactory(new ViewportCalculatorServiceFactory(), 16);
92
        $sourceSizeList    = $this->createMock(SourceSizeListInterface::class);
93
        $sourceSizeList->/** @scrutinizer ignore-call */
94
        method('findImageCandidate')
95
            ->will($this->getImageCandidateMatches($imageCandidateSet));
96
97
        $compiler = new WidthCssRulesetCompilerService(
98
            $ruleset,
99
            [new AbsoluteLength(400), new AbsoluteLength(800), new AbsoluteLength(1200)],
100
            $imageCandidateSet,
101
            $lengthFactory,
102
            $sourceSizeList
103
        );
104
        $this->assertInstanceOf(WidthCssRulesetCompilerService::class, $compiler);
105
106
        $cssRuleset = $compiler->compile(1);
107
        $this->assertInstanceOf(CssRulesetInterface::class, $cssRuleset);
108
    }
109
110
    /**
111
     * Create and return an image candidate set
112
     *
113
     * @return ImageCandidateSet Image candidate set
114
     */
115
    protected function createImageCandidateSet(): ImageCandidateSet
116
    {
117
        $imageCandidateSet   = new ImageCandidateSet();
118
        $imageCandidateSet[] = new WidthImageCandidate('small.jpg', 400);
119
        $imageCandidateSet[] = new WidthImageCandidate('medium.jpg', 800);
120
        $imageCandidateSet[] = new WidthImageCandidate('large.jpg', 1200);
121
        $imageCandidateSet[] = new WidthImageCandidate('extralarge.jpg', 1600);
122
        return $imageCandidateSet;
123
    }
124
125
    /**
126
     * Create a list of consecutive image candidate matches
127
     *
128
     * @param ImageCandidateSet $imageCandidateSet Image candidate set
129
     *
130
     * @return ConsecutiveCalls Image candidate matches
131
     * @throws \ReflectionException
132
     */
133
    protected function getImageCandidateMatches(ImageCandidateSet $imageCandidateSet): ConsecutiveCalls
134
    {
135
        $imageCandidates = [];
136
        foreach ($imageCandidateSet as $imageCandidate) {
137
            $imageCandidateReturn = $this->createMock(SourceSizeImageCandidateMatch::class);
138
            $imageCandidateReturn->/** @scrutinizer ignore-call */
139
            method('getMediaCondition')->willReturn(
140
                new MediaCondition('', '(min-width: '.$imageCandidate->getValue().'px)')
141
            );
142
            $imageCandidateReturn->/** @scrutinizer ignore-call */
143
            method('getImageCandidate')->willReturn($imageCandidate);
144
            $imageCandidates[] = $imageCandidateReturn;
145
        }
146
147
        return call_user_func_array([$this, 'onConsecutiveCalls'], $imageCandidates);
148
    }
149
}
150