CssMediaConditionFactoryTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testCssMediaConditionFactoryWidth() 0 14 2
A testCssMediaConditionFactoryResolution() 0 17 2
A testCssMediaConditionFactory() 0 11 1
1
<?php
2
3
/**
4
 * responsive-images-css
5
 *
6
 * @category   Jkphl
7
 * @package    Jkphl\Respimgcss
8
 * @subpackage Jkphl\Respimgcss\Tests
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\LengthFactory;
40
use Jkphl\Respimgcss\Domain\Contract\CssMinMaxMediaConditionInterface;
41
use Jkphl\Respimgcss\Domain\Model\Css\MediaCondition;
42
use Jkphl\Respimgcss\Domain\Model\Css\ResolutionMediaCondition;
43
use Jkphl\Respimgcss\Domain\Model\Css\WidthMediaCondition;
44
use Jkphl\Respimgcss\Infrastructure\CssMediaCondition;
45
use Jkphl\Respimgcss\Infrastructure\CssMediaConditionFactory;
46
use Jkphl\Respimgcss\Infrastructure\CssMediaConditionInterface;
47
use Jkphl\Respimgcss\Infrastructure\ViewportCalculatorServiceFactory;
48
use Jkphl\Respimgcss\Tests\AbstractTestBase;
49
50
/**
51
 * CSS media condition factory tests
52
 *
53
 * @package    Jkphl\Respimgcss
54
 * @subpackage Jkphl\Respimgcss\Tests
55
 */
56
class CssMediaConditionFactoryTest extends AbstractTestBase
57
{
58
    /**
59
     * Test the CSS media condition factory
60
     */
61
    public function testCssMediaConditionFactory()
62
    {
63
        $mediaConditions = CssMediaConditionFactory::createFromMediaCondition(
64
            new MediaCondition('property', 'value')
65
        );
66
        $this->assertTrue(is_array($mediaConditions));
67
        $this->assertEquals(1, count($mediaConditions));
68
69
        /** @var CssMediaConditionInterface $mediaCondition */
70
        $mediaCondition = $mediaConditions[0];
71
        $this->assertInstanceOf(CssMediaConditionInterface::class, $mediaCondition);
72
    }
73
74
    /**
75
     * Test the CSS media condition factory with a resolution condition
76
     */
77
    public function testCssMediaConditionFactoryResolution()
78
    {
79
        $lengthFactory   = new LengthFactory(new ViewportCalculatorServiceFactory(), 16);
80
        $mediaConditions = CssMediaConditionFactory::createFromMediaCondition(
81
            new ResolutionMediaCondition($lengthFactory->createAbsoluteLength(2), CssMinMaxMediaConditionInterface::MAX)
82
        );
83
        $this->assertTrue(is_array($mediaConditions));
84
        $this->assertEquals(3, count($mediaConditions));
85
        $mediaConditionsSpecs = [
86
            '(-webkit-max-device-pixel-ratio: 2)',
87
            '(max-resolution: 192dpi)',
88
            '(max-resolution: 2ddpx)'
89
        ];
90
        /** @var CssMediaCondition $mediaCondition */
91
        foreach ($mediaConditions as $mediaCondition) {
92
            $this->assertInstanceOf(CssMediaCondition::class, $mediaCondition);
93
            $this->assertEquals(array_shift($mediaConditionsSpecs), strval($mediaCondition));
94
        }
95
    }
96
97
    /**
98
     * Test the CSS media condition factory with a width condition
99
     */
100
    public function testCssMediaConditionFactoryWidth()
101
    {
102
        $lengthFactory       = new LengthFactory(new ViewportCalculatorServiceFactory(), 16);
103
        $widthMediaCondition = new WidthMediaCondition(
104
            $lengthFactory->createAbsoluteLengthFromString('1000px'),
105
            CssMinMaxMediaConditionInterface::MAX
106
        );
107
        $mediaConditions     = CssMediaConditionFactory::createFromMediaCondition($widthMediaCondition);
108
        $this->assertTrue(is_array($mediaConditions));
109
        $this->assertEquals(1, count($mediaConditions));
110
        /** @var CssMediaCondition $mediaCondition */
111
        foreach ($mediaConditions as $mediaCondition) {
112
            $this->assertInstanceOf(CssMediaCondition::class, $mediaCondition);
113
            $this->assertEquals('(max-width: 1000px)', strval($mediaCondition));
114
        }
115
    }
116
}
117