1111
1212use Contentful \Core \Api \BaseClient ;
1313use Contentful \Core \Api \Exception ;
14- use Contentful \Core \Api \Message ;
1514use Contentful \Tests \Core \TestCase ;
1615use Contentful \Tests \Core \Unit \Api \Exception \BadRequestException ;
1716use GuzzleHttp \Client as HttpClient ;
1817use GuzzleHttp \Exception \ClientException ;
1918use GuzzleHttp \Handler \CurlHandler ;
2019use GuzzleHttp \HandlerStack ;
2120use GuzzleHttp \Psr7 \Response ;
21+ use Monolog \Handler \TestHandler ;
22+ use Monolog \Logger ;
2223use Psr \Http \Message \RequestInterface ;
23- use Psr \Log \LoggerInterface ;
24+ use function GuzzleHttp \Psr7 \parse_request as guzzle_parse_request ;
25+ use function GuzzleHttp \Psr7 \parse_response as guzzle_parse_response ;
2426
2527class BaseClientTest extends TestCase
2628{
@@ -29,11 +31,6 @@ class BaseClientTest extends TestCase
2931 */
3032 private $ requestHandler ;
3133
32- public function setUp ()
33- {
34- ConcreteLogger::reset ();
35- }
36-
3734 public function createHttpClient ()
3835 {
3936 $ stack = new HandlerStack ();
@@ -51,8 +48,10 @@ public function createHttpClient()
5148
5249 public function testClient ()
5350 {
51+ $ handler = new TestHandler ();
52+ $ logger = new Logger ('test ' , [$ handler ]);
5453 $ httpClient = $ this ->createHttpClient ();
55- $ client = new ConcreteClient ('b4c0n73n7fu1 ' , 'https://cdn.contentful.com/ ' , new ConcreteLogger () , $ httpClient );
54+ $ client = new ConcreteClient ('b4c0n73n7fu1 ' , 'https://cdn.contentful.com/ ' , $ logger , $ httpClient );
5655 $ client ->setApplication ('sdk-test-application ' , '1.0 ' );
5756
5857 $ this ->assertSame ('DELIVERY ' , $ client ->getApi ());
@@ -61,16 +60,33 @@ public function testClient()
6160 $ jsonResponse = $ client ->request ('GET ' , '/spaces/cfexampleapi ' );
6261
6362 $ this ->assertSame ('cfexampleapi ' , $ jsonResponse ['sys ' ]['id ' ]);
64- $ logs = $ client -> getLogger ()-> getLogs ( ' INFO ' );
65- $ this ->assertCount (1 , $ logs );
63+ $ logs = $ handler -> getRecords ( );
64+ $ this ->assertCount (2 , $ logs );
6665
67- $ message = Message::createFromString ($ logs [0 ]);
68- $ this ->assertSame (200 , $ message ->getResponse ()->getStatusCode ());
66+ $ this ->assertSame ('INFO ' , $ logs [0 ]['level_name ' ]);
67+ $ this ->assertRegExp ('/GET https\:\/\/cdn\.contentful\.com\/spaces\/cfexampleapi \(([0-9]{1,})\.([0-9]{3})s\)/ ' , $ logs [0 ]['message ' ]);
68+
69+ $ this ->assertSame ('DEBUG ' , $ logs [1 ]['level_name ' ]);
70+ $ context = $ logs [1 ]['context ' ];
71+ $ this ->assertSame ('DELIVERY ' , $ context ['api ' ]);
72+ $ this ->assertInternalType ('float ' , $ context ['duration ' ]);
73+ $ this ->assertNull (\unserialize ($ context ['exception ' ]));
74+
75+ try {
76+ $ request = guzzle_parse_request ($ context ['request ' ]);
77+ if ($ context ['response ' ]) {
78+ $ response = guzzle_parse_response ($ context ['response ' ]);
79+ $ this ->assertSame (200 , $ response ->getStatusCode ());
80+ }
81+ } catch (\Exception $ exception ) {
82+ $ this ->fail ('Creating request and response from strings failed ' );
83+
84+ return ;
85+ }
6986
7087 // String representations of HTTP messages have no real way of storing the HTTPS vs HTTP
7188 // information. Because of this, after serialization the protocol is defaulted to HTTP.
7289 // To get the original request, use a Message object retrieved from BaseClient::getMessages().
73- $ request = $ message ->getRequest ();
7490 $ this ->assertSame ('http://cdn.contentful.com/spaces/cfexampleapi ' , (string ) $ request ->getUri ());
7591 $ this ->assertSame ('Bearer b4c0n73n7fu1 ' , $ request ->getHeaderLine ('Authorization ' ));
7692 $ this ->assertRegExp (
@@ -88,7 +104,7 @@ public function testClient()
88104 public function testErrorPage ()
89105 {
90106 $ httpClient = $ this ->createHttpClient ();
91- $ client = new ConcreteClient ('b4c0n73n7fu1 ' , 'https://cdn.contentful.com ' , new ConcreteLogger () , $ httpClient );
107+ $ client = new ConcreteClient ('b4c0n73n7fu1 ' , 'https://cdn.contentful.com ' , null , $ httpClient );
92108
93109 $ this ->requestHandler = function (RequestInterface $ request , array $ options ) {
94110 $ response = new Response (404 , [], $ this ->getFixtureContent ('not_found.json ' ));
@@ -102,7 +118,7 @@ public function testErrorPage()
102118 public function testCustomException ()
103119 {
104120 $ httpClient = $ this ->createHttpClient ();
105- $ client = new CustomExceptionConcreteClient ('b4c0n73n7fu1 ' , 'https://api.contentful.com ' , new ConcreteLogger () , $ httpClient );
121+ $ client = new CustomExceptionConcreteClient ('b4c0n73n7fu1 ' , 'https://api.contentful.com ' , null , $ httpClient );
106122 $ client ->setIntegration ('sdk-test-integration ' , '1.0.0-beta ' );
107123
108124 $ this ->assertSame ('MANAGEMENT ' , $ client ->getApi ());
@@ -214,63 +230,3 @@ protected function getExceptionNamespace()
214230 return __NAMESPACE__ .'\\Exception ' ;
215231 }
216232}
217-
218- class ConcreteLogger implements LoggerInterface
219- {
220- private static $ logs = [
221- 'ERROR ' => [],
222- 'INFO ' => [],
223- ];
224-
225- public static function reset ()
226- {
227- self ::$ logs = [
228- 'ERROR ' => [],
229- 'INFO ' => [],
230- ];
231- }
232-
233- public function getLogs ($ level )
234- {
235- return self ::$ logs [$ level ];
236- }
237-
238- public function emergency ($ message , array $ context = [])
239- {
240- }
241-
242- public function alert ($ message , array $ context = [])
243- {
244- }
245-
246- public function critical ($ message , array $ context = [])
247- {
248- }
249-
250- public function error ($ message , array $ context = [])
251- {
252- $ this ->log ('ERROR ' , $ message , $ context );
253- }
254-
255- public function warning ($ message , array $ context = [])
256- {
257- }
258-
259- public function notice ($ message , array $ context = [])
260- {
261- }
262-
263- public function info ($ message , array $ context = [])
264- {
265- $ this ->log ('INFO ' , $ message , $ context );
266- }
267-
268- public function debug ($ message , array $ context = [])
269- {
270- }
271-
272- public function log ($ level , $ message , array $ context = [])
273- {
274- self ::$ logs [$ level ][] = $ message ;
275- }
276- }
0 commit comments