Passed
Push — master ( 910d8e...f583f7 )
by Joschi
02:39
created

testWidthCssRulesetCompilerServiceSourceSizes()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 25
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\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   = new ImageCandidateSet();
91
        $imageCandidateSet[] = new WidthImageCandidate('small.jpg', 400);
92
        $imageCandidateSet[] = new WidthImageCandidate('medium.jpg', 800);
93
        $imageCandidateSet[] = new WidthImageCandidate('large.jpg', 1200);
94
        $imageCandidateSet[] = new WidthImageCandidate('extralarge.jpg', 1600);
95
        $lengthFactory       = new LengthFactory(new ViewportCalculatorServiceFactory(), 16);
96
        $sourceSizeList      = $this->createMock(SourceSizeListInterface::class);
97
        $sourceSizeList->/** @scrutinizer ignore-call */
98
        method('findImageCandidate')
99
            ->will($this->getImageCandidateMatches($imageCandidateSet));
100
101
        $compiler = new WidthCssRulesetCompilerService(
102
            $ruleset,
103
            [new AbsoluteLength(400), new AbsoluteLength(800), new AbsoluteLength(1200)],
104
            $imageCandidateSet,
105
            $lengthFactory,
106
            $sourceSizeList
107
        );
108
        $this->assertInstanceOf(WidthCssRulesetCompilerService::class, $compiler);
109
110
        $cssRuleset = $compiler->compile(1);
111
        $this->assertInstanceOf(CssRulesetInterface::class, $cssRuleset);
112
    }
113
114
    /**
115
     * Create a list of consecutive image candidate matches
116
     *
117
     * @param ImageCandidateSet $imageCandidateSet Image candidate set
118
     *
119
     * @return ConsecutiveCalls Image candidate matches
120
     * @throws \ReflectionException
121
     */
122
    protected function getImageCandidateMatches(ImageCandidateSet $imageCandidateSet): ConsecutiveCalls
123
    {
124
        $imageCandidates = [];
125
        foreach ($imageCandidateSet as $imageCandidate) {
126
            $imageCandidateReturn = $this->createMock(SourceSizeImageCandidateMatch::class);
127
            $imageCandidateReturn->method('getMediaCondition')->willReturn(
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
            $imageCandidateReturn->/** @scrutinizer ignore-call */ 
128
                                   method('getMediaCondition')->willReturn(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
                new MediaCondition('', '(min-width: '.$imageCandidate->getValue().'px)')
129
            );
130
            $imageCandidateReturn->method('getImageCandidate')->willReturn($imageCandidate);
131
            $imageCandidates[] = $imageCandidateReturn;
132
        }
133
134
        return call_user_func_array([$this, 'onConsecutiveCalls'], $imageCandidates);
135
    }
136
}