Skip to content

Commit

Permalink
Add tests for helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmered committed Sep 18, 2024
1 parent 96ca85a commit b25da40
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/UrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ public static function parseQueryTokenFromUrl(string $url, string $queryKey = 't
return self::parseTokenFromQueryString($queryString, $queryKey);
}

public static function parseTokenFromQueryString(string $queryString, string $queryKey = 'token'): string|array
public static function parseTokenFromQueryString(string $queryString, string $queryKey = 'token'): null|string|array
{
parse_str($queryString, $output);

return $output[$queryKey];
if (! isset($output[$queryKey])) {
return null;
}

return $output[$queryKey] ?? null;
}
}
41 changes: 41 additions & 0 deletions tests/Unit/UrlHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

uses(\Pelmered\LaravelHttpOAuthHelper\Tests\TestCase::class);

use Illuminate\Support\Facades\Http;
use Pelmered\LaravelHttpOAuthHelper\UrlHelper;

test('UrLHelper parseQueryTokenFromResponse', function (array $params, ?string $result) {
$response = Http::post(...$params[0]);
$token = UrlHelper::parseQueryTokenFromResponse($response, $params[1]);

expect($token)->toEqual($result);
})->with([
'normal' => [[['https://example.com/oauth/token?token=this_is_my_access_token_from_url', []], 'token'], 'this_is_my_access_token_from_url'],
'empty' => [[['https://example.com/oauth/token?token=', []], 'token'], null],
'custom' => [[['https://example.com/oauth/token?custom=this_is_my_access_token_from_url', []], 'custom'], 'this_is_my_access_token_from_url'],
'wrong' => [[['https://example.com/oauth/token?token=this_is_my_access_token_from_url', []], 'wrong'], null],
]);

test('UrLHelper parseQueryTokenFromUrl', function (array $params, ?string $result) {
$token = UrlHelper::parseQueryTokenFromUrl(...$params);

expect($token)->toEqual($result);
})->with([
'normal' => [['https://example.com/oauth/token?token=this_is_my_access_token_from_url'], 'this_is_my_access_token_from_url'],
'empty' => [['https://example.com/oauth/token'], null],
'custom' => [['https://example.com/oauth/token?custom=this_is_my_access_token_from_url', 'custom'], 'this_is_my_access_token_from_url'],
'wrong' => [['https://example.com/oauth/token?token=this_is_my_access_token_from_url', 'wrong'], null],
]);

test('UrLHelper parseTokenFromQueryString', function ($params, $result) {

$token = UrlHelper::parseTokenFromQueryString(...$params);

expect($token)->toEqual($result);
})->with([
'normal' => [['token=this_is_my_access_token_from_url'], 'this_is_my_access_token_from_url'],
'invalid query string' => [['something_random'], null],
'custom key' => [['key=this_is_another_token_from_url', 'key'], 'this_is_another_token_from_url'],
'wrong key specified' => [['key=this_is_another_token_from_url', 'other_key'], null],
]);

0 comments on commit b25da40

Please sign in to comment.