Skip to content
Snippets Groups Projects
Commit 09d36d36 authored by Andrew Hoffmann's avatar Andrew Hoffmann
Browse files

LocalUserDetailsProvider throws Exception if JSON is invalid, returns null if JSON is missing

parent b74fd618
No related branches found
No related tags found
1 merge request!12Test all the things!
<?php
namespace edu\wisc\doit\uwphps\local;
/**
* Exception when JSON cannot be decoded
*/
class JsonDecodingException extends \RuntimeException
{
}
\ No newline at end of file
......@@ -26,16 +26,23 @@ class LocalUserDetailsProvider extends PreauthUserDetailsProvider
}
/**
* {@inheritdoc}
* Loads user from configured JSON file.
*
* @return UWUserDetails user, null if file is missing
* @throws JsonDecodingException if unable to decode user JSON file
*/
public function loadUser()
{
$jsonString = file_get_contents($this->filePath);
if ($jsonString === false) {
if (!is_readable($this->filePath)) {
return null;
}
$attributes = json_decode($jsonString, true);
$attributes = json_decode(file_get_contents($this->filePath), true);
// Throw exception if file cannot be decoded
if ($attributes === null) {
throw new JsonDecodingException('Unable to parse JSON in file: ' . realpath($this->filePath));
}
return new UWUserDetails(
$attributes[static::EPPN],
......
<?php
use edu\wisc\doit\uwphps\local\LocalUserDetailsProvider;
namespace edu\wisc\doit\uwphps\local;
/**
* Tests for {@link LocalUserDetailsProvider}.
......@@ -8,12 +7,12 @@ use edu\wisc\doit\uwphps\local\LocalUserDetailsProvider;
class LocalUserDetailsProviderTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function loadUser()
private $resourcesDir = __DIR__ . '/../../../../../resources';
/** @test */
public function loadsUser()
{
$userDetailsService = new LocalUserDetailsProvider(__DIR__ . "/../../../../../resources/localuser.json");
$userDetailsService = new LocalUserDetailsProvider("{$this->resourcesDir}/localuser.json");
$user = $userDetailsService->loadUser();
$this->assertEquals("bbadger@wisc.edu", $user->getEppn());
$this->assertEquals("UW123A456", $user->getPvi());
......@@ -25,4 +24,21 @@ class LocalUserDetailsProviderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals("BADGER", $user->getLastName());
}
/** @test */
public function loadsNullUserWhenMissingFile()
{
$provider = new LocalUserDetailsProvider("{$this->resourcesDir}/nobody.json");
$this->assertNull($provider->loadUser());
}
/**
* @test
* @expectedException \edu\wisc\doit\uwphps\local\JsonDecodingException
*/
public function throwsIfInvalidJson()
{
$provider = new LocalUserDetailsProvider("{$this->resourcesDir}/badjson.json");
$provider->loadUser();
}
}
This file should not contain valid JSON.
See: LocalUserDetailsProviderTest
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment