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

hjerichen / prophecy-php / 13120862476

03 Feb 2025 07:02PM UTC coverage: 98.052% (+0.03%) from 98.026%
13120862476

push

github

Heiko Jerichen
Add psalm for static code analysis.

3 of 3 new or added lines in 2 files covered. (100.0%)

1 existing line in 1 file now uncovered.

151 of 154 relevant lines covered (98.05%)

13.39 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
     * @psalm-suppress PossiblyUnusedMethod
29
     */
30
    #[PostCondition]
31
    protected function verifyPhpProphecyDoubles(): void
32
    {
33
        if ($this->prophecyIsUsed()) return;
17✔
34
        if (!$this->phpProphecyIsUsed()) return;
17✔
35

36
        try {
37
            $this->phpProphet->checkPredictions();
16✔
38
        } catch (PredictionException $e) {
×
39
            /** @psalm-suppress InternalClass, InternalMethod */
UNCOV
40
            throw new AssertionFailedError($e->getMessage());
×
41
        } finally {
42
            $this->countProphecyAssertions();
16✔
43
        }
44
    }
45

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

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

71
    private function getProphetFromTestCase(): Prophet
72
    {
73
        return $this->prophet ?? new Prophet();
17✔
74
    }
75

76
    private function prophecyIsUsed(): bool
77
    {
78
        return isset($this->prophet) || isset($this->prophecyAssertionsCounted);
17✔
79
    }
80

81
    private function phpProphecyIsUsed(): bool
82
    {
83
        return isset($this->phpProphet);
17✔
84
    }
85

86
    /** @internal */
87
    private function countProphecyAssertions(): void
88
    {
89
        assert($this instanceof TestCase);
90
        assert(isset($this->phpProphet));
91
        $this->phpProphecyAssertionsCounted = true;
16✔
92

93
        foreach ($this->phpProphet->prophet->getProphecies() as $objectProphecy) {
16✔
94
            foreach ($objectProphecy->getMethodProphecies() as $methodProphecies) {
13✔
95
                foreach ($methodProphecies as $methodProphecy) {
13✔
96
                    /** @psalm-suppress InternalMethod */
97
                    $this->addToAssertionCount(count($methodProphecy->getCheckedPredictions()));
13✔
98
                }
99
            }
100
        }
101
    }
102
}
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

© 2025 Coveralls, Inc