| 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(1, $this->countViewportTokens($refinedCalculationTokens)); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         // Evaluate | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         $this->assertEquals(900, $this->calculatorService->evaluate($refinedCalculationTokens)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * Count the number of viewport tokens | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param array $tokens Calculation tokens | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @return int Number of viewport tokens | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     protected function countViewportTokens(array $tokens): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return array_reduce( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             $tokens, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             function ($sum, $token) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 return $sum + $this->calculatorService->isViewportToken($token) * 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * Run other atomic calculator service tests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @param string $calculationString      Calculation string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param int $numTokens                 Number of parsed tokens | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @param int $numRefinedTokens          Number of refined tokens | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @param string|null $expectedException Optional: Expected exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @param int $expectedExceptionCode     Optional: Expected exception code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @dataProvider          provideCalculatorData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     public function testViewportCalculatorServiceAtoms( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         string $calculationString, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         int $numTokens, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         int $numRefinedTokens, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         string $expectedException = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         int $expectedExceptionCode = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $this->assertInstanceOf(CalculatorServiceInterface::class, $this->calculatorService); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         if ($expectedException) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             $this->expectException($expectedException); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             $this->expectExceptionCode($expectedExceptionCode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         // Tokenize | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $calculationTokens = $this->calculatorService->tokenize($calculationString); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $this->assertTrue(is_array($calculationTokens)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $this->assertEquals($numTokens, count($calculationTokens)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         // Refine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $refinedCalculationTokens = $this->calculatorService->refineCalculationTokens($calculationTokens, 16); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $this->assertTrue(is_array($refinedCalculationTokens)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $this->assertEquals($numRefinedTokens, count($refinedCalculationTokens)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * Data provider for atomic calculation tests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * @return array Calculation data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     public function provideCalculatorData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             ['1 + 2', 3, 3], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             ['1 calc(1)', 5, 4], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             ['1abc', 2, 0, InvalidArgumentException::class, 1522701212], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         ]; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 169 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 170 |  |  | } |