22
33namespace Fapi \HttpClient ;
44
5+ use LogicException ;
56use Psr \Http \Message \RequestInterface ;
67use Psr \Http \Message \ResponseInterface ;
78use Tester \Dumper ;
@@ -21,6 +22,8 @@ class CapturingHttpClient implements IHttpClient
2122 /** @var array<ResponseInterface> */
2223 private array $ httpResponses = [];
2324
25+ private int $ closed = 0 ;
26+
2427 public function __construct (private IHttpClient $ httpClient , private string $ file , private string $ className )
2528 {
2629 if (!class_exists ('Tester\Dumper ' )) {
@@ -56,6 +59,8 @@ private function capture(RequestInterface $httpRequest, ResponseInterface $httpR
5659
5760 public function close (): void
5861 {
62+ $ this ->closed ++;
63+
5964 if ($ this ->httpClient instanceof $ this ->className ) {
6065 return ;
6166 }
@@ -66,7 +71,8 @@ public function close(): void
6671 private function writeToPhpFile (string $ fileName , string $ className ): void
6772 {
6873 preg_match ('#^(?:(.*) \\\\)?([^ \\\\]+)\z# ' , $ className , $ match );
69- [, $ namespace , $ className ] = $ match ;
74+ $ namespace = $ match [1 ] ?? null ;
75+ $ className = $ match [2 ] ?? null ;
7076
7177 $ code = '<?php declare(strict_types = 1); ' . "\n" ;
7278 $ code .= "\n" ;
@@ -120,4 +126,13 @@ private function exportValue(mixed $value, string $indent = ''): string
120126 return $ s ;
121127 }
122128
129+ public function __destruct ()
130+ {
131+ if ($ this ->closed !== 1 ) {
132+ throw new LogicException (
133+ 'Method: ' . self ::class . '::closed Must be called exactly once. Called ' . $ this ->closed . ' times. ' ,
134+ );
135+ }
136+ }
137+
123138}
0 commit comments