Skip to content

Commit 1b84e44

Browse files
committed
Remove null coalescing; add service tests
1 parent f02ed3b commit 1b84e44

File tree

4 files changed

+88
-10
lines changed

4 files changed

+88
-10
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"phpunit/phpunit": "^4 | ^7",
1313
"squizlabs/php_codesniffer": "^3.2",
1414
"phpcompatibility/php-compatibility": "^8.1",
15-
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0"
15+
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
16+
"orchestra/testbench": "^3.8"
1617
},
1718
"scripts": {
1819
"test": "SHELL_INTERACTIVE=1 \"vendor/bin/phpunit\" --coverage-text ",

phpunit.xml.dist

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,4 @@
1919
<directory suffix=".php">src/</directory>
2020
</whitelist>
2121
</filter>
22-
<logging>
23-
<log type="tap" target="build/report.tap"/>
24-
<log type="junit" target="build/report.junit.xml"/>
25-
<log type="coverage-html" target="build/coverage"/>
26-
<log type="coverage-text" target="build/coverage.txt"/>
27-
<log type="coverage-clover" target="build/logs/clover.xml"/>
28-
</logging>
2922
</phpunit>

src/Auth0/Login/Auth0Service.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public function __construct(
4545
$auth0Config = config('laravel-auth0');
4646
}
4747

48-
$store = $auth0Config['store'] ?? $store;
48+
$store = isset( $auth0Config['store'] ) ? $auth0Config['store'] : $store;
4949
if (false !== $store && !$store instanceof StoreInterface) {
5050
$store = new LaravelSessionStore();
5151
}
5252

53-
$stateHandler = $auth0Config['state_handler'] ?? $stateHandler;
53+
$stateHandler = isset( $auth0Config['state_handler'] ) ? $auth0Config['state_handler'] : $stateHandler;
5454
if (false !== $stateHandler && !$stateHandler instanceof StateHandler) {
5555
$stateHandler = new SessionStateHandler($store);
5656
}

tests/Auth0ServiceTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
namespace Auth0\Login\Tests;
3+
4+
use Auth0\Login\Auth0Service;
5+
use Auth0\Login\Facade\Auth0 as Auth0Facade;
6+
use Auth0\Login\LoginServiceProvider as Auth0ServiceProvider;
7+
use Auth0\SDK\API\Helpers\State\DummyStateHandler;
8+
use Auth0\SDK\Store\EmptyStore;
9+
use Orchestra\Testbench\TestCase as OrchestraTestCase;
10+
use Session;
11+
12+
class Auth0ServiceTest extends OrchestraTestCase
13+
{
14+
public static $defaultConfig;
15+
16+
public static function setUpBeforeClass()
17+
{
18+
parent::setUpBeforeClass();
19+
self::$defaultConfig = [
20+
'domain' => 'test.auth0.com',
21+
'client_id' => '__test_client_id__',
22+
'client_secret' => '__test_client_secret__',
23+
'redirect_uri' => 'https://example.com/callback',
24+
];
25+
}
26+
27+
public function testThatServiceUsesSessionStoreByDefault()
28+
{
29+
Session::put('auth0__user', '__test_user__');
30+
$service = new Auth0Service(self::$defaultConfig);
31+
$user = $service->getUser();
32+
33+
$this->assertArrayHasKey('profile', $user);
34+
$this->assertEquals('__test_user__', $user['profile']);
35+
}
36+
37+
public function testThatServiceSetsEmptyStoreFromConfigAndConstructor()
38+
{
39+
Session::put('auth0__user', '__test_user__');
40+
41+
$service = new Auth0Service(self::$defaultConfig + ['store' => false, 'state_handler' => false]);
42+
$this->assertNull($service->getUser());
43+
44+
$service = new Auth0Service(self::$defaultConfig, new EmptyStore(), new DummyStateHandler());
45+
$this->assertNull($service->getUser());
46+
47+
$service = new Auth0Service(self::$defaultConfig);
48+
$this->assertIsArray($service->getUser());
49+
}
50+
51+
public function testThatServiceLoginReturnsRedirect()
52+
{
53+
54+
$service = new Auth0Service(self::$defaultConfig);
55+
$redirect = $service->login();
56+
57+
$this->assertInstanceOf( \Illuminate\Http\RedirectResponse::class, $redirect );
58+
59+
$targetUrl = parse_url($redirect->getTargetUrl());
60+
61+
$this->assertEquals('test.auth0.com', $targetUrl['host']);
62+
63+
$targetUrlQuery = explode('&', $targetUrl['query']);
64+
65+
$this->assertContains('redirect_uri=https%3A%2F%2Fexample.com%2Fcallback', $targetUrlQuery);
66+
$this->assertContains('client_id=__test_client_id__', $targetUrlQuery);
67+
}
68+
69+
/*
70+
* Test suite helpers
71+
*/
72+
73+
protected function getPackageProviders($app)
74+
{
75+
return [Auth0ServiceProvider::class];
76+
}
77+
78+
protected function getPackageAliases($app)
79+
{
80+
return [
81+
'Auth0' => Auth0Facade::class,
82+
];
83+
}
84+
}

0 commit comments

Comments
 (0)