|
8 | 8 | use Blackfire\Exception\LogicException; |
9 | 9 | use Blackfire\Probe; |
10 | 10 | use Blackfire\Profile\Configuration; |
11 | | -use Chubbyphp\Mock\Argument\ArgumentInstanceOf; |
12 | | -use Chubbyphp\Mock\Call; |
13 | | -use Chubbyphp\Mock\MockByCallsTrait; |
| 11 | +use Chubbyphp\Mock\MockMethod\WithCallback; |
| 12 | +use Chubbyphp\Mock\MockMethod\WithException; |
| 13 | +use Chubbyphp\Mock\MockMethod\WithoutReturn; |
| 14 | +use Chubbyphp\Mock\MockMethod\WithReturn; |
| 15 | +use Chubbyphp\Mock\MockObjectBuilder; |
14 | 16 | use Chubbyphp\WorkermanRequestHandler\Adapter\BlackfireOnMessageAdapter; |
15 | 17 | use Chubbyphp\WorkermanRequestHandler\OnMessageInterface; |
16 | | -use PHPUnit\Framework\MockObject\MockObject; |
| 18 | +use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; |
17 | 19 | use PHPUnit\Framework\TestCase; |
18 | 20 | use Psr\Log\LoggerInterface; |
19 | 21 | use Workerman\Connection\TcpConnection as WorkermanTcpConnection; |
|
26 | 28 | */ |
27 | 29 | final class BlackfireOnMessageAdapterTest extends TestCase |
28 | 30 | { |
29 | | - use MockByCallsTrait; |
30 | | - |
| 31 | + #[DoesNotPerformAssertions] |
31 | 32 | public function testInvokeWithoutHeaderWithoutConfigAndWithoutLogger(): void |
32 | 33 | { |
33 | | - /** @var MockObject|WorkermanTcpConnection $workermanTcpConnection */ |
34 | | - $workermanTcpConnection = $this->getMockByCalls(WorkermanTcpConnection::class); |
| 34 | + $builder = new MockObjectBuilder(); |
| 35 | + |
| 36 | + /** @var WorkermanTcpConnection $workermanTcpConnection */ |
| 37 | + $workermanTcpConnection = $builder->create(WorkermanTcpConnection::class, []); |
35 | 38 |
|
36 | | - /** @var MockObject|WorkermanRequest $workermanRequest */ |
37 | | - $workermanRequest = $this->getMockByCalls(WorkermanRequest::class, [ |
38 | | - Call::create('header')->with('x-blackfire-query', null)->willReturn(null), |
| 39 | + /** @var WorkermanRequest $workermanRequest */ |
| 40 | + $workermanRequest = $builder->create(WorkermanRequest::class, [ |
| 41 | + new WithReturn('header', ['x-blackfire-query', null], null), |
39 | 42 | ]); |
40 | 43 |
|
41 | | - /** @var MockObject|OnMessageInterface $onMessage */ |
42 | | - $onMessage = $this->getMockByCalls(OnMessageInterface::class, [ |
43 | | - Call::create('__invoke')->with($workermanTcpConnection, $workermanRequest), |
| 44 | + /** @var OnMessageInterface $onMessage */ |
| 45 | + $onMessage = $builder->create(OnMessageInterface::class, [ |
| 46 | + new WithoutReturn('__invoke', [$workermanTcpConnection, $workermanRequest]), |
44 | 47 | ]); |
45 | 48 |
|
46 | | - /** @var Client|MockObject $client */ |
47 | | - $client = $this->getMockByCalls(Client::class); |
| 49 | + /** @var Client $client */ |
| 50 | + $client = $builder->create(Client::class, []); |
48 | 51 |
|
49 | 52 | $adapter = new BlackfireOnMessageAdapter($onMessage, $client); |
50 | 53 | $adapter($workermanTcpConnection, $workermanRequest); |
51 | 54 | } |
52 | 55 |
|
53 | 56 | public function testInvokeWithoutConfigAndWithoutLogger(): void |
54 | 57 | { |
55 | | - /** @var MockObject|WorkermanTcpConnection $workermanTcpConnection */ |
56 | | - $workermanTcpConnection = $this->getMockByCalls(WorkermanTcpConnection::class); |
| 58 | + $builder = new MockObjectBuilder(); |
57 | 59 |
|
58 | | - /** @var MockObject|WorkermanRequest $workermanRequest */ |
59 | | - $workermanRequest = $this->getMockByCalls(WorkermanRequest::class, [ |
60 | | - Call::create('header')->with('x-blackfire-query', null)->willReturn('workerman'), |
| 60 | + /** @var WorkermanTcpConnection $workermanTcpConnection */ |
| 61 | + $workermanTcpConnection = $builder->create(WorkermanTcpConnection::class, []); |
| 62 | + |
| 63 | + /** @var WorkermanRequest $workermanRequest */ |
| 64 | + $workermanRequest = $builder->create(WorkermanRequest::class, [ |
| 65 | + new WithReturn('header', ['x-blackfire-query', null], 'workerman'), |
61 | 66 | ]); |
62 | 67 |
|
63 | | - /** @var MockObject|OnMessageInterface $onMessage */ |
64 | | - $onMessage = $this->getMockByCalls(OnMessageInterface::class, [ |
65 | | - Call::create('__invoke')->with($workermanTcpConnection, $workermanRequest), |
| 68 | + /** @var OnMessageInterface $onMessage */ |
| 69 | + $onMessage = $builder->create(OnMessageInterface::class, [ |
| 70 | + new WithoutReturn('__invoke', [$workermanTcpConnection, $workermanRequest]), |
66 | 71 | ]); |
67 | 72 |
|
68 | | - /** @var MockObject|Probe $probe */ |
69 | | - $probe = $this->getMockByCalls(Probe::class); |
| 73 | + /** @var Probe $probe */ |
| 74 | + $probe = $builder->create(Probe::class, []); |
| 75 | + |
| 76 | + /** @var Client $client */ |
| 77 | + $client = $builder->create(Client::class, [ |
| 78 | + new WithCallback('createProbe', static function (Configuration $configuration, bool $enabled) use ($probe): Probe { |
| 79 | + self::assertTrue($enabled); |
70 | 80 |
|
71 | | - /** @var Client|MockObject $client */ |
72 | | - $client = $this->getMockByCalls(Client::class, [ |
73 | | - Call::create('createProbe')->with(new ArgumentInstanceOf(Configuration::class), true)->willReturn($probe), |
74 | | - Call::create('endProbe')->with($probe), |
| 81 | + return $probe; |
| 82 | + }), |
| 83 | + new WithoutReturn('endProbe', [$probe]), |
75 | 84 | ]); |
76 | 85 |
|
77 | 86 | $adapter = new BlackfireOnMessageAdapter($onMessage, $client); |
78 | 87 | $adapter($workermanTcpConnection, $workermanRequest); |
79 | 88 | } |
80 | 89 |
|
| 90 | + #[DoesNotPerformAssertions] |
81 | 91 | public function testInvokeWithConfigAndWithLogger(): void |
82 | 92 | { |
83 | | - /** @var MockObject|WorkermanTcpConnection $workermanTcpConnection */ |
84 | | - $workermanTcpConnection = $this->getMockByCalls(WorkermanTcpConnection::class); |
| 93 | + $builder = new MockObjectBuilder(); |
85 | 94 |
|
86 | | - /** @var MockObject|WorkermanRequest $workermanRequest */ |
87 | | - $workermanRequest = $this->getMockByCalls(WorkermanRequest::class, [ |
88 | | - Call::create('header')->with('x-blackfire-query', null)->willReturn('workerman'), |
| 95 | + /** @var WorkermanTcpConnection $workermanTcpConnection */ |
| 96 | + $workermanTcpConnection = $builder->create(WorkermanTcpConnection::class, []); |
| 97 | + |
| 98 | + /** @var WorkermanRequest $workermanRequest */ |
| 99 | + $workermanRequest = $builder->create(WorkermanRequest::class, [ |
| 100 | + new WithReturn('header', ['x-blackfire-query', null], 'workerman'), |
89 | 101 | ]); |
90 | 102 |
|
91 | | - /** @var MockObject|OnMessageInterface $onMessage */ |
92 | | - $onMessage = $this->getMockByCalls(OnMessageInterface::class, [ |
93 | | - Call::create('__invoke')->with($workermanTcpConnection, $workermanRequest), |
| 103 | + /** @var OnMessageInterface $onMessage */ |
| 104 | + $onMessage = $builder->create(OnMessageInterface::class, [ |
| 105 | + new WithoutReturn('__invoke', [$workermanTcpConnection, $workermanRequest]), |
94 | 106 | ]); |
95 | 107 |
|
96 | | - /** @var Configuration|MockObject $config */ |
97 | | - $config = $this->getMockByCalls(Configuration::class); |
| 108 | + /** @var Configuration $config */ |
| 109 | + $config = $builder->create(Configuration::class, []); |
98 | 110 |
|
99 | | - /** @var MockObject|Probe $probe */ |
100 | | - $probe = $this->getMockByCalls(Probe::class); |
| 111 | + /** @var Probe $probe */ |
| 112 | + $probe = $builder->create(Probe::class, []); |
101 | 113 |
|
102 | | - /** @var Client|MockObject $client */ |
103 | | - $client = $this->getMockByCalls(Client::class, [ |
104 | | - Call::create('createProbe')->with($config, true)->willReturn($probe), |
105 | | - Call::create('endProbe')->with($probe), |
| 114 | + /** @var Client $client */ |
| 115 | + $client = $builder->create(Client::class, [ |
| 116 | + new WithReturn('createProbe', [$config, true], $probe), |
| 117 | + new WithoutReturn('endProbe', [$probe]), |
106 | 118 | ]); |
107 | 119 |
|
108 | | - /** @var LoggerInterface|MockObject $logger */ |
109 | | - $logger = $this->getMockByCalls(LoggerInterface::class); |
| 120 | + /** @var LoggerInterface $logger */ |
| 121 | + $logger = $builder->create(LoggerInterface::class, []); |
110 | 122 |
|
111 | 123 | $adapter = new BlackfireOnMessageAdapter($onMessage, $client, $config, $logger); |
112 | 124 | $adapter($workermanTcpConnection, $workermanRequest); |
113 | 125 | } |
114 | 126 |
|
| 127 | + #[DoesNotPerformAssertions] |
115 | 128 | public function testInvokeWithExceptionOnCreateProbe(): void |
116 | 129 | { |
117 | | - /** @var MockObject|WorkermanTcpConnection $workermanTcpConnection */ |
118 | | - $workermanTcpConnection = $this->getMockByCalls(WorkermanTcpConnection::class); |
| 130 | + $builder = new MockObjectBuilder(); |
| 131 | + |
| 132 | + /** @var WorkermanTcpConnection $workermanTcpConnection */ |
| 133 | + $workermanTcpConnection = $builder->create(WorkermanTcpConnection::class, []); |
119 | 134 |
|
120 | | - /** @var MockObject|WorkermanRequest $workermanRequest */ |
121 | | - $workermanRequest = $this->getMockByCalls(WorkermanRequest::class, [ |
122 | | - Call::create('header')->with('x-blackfire-query', null)->willReturn('workerman'), |
| 135 | + /** @var WorkermanRequest $workermanRequest */ |
| 136 | + $workermanRequest = $builder->create(WorkermanRequest::class, [ |
| 137 | + new WithReturn('header', ['x-blackfire-query', null], 'workerman'), |
123 | 138 | ]); |
124 | 139 |
|
125 | | - /** @var MockObject|OnMessageInterface $onMessage */ |
126 | | - $onMessage = $this->getMockByCalls(OnMessageInterface::class, [ |
127 | | - Call::create('__invoke')->with($workermanTcpConnection, $workermanRequest), |
| 140 | + /** @var OnMessageInterface $onMessage */ |
| 141 | + $onMessage = $builder->create(OnMessageInterface::class, [ |
| 142 | + new WithoutReturn('__invoke', [$workermanTcpConnection, $workermanRequest]), |
128 | 143 | ]); |
129 | 144 |
|
130 | | - /** @var Configuration|MockObject $config */ |
131 | | - $config = $this->getMockByCalls(Configuration::class); |
| 145 | + /** @var Configuration $config */ |
| 146 | + $config = $builder->create(Configuration::class, []); |
132 | 147 |
|
133 | 148 | $exception = new LogicException('Something went wrong'); |
134 | 149 |
|
135 | | - /** @var Client|MockObject $client */ |
136 | | - $client = $this->getMockByCalls(Client::class, [ |
137 | | - Call::create('createProbe')->with($config, true)->willThrowException($exception), |
| 150 | + /** @var Client $client */ |
| 151 | + $client = $builder->create(Client::class, [ |
| 152 | + new WithException('createProbe', [$config, true], $exception), |
138 | 153 | ]); |
139 | 154 |
|
140 | | - /** @var LoggerInterface|MockObject $logger */ |
141 | | - $logger = $this->getMockByCalls(LoggerInterface::class, [ |
142 | | - Call::create('error')->with('Blackfire exception: Something went wrong', []), |
| 155 | + /** @var LoggerInterface $logger */ |
| 156 | + $logger = $builder->create(LoggerInterface::class, [ |
| 157 | + new WithoutReturn('error', ['Blackfire exception: Something went wrong', []]), |
143 | 158 | ]); |
144 | 159 |
|
145 | 160 | $adapter = new BlackfireOnMessageAdapter($onMessage, $client, $config, $logger); |
146 | 161 | $adapter($workermanTcpConnection, $workermanRequest); |
147 | 162 | } |
148 | 163 |
|
| 164 | + #[DoesNotPerformAssertions] |
149 | 165 | public function testInvokeWithExceptionOnProbeEnd(): void |
150 | 166 | { |
151 | | - /** @var MockObject|WorkermanTcpConnection $workermanTcpConnection */ |
152 | | - $workermanTcpConnection = $this->getMockByCalls(WorkermanTcpConnection::class); |
| 167 | + $builder = new MockObjectBuilder(); |
| 168 | + |
| 169 | + /** @var WorkermanTcpConnection $workermanTcpConnection */ |
| 170 | + $workermanTcpConnection = $builder->create(WorkermanTcpConnection::class, []); |
153 | 171 |
|
154 | | - /** @var MockObject|WorkermanRequest $workermanRequest */ |
155 | | - $workermanRequest = $this->getMockByCalls(WorkermanRequest::class, [ |
156 | | - Call::create('header')->with('x-blackfire-query', null)->willReturn('workerman'), |
| 172 | + /** @var WorkermanRequest $workermanRequest */ |
| 173 | + $workermanRequest = $builder->create(WorkermanRequest::class, [ |
| 174 | + new WithReturn('header', ['x-blackfire-query', null], 'workerman'), |
157 | 175 | ]); |
158 | 176 |
|
159 | | - /** @var MockObject|OnMessageInterface $onMessage */ |
160 | | - $onMessage = $this->getMockByCalls(OnMessageInterface::class, [ |
161 | | - Call::create('__invoke')->with($workermanTcpConnection, $workermanRequest), |
| 177 | + /** @var OnMessageInterface $onMessage */ |
| 178 | + $onMessage = $builder->create(OnMessageInterface::class, [ |
| 179 | + new WithoutReturn('__invoke', [$workermanTcpConnection, $workermanRequest]), |
162 | 180 | ]); |
163 | 181 |
|
164 | | - /** @var Configuration|MockObject $config */ |
165 | | - $config = $this->getMockByCalls(Configuration::class); |
| 182 | + /** @var Configuration $config */ |
| 183 | + $config = $builder->create(Configuration::class, []); |
166 | 184 |
|
167 | | - /** @var MockObject|Probe $probe */ |
168 | | - $probe = $this->getMockByCalls(Probe::class); |
| 185 | + /** @var Probe $probe */ |
| 186 | + $probe = $builder->create(Probe::class, []); |
169 | 187 |
|
170 | 188 | $exception = new LogicException('Something went wrong'); |
171 | 189 |
|
172 | | - /** @var Client|MockObject $client */ |
173 | | - $client = $this->getMockByCalls(Client::class, [ |
174 | | - Call::create('createProbe')->with($config, true)->willReturn($probe), |
175 | | - Call::create('endProbe')->with($probe)->willThrowException($exception), |
| 190 | + /** @var Client $client */ |
| 191 | + $client = $builder->create(Client::class, [ |
| 192 | + new WithReturn('createProbe', [$config, true], $probe), |
| 193 | + new WithException('endProbe', [$probe], $exception), |
176 | 194 | ]); |
177 | 195 |
|
178 | | - /** @var LoggerInterface|MockObject $logger */ |
179 | | - $logger = $this->getMockByCalls(LoggerInterface::class, [ |
180 | | - Call::create('error')->with('Blackfire exception: Something went wrong', []), |
| 196 | + /** @var LoggerInterface $logger */ |
| 197 | + $logger = $builder->create(LoggerInterface::class, [ |
| 198 | + new WithoutReturn('error', ['Blackfire exception: Something went wrong', []]), |
181 | 199 | ]); |
182 | 200 |
|
183 | 201 | $adapter = new BlackfireOnMessageAdapter($onMessage, $client, $config, $logger); |
|
0 commit comments