• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hjerichen / prophecy-php / 13100683382

02 Feb 2025 04:48PM UTC coverage: 98.113% (-1.9%) from 100.0%
13100683382

push

github

Heiko Jerichen
Set support for PHPUnit versions of "^9.6.21 | ^10.0 | ^11.0"

17 of 20 new or added lines in 2 files covered. (85.0%)

156 of 159 relevant lines covered (98.11%)

15.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.96
/src/PHPProphetTrait.php
1
<?php declare(strict_types=1);
2

3
namespace HJerichen\ProphecyPHP;
4

5
use PHPUnit\Framework\AssertionFailedError;
6
use PHPUnit\Framework\Attributes\After;
7
use PHPUnit\Framework\Attributes\PostCondition;
8
use PHPUnit\Framework\TestCase;
9
use Prophecy\Exception\Prediction\PredictionException;
10
use Prophecy\Prophecy\MethodProphecy;
11
use Prophecy\Prophet;
12

13
/**
14
 * @author Heiko Jerichen <heiko@jerichen.de>
15
 */
16
trait PHPProphetTrait
17
{
18
    private PHPProphet $phpProphet;
19
    private bool $phpProphecyAssertionsCounted = false;
20

21
    private function prophesizePHP(string $namespace): NamespaceProphecy
22
    {
23
        return $this->getPHPProphet()->prophesize($namespace);
17✔
24
    }
25

26
    /**
27
     * @postCondition
28
     */
29
    #[PostCondition]
30
    protected function verifyPhpProphecyDoubles(): void
31
    {
32
        if ($this->prophecyIsUsed()) return;
17✔
33
        if (!$this->phpProphecyIsUsed()) return;
17✔
34

35
        try {
36
            $this->phpProphet->checkPredictions();
16✔
NEW
37
        } catch (PredictionException $e) {
×
NEW
38
            throw new AssertionFailedError($e->getMessage());
×
39
        } finally {
40
            $this->countProphecyAssertions();
16✔
41
        }
42
    }
43

44
    /**
45
     * @after
46
     */
47
    #[After]
48
    protected function tearDownPhpProphecy(): void
49
    {
50
        try {
51
            if ($this->prophecyIsUsed()) return;
17✔
52
            if (!$this->phpProphecyIsUsed()) return;
17✔
53
            if ($this->phpProphecyAssertionsCounted) return;
16✔
NEW
54
            $this->countProphecyAssertions();
×
55
        } finally {
56
            unset($this->phpProphet);
17✔
57
        }
58
    }
59

60
    private function getPHPProphet(): PHPProphet
61
    {
62
        if (!isset($this->phpProphet)) {
17✔
63
            $this->phpProphet = new PHPProphet($this->getProphetFromTestCase());
17✔
64
        }
65
        return $this->phpProphet;
17✔
66
    }
67

68
    private function getProphetFromTestCase(): Prophet
69
    {
70
        return $this->prophet ?? new Prophet();
17✔
71
    }
72

73
    private function prophecyIsUsed(): bool
74
    {
75
        return isset($this->prophet) || isset($this->prophecyAssertionsCounted);
17✔
76
    }
77

78
    private function phpProphecyIsUsed(): bool
79
    {
80
        return isset($this->phpProphet);
17✔
81
    }
82

83
    /** @internal */
84
    private function countProphecyAssertions(): void
85
    {
86
        assert($this instanceof TestCase);
87
        assert($this->phpProphet !== null);
88
        $this->phpProphecyAssertionsCounted = true;
16✔
89

90
        foreach ($this->phpProphet->prophet->getProphecies() as $objectProphecy) {
16✔
91
            foreach ($objectProphecy->getMethodProphecies() as $methodProphecies) {
13✔
92
                foreach ($methodProphecies as $methodProphecy) {
13✔
93
                    assert($methodProphecy instanceof MethodProphecy);
94

95
                    $this->addToAssertionCount(count($methodProphecy->getCheckedPredictions()));
13✔
96
                }
97
            }
98
        }
99
    }
100
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc