4
4
5
5
namespace Tests \Libraries ;
6
6
7
+ use GuzzleHttp \Exception \ConnectException ;
7
8
use GuzzleHttp \Exception \GuzzleException ;
8
9
use GuzzleHttp \HandlerStack ;
9
10
use GuzzleHttp \Psr7 \MultipartStream ;
@@ -31,7 +32,11 @@ public function testCallEksternal1(): void
31
32
['content-type ' => 'application/json ' ],
32
33
json_encode (['message ' => 'hello world ' ])
33
34
);
34
- $ client = new ClientExternal ();
35
+ $ mock = new MockHandler ([
36
+ new Response (200 , ['Content-Type ' => 'application/json ' ], json_encode (['status ' => 'ok ' , 'message ' => 'hello world ' ])),
37
+ ]);
38
+ $ handlerStack = new HandlerStack ($ mock );
39
+ $ client = new ClientExternal (['handler ' => $ handlerStack ]);
35
40
$ response = $ client ->call ($ request );
36
41
$ contents = $ response ->getBody ()->getContents ();
37
42
$ contents_arr = json_decode ($ contents , true , 512 );
@@ -50,6 +55,11 @@ public function testCallExternalMultipartSuccess(): void
50
55
$ f = fopen ('public/docs/hello.txt ' , 'w ' );
51
56
fwrite ($ f , 'hello world ' );
52
57
fclose ($ f );
58
+ $ mock = new MockHandler ([
59
+ new Response (200 , ['Content-Type ' => 'application/json ' ], json_encode (['id ' => '101 ' , 'status ' => 'ok ' , 'message ' => 'well done ' ])),
60
+ ]);
61
+ $ handlerStack = new HandlerStack ($ mock );
62
+ $ client = new ClientExternal (['handler ' => $ handlerStack ]);
53
63
$ request = new Request (
54
64
'POST ' ,
55
65
'https://jsonplaceholder.typicode.com/posts ' ,
@@ -61,7 +71,6 @@ public function testCallExternalMultipartSuccess(): void
61
71
]
62
72
])
63
73
);
64
- $ client = new ClientExternal ();
65
74
$ resp = $ client
66
75
->injectRequestHeader (['X-Unit-Test ' => ['clover ' ]])
67
76
->injectResponseHeader (['X-Unit-Test-Response ' => ['clover-response ' ]])
@@ -88,7 +97,11 @@ public function testCallExternalMultipartError(): void
88
97
]
89
98
])
90
99
);
91
- $ client = new ClientExternal ();
100
+ $ mock = new MockHandler ([
101
+ new ConnectException ('simulated error ' , $ request )
102
+ ]);
103
+ $ handlerStack = new HandlerStack ($ mock );
104
+ $ client = new ClientExternal (['handler ' => $ handlerStack ]);
92
105
$ client ->call ($ request );
93
106
}
94
107
0 commit comments