Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ProphecyTrait.php

trait ProphecyTrait

@mixin TestCase

Hierarchy

  • trait \Prophecy\PhpUnit\ProphecyTrait

File

vendor/phpspec/prophecy-phpunit/src/ProphecyTrait.php, line 19

Namespace

Prophecy\PhpUnit
View source
trait ProphecyTrait {
    
    /**
     * @var Prophet|null
     *
     * @internal
     */
    private $prophet;
    
    /**
     * @var bool
     *
     * @internal
     */
    private $prophecyAssertionsCounted = false;
    
    /**
     * @throws DoubleException
     * @throws InterfaceNotFoundException
     *
     * @template T of object
     * @phpstan-param class-string<T>|null $classOrInterface
     * @phpstan-return ($classOrInterface is null ? ObjectProphecy<object> : ObjectProphecy<T>)
     *
     * @not-deprecated
     */
    protected function prophesize(?string $classOrInterface = null) : ObjectProphecy {
        static $isPhpUnit9;
        $isPhpUnit9 = $isPhpUnit9 ?? method_exists($this, 'recordDoubledType');
        if (!$isPhpUnit9) {
            // PHPUnit 10.1
            $this->registerFailureType(PredictionException::class);
        }
        elseif (\is_string($classOrInterface)) {
            // PHPUnit 9
            \assert($this instanceof TestCase);
            $this->recordDoubledType($classOrInterface);
        }
        return $this->getProphet()
            ->prophesize($classOrInterface);
    }
    
    /**
     * @postCondition
     */
    protected function verifyProphecyDoubles() : void {
        if ($this->prophet === null) {
            return;
        }
        try {
            $this->prophet
                ->checkPredictions();
        } catch (PredictionException $e) {
            throw new AssertionFailedError($e->getMessage());
        } finally {
            $this->countProphecyAssertions();
        }
    }
    
    /**
     * @after
     */
    protected function tearDownProphecy() : void {
        if (null !== $this->prophet && !$this->prophecyAssertionsCounted) {
            // Some Prophecy assertions may have been done in tests themselves even when a failure happened before checking mock objects.
            $this->countProphecyAssertions();
        }
        $this->prophet = null;
    }
    
    /**
     * @internal
     */
    private function countProphecyAssertions() : void {
        \assert($this instanceof TestCase);
        \assert($this->prophet !== null);
        $this->prophecyAssertionsCounted = true;
        foreach ($this->prophet
            ->getProphecies() as $objectProphecy) {
            foreach ($objectProphecy->getMethodProphecies() as $methodProphecies) {
                foreach ($methodProphecies as $methodProphecy) {
                    \assert($methodProphecy instanceof MethodProphecy);
                    $this->addToAssertionCount(\count($methodProphecy->getCheckedPredictions()));
                }
            }
        }
    }
    
    /**
     * @internal
     */
    private function getProphet() : Prophet {
        if ($this->prophet === null) {
            $this->prophet = new Prophet();
        }
        return $this->prophet;
    }

}

Members

Title Sort descending Modifiers Object type Summary
ProphecyTrait::$prophecyAssertionsCounted private property @internal
ProphecyTrait::$prophet private property @internal
ProphecyTrait::countProphecyAssertions private function @internal
ProphecyTrait::getProphet private function @internal
ProphecyTrait::prophesize protected function @template T of object
@phpstan-param class-string&lt;T&gt;|null $classOrInterface
@phpstan-return ($classOrInterface is null ? ObjectProphecy&lt;object&gt; : ObjectProphecy&lt;T&gt;)
ProphecyTrait::tearDownProphecy protected function @after
ProphecyTrait::verifyProphecyDoubles protected function @postCondition
RSS feed
Powered by Drupal