Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
:octocat: +OAuthProviderTestAbstract::get/setReflectionProperty()
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Mar 6, 2024
1 parent e895010 commit 10162fb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
10 changes: 5 additions & 5 deletions tests/Providers/OAuth2ProviderTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testGetAuthURL():void{
$this::assertArrayNotHasKey('client_secret', $query);
$this::assertSame($this->options->key, $query['client_id']);
$this::assertSame('code', $query['response_type']);
$this::assertSame(explode('?', (string)$url)[0], $this->reflection->getProperty('authURL')->getValue($this->provider));
$this::assertSame(explode('?', (string)$url)[0], $this->getReflectionProperty('authURL'));
}

public function testGetAccessToken():void{
Expand Down Expand Up @@ -107,19 +107,19 @@ public function testGetRequestAuthorization():void{
$request = $this->requestFactory->createRequest('GET', 'https://foo.bar');
$token = new AccessToken(['accessTokenSecret' => 'test_token_secret', 'accessToken' => 'test_token']);

$authMethod = $this->reflection->getProperty('authMethod')->getValue($this->provider);
$authMethod = $this->getReflectionProperty('authMethod');

// header (default)
if($authMethod === OAuth2Interface::AUTH_METHOD_HEADER){
$this::assertStringContainsString(
$this->reflection->getProperty('authMethodHeader')->getValue($this->provider).' test_token',
$this->getReflectionProperty('authMethodHeader').' test_token',
$this->provider->getRequestAuthorization($request, $token)->getHeaderLine('Authorization')
);
}
// query
elseif($authMethod === OAuth2Interface::AUTH_METHOD_QUERY){
$this::assertStringContainsString(
$this->reflection->getProperty('authMethodQuery')->getValue($this->provider).'=test_token',
$this->getReflectionProperty('authMethodQuery').'=test_token',
$this->provider->getRequestAuthorization($request, $token)->getUri()->getQuery()
);
}
Expand All @@ -137,7 +137,7 @@ public function testRequestInvalidAuthTypeException():void{
$this->expectException(OAuthException::class);
$this->expectExceptionMessage('invalid auth type');

$this->reflection->getProperty('authMethod')->setValue($this->provider, -1);
$this->setReflectionProperty('authMethod', -1);

$token = new AccessToken(['accessToken' => 'test_access_token_secret', 'expires' => 1]);
$this->provider->storeAccessToken($token);
Expand Down
12 changes: 10 additions & 2 deletions tests/Providers/OAuthProviderTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,15 @@ protected function initTestProperties(array $properties):void{
}
}

protected function invokeReflectionMethod(string $method, array $args = []){
protected function setReflectionProperty(string $property, mixed $value):void{
$this->reflection->getProperty($property)->setValue($this->provider, $value);
}

protected function getReflectionProperty(string $property):mixed{
return $this->reflection->getProperty($property)->getValue($this->provider);
}

protected function invokeReflectionMethod(string $method, array $args = []):mixed{
return $this->reflection->getMethod($method)->invokeArgs($this->provider, $args);
}

Expand Down Expand Up @@ -166,7 +174,7 @@ public static function requestTargetProvider():array{

#[DataProvider('requestTargetProvider')]
public function testGetRequestTarget(string $path, string $expected):void{
$this->reflection->getProperty('apiURL')->setValue($this->provider, 'https://localhost/api/');
$this->setReflectionProperty('apiURL', 'https://localhost/api/');

$this::assertSame($expected, $this->invokeReflectionMethod('getRequestTarget', [$path]));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Providers/Unit/MailChimpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testRequestInvalidAuthTypeException():void{
$this->expectException(OAuthException::class);
$this->expectExceptionMessage('invalid auth type');

$this->reflection->getProperty('authMethod')->setValue($this->provider, -1);
$this->setReflectionProperty('authMethod', -1);

$this->storage->storeAccessToken($this->token, $this->provider->serviceName);

Expand Down
6 changes: 3 additions & 3 deletions tests/Providers/Unit/NPROneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testRequestInvalidAuthTypeException():void{
$this->expectException(OAuthException::class);
$this->expectExceptionMessage('invalid auth type');

$this->reflection->getProperty('authMethod')->setValue($this->provider, -1);
$this->setReflectionProperty('authMethod', -1);

$token = new AccessToken(['accessToken' => 'test_access_token_secret', 'expires' => 1]);
$this->storage->storeAccessToken($token, $this->provider->serviceName);
Expand All @@ -73,11 +73,11 @@ public static function requestTargetProvider():array{
public function testSetAPI():void{
$this->provider = $this->initProvider($this->getProviderFQCN());

$this::assertSame('https://listening.api.npr.org', $this->reflection->getProperty('apiURL')->getValue($this->provider));
$this::assertSame('https://listening.api.npr.org', $this->getReflectionProperty('apiURL'));

$this->provider->setAPI('station');

$this::assertSame('https://station.api.npr.org', $this->reflection->getProperty('apiURL')->getValue($this->provider));
$this::assertSame('https://station.api.npr.org', $this->getReflectionProperty('apiURL'));
}

}

0 comments on commit 10162fb

Please sign in to comment.