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

testWidthCssRulesetCompilerService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 18
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   = 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->method('findImageCandidate')->will($this->getImageCandidateMatches($imageCandidateSet));
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

97
        $sourceSizeList->/** @scrutinizer ignore-call */ 
98
                         method('findImageCandidate')->will($this->getImageCandidateMatches($imageCandidateSet));

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...
98
99
        $compiler = new WidthCssRulesetCompilerService(
100
            $ruleset,
101
            [new AbsoluteLength(400), new AbsoluteLength(800), new AbsoluteLength(1200)],
102
            $imageCandidateSet,
103
            $lengthFactory,
104
            $sourceSizeList
105
        );
106
        $this->assertInstanceOf(WidthCssRulesetCompilerService::class, $compiler);
107
108
        $cssRuleset = $compiler->compile(1);
109
        $this->assertInstanceOf(CssRulesetInterface::class, $cssRuleset);
110
    }
111
112
    /**
113
     * Create a list of consecutive image candidate matches
114
     *
115
     * @param ImageCandidateSet $imageCandidateSet Image candidate set
116
     *
117
     * @return ConsecutiveCalls Image candidate matches
118
     * @throws \ReflectionException
119
     */
120
    protected function getImageCandidateMatches(ImageCandidateSet $imageCandidateSet): ConsecutiveCalls
121
    {
122
        $imageCandidates = [];
123
        foreach ($imageCandidateSet as $imageCandidate) {
124
            $imageCandidateReturn = $this->createMock(SourceSizeImageCandidateMatch::class);
125
            $imageCandidateReturn->method('getMediaCondition')->willReturn(
126
                new MediaCondition('', '(min-width: '.$imageCandidate->getValue().'px)')
127
            );
128
            $imageCandidateReturn->method('getImageCandidate')->willReturn($imageCandidate);
129
            $imageCandidates[] = $imageCandidateReturn;
130
        }
131
132
        return call_user_func_array([$this, 'onConsecutiveCalls'], $imageCandidates);
133
    }
134
}