|
8 | 8 | use Sabberworm\CSS\CSSElement; |
9 | 9 | use Sabberworm\CSS\CSSList\CSSListItem; |
10 | 10 | use Sabberworm\CSS\Parsing\ParserState; |
| 11 | +use Sabberworm\CSS\Parsing\UnexpectedTokenException; |
11 | 12 | use Sabberworm\CSS\Position\Positionable; |
12 | 13 | use Sabberworm\CSS\Property\Selector; |
13 | 14 | use Sabberworm\CSS\Rule\Rule; |
@@ -293,4 +294,73 @@ public function setSelectorsIgnoresKeys(): void |
293 | 294 |
|
294 | 295 | self::assertSame([0, 1], \array_keys($result)); |
295 | 296 | } |
| 297 | + |
| 298 | + /** |
| 299 | + * @test |
| 300 | + * |
| 301 | + * @param non-empty-string $selector |
| 302 | + * |
| 303 | + * @dataProvider provideSelector |
| 304 | + */ |
| 305 | + public function setSelectorsSetsSingleSelectorProvidedAsString(string $selector): void |
| 306 | + { |
| 307 | + $subject = new DeclarationBlock(); |
| 308 | + |
| 309 | + $subject->setSelectors($selector); |
| 310 | + |
| 311 | + $result = $subject->getSelectors(); |
| 312 | + self::assertSame([$selector], self::getSelectorsAsStrings($subject)); |
| 313 | + } |
| 314 | + |
| 315 | + /** |
| 316 | + * @test |
| 317 | + * |
| 318 | + * @param non-empty-string $firstSelector |
| 319 | + * @param non-empty-string $secondSelector |
| 320 | + * |
| 321 | + * @dataProvider provideTwoSelectors |
| 322 | + */ |
| 323 | + public function setSelectorsSetsTwoCommaSeparatedSelectorsProvidedAsString( |
| 324 | + string $firstSelector, |
| 325 | + string $secondSelector |
| 326 | + ): void { |
| 327 | + $joinedSelectors = $firstSelector . ', ' . $secondSelector; |
| 328 | + $subject = new DeclarationBlock(); |
| 329 | + |
| 330 | + $subject->setSelectors($joinedSelectors); |
| 331 | + |
| 332 | + $result = $subject->getSelectors(); |
| 333 | + self::assertSame([$firstSelector, $secondSelector], self::getSelectorsAsStrings($subject)); |
| 334 | + } |
| 335 | + |
| 336 | + /** |
| 337 | + * Provides selectors that would be parsed without error in the context of full CSS, but are nonetheless invalid. |
| 338 | + * |
| 339 | + * @return array<non-empty-string, array{0: non-empty-string}> |
| 340 | + */ |
| 341 | + public static function provideInvalidStandaloneSelector(): array |
| 342 | + { |
| 343 | + return [ |
| 344 | + 'rogue `{`' => ['a { b'], |
| 345 | + 'rogue `}`' => ['a } b'], |
| 346 | + ]; |
| 347 | + } |
| 348 | + |
| 349 | + /** |
| 350 | + * @test |
| 351 | + * |
| 352 | + * @param non-empty-string $selector |
| 353 | + * |
| 354 | + * @dataProvider provideInvalidSelector |
| 355 | + * @dataProvider provideInvalidStandaloneSelector |
| 356 | + */ |
| 357 | + public function setSelectorsThrowsExceptionWithInvalidSelector(string $selector): void |
| 358 | + { |
| 359 | + $this->expectException(UnexpectedTokenException::class); |
| 360 | + $this->expectExceptionMessageMatches('/^Selector\\(s\\) string is not valid. /'); |
| 361 | + |
| 362 | + $subject = new DeclarationBlock(); |
| 363 | + |
| 364 | + $subject->setSelectors($selector); |
| 365 | + } |
296 | 366 | } |
0 commit comments