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

RonasIT / laravel-helpers / 7326352764

26 Dec 2023 03:41AM UTC coverage: 66.667% (+2.5%) from 64.12%
7326352764

push

github

web-flow
Merge pull request #100 from RonasIT/prepare-to-phpunit10

Prepare to phpunit10

40 of 41 new or added lines in 1 file covered. (97.56%)

710 of 1065 relevant lines covered (66.67%)

5.92 hits per line

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

97.92
/src/Traits/MockClassTrait.php
1
<?php
2

3
namespace RonasIT\Support\Traits;
4

5
use Illuminate\Support\Arr;
6
use Closure;
7
use PHPUnit\Framework\MockObject\MockObject;
8

9
trait MockClassTrait
10
{
11
    /**
12
     * Mock selected class. Call chain should looks like:
13
     *
14
     * [
15
     *     [
16
     *         'method' => 'yourMethod',
17
     *         'arguments' => ['firstArgumentValue', 2, true],
18
     *         'result' => 'result_fixture.json'
19
     *     ]
20
     * ]
21
     *
22
     * @param string $class
23
     * @param array $callChain
24
     */
25
    public function mockClass(string $class, array $callChain, $disableConstructor = false): MockObject
26
    {
27
        $this->app->offsetUnset($class);
17✔
28

29
        $methodsCalls = collect($callChain)->groupBy('method');
17✔
30

31
        $mock = $this
17✔
32
            ->getMockBuilder($class)
17✔
33
            ->onlyMethods($methodsCalls->keys()->toArray());
17✔
34

35
        if ($disableConstructor) {
17✔
36
            $mock->disableOriginalConstructor();
3✔
37
        }
38

39
        $mock = $mock->getMock();
17✔
40

41
        $methodsCalls->each(function ($calls, $method) use ($mock, $class) {
17✔
42
            $matcher = $this->exactly($calls->count());
17✔
43

44
            $mock
17✔
45
                ->expects($matcher)
17✔
46
                ->method($method)
17✔
47
                ->willReturnCallback(function (...$args) use ($matcher, $calls, $method, $class) {
17✔
48
                    $callIndex = $matcher->getInvocationCount() - 1;
17✔
49
                    $expectedCall = $calls[$callIndex];
17✔
50

51
                    $expectedArguments = Arr::get($expectedCall, 'arguments');
17✔
52

53
                    if (!empty($expectedArguments)) {
17✔
54
                        $this->assertArguments(
17✔
55
                            $args,
17✔
56
                            $expectedArguments,
17✔
57
                            $class,
17✔
58
                            $method,
17✔
59
                            $callIndex
17✔
60
                        );
17✔
61
                    }
62

63
                    return $expectedCall['result'];
17✔
64
                });
17✔
65
        });
17✔
66

67
        $this->app->instance($class, $mock);
17✔
68

69
        return $mock;
17✔
70
    }
71

72
    protected function assertArguments($actual, $expected, string $class, string $method, int $callIndex): void
73
    {
74
        foreach ($actual as $index => $argument) {
17✔
75
            $this->assertEquals(
17✔
76
                $expected[$index],
17✔
77
                $argument,
17✔
78
                "Failed asserting that arguments are equals to expected.\n" .
17✔
79
                "Class '{$class}'\nMethod: '{$method}'\nMethod call index: {$callIndex}\nArgument index: {$index}"
17✔
80
            );
17✔
81
        }
82
    }
83

84
    protected function mockNoCalls(
85
        string $className,
86
        Closure $mockCallback = null,
87
        $disableConstructor = false
88
    ): MockObject {
89
        $mock = $this->getMockBuilder($className);
1✔
90

91
        if (!empty($mockCallback)) {
1✔
NEW
92
            $mockCallback($mock);
×
93
        }
94

95
        if ($disableConstructor) {
1✔
96
            $mock->disableOriginalConstructor();
1✔
97
        }
98

99
        $mock = $mock->getMock();
1✔
100

101
        $mock
1✔
102
            ->expects($this->never())
1✔
103
            ->method($this->anything());
1✔
104

105
        $this->app->instance($className, $mock);
1✔
106

107
        return $mock;
1✔
108
    }
109
}
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