Skip to content

Commit e0b4046

Browse files
committed
Switch to ::class magic method where appropriate
1 parent c5ebd75 commit e0b4046

9 files changed

+124
-98
lines changed

src/Controllers/AbstractOAuth2Controller.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
namespace NPR\One\Controllers;
44

55
use GuzzleHttp\Client;
6-
use GuzzleHttp\Psr7\Response;
76
use NPR\One\DI\DI;
87
use NPR\One\Exceptions\ApiException;
98
use NPR\One\Interfaces\ConfigInterface;
109
use NPR\One\Interfaces\EncryptionInterface;
1110
use NPR\One\Interfaces\StorageInterface;
1211
use NPR\One\Models\AccessTokenModel;
1312
use NPR\One\Providers\CookieProvider;
13+
use NPR\One\Providers\EncryptionProvider;
1414
use NPR\One\Providers\SecureCookieProvider;
1515

1616

@@ -54,8 +54,8 @@ public function __construct()
5454
'X-Longitude' => $_SERVER['GEOIP_LONGITUDE']
5555
];
5656
}
57-
$this->encryption = DI::container()->get('NPR\One\Providers\EncryptionProvider');
58-
$this->secureStorage = DI::container()->get('NPR\One\Providers\SecureCookieProvider');
57+
$this->encryption = DI::container()->get(EncryptionProvider::class);
58+
$this->secureStorage = DI::container()->get(SecureCookieProvider::class);
5959
$this->secureStorage->setEncryptionProvider($this->encryption);
6060
}
6161

@@ -222,7 +222,7 @@ final protected function createAccessToken($grantType, $additionalParams = [])
222222
$this->ensureExternalProvidersExist();
223223

224224
/** @var Client $client */
225-
$client = DI::container()->get('GuzzleHttp\Client');
225+
$client = DI::container()->get(Client::class);
226226
$response = $client->request('POST', $this->config->getNprApiHost() . '/authorization/v2/token', [
227227
'headers' => $this->headers,
228228
'form_params' => array_merge([

src/Controllers/AuthCodeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class AuthCodeController extends AbstractOAuth2Controller
3636
public function __construct()
3737
{
3838
parent::__construct();
39-
$this->cookies = DI::container()->get('NPR\One\Providers\CookieProvider');
39+
$this->cookies = DI::container()->get(CookieProvider::class);
4040
}
4141

4242
/**

src/Controllers/DeviceCodeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function pollDeviceCodeGrant()
8080
private function createDeviceCode(array $scopes)
8181
{
8282
/** @var Client $client */
83-
$client = DI::container()->get('GuzzleHttp\Client');
83+
$client = DI::container()->get(Client::class);
8484
$response = $client->request('POST', $this->getConfigProvider()->getNprApiHost() . '/authorization/v2/device', [
8585
'headers' => $this->getHeaders(),
8686
'form_params' => [

src/Controllers/LogoutController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private function revokeToken($token, $isRefreshToken = false)
7272
}
7373

7474
/** @var Client $client */
75-
$client = DI::container()->get('GuzzleHttp\Client');
75+
$client = DI::container()->get(Client::class);
7676
$response = $client->request('POST', $this->getConfigProvider()->getNprApiHost() . '/authorization/v2/token/revoke', [
7777
'headers' => [
7878
'Authorization' => 'Bearer ' . $this->getConfigProvider()->getClientCredentialsToken()

tests/unit/AuthCodeControllerTests.php

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,30 @@
77

88
use NPR\One\Controllers\AuthCodeController;
99
use NPR\One\DI\DI;
10+
use NPR\One\Interfaces\ConfigInterface;
11+
use NPR\One\Interfaces\StorageInterface;
12+
use NPR\One\Models\AccessTokenModel;
13+
use NPR\One\Providers\CookieProvider;
14+
use NPR\One\Providers\EncryptionProvider;
15+
use NPR\One\Providers\SecureCookieProvider;
1016

1117

1218
class AuthCodeControllerTests extends PHPUnit_Framework_TestCase
1319
{
1420
const ACCESS_TOKEN_RESPONSE = '{"access_token": "LT8gvVDyeKwQJVVf6xwKAWdK0bOik64faketoken","token_type": "Bearer","expires_in": 690448786,"refresh_token": "6KVn9BOhHhUFR1Yqi2T2pzpTWI9WIfakerefresh"}';
1521
const ACCESS_TOKEN_RESPONSE_2 = '{"access_token": "LT8gvVDyeKwQJVVf6xwKAWdK0bOik64faketoken","token_type": "Bearer","expires_in": 690448786}';
1622

17-
/** @var \NPR\One\Providers\CookieProvider */
23+
/** @var CookieProvider */
1824
private $mockCookie;
19-
/** @var \NPR\One\Providers\SecureCookieProvider */
25+
/** @var SecureCookieProvider */
2026
private $mockSecureCookie;
21-
/** @var \NPR\One\Providers\EncryptionProvider */
27+
/** @var EncryptionProvider */
2228
private $mockEncryption;
23-
/** @var \NPR\One\Interfaces\StorageInterface */
29+
/** @var StorageInterface */
2430
private $mockStorage;
25-
/** @var \NPR\One\Interfaces\ConfigInterface */
31+
/** @var ConfigInterface */
2632
private $mockConfig;
27-
/** @var \GuzzleHttp\Client */
33+
/** @var Client */
2834
private $mockClient;
2935

3036
/** @var string */
@@ -33,18 +39,18 @@ class AuthCodeControllerTests extends PHPUnit_Framework_TestCase
3339

3440
public function setUp()
3541
{
36-
$this->mockCookie = $this->getMock('NPR\One\Providers\CookieProvider');
42+
$this->mockCookie = $this->getMock(CookieProvider::class);
3743

38-
$this->mockSecureCookie = $this->getMock('NPR\One\Providers\SecureCookieProvider');
44+
$this->mockSecureCookie = $this->getMock(SecureCookieProvider::class);
3945

40-
$this->mockEncryption = $this->getMock('NPR\One\Providers\EncryptionProvider');
46+
$this->mockEncryption = $this->getMock(EncryptionProvider::class);
4147
$this->mockEncryption->method('isValid')->willReturn(true);
4248
$this->mockEncryption->method('set')->willReturn(true);
4349

44-
$this->mockStorage = $this->getMock('NPR\One\Interfaces\StorageInterface');
50+
$this->mockStorage = $this->getMock(StorageInterface::class);
4551
$this->mockStorage->method('compare')->willReturn(true);
4652

47-
$this->mockConfig = $this->getMock('NPR\One\Interfaces\ConfigInterface');
53+
$this->mockConfig = $this->getMock(ConfigInterface::class);
4854
$this->mockConfig->method('getClientId')->willReturn(self::$clientId);
4955
$this->mockConfig->method('getNprApiHost')->willReturn('https://api.npr.org');
5056
$this->mockConfig->method('getClientUrl')->willReturn('https://one.example.com');
@@ -54,10 +60,10 @@ public function setUp()
5460

5561
$this->mockClient = new Client(['handler' => HandlerStack::create(new MockHandler())]);
5662

57-
DI::container()->set('NPR\One\Providers\CookieProvider', $this->mockCookie);
58-
DI::container()->set('NPR\One\Providers\SecureCookieProvider', $this->mockSecureCookie);
59-
DI::container()->set('NPR\One\Providers\EncryptionProvider', $this->mockEncryption);
60-
DI::container()->set('GuzzleHttp\Client', $this->mockClient); // just in case
63+
DI::container()->set(CookieProvider::class, $this->mockCookie);
64+
DI::container()->set(SecureCookieProvider::class, $this->mockSecureCookie);
65+
DI::container()->set(EncryptionProvider::class, $this->mockEncryption);
66+
DI::container()->set(Client::class, $this->mockClient); // just in case
6167
}
6268

6369
public function testGeoIpHeadersSetInConstructor()
@@ -113,7 +119,7 @@ public function testSecureStorageProviderException()
113119
*/
114120
public function testEncryptionProviderException()
115121
{
116-
$mockEncryption = $this->getMock('NPR\One\Providers\EncryptionProvider');
122+
$mockEncryption = $this->getMock(EncryptionProvider::class);
117123
$mockEncryption->method('isValid')->willReturn(false);
118124

119125
$controller = new AuthCodeController();
@@ -221,7 +227,7 @@ public function testCompleteAuthorizationGrantWithSwapAuthCodeNoSeparatorFailure
221227
$handler = HandlerStack::create($mock);
222228
$client = new Client(['handler' => $handler, 'http_errors' => false]);
223229

224-
DI::container()->set('GuzzleHttp\Client', $client);
230+
DI::container()->set(Client::class, $client);
225231

226232
$controller = new AuthCodeController();
227233
$controller->setConfigProvider($this->mockConfig);
@@ -243,9 +249,9 @@ public function testCompleteAuthorizationGrantWithSwapAuthCodeBadState()
243249
$handler = HandlerStack::create($mock);
244250
$client = new Client(['handler' => $handler, 'http_errors' => false]);
245251

246-
DI::container()->set('GuzzleHttp\Client', $client);
252+
DI::container()->set(Client::class, $client);
247253

248-
$mockStorage = $this->getMock('NPR\One\Interfaces\StorageInterface');
254+
$mockStorage = $this->getMock(StorageInterface::class);
249255
$mockStorage->method('compare')->willReturn(false);
250256

251257
$controller = new AuthCodeController();
@@ -264,7 +270,7 @@ public function testCompleteAuthorizationGrant()
264270
$handler = HandlerStack::create($mock);
265271
$client = new Client(['handler' => $handler]);
266272

267-
DI::container()->set('GuzzleHttp\Client', $client);
273+
DI::container()->set(Client::class, $client);
268274

269275
$this->mockCookie->expects($this->once())->method('set'); //access token
270276

@@ -273,7 +279,7 @@ public function testCompleteAuthorizationGrant()
273279
$controller->setStorageProvider($this->mockStorage);
274280
$accessToken = $controller->completeAuthorizationGrant('fake_grant_code', 'fake:state');
275281

276-
$this->assertInstanceOf('NPR\One\Models\AccessTokenModel', $accessToken, 'completeAuthorizationGrant response was not of type AccessTokenModel: ' . print_r($accessToken, 1));
282+
$this->assertInstanceOf(AccessTokenModel::class, $accessToken, 'completeAuthorizationGrant response was not of type AccessTokenModel: ' . print_r($accessToken, 1));
277283
$this->assertEquals(0, $mock->count(), 'Expected additional HTTP requests to be made');
278284
}
279285

@@ -286,7 +292,7 @@ public function testCompleteAuthorizationGrantNoRefreshToken()
286292
$handler = HandlerStack::create($mock);
287293
$client = new Client(['handler' => $handler]);
288294

289-
DI::container()->set('GuzzleHttp\Client', $client);
295+
DI::container()->set(Client::class, $client);
290296

291297
$this->mockCookie->expects($this->once())->method('set'); //access token
292298

@@ -295,7 +301,7 @@ public function testCompleteAuthorizationGrantNoRefreshToken()
295301
$controller->setStorageProvider($this->mockStorage);
296302
$accessToken = $controller->completeAuthorizationGrant('fake_grant_code', 'fake:state');
297303

298-
$this->assertInstanceOf('NPR\One\Models\AccessTokenModel', $accessToken, 'completeAuthorizationGrant response was not of type AccessTokenModel: ' . print_r($accessToken, 1));
304+
$this->assertInstanceOf(AccessTokenModel::class, $accessToken, 'completeAuthorizationGrant response was not of type AccessTokenModel: ' . print_r($accessToken, 1));
299305
$this->assertEquals(0, $mock->count(), 'Expected additional HTTP requests to be made');
300306
}
301307
}

tests/unit/CustomControllerTests.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,25 @@
77

88
use NPR\One\Controllers\AbstractOAuth2Controller;
99
use NPR\One\DI\DI;
10+
use NPR\One\Interfaces\ConfigInterface;
11+
use NPR\One\Models\AccessTokenModel;
12+
use NPR\One\Providers\CookieProvider;
13+
use NPR\One\Providers\EncryptionProvider;
14+
use NPR\One\Providers\SecureCookieProvider;
1015

1116

1217
class CustomControllerTests extends PHPUnit_Framework_TestCase
1318
{
1419
const ACCESS_TOKEN_RESPONSE = '{"access_token": "LT8gvVDyeKwQJVVf6xwKAWdK0bOik64faketoken","token_type": "Bearer","expires_in": 690448786,"refresh_token": "6KVn9BOhHhUFR1Yqi2T2pzpTWI9WIfakerefresh"}';
1520
const ACCESS_TOKEN_RESPONSE_2 = '{"access_token": "LT8gvVDyeKwQJVVf6xwKAWdK0bOik64faketoken","token_type": "Bearer","expires_in": 690448786}';
1621

17-
/** @var \NPR\One\Providers\SecureCookieProvider */
22+
/** @var SecureCookieProvider */
1823
private $mockSecureCookie;
19-
/** @var \NPR\One\Providers\EncryptionProvider */
24+
/** @var EncryptionProvider */
2025
private $mockEncryption;
21-
/** @var \NPR\One\Interfaces\ConfigInterface */
26+
/** @varConfigInterface */
2227
private $mockConfig;
23-
/** @var \GuzzleHttp\Client */
28+
/** @var Client */
2429
private $mockClient;
2530

2631
/** @var string */
@@ -29,23 +34,23 @@ class CustomControllerTests extends PHPUnit_Framework_TestCase
2934

3035
public function setUp()
3136
{
32-
$this->mockSecureCookie = $this->getMock('NPR\One\Providers\SecureCookieProvider');
37+
$this->mockSecureCookie = $this->getMock(SecureCookieProvider::class);
3338

34-
$this->mockEncryption = $this->getMock('NPR\One\Providers\EncryptionProvider');
39+
$this->mockEncryption = $this->getMock(EncryptionProvider::class);
3540
$this->mockEncryption->method('isValid')->willReturn(true);
3641
$this->mockEncryption->method('set')->willReturn(true);
3742

38-
$this->mockConfig = $this->getMock('NPR\One\Interfaces\ConfigInterface');
43+
$this->mockConfig = $this->getMock(ConfigInterface::class);
3944
$this->mockConfig->method('getClientId')->willReturn(self::$clientId);
4045
$this->mockConfig->method('getNprApiHost')->willReturn('https://api.npr.org');
4146
$this->mockConfig->method('getCookieDomain')->willReturn('.example.com');
4247
$this->mockConfig->method('getEncryptionSalt')->willReturn('asYh&%D9ne!j8HKQ');
4348

4449
$this->mockClient = new Client(['handler' => HandlerStack::create(new MockHandler())]);
4550

46-
DI::container()->set('NPR\One\Providers\SecureCookieProvider', $this->mockSecureCookie);
47-
DI::container()->set('NPR\One\Providers\EncryptionProvider', $this->mockEncryption);
48-
DI::container()->set('GuzzleHttp\Client', $this->mockClient); // just in case
51+
DI::container()->set(SecureCookieProvider::class, $this->mockSecureCookie);
52+
DI::container()->set(EncryptionProvider::class, $this->mockEncryption);
53+
DI::container()->set(Client::class, $this->mockClient); // just in case
4954
}
5055

5156
/**
@@ -75,7 +80,7 @@ public function testSecureStorageProviderException()
7580
*/
7681
public function testSecureStorageProviderWarning()
7782
{
78-
$mockCookie = $this->getMock('NPR\One\Providers\CookieProvider');
83+
$mockCookie = $this->getMock(CookieProvider::class);
7984

8085
$controller = new CustomController();
8186
$controller->setConfigProvider($this->mockConfig);
@@ -101,7 +106,7 @@ public function testEncryptionProviderException()
101106
*/
102107
public function testEncryptionProviderInvalidException()
103108
{
104-
$mockEncryption = $this->getMock('NPR\One\Providers\EncryptionProvider');
109+
$mockEncryption = $this->getMock(EncryptionProvider::class);
105110
$mockEncryption->method('isValid')->willReturn(false);
106111

107112
$controller = new CustomController();
@@ -135,7 +140,7 @@ public function testIssueAccessTokenWithApiError()
135140

136141
$handler = HandlerStack::create($mock);
137142
$client = new Client(['handler' => $handler]);
138-
DI::container()->set('GuzzleHttp\Client', $client);
143+
DI::container()->set(Client::class, $client);
139144

140145
$controller = new CustomController();
141146
$controller->setConfigProvider($this->mockConfig);
@@ -150,15 +155,15 @@ public function testIssueAccessToken()
150155

151156
$handler = HandlerStack::create($mock);
152157
$client = new Client(['handler' => $handler]);
153-
DI::container()->set('GuzzleHttp\Client', $client);
158+
DI::container()->set(Client::class, $client);
154159

155160
$controller = new CustomController();
156161
$controller->setConfigProvider($this->mockConfig);
157162
$controller->setSecureStorageProvider($this->mockSecureCookie);
158163
$controller->setEncryptionProvider($this->mockEncryption);
159164
$accessToken = $controller->issueAccessToken();
160165

161-
$this->assertInstanceOf('NPR\One\Models\AccessTokenModel', $accessToken, 'issueAccessToken response was not of type AccessTokenModel: ' . print_r($accessToken, 1));
166+
$this->assertInstanceOf(AccessTokenModel::class, $accessToken, 'issueAccessToken response was not of type AccessTokenModel: ' . print_r($accessToken, 1));
162167
$this->assertEquals(0, $mock->count(), 'Expected additional HTTP requests to be made');
163168
}
164169

@@ -170,15 +175,15 @@ public function testIssueAccessTokenNoRefreshToken()
170175

171176
$handler = HandlerStack::create($mock);
172177
$client = new Client(['handler' => $handler]);
173-
DI::container()->set('GuzzleHttp\Client', $client);
178+
DI::container()->set(Client::class, $client);
174179

175180
$controller = new CustomController();
176181
$controller->setConfigProvider($this->mockConfig);
177182
$controller->setSecureStorageProvider($this->mockSecureCookie);
178183
$controller->setEncryptionProvider($this->mockEncryption);
179184
$accessToken = $controller->issueAccessToken();
180185

181-
$this->assertInstanceOf('NPR\One\Models\AccessTokenModel', $accessToken, 'issueAccessToken response was not of type AccessTokenModel: ' . print_r($accessToken, 1));
186+
$this->assertInstanceOf(AccessTokenModel::class, $accessToken, 'issueAccessToken response was not of type AccessTokenModel: ' . print_r($accessToken, 1));
182187
$this->assertEquals(0, $mock->count(), 'Expected additional HTTP requests to be made');
183188
}
184189
}

0 commit comments

Comments
 (0)