|  | 
|  | 1 | +<?php | 
|  | 2 | + | 
|  | 3 | +/* | 
|  | 4 | + * This file is part of the Symfony package. | 
|  | 5 | + * | 
|  | 6 | + * (c) Fabien Potencier <[email protected]> | 
|  | 7 | + * | 
|  | 8 | + * For the full copyright and license information, please view the LICENSE | 
|  | 9 | + * file that was distributed with this source code. | 
|  | 10 | + */ | 
|  | 11 | + | 
|  | 12 | +namespace Symfony\Component\Workflow\Tests\Debug; | 
|  | 13 | + | 
|  | 14 | +use PHPUnit\Framework\MockObject\MockObject; | 
|  | 15 | +use PHPUnit\Framework\TestCase; | 
|  | 16 | +use Symfony\Component\Stopwatch\Stopwatch; | 
|  | 17 | +use Symfony\Component\Workflow\Debug\TraceableWorkflow; | 
|  | 18 | +use Symfony\Component\Workflow\Marking; | 
|  | 19 | +use Symfony\Component\Workflow\TransitionBlockerList; | 
|  | 20 | +use Symfony\Component\Workflow\Workflow; | 
|  | 21 | + | 
|  | 22 | +class TraceableWorkflowTest extends TestCase | 
|  | 23 | +{ | 
|  | 24 | +    private MockObject|Workflow $innerWorkflow; | 
|  | 25 | + | 
|  | 26 | +    private StopWatch $stopwatch; | 
|  | 27 | + | 
|  | 28 | +    private TraceableWorkflow $traceableWorkflow; | 
|  | 29 | + | 
|  | 30 | +    protected function setUp(): void | 
|  | 31 | +    { | 
|  | 32 | +        $this->innerWorkflow = $this->createMock(Workflow::class); | 
|  | 33 | +        $this->stopwatch = new Stopwatch(); | 
|  | 34 | + | 
|  | 35 | +        $this->traceableWorkflow = new TraceableWorkflow( | 
|  | 36 | +            $this->innerWorkflow, | 
|  | 37 | +            $this->stopwatch | 
|  | 38 | +        ); | 
|  | 39 | +    } | 
|  | 40 | + | 
|  | 41 | +    /** | 
|  | 42 | +     * @dataProvider provideFunctionNames | 
|  | 43 | +     */ | 
|  | 44 | +    public function testCallsInner(string $function, array $args, mixed $returnValue) | 
|  | 45 | +    { | 
|  | 46 | +        $this->innerWorkflow->expects($this->once()) | 
|  | 47 | +            ->method($function) | 
|  | 48 | +            ->willReturn($returnValue); | 
|  | 49 | + | 
|  | 50 | +        $this->assertSame($returnValue, $this->traceableWorkflow->{$function}(...$args)); | 
|  | 51 | + | 
|  | 52 | +        $calls = $this->traceableWorkflow->getCalls(); | 
|  | 53 | + | 
|  | 54 | +        $this->assertCount(1, $calls); | 
|  | 55 | +        $this->assertSame($function, $calls[0]['method']); | 
|  | 56 | +        $this->assertArrayHasKey('duration', $calls[0]); | 
|  | 57 | +        $this->assertSame($returnValue, $calls[0]['return']); | 
|  | 58 | +    } | 
|  | 59 | + | 
|  | 60 | +    public function testCallsInnerCatchesException() | 
|  | 61 | +    { | 
|  | 62 | +        $exception = new \Exception('foo'); | 
|  | 63 | +        $this->innerWorkflow->expects($this->once()) | 
|  | 64 | +            ->method('can') | 
|  | 65 | +            ->willThrowException($exception); | 
|  | 66 | + | 
|  | 67 | +        try { | 
|  | 68 | +            $this->traceableWorkflow->can(new \stdClass(), 'foo'); | 
|  | 69 | + | 
|  | 70 | +            $this->fail('An exception should have been thrown.'); | 
|  | 71 | +        } catch (\Exception $e) { | 
|  | 72 | +            $this->assertSame($exception, $e); | 
|  | 73 | + | 
|  | 74 | +            $calls = $this->traceableWorkflow->getCalls(); | 
|  | 75 | + | 
|  | 76 | +            $this->assertCount(1, $calls); | 
|  | 77 | +            $this->assertSame('can', $calls[0]['method']); | 
|  | 78 | +            $this->assertArrayHasKey('duration', $calls[0]); | 
|  | 79 | +            $this->assertArrayHasKey('exception', $calls[0]); | 
|  | 80 | +            $this->assertSame($exception, $calls[0]['exception']); | 
|  | 81 | +        } | 
|  | 82 | +    } | 
|  | 83 | + | 
|  | 84 | +    public static function provideFunctionNames(): \Generator | 
|  | 85 | +    { | 
|  | 86 | +        $subject = new \stdClass(); | 
|  | 87 | + | 
|  | 88 | +        yield ['getMarking', [$subject], new Marking(['place' => 1])]; | 
|  | 89 | + | 
|  | 90 | +        yield ['can', [$subject, 'foo'], true]; | 
|  | 91 | + | 
|  | 92 | +        yield ['buildTransitionBlockerList', [$subject, 'foo'], new TransitionBlockerList()]; | 
|  | 93 | + | 
|  | 94 | +        yield ['apply', [$subject, 'foo'], new Marking(['place' => 1])]; | 
|  | 95 | + | 
|  | 96 | +        yield ['getEnabledTransitions', [$subject], []]; | 
|  | 97 | + | 
|  | 98 | +        yield ['getEnabledTransition', [$subject, 'foo'], null]; | 
|  | 99 | +    } | 
|  | 100 | +} | 
0 commit comments