22
33declare (strict_types=1 );
44
5- namespace Tests \ Assert ;
5+ namespace Testo \ Testing \ Traits ;
66
77use InvalidArgumentException as InvalidArgument ;
88use Testo \Application \Application ;
99use Testo \Application \Config \ApplicationConfig ;
1010use Testo \Application \Config \FinderConfig ;
1111use Testo \Application \Config \SuiteConfig ;
1212use Testo \Application \Value \Filter ;
13+ use Testo \Common \Reflection ;
1314use Testo \Core \Context \TestResult ;
15+ use Testo \Testing \Attribute \TestingSuite ;
1416
15- final class StubRunner
17+ trait TestRunner
1618{
1719 /**
1820 * Run a specific test function and return its result.
@@ -30,7 +32,7 @@ public static function runTest(array|string $testFunction): TestResult
3032 $ filter = new Filter ();
3133
3234 # Run application to get test results
33- $ suites = self ::app ()->run ($ filter )->results ;
35+ $ suites = self ::getTestoApp ()->run ($ filter )->results ;
3436
3537 # Find and return the test result for the given test function
3638 foreach ($ suites as $ suite ) {
@@ -57,28 +59,34 @@ public static function runTest(array|string $testFunction): TestResult
5759 throw new InvalidArgument ('Test function not found: ' . $ testFunction ());
5860 }
5961
60- private static function app (): Application
62+ protected static function getTestoApp (): Application
6163 {
62- # Create and cache application instance
63- static $ app = null ;
64- if ($ app !== null ) {
65- return $ app ;
66- }
64+ /**
65+ * Try to create Application instance from Call Stack Attributes
66+ * @var list<\ReflectionAttribute<TestingSuite>> $suiteConfigs
67+ */
68+ $ suiteConfigs = Reflection::getAttributesFromCallStack (
69+ attributeClass: TestingSuite::class,
70+ includeClasses: true ,
71+ limit: 1 ,
72+ );
73+
74+ $ suiteConfigs === [] and throw new \RuntimeException ('Testing Suite is not configured. ' );
75+ $ config = \reset ($ suiteConfigs )->newInstance ();
6776
68- $ app = Application::createFromConfig (
77+ return Application::createFromConfig (
6978 new ApplicationConfig (
7079 src: null ,
7180 suites: [
7281 new SuiteConfig (
73- 'Stubs ' ,
82+ 'Testing ' ,
7483 location: new FinderConfig (
75- include : [__DIR__ . ' /Stub ' ],
84+ include : [$ config -> path ],
7685 ),
7786 ),
7887 ],
7988 ),
8089 );
81- return $ app ;
8290 }
8391
8492 /**
0 commit comments