Skip to content

Commit 722a874

Browse files
committed
feat(Testing): Add TestRunner trait with TestingSuite attribute
1 parent 28af07b commit 722a874

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Testo\Testing\Attribute;
6+
7+
use Internal\Path;
8+
9+
/**
10+
* Configure Test Suite for the testing tools.
11+
*/
12+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION)]
13+
final class TestingSuite
14+
{
15+
public function __construct(
16+
/**
17+
* @var string|Path Stub directory or file path.
18+
*/
19+
public readonly string|Path $path,
20+
) {}
21+
}
Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
declare(strict_types=1);
44

5-
namespace Tests\Assert;
5+
namespace Testo\Testing\Traits;
66

77
use InvalidArgumentException as InvalidArgument;
88
use Testo\Application\Application;
99
use Testo\Application\Config\ApplicationConfig;
1010
use Testo\Application\Config\FinderConfig;
1111
use Testo\Application\Config\SuiteConfig;
1212
use Testo\Application\Value\Filter;
13+
use Testo\Common\Reflection;
1314
use 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
/**

tests/Assert/Feature/CommonTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@
77
use Testo\Application\Attribute\Test;
88
use Testo\Assert;
99
use Testo\Core\Value\Status;
10+
use Testo\Testing\Attribute\TestingSuite;
11+
use Testo\Testing\Traits\TestRunner;
1012
use Tests\Assert\Stub\Common;
11-
use Tests\Assert\StubRunner;
1213

14+
#[TestingSuite(path: __DIR__ . '/../Stub')]
1315
final class CommonTest
1416
{
17+
use TestRunner;
18+
1519
#[Test(description: 'A successfully finished test without any assertion marked as Risky')]
1620
public function noAssertions(): void
1721
{
18-
$result = StubRunner::runTest([Common::class, 'risky']);
22+
$result = self::runTest([Common::class, 'risky']);
1923
Assert::same(Status::Risky, $result->status);
2024
}
2125
}

0 commit comments

Comments
 (0)