1212
1313namespace chillerlan \OAuthTest \Providers \LastFM ;
1414
15- use chillerlan \OAuthTest \Providers \ProviderTestHttpClient ;
16- use chillerlan \HTTP \{Psr17 , Psr7 };
1715use chillerlan \HTTP \Psr7 \Response ;
1816use chillerlan \OAuth \Core \ProviderException ;
1917use chillerlan \OAuth \Providers \LastFM \LastFM ;
2018use chillerlan \OAuthTest \Providers \ProviderTestAbstract ;
21- use Psr \Http \Client \ClientInterface ;
22- use Psr \Http \Message \{RequestInterface , ResponseInterface };
19+
20+ use function chillerlan \HTTP \Psr17 \create_stream ;
21+ use function chillerlan \HTTP \Psr7 \get_json ;
2322
2423/**
2524 * @property \chillerlan\OAuth\Providers\LastFM\LastFM $provider
@@ -30,31 +29,15 @@ class LastFMTest extends ProviderTestAbstract{
3029
3130 public function setUp ():void {
3231
33- $ this ->responses ['/lastfm/auth ' ] = [
34- 'session ' => ['key ' => 'session_key ' ],
35- ];
36-
37- $ this ->responses ['/lastfm/api/request ' ] = [
38- 'data ' => 'such data! much wow! ' ,
39- ];
32+ $ this ->responses ['/lastfm/auth ' ] = '{"session":{"key":"session_key"}} ' ;
33+ $ this ->responses ['/lastfm/api/request ' ] = '{"data":"such data! much wow!"} ' ;
4034
35+ // setup after adding responses -> ProviderTestAbstract::initHTTP()
4136 parent ::setUp ();
4237
4338 $ this ->setProperty ($ this ->provider , 'apiURL ' , '/lastfm/api/request ' );
4439 }
4540
46- protected function initHttp ():ClientInterface {
47- return new class ($ this ->responses , $ this ->logger ) extends ProviderTestHttpClient{
48-
49- public function sendRequest (RequestInterface $ request ):ResponseInterface {
50- $ stream = Psr17 \create_stream_from_input (json_encode ($ this ->responses [$ request ->getUri ()->getPath ()]));
51-
52- return $ this ->logRequest ($ request , (new Response )->withBody ($ stream ));
53- }
54-
55- };
56- }
57-
5841 public function testGetAuthURL (){
5942 $ url = $ this ->provider ->getAuthURL (['foo ' => 'bar ' ]);
6043
@@ -70,7 +53,7 @@ public function testGetSignature(){
7053 }
7154
7255 public function testParseTokenResponse (){
73- $ r = (new Response )->withBody (Psr17 \ create_stream (json_encode ([ ' session ' => [ ' key ' => ' whatever ' ]]) ));
56+ $ r = (new Response )->withBody (create_stream (' {" session":{" key":" whatever"}} ' ));
7457
7558 $ token = $ this
7659 ->getMethod ('parseTokenResponse ' )
@@ -92,7 +75,8 @@ public function testParseTokenResponseError(){
9275 $ this ->expectException (ProviderException::class);
9376 $ this ->expectExceptionMessage ('error retrieving access token: ' );
9477
95- $ r = (new Response )->withBody (Psr17 \create_stream (json_encode (['error ' => 42 , 'message ' => 'whatever ' ])));
78+ $ r = (new Response )->withBody (create_stream ('{"error":42,"message":"whatever"} ' ));
79+
9680 $ this
9781 ->getMethod ('parseTokenResponse ' )
9882 ->invokeArgs ($ this ->provider , [$ r ]);
@@ -102,7 +86,8 @@ public function testParseTokenResponseNoToken(){
10286 $ this ->expectException (ProviderException::class);
10387 $ this ->expectExceptionMessage ('token missing ' );
10488
105- $ r = (new Response )->withBody (Psr17 \create_stream (json_encode (['session ' => []])));
89+ $ r = (new Response )->withBody (create_stream ('{"session":[]} ' ));
90+
10691 $ this
10792 ->getMethod ('parseTokenResponse ' )
10893 ->invokeArgs ($ this ->provider , [$ r ]);
@@ -131,14 +116,14 @@ public function testGetAccessToken(){
131116 public function testRequest (){
132117 $ r = $ this ->provider ->request ('' );
133118
134- $ this ->assertSame ('such data! much wow! ' , Psr7 \ get_json ($ r )->data );
119+ $ this ->assertSame ('such data! much wow! ' , get_json ($ r )->data );
135120 }
136121
137122 // coverage
138123 public function testRequestPost (){
139124 $ r = $ this ->provider ->request ('' , [], 'POST ' , ['foo ' => 'bar ' ], ['Content-Type ' => 'whatever ' ]);
140125
141- $ this ->assertSame ('such data! much wow! ' , Psr7 \ get_json ($ r )->data );
126+ $ this ->assertSame ('such data! much wow! ' , get_json ($ r )->data );
142127 }
143128
144129}
0 commit comments