|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Testo\Sample\Internal; |
| 6 | + |
| 7 | +use Psr\EventDispatcher\EventDispatcherInterface; |
| 8 | +use Testo\Attribute\Test; |
| 9 | +use Testo\Interceptor\CaseLocatorInterceptor; |
| 10 | +use Testo\Interceptor\FileLocatorInterceptor; |
| 11 | +use Testo\Interceptor\Reflection\Reflection; |
| 12 | +use Testo\Interceptor\TestRunInterceptor; |
| 13 | +use Testo\Module\Tokenizer\Reflection\FileDefinitions; |
| 14 | +use Testo\Module\Tokenizer\Reflection\TokenizedFile; |
| 15 | +use Testo\Sample\MultipleResult; |
| 16 | +use Testo\Sample\TestInline; |
| 17 | +use Testo\Test\Dto\CaseDefinitions; |
| 18 | +use Testo\Test\Dto\Status; |
| 19 | +use Testo\Test\Dto\TestInfo; |
| 20 | +use Testo\Test\Dto\TestResult; |
| 21 | +use Testo\Test\Event\Test\TestBatchFinished; |
| 22 | +use Testo\Test\Event\Test\TestBatchStarting; |
| 23 | +use Testo\Test\Event\Test\TestDataSetFinished; |
| 24 | +use Testo\Test\Event\Test\TestDataSetStarting; |
| 25 | + |
| 26 | +/** |
| 27 | + * Interceptor that runs the target method as a pure function with provided arguments and expected result. |
| 28 | + */ |
| 29 | +final class TestInlineInterceptor implements TestRunInterceptor |
| 30 | +{ |
| 31 | + /** @var callable(TestInfo): mixed Invoker for the test method. */ |
| 32 | + private readonly \CLosure $invoker; |
| 33 | + |
| 34 | + public function __construct( |
| 35 | + private readonly EventDispatcherInterface $eventDispatcher, |
| 36 | + ) { |
| 37 | + $this->invoker = (new InlineTestInvoker())(...); |
| 38 | + } |
| 39 | + |
| 40 | + #[\Override] |
| 41 | + public function runTest(TestInfo $info, callable $next): TestResult |
| 42 | + { |
| 43 | + /** @var TestInline[] $attributes */ |
| 44 | + $attributes = $info->getAttribute(TestInline::class); |
| 45 | + if ($attributes === []) { |
| 46 | + return $next($info); |
| 47 | + } |
| 48 | + |
| 49 | + if (\count($attributes) === 1) { |
| 50 | + $inline = \reset($attributes); |
| 51 | + return $next($info->with(arguments: $inline->arguments)->withAttribute(TestInline::class, $inline)); |
| 52 | + } |
| 53 | + |
| 54 | + # Dispatch batch starting event |
| 55 | + $this->eventDispatcher->dispatch(new TestBatchStarting($info)); |
| 56 | + |
| 57 | + // Run the test for each data set |
| 58 | + $results = []; |
| 59 | + $status = Status::Passed; |
| 60 | + foreach ($attributes as $index => $inline) { |
| 61 | + $newInfo = $info->with(arguments: $inline->arguments); |
| 62 | + $label = "$index"; |
| 63 | + |
| 64 | + # Dispatch dataset starting event |
| 65 | + $this->eventDispatcher->dispatch( |
| 66 | + new TestDataSetStarting($newInfo, $label, $index) |
| 67 | + ); |
| 68 | + |
| 69 | + try { |
| 70 | + $result = $next($newInfo); |
| 71 | + } catch (\Throwable $throwable) { |
| 72 | + $result = new TestResult(info: $newInfo, status: Status::Error, failure: $throwable); |
| 73 | + } |
| 74 | + |
| 75 | + # Dispatch dataset finished event |
| 76 | + $this->eventDispatcher->dispatch( |
| 77 | + new TestDataSetFinished($newInfo, $result, $label, $index) |
| 78 | + ); |
| 79 | + |
| 80 | + unset($inline, $newInfo); |
| 81 | + $result->status->isFailure() and ($status = Status::Failed); |
| 82 | + $results[] = $result; |
| 83 | + } |
| 84 | + |
| 85 | + $results = new MultipleResult($results); |
| 86 | + |
| 87 | + $finalResult = new TestResult(info: $info, status: $status, result: $results, attributes: [ |
| 88 | + MultipleResult::class => $results, |
| 89 | + ]); |
| 90 | + |
| 91 | + # Dispatch batch finished event |
| 92 | + $this->eventDispatcher->dispatch(new TestBatchFinished($info, $finalResult)); |
| 93 | + |
| 94 | + return $finalResult; |
| 95 | + } |
| 96 | +} |
0 commit comments