99use Sabberworm \CSS \Parsing \ParserState ;
1010use Sabberworm \CSS \Parsing \UnexpectedTokenException ;
1111use Sabberworm \CSS \Property \Selector ;
12+ use Sabberworm \CSS \Property \Selector \Combinator ;
13+ use Sabberworm \CSS \Property \Selector \Component ;
14+ use Sabberworm \CSS \Property \Selector \CompoundSelector ;
1215use Sabberworm \CSS \Renderable ;
1316use Sabberworm \CSS \Settings ;
1417use TRegx \PhpUnit \DataProviders \DataProvider ;
@@ -315,10 +318,11 @@ public function parseExtractsTwoCommentsFromSelector(): void
315318 * @test
316319 *
317320 * @dataProvider provideInvalidSelectors
321+ * @dataProvider provideInvalidSelectorsForParse
318322 */
319323 public function constructorThrowsExceptionWithInvalidSelector (string $ selector ): void
320324 {
321- $ this ->expectException (\UnexpectedValueException ::class);
325+ $ this ->expectException (UnexpectedTokenException ::class);
322326
323327 new Selector ($ selector );
324328 }
@@ -327,16 +331,141 @@ public function constructorThrowsExceptionWithInvalidSelector(string $selector):
327331 * @test
328332 *
329333 * @dataProvider provideInvalidSelectors
334+ * @dataProvider provideInvalidSelectorsForParse
330335 */
331336 public function setSelectorThrowsExceptionWithInvalidSelector (string $ selector ): void
332337 {
333- $ this ->expectException (\UnexpectedValueException ::class);
338+ $ this ->expectException (UnexpectedTokenException ::class);
334339
335340 $ subject = new Selector ('a ' );
336341
337342 $ subject ->setSelector ($ selector );
338343 }
339344
345+ /**
346+ * @return array<
347+ * non-empty-string,
348+ * array{
349+ * 0: non-empty-list<Component>,
350+ * 1: non-empty-list<array{class: non-empty-string, value: non-empty-string}>
351+ * }
352+ * >
353+ */
354+ public static function provideComponentsAndArrayRepresentation (): array
355+ {
356+ return [
357+ 'simple selector ' => [
358+ [new CompoundSelector ('p ' )],
359+ [
360+ [
361+ 'class ' => 'CompoundSelector ' ,
362+ 'value ' => 'p ' ,
363+ ],
364+ ],
365+ ],
366+ 'selector with combinator ' => [
367+ [
368+ new CompoundSelector ('ul ' ),
369+ new Combinator ('> ' ),
370+ new CompoundSelector ('li ' ),
371+ ],
372+ [
373+ [
374+ 'class ' => 'CompoundSelector ' ,
375+ 'value ' => 'ul ' ,
376+ ],
377+ [
378+ 'class ' => 'Combinator ' ,
379+ 'value ' => '> ' ,
380+ ],
381+ [
382+ 'class ' => 'CompoundSelector ' ,
383+ 'value ' => 'li ' ,
384+ ],
385+ ],
386+ ],
387+ ];
388+ }
389+
390+ /**
391+ * @test
392+ *
393+ * @param non-empty-list<Component> $components
394+ * @param non-empty-list<array{class: non-empty-string, value: non-empty-string}> $expectedRepresenation
395+ *
396+ * @dataProvider provideComponentsAndArrayRepresentation
397+ */
398+ public function constructsWithComponentsProvided (array $ components , array $ expectedRepresenation ): void
399+ {
400+ $ subject = new Selector ($ components );
401+
402+ $ representation = $ subject ->getArrayRepresentation ()['components ' ];
403+ self ::assertSame ($ expectedRepresenation , $ representation );
404+ }
405+
406+ /**
407+ * @test
408+ */
409+ public function setComponentsProvidesFluentInterface (): void
410+ {
411+ $ subject = new Selector ([new CompoundSelector ('p ' )]);
412+
413+ $ result = $ subject ->setComponents ([new CompoundSelector ('li ' )]);
414+
415+ self ::assertSame ($ subject , $ result );
416+ }
417+
418+ /**
419+ * @test
420+ *
421+ * @param non-empty-list<Component> $components
422+ * @param non-empty-list<array{class: non-empty-string, value: non-empty-string}> $expectedRepresenation
423+ *
424+ * @dataProvider provideComponentsAndArrayRepresentation
425+ */
426+ public function setComponentsSetsComponentsProvided (array $ components , array $ expectedRepresenation ): void
427+ {
428+ $ subject = new Selector ([new CompoundSelector ('p ' )]);
429+
430+ $ subject ->setComponents ($ components );
431+
432+ $ representation = $ subject ->getArrayRepresentation ()['components ' ];
433+ self ::assertSame ($ expectedRepresenation , $ representation );
434+ }
435+
436+ /**
437+ * @test
438+ *
439+ * @param non-empty-list<Component> $components
440+ *
441+ * @dataProvider provideComponentsAndArrayRepresentation
442+ */
443+ public function getComponentsReturnsComponentsProvidedToConstructor (array $ components ): void
444+ {
445+ $ subject = new Selector ($ components );
446+
447+ $ result = $ subject ->getComponents ();
448+
449+ self ::assertSame ($ components , $ result );
450+ }
451+
452+ /**
453+ * @test
454+ *
455+ * @param non-empty-list<Component> $components
456+ *
457+ * @dataProvider provideComponentsAndArrayRepresentation
458+ */
459+ public function getComponentsReturnsComponentsSet (array $ components ): void
460+ {
461+ $ subject = new Selector ([new CompoundSelector ('p ' )]);
462+ $ subject ->setComponents ($ components );
463+
464+ $ result = $ subject ->getComponents ();
465+
466+ self ::assertSame ($ components , $ result );
467+ }
468+
340469 /**
341470 * @test
342471 *
@@ -446,10 +575,22 @@ public function doesNotCleanupSpacesWithinAttributeSelector(): void
446575 */
447576 public function getArrayRepresentationIncludesClassName (): void
448577 {
449- $ subject = new Selector (' a ' );
578+ $ subject = new Selector ([ new CompoundSelector ( ' p ' )] );
450579
451580 $ result = $ subject ->getArrayRepresentation ();
452581
453582 self ::assertSame ('Selector ' , $ result ['class ' ]);
454583 }
584+
585+ /**
586+ * @test
587+ */
588+ public function getArrayRepresentationIncludesComponent (): void
589+ {
590+ $ subject = new Selector ([new CompoundSelector ('p.test ' )]);
591+
592+ $ result = $ subject ->getArrayRepresentation ();
593+
594+ self ::assertSame ('p.test ' , $ result ['components ' ][0 ]['value ' ]);
595+ }
455596}
0 commit comments