|
13 | 13 | use Cloudinary\Transformation\Crop; |
14 | 14 | use Cloudinary\Transformation\Expression\Expression; |
15 | 15 | use Cloudinary\Transformation\Expression\ExpressionComponent; |
| 16 | +use Cloudinary\Transformation\Expression\ExpressionUtils; |
16 | 17 | use Cloudinary\Transformation\Expression\LogicalOperator; |
17 | 18 | use Cloudinary\Transformation\Expression\PVar; |
18 | 19 | use Cloudinary\Transformation\Expression\RelationalOperator; |
@@ -117,4 +118,62 @@ public function testRawExpression() |
117 | 118 |
|
118 | 119 | self::assertEquals($expectedExpr . '_gt_997', (string)$rawExpr->greaterThan()->numeric(997)); |
119 | 120 | } |
| 121 | + |
| 122 | + /** |
| 123 | + * Check expression normalization |
| 124 | + * |
| 125 | + * @dataProvider testNormalizationDataProvider |
| 126 | + * |
| 127 | + * @param mixed $input Value to normalize |
| 128 | + * @param null|string $expectedOutput Expected normalized output |
| 129 | + */ |
| 130 | + public function testExpressionNormalization($input, $expectedOutput) |
| 131 | + { |
| 132 | + $actual = ExpressionUtils::normalize($input); |
| 133 | + self::assertEquals($expectedOutput, $actual); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Data provider for testExpressionNormalization |
| 138 | + * |
| 139 | + * @return array[] |
| 140 | + */ |
| 141 | + public static function testNormalizationDataProvider() |
| 142 | + { |
| 143 | + return [ |
| 144 | + 'null is not affected' => [null, null], |
| 145 | + 'number replaced with a string value' => [10, '10'], |
| 146 | + 'empty string is not affected' => ['', ''], |
| 147 | + 'single space is replaced with a single underscore' => [' ', '_'], |
| 148 | + 'blank string is replaced with a single underscore' => [' ', '_'], |
| 149 | + 'underscore is not affected' => ['_', '_'], |
| 150 | + 'sequence of underscores and spaces is replaced with a single underscore' => [' _ __ _', '_'], |
| 151 | + 'arbitrary text is not affected' => ['foobar', 'foobar'], |
| 152 | + 'double ampersand replaced with and operator' => ['foo && bar', 'foo_and_bar'], |
| 153 | + 'double ampersand with no space at the end is not affected' => ['foo&&bar', 'foo&&bar'], |
| 154 | + 'width recognized as variable and replaced with w' => ['width', 'w'], |
| 155 | + 'initial aspect ratio recognized as variable and replaced with iar' => ['initial_aspect_ratio', 'iar'], |
| 156 | + '$width recognized as user variable and not affected' => ['$width', '$width'], |
| 157 | + '$initial_aspect_ratio recognized as user variable followed by aspect_ratio variable' => [ |
| 158 | + '$initial_aspect_ratio', |
| 159 | + '$initial_ar', |
| 160 | + ], |
| 161 | + '$mywidth recognized as user variable and not affected' => ['$mywidth', '$mywidth'], |
| 162 | + '$widthwidth recognized as user variable and not affected' => ['$widthwidth', '$widthwidth'], |
| 163 | + '$_width recognized as user variable and not affected' => ['$_width', '$_width'], |
| 164 | + '$__width recognized as user variable and not affected' => ['$__width', '$_width'], |
| 165 | + '$$width recognized as user variable and not affected' => ['$$width', '$$width'], |
| 166 | + '$height recognized as user variable and not affected' => ['$height_100', '$height_100'], |
| 167 | + '$heightt_100 recognized as user variable and not affected' => ['$heightt_100', '$heightt_100'], |
| 168 | + '$$height_100 recognized as user variable and not affected' => ['$$height_100', '$$height_100'], |
| 169 | + '$heightmy_100 recognized as user variable and not affected' => ['$heightmy_100', '$heightmy_100'], |
| 170 | + '$myheight_100 recognized as user variable and not affected' => ['$myheight_100', '$myheight_100'], |
| 171 | + '$heightheight_100 recognized as user variable and not affected' => [ |
| 172 | + '$heightheight_100', |
| 173 | + '$heightheight_100', |
| 174 | + ], |
| 175 | + '$theheight_100 recognized as user variable and not affected' => ['$theheight_100', '$theheight_100'], |
| 176 | + '$__height_100 recognized as user variable and not affected' => ['$__height_100', '$_height_100'] |
| 177 | + ]; |
| 178 | + } |
120 | 179 | } |
0 commit comments