@@ -45,6 +45,66 @@ public function getTokens_withCall()
4545 $ this ->assertSame ([1 , null ], $ client ->getTokens ());
4646 }
4747
48+ /**
49+ * @test
50+ * @group unit
51+ * @covers ::end
52+ * @uses \DominionEnterprises\Api\Client::startIndex
53+ * @uses \DominionEnterprises\Api\Client::end
54+ * @expectedException Exception
55+ * @expectedExceptionMessage Invalid Credentials
56+ */
57+ public function exceptionIsThrownOnBadCredentials ()
58+ {
59+ $ adapter = new AccessTokenInvalidClientAdapter ();
60+ $ authentication = Authentication::createClientCredentials ('not under test ' , 'not under test ' );
61+ $ client = new Client ($ adapter , $ authentication , 'a url ' );
62+ $ client ->end ($ client ->startIndex ('a resource ' , []))->getHttpCode ();
63+ }
64+
65+ /**
66+ * @test
67+ * @group unit
68+ * @covers ::end
69+ * @uses \DominionEnterprises\Api\Client::startIndex
70+ * @uses \DominionEnterprises\Api\Client::end
71+ */
72+ public function invalidTokenIsRefreshed ()
73+ {
74+ $ adapter = new InvalidAccessTokenAdapter ();
75+ $ authentication = Authentication::createClientCredentials ('not under test ' , 'not under test ' );
76+ $ client = new Client ($ adapter , $ authentication , 'a url ' , Client::CACHE_MODE_NONE , null , 'foo ' );
77+ $ this ->assertSame (200 , $ client ->end ($ client ->startIndex ('a resource ' , []))->getHttpCode ());
78+ }
79+
80+ /**
81+ * @test
82+ * @group unit
83+ * @covers ::setDefaultHeaders
84+ * @uses \DominionEnterprises\Api\Client::setDefaultHeaders
85+ * @uses \DominionEnterprises\Api\Client::startIndex
86+ * @uses \DominionEnterprises\Api\Client::end
87+ */
88+ public function defaultHeadersArePassed ()
89+ {
90+ $ adapter = $ this ->getMockBuilder ('\DominionEnterprises\Api\Adapter ' )->setMethods (['start ' , 'end ' ])->getMock ();
91+ $ adapter ->expects ($ this ->once ())->method ('start ' )->with (
92+ $ this ->callback (
93+ function ($ request ) {
94+ $ this ->assertEquals ('foo ' , $ request ->getHeaders ()['testHeader ' ]);
95+ return true ;
96+ }
97+ )
98+ );
99+ $ adapter ->expects ($ this ->once ())->method ('end ' )->will (
100+ $ this ->returnValue (new Response (200 , ['Content-Type ' => ['application/json ' ]], []))
101+ );
102+ $ authentication = Authentication::createClientCredentials ('not under test ' , 'not under test ' );
103+ $ client = new Client ($ adapter , $ authentication , 'a url ' , Client::CACHE_MODE_NONE , null , 'foo ' );
104+ $ client ->setDefaultHeaders (['testHeader ' => 'foo ' ]);
105+ $ this ->assertSame (200 , $ client ->end ($ client ->startIndex ('a resource ' , []))->getHttpCode ());
106+ }
107+
48108 /**
49109 * @test
50110 * @group unit
@@ -691,6 +751,23 @@ public function end($handle)
691751 }
692752}
693753
754+ final class AccessTokenInvalidClientAdapter implements Adapter
755+ {
756+ private $ _request ;
757+
758+ public function start (Request $ request )
759+ {
760+ $ this ->_request = $ request ;
761+ }
762+
763+ public function end ($ handle )
764+ {
765+ if (substr_count ($ this ->_request ->getUrl (), 'token ' ) == 1 ) {
766+ return new Response (200 , ['Content-Type ' => ['application/json ' ]], ['error ' => 'invalid_client ' ]);
767+ }
768+ }
769+ }
770+
694771final class AccessTokenAdapter implements Adapter
695772{
696773 private $ _request ;
@@ -718,6 +795,35 @@ public function end($handle)
718795 }
719796}
720797
798+ final class InvalidAccessTokenAdapter implements Adapter
799+ {
800+ private $ _request ;
801+ private $ _count = 0 ;
802+
803+ public function start (Request $ request )
804+ {
805+ $ this ->_request = $ request ;
806+ }
807+
808+ public function end ($ handle )
809+ {
810+ if (substr_count ($ this ->_request ->getUrl (), 'token ' ) == 1 ) {
811+ $ response = new Response (200 , ['Content-Type ' => ['application/json ' ]], ['access_token ' => $ this ->_count , 'expires_in ' => 1 ]);
812+ ++$ this ->_count ;
813+ return $ response ;
814+ }
815+
816+ $ headers = $ this ->_request ->getHeaders ();
817+ if ($ headers ['Authorization ' ] === 'Bearer foo ' ) {
818+ return new Response (401 , ['Content-Type ' => ['application/json ' ]], ['error ' => ['code ' => 'invalid_token ' ]]);
819+ } elseif ($ headers ['Authorization ' ] === 'Bearer 0 ' ) {
820+ return new Response (200 , ['Content-Type ' => ['application/json ' ]], []);
821+ }
822+
823+ return new Response (401 , ['Content-Type ' => ['application/json ' ]], ['error ' => 'invalid_grant ' ]);
824+ }
825+ }
826+
721827final class RefreshTokenAdapter implements Adapter
722828{
723829 private $ _request ;
0 commit comments