PHP Classes

File: src/Traits/ExpectWorkflowExceptionTrait.php

Recommend this page to a friend!
  Packages of Rodolfo Berrios Arce   Workflow   src/Traits/ExpectWorkflowExceptionTrait.php   Download  
File: src/Traits/ExpectWorkflowExceptionTrait.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Workflow
Create and run action workflows
Author: By
Last change:
Date: 1 year ago
Size: 1,813 bytes
 

Contents

Class file image Download
<?php

/*
 * This file is part of Chevere.
 *
 * (c) Rodolfo Berrios <rodolfo@chevere.org>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace Chevere\Workflow\Traits;

use Chevere\Workflow\Exceptions\RunnerException;
use Closure;

/**
 * Shorthand for testing a job runtime WorkflowException for PHPUnit.
 */
trait ExpectWorkflowExceptionTrait // @phpstan-ignore-line
{
    abstract public static function assertInstanceOf(string $expected, mixed $actual, string $message = ''): void;

    abstract public static function assertSame(mixed $expected, mixed $actual, string $message = ''): void;

    /**
     * Expect a WorkflowException.
     *
     * @param Closure $closure The logic to test.
     * @param string $job The expected job that will throw exception.
     * @param string $instance The expected job exception instance.
     * @param int $code The expected job exception code.
     */
    private function expectWorkflowException(
        Closure $closure,
        string $instance,
        ?string $job,
        ?string $message,
        ?int $code
    ): void {
        try {
            $closure();
        } catch (RunnerException $e) {
            $this->assertSame(
                $e->getMessage(),
                "[{$job}]: {$message}"
            );
            $this->assertInstanceOf($instance, $e->throwable);
            if ($job !== null) {
                $this->assertSame($job, $e->name);
            }
            if ($message !== null) {
                $this->assertSame($message, $e->throwable->getMessage());
            }
            if ($code !== null) {
                $this->assertSame($code, $e->throwable->getCode());
            }
        }
    }
}