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\Contract\CalculatorServiceInterface; |
||
40 | use Jkphl\Respimgcss\Application\Factory\LengthFactory; |
||
41 | use Jkphl\Respimgcss\Domain\Contract\AbsoluteLengthInterface; |
||
42 | use Jkphl\Respimgcss\Infrastructure\ViewportCalculatorService; |
||
43 | use Jkphl\Respimgcss\Infrastructure\ViewportCalculatorServiceFactory; |
||
44 | use Jkphl\Respimgcss\Ports\InvalidArgumentException; |
||
45 | use Jkphl\Respimgcss\Tests\AbstractTestBase; |
||
46 | |||
47 | /** |
||
48 | * Viewport calculator service tests |
||
49 | * |
||
50 | * @package Jkphl\Respimgcss |
||
51 | * @subpackage Jkphl\Respimgcss\Tests\Infrastructure |
||
52 | */ |
||
53 | class ViewportCalculatorServiceTest extends AbstractTestBase |
||
54 | { |
||
55 | /** |
||
56 | * Absolute length |
||
57 | * |
||
58 | * @var AbsoluteLengthInterface |
||
59 | */ |
||
60 | protected $absoluteLength; |
||
61 | /** |
||
62 | * Calculator service |
||
63 | * |
||
64 | * @var CalculatorServiceInterface |
||
65 | */ |
||
66 | protected $calculatorService; |
||
67 | |||
68 | /** |
||
69 | * Test setup |
||
70 | */ |
||
71 | protected function setUp() |
||
72 | { |
||
73 | parent::setUp(); |
||
74 | $this->absoluteLength = (new LengthFactory(new ViewportCalculatorServiceFactory(), 16)) |
||
75 | ->createAbsoluteLength(1000); |
||
76 | $this->calculatorService = new ViewportCalculatorService($this->absoluteLength); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Test the viewport calculator service |
||
81 | */ |
||
82 | public function testViewportCalculatorService() |
||
83 | { |
||
84 | $this->assertInstanceOf(CalculatorServiceInterface::class, $this->calculatorService); |
||
85 | |||
86 | // Tokenize |
||
87 | $viewportCalculationTokens = $this->calculatorService->tokenize('calc(100vw - 100px)'); |
||
88 | $this->assertTrue(is_array($viewportCalculationTokens)); |
||
89 | $this->assertEquals(8, count($viewportCalculationTokens)); |
||
90 | |||
91 | // Refine |
||
92 | $refinedCalculationTokens = $this->calculatorService->refineCalculationTokens($viewportCalculationTokens, 16); |
||
93 | $this->assertTrue(is_array($refinedCalculationTokens)); |
||
94 | $this->assertEquals(11, count($refinedCalculationTokens)); |
||
95 | |||
96 | // Test for viewport tokens |
||
97 | $this->assertEquals( |
||
98 | 1, |
||
99 | array_reduce( |
||
100 | $refinedCalculationTokens, |
||
101 | function($sum, $token) { |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
102 | return $sum + $this->calculatorService->isViewportToken($token) * 1; |
||
103 | }, |
||
104 | 0 |
||
105 | ) |
||
106 | ); |
||
107 | |||
108 | // Evaluate |
||
109 | $this->assertEquals(900, $this->calculatorService->evaluate($refinedCalculationTokens)); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Run other atomic calculator service tests |
||
114 | * |
||
115 | * @param string $calculationString Calculation string |
||
116 | * @param int $numTokens Number of parsed tokens |
||
117 | * @param int $numRefinedTokens Number of refined tokens |
||
118 | * @param string|null $expectedException Optional: Expected exception |
||
119 | * @param int $expectedExceptionCode Optional: Expected exception code |
||
120 | * |
||
121 | * @dataProvider provideCalculatorData |
||
122 | */ |
||
123 | public function testViewportCalculatorServiceAtoms( |
||
124 | string $calculationString, |
||
125 | int $numTokens, |
||
126 | int $numRefinedTokens, |
||
127 | string $expectedException = null, |
||
128 | int $expectedExceptionCode = 0 |
||
129 | ) { |
||
130 | $this->assertInstanceOf(CalculatorServiceInterface::class, $this->calculatorService); |
||
131 | |||
132 | if ($expectedException) { |
||
133 | $this->expectException($expectedException); |
||
134 | } |
||
135 | |||
136 | if ($expectedExceptionCode) { |
||
137 | $this->expectExceptionCode($expectedExceptionCode); |
||
138 | } |
||
139 | |||
140 | // Tokenize |
||
141 | $calculationTokens = $this->calculatorService->tokenize($calculationString); |
||
142 | $this->assertTrue(is_array($calculationTokens)); |
||
143 | $this->assertEquals($numTokens, count($calculationTokens)); |
||
144 | |||
145 | // Refine |
||
146 | $refinedCalculationTokens = $this->calculatorService->refineCalculationTokens($calculationTokens, 16); |
||
147 | $this->assertTrue(is_array($refinedCalculationTokens)); |
||
148 | $this->assertEquals($numRefinedTokens, count($refinedCalculationTokens)); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Data provider for atomic calculation tests |
||
153 | * |
||
154 | * @return array Calculation data |
||
155 | */ |
||
156 | public function provideCalculatorData() |
||
157 | { |
||
158 | return [ |
||
159 | ['1 + 2', 3, 3], |
||
160 | ['1 calc(1)', 5, 4], |
||
161 | ['1abc', 2, 0, InvalidArgumentException::class, 1522701212], |
||
162 | ]; |
||
163 | } |
||
164 | } |