Skip to content

Commit

Permalink
Fix for SdkConfiguration::setScope([]) not assigning default values (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
evansims authored Oct 20, 2022
1 parent 34a4aa3 commit 66906e4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
18 changes: 11 additions & 7 deletions src/Configuration/SdkConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function __construct(
/**
* @param null|array<string> $audience An allowlist array of API identifiers/audiences.
*/
public function setAudience(?array $audience): self
public function setAudience(?array $audience = null): self
{
if (null !== $audience && [] === $audience) {
$audience = null;
Expand Down Expand Up @@ -201,7 +201,7 @@ public function pushAudience(array|string $audiences): ?array
return $this->audience;
}

public function setCookieDomain(?string $cookieDomain): self
public function setCookieDomain(?string $cookieDomain = null): self
{
if (null !== $cookieDomain && '' === trim($cookieDomain)) {
$cookieDomain = null;
Expand Down Expand Up @@ -262,7 +262,7 @@ public function hasCookiePath(): bool
return true;
}

public function setCookieSameSite(?string $cookieSameSite): self
public function setCookieSameSite(?string $cookieSameSite = null): self
{
if (null !== $cookieSameSite && '' === trim($cookieSameSite)) {
$cookieSameSite = null;
Expand All @@ -283,7 +283,7 @@ public function hasCookieSameSite(): bool
return null !== $this->cookieSameSite;
}

public function setCookieSecret(?string $cookieSecret): self
public function setCookieSecret(?string $cookieSecret = null): self
{
if (null !== $cookieSecret && '' === trim($cookieSecret)) {
$cookieSecret = null;
Expand All @@ -304,7 +304,7 @@ public function hasCookieSecret(): bool
return null !== $this->cookieSecret;
}

public function setCookieSecure(bool $cookieSecure): self
public function setCookieSecure(bool $cookieSecure = false): self
{
$this->cookieSecure = $cookieSecure;
return $this;
Expand Down Expand Up @@ -766,10 +766,14 @@ public function hasResponseType(): bool
}

/**
* @param array<string>|null $scope An array of scopes to request during authentication steps.
* @param array<string> $scope An array of scopes to request during authentication steps.
*/
public function setScope(array $scope = null): self
public function setScope(array $scope = ['openid', 'profile', 'email']): self
{
if ([] === $scope) {
$scope = ['openid', 'profile', 'email'];
}

$this->scope = $this->filterArray($scope) ?? [];
return $this;
}
Expand Down
21 changes: 19 additions & 2 deletions tests/Unit/Configuration/SdkConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@
expect($sdk->formatCustomDomain())->toBeNull();
});

test('formatScope() returns an empty string when there are no scopes defined', function(): void
test('formatScope() returns the default scopes when there are no scopes defined', function(): void
{
$sdk = new SdkConfiguration([
'domain' => MockDomain::valid(),
Expand All @@ -369,7 +369,24 @@
'scope' => [],
]);

expect($sdk->formatScope())->toEqual('');
expect($sdk->formatScope())->toEqual('openid profile email');
});

test('formatScope() returns the correct string when there scopes are defined', function(): void
{
$scope1 = uniqid();
$scope2 = uniqid();
$scope3 = uniqid();

$sdk = new SdkConfiguration([
'domain' => MockDomain::valid(),
'cookieSecret' => uniqid(),
'clientId' => uniqid(),
'redirectUri' => uniqid(),
'scope' => [$scope1, $scope2, $scope3],
]);

expect($sdk->formatScope())->toEqual(implode(' ', [$scope1, $scope2, $scope3]));
});

test('scope() successfully converts the array to a string', function(): void
Expand Down

0 comments on commit 66906e4

Please sign in to comment.