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