@@ -522,6 +522,116 @@ public function isForTestClass(): bool
522
522
return class_exists ($ this ->name , false ) && is_subclass_of ($ this ->name , TestCase::class);
523
523
}
524
524
525
+ /**
526
+ * @param ReflectionClass<TestCase> $class
527
+ * @param list<non-empty-string> $groups
528
+ *
529
+ * @throws Exception
530
+ */
531
+ protected function addTestMethod (ReflectionClass $ class , ReflectionMethod $ method , array $ groups ): void
532
+ {
533
+ $ className = $ class ->getName ();
534
+ $ methodName = $ method ->getName ();
535
+
536
+ assert (!empty ($ methodName ));
537
+
538
+ try {
539
+ $ test = (new TestBuilder )->build ($ class , $ methodName , $ groups );
540
+ } catch (InvalidDataProviderException $ e ) {
541
+ Event \Facade::emitter ()->testTriggeredPhpunitError (
542
+ new TestMethod (
543
+ $ className ,
544
+ $ methodName ,
545
+ $ class ->getFileName (),
546
+ $ method ->getStartLine (),
547
+ Event \Code \TestDoxBuilder::fromClassNameAndMethodName (
548
+ $ className ,
549
+ $ methodName ,
550
+ ),
551
+ MetadataCollection::fromArray ([]),
552
+ Event \TestData \TestDataCollection::fromArray ([]),
553
+ ),
554
+ sprintf (
555
+ "The data provider specified for %s::%s is invalid \n%s " ,
556
+ $ className ,
557
+ $ methodName ,
558
+ $ this ->throwableToString ($ e ),
559
+ ),
560
+ );
561
+
562
+ return ;
563
+ }
564
+
565
+ if ($ test instanceof TestCase || $ test instanceof DataProviderTestSuite) {
566
+ $ test ->setDependencies (
567
+ Dependencies::dependencies ($ class ->getName (), $ methodName ),
568
+ );
569
+ }
570
+
571
+ $ this ->addTest (
572
+ $ test ,
573
+ array_merge (
574
+ $ groups ,
575
+ (new Groups )->groups ($ class ->getName (), $ methodName ),
576
+ ),
577
+ );
578
+ }
579
+
580
+ private function clearCaches (): void
581
+ {
582
+ $ this ->providedTests = null ;
583
+ $ this ->requiredTests = null ;
584
+ }
585
+
586
+ /**
587
+ * @param list<non-empty-string> $groups
588
+ */
589
+ private function containsOnlyVirtualGroups (array $ groups ): bool
590
+ {
591
+ foreach ($ groups as $ group ) {
592
+ if (!str_starts_with ($ group , '__phpunit_ ' )) {
593
+ return false ;
594
+ }
595
+ }
596
+
597
+ return true ;
598
+ }
599
+
600
+ private function methodDoesNotExistOrIsDeclaredInTestCase (string $ methodName ): bool
601
+ {
602
+ $ reflector = new ReflectionClass ($ this ->name );
603
+
604
+ return !$ reflector ->hasMethod ($ methodName ) ||
605
+ $ reflector ->getMethod ($ methodName )->getDeclaringClass ()->getName () === TestCase::class;
606
+ }
607
+
608
+ /**
609
+ * @throws Exception
610
+ */
611
+ private function throwableToString (Throwable $ t ): string
612
+ {
613
+ $ message = $ t ->getMessage ();
614
+
615
+ if (empty (trim ($ message ))) {
616
+ $ message = '<no message> ' ;
617
+ }
618
+
619
+ if ($ t instanceof InvalidDataProviderException) {
620
+ return sprintf (
621
+ "%s \n%s " ,
622
+ $ message ,
623
+ Filter::stackTraceFromThrowableAsString ($ t ),
624
+ );
625
+ }
626
+
627
+ return sprintf (
628
+ "%s: %s \n%s " ,
629
+ $ t ::class,
630
+ $ message ,
631
+ Filter::stackTraceFromThrowableAsString ($ t ),
632
+ );
633
+ }
634
+
525
635
/**
526
636
* @throws Exception
527
637
* @throws NoPreviousThrowableException
@@ -646,114 +756,4 @@ public function invokeMethodsAfterLastTest(Event\Emitter $emitter): void
646
756
);
647
757
}
648
758
}
649
-
650
- /**
651
- * @param ReflectionClass<TestCase> $class
652
- * @param list<non-empty-string> $groups
653
- *
654
- * @throws Exception
655
- */
656
- protected function addTestMethod (ReflectionClass $ class , ReflectionMethod $ method , array $ groups ): void
657
- {
658
- $ className = $ class ->getName ();
659
- $ methodName = $ method ->getName ();
660
-
661
- assert (!empty ($ methodName ));
662
-
663
- try {
664
- $ test = (new TestBuilder )->build ($ class , $ methodName , $ groups );
665
- } catch (InvalidDataProviderException $ e ) {
666
- Event \Facade::emitter ()->testTriggeredPhpunitError (
667
- new TestMethod (
668
- $ className ,
669
- $ methodName ,
670
- $ class ->getFileName (),
671
- $ method ->getStartLine (),
672
- Event \Code \TestDoxBuilder::fromClassNameAndMethodName (
673
- $ className ,
674
- $ methodName ,
675
- ),
676
- MetadataCollection::fromArray ([]),
677
- Event \TestData \TestDataCollection::fromArray ([]),
678
- ),
679
- sprintf (
680
- "The data provider specified for %s::%s is invalid \n%s " ,
681
- $ className ,
682
- $ methodName ,
683
- $ this ->throwableToString ($ e ),
684
- ),
685
- );
686
-
687
- return ;
688
- }
689
-
690
- if ($ test instanceof TestCase || $ test instanceof DataProviderTestSuite) {
691
- $ test ->setDependencies (
692
- Dependencies::dependencies ($ class ->getName (), $ methodName ),
693
- );
694
- }
695
-
696
- $ this ->addTest (
697
- $ test ,
698
- array_merge (
699
- $ groups ,
700
- (new Groups )->groups ($ class ->getName (), $ methodName ),
701
- ),
702
- );
703
- }
704
-
705
- private function clearCaches (): void
706
- {
707
- $ this ->providedTests = null ;
708
- $ this ->requiredTests = null ;
709
- }
710
-
711
- /**
712
- * @param list<non-empty-string> $groups
713
- */
714
- private function containsOnlyVirtualGroups (array $ groups ): bool
715
- {
716
- foreach ($ groups as $ group ) {
717
- if (!str_starts_with ($ group , '__phpunit_ ' )) {
718
- return false ;
719
- }
720
- }
721
-
722
- return true ;
723
- }
724
-
725
- private function methodDoesNotExistOrIsDeclaredInTestCase (string $ methodName ): bool
726
- {
727
- $ reflector = new ReflectionClass ($ this ->name );
728
-
729
- return !$ reflector ->hasMethod ($ methodName ) ||
730
- $ reflector ->getMethod ($ methodName )->getDeclaringClass ()->getName () === TestCase::class;
731
- }
732
-
733
- /**
734
- * @throws Exception
735
- */
736
- private function throwableToString (Throwable $ t ): string
737
- {
738
- $ message = $ t ->getMessage ();
739
-
740
- if (empty (trim ($ message ))) {
741
- $ message = '<no message> ' ;
742
- }
743
-
744
- if ($ t instanceof InvalidDataProviderException) {
745
- return sprintf (
746
- "%s \n%s " ,
747
- $ message ,
748
- Filter::stackTraceFromThrowableAsString ($ t ),
749
- );
750
- }
751
-
752
- return sprintf (
753
- "%s: %s \n%s " ,
754
- $ t ::class,
755
- $ message ,
756
- Filter::stackTraceFromThrowableAsString ($ t ),
757
- );
758
- }
759
759
}
0 commit comments