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

RonasIT / laravel-helpers / 10508933565

22 Aug 2024 01:20PM UTC coverage: 77.706% (-0.2%) from 77.868%
10508933565

push

github

web-flow
Merge pull request #139 from RonasIT/feat/update-dependencies

Update dependencies

9 of 16 new or added lines in 8 files covered. (56.25%)

1 existing line in 1 file now uncovered.

962 of 1238 relevant lines covered (77.71%)

11.0 hits per line

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

97.5
/src/Traits/MockTrait.php
1
<?php
2

3
namespace RonasIT\Support\Traits;
4

5
use Illuminate\Support\Arr;
6
use Closure;
7
use phpmock\phpunit\PHPMock;
8
use PHPUnit\Framework\MockObject\MockObject;
9
use PHPUnit\Framework\MockObject\Rule\InvokedCount;
10

11
trait MockTrait
12
{
13
    use PHPMock;
14

15
    /**
16
     * Mock selected class. Call chain should looks like:
17
     *
18
     * [
19
     *     [
20
     *         'function' => 'yourMethod',
21
     *         'arguments' => ['firstArgumentValue', 2, true],
22
     *         'result' => 'result_fixture.json'
23
     *     ],
24
     *     $this->functionCall('yourMethod', ['firstArgumentValue', 2, true], 'result_fixture.json')
25
     * ]
26
     *
27
     * @param string $class
28
     * @param array $callChain
29
     * @param bool $disableConstructor
30
     * @return MockObject
31
     */
32
    public function mockClass(string $class, array $callChain, bool $disableConstructor = false): MockObject
33
    {
34
        $this->app->offsetUnset($class);
18✔
35

36
        $methodsCalls = collect($callChain)->groupBy('function');
18✔
37

38
        $mock = $this
18✔
39
            ->getMockBuilder($class)
18✔
40
            ->onlyMethods($methodsCalls->keys()->toArray());
18✔
41

42
        if ($disableConstructor) {
18✔
43
            $mock->disableOriginalConstructor();
4✔
44
        }
45

46
        $mock = $mock->getMock();
18✔
47

48
        $methodsCalls->each(function ($calls, $method) use ($mock, $class) {
18✔
49
            $matcher = $this->exactly($calls->count());
18✔
50

51
            $mock
18✔
52
                ->expects($matcher)
18✔
53
                ->method($method)
18✔
54
                ->willReturnCallback(function (...$args) use ($matcher, $calls, $method, $class) {
18✔
55
                    $callIndex = $this->getInvocationCount($matcher) - 1;
18✔
56
                    $expectedCall = $calls[$callIndex];
18✔
57

58
                    $expectedArguments = Arr::get($expectedCall, 'arguments');
18✔
59

60
                    if (!empty($expectedArguments)) {
18✔
61
                        $this->assertArguments(
18✔
62
                            $args,
18✔
63
                            $expectedArguments,
18✔
64
                            $class,
18✔
65
                            $method,
18✔
66
                            $callIndex
18✔
67
                        );
18✔
68
                    }
69

70
                    return $expectedCall['result'];
18✔
71
                });
18✔
72
        });
18✔
73

74
        $this->app->instance($class, $mock);
18✔
75

76
        return $mock;
18✔
77
    }
78

79
    /**
80
     * Mock native function. Call chain should looks like:
81
     *
82
     * [
83
     *     [
84
     *         'function' => 'function_name',
85
     *         'arguments' => ['firstArgumentValue', 2, true],
86
     *         'result' => '123'
87
     *     ],
88
     *     $this->functionCall('function_name', ['firstArgumentValue', 2, true], '123')
89
     * ]
90
     *
91
     * @param string $namespace
92
     * @param array $callChain
93
     */
94
    public function mockNativeFunction(string $namespace, array $callChain)
95
    {
96
        $methodsCalls = collect($callChain)->groupBy('function');
3✔
97

98
        $methodsCalls->each(function ($calls, $function) use ($namespace) {
3✔
99
            $matcher = $this->exactly($calls->count());
3✔
100

101
            $mock = $this->getFunctionMock($namespace, $function);
3✔
102

103
            $mock
3✔
104
                ->expects($matcher)
3✔
105
                ->willReturnCallback(function (...$args) use ($matcher, $calls, $namespace, $function) {
3✔
106
                    $callIndex = $this->getInvocationCount($matcher) - 1;
3✔
107
                    $expectedCall = $calls[$callIndex];
3✔
108

109
                    $expectedArguments = Arr::get($expectedCall, 'arguments');
3✔
110

111
                    if (!empty($expectedArguments)) {
3✔
112
                        $this->assertArguments(
3✔
113
                            $args,
3✔
114
                            $expectedArguments,
3✔
115
                            $namespace,
3✔
116
                            $function,
3✔
117
                            $callIndex,
3✔
118
                            false
3✔
119
                        );
3✔
120
                    }
121

122
                    return $expectedCall['result'];
3✔
123
                });
3✔
124
        });
3✔
125
    }
126

127
    protected function assertArguments(
128
        $actual,
129
        $expected,
130
        string $class,
131
        string $function,
132
        int $callIndex,
133
        bool $isClass = true
134
    ): void {
135
        $message = ($isClass)
21✔
136
            ? "Class '{$class}'\nMethod: '{$function}'\nMethod call index: {$callIndex}"
18✔
137
            : "Namespace '{$class}'\nFunction: '{$function}'\nCall index: {$callIndex}";
3✔
138

139
        foreach ($actual as $index => $argument) {
21✔
140
            $this->assertEquals(
21✔
141
                $expected[$index],
21✔
142
                $argument,
21✔
143
                "Failed asserting that arguments are equals to expected.\n{$message}\nArgument index: {$index}"
21✔
144
            );
21✔
145
        }
146
    }
147

148
    protected function mockNoCalls(
149
        string $className,
150
        Closure $mockCallback = null,
151
        $disableConstructor = false
152
    ): MockObject {
153
        $mock = $this->getMockBuilder($className);
1✔
154

155
        if (!empty($mockCallback)) {
1✔
156
            $mockCallback($mock);
×
157
        }
158

159
        if ($disableConstructor) {
1✔
160
            $mock->disableOriginalConstructor();
1✔
161
        }
162

163
        $mock = $mock->getMock();
1✔
164

165
        $mock
1✔
166
            ->expects($this->never())
1✔
167
            ->method($this->anything());
1✔
168

169
        $this->app->instance($className, $mock);
1✔
170

171
        return $mock;
1✔
172
    }
173

174
    public function functionCall(string $name, array $arguments = [], $result = true): array
175
    {
176
        return [
21✔
177
            'function' => $name,
21✔
178
            'arguments' => $arguments,
21✔
179
            'result' => $result,
21✔
180
        ];
21✔
181
    }
182

183
    protected function getInvocationCount(InvokedCount $matcher): int
184
    {
185
        return method_exists($matcher, 'getInvocationCount')
21✔
UNCOV
186
            ? $matcher->getInvocationCount()
×
187
            : $matcher->numberOfInvocations();
21✔
188
    }
189
}
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