Skip to content

Commit

Permalink
Added a new exception and updated tests to ensure 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyamcl committed Oct 23, 2019
1 parent c803903 commit 545c175
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 41 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,13 @@ All notable changes to *Settings Manager* will be documented in this file.

Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## UNRELEASED
### Added
- New exception: `SettingDefinitionNotFoundException` (thrown when attempting to get a definition from the registry that doesn't exist)
### Changed
- Name: `AbstractSettingTest` -> `AbstractSettingDefinitionTest`
- Removed unnecessary lines from `AbstractSettingDefinitionTest`

## [0.9] - 2019-10-22
### Added
- `SettingProviderInterface::findValue()` shortcut method to get the value directly
Expand Down
14 changes: 10 additions & 4 deletions README.md
Expand Up @@ -42,10 +42,10 @@ A **Setting Definition** is simply a PHP class that implements the

Setting definitions are added to the `SettingDefinitionRegistry`.

A **Setting Provider** is a service class that loads setting values from a source. Sources can be
A **Setting Provider** is a service class that loads setting values from a source. Sources can be configuration files,
databases, or really anything. See [usage](#usage) section below for a list of bundled providers.

Multiple providers can be chained together so that
setting values are loaded in a cascading way. Several providers have been bundled (see below), but you can feel free to add
Multiple providers can be chained together so that setting values are loaded in a cascading way. Several providers have been bundled (see below), but you can feel free to add
your own by extending the `SettingProvider` interface. Providers have similar attributes to definitions:

* A name (e.g. a machine name/slug)
Expand All @@ -67,7 +67,13 @@ $ composer require caseyamcl/settings-manager

## Usage

TODO: Usage
TODO: Basic usage (adding setting definitions and loading values)

TODO: bundled providers

TODO: creating your own provider implementation

TODO: handling exceptions (all implement the `SettingException` interface)

## Change log

Expand Down
40 changes: 40 additions & 0 deletions src/Exception/SettingDefinitionNotFoundException.php
@@ -0,0 +1,40 @@
<?php

/**
* Settings Manager
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/caseyamcl/settings-manager
* @package caseyamcl/settings-manager
* @author Casey McLaughlin <caseyamcl@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* ------------------------------------------------------------------
*/

declare(strict_types=1);

namespace SettingsManager\Exception;

use RuntimeException;
use Throwable;

/**
* Class SettingDefinitionNotFoundException
*
* @author Casey McLaughlin <caseyamcl@gmail.com>
*/
class SettingDefinitionNotFoundException extends RuntimeException implements SettingException
{
/**
* @param string $name
* @param Throwable|null $previous
* @return static
*/
public static function forSetting(string $name, Throwable $previous = null): self
{
return new static("Setting definition not found: " . $name, 0, $previous);
}
}
4 changes: 2 additions & 2 deletions src/Registry/SettingDefinitionRegistry.php
Expand Up @@ -21,8 +21,8 @@
use ArrayIterator;
use Countable;
use IteratorAggregate;
use RuntimeException;
use SettingsManager\Contract\SettingDefinition;
use SettingsManager\Exception\SettingDefinitionNotFoundException;
use SettingsManager\Exception\SettingNameCollisionException;

/**
Expand Down Expand Up @@ -66,7 +66,7 @@ final public function get(string $name): SettingDefinition
if ($this->has($name)) {
return $this->items[$name];
} else {
throw new RuntimeException("Setting not found: " . $name);
throw SettingDefinitionNotFoundException::forSetting($name);
}
}

Expand Down
Expand Up @@ -7,17 +7,12 @@
use LogicException;
use PHPUnit\Framework\TestCase;

class AbstractSettingTest extends TestCase
class AbstractSettingDefinitionTest extends TestCase
{
public function testIsSensitive()
{
$obj = new class extends AbstractSettingDefinition {
public const SENSITIVE = false;

public function processValue($value)
{
return $value;
}
};

$this->assertFalse($obj->isSensitive());
Expand All @@ -27,11 +22,6 @@ public function testGetNotes()
{
$obj = new class extends AbstractSettingDefinition {
public const NOTES = 'test';

public function processValue($value)
{
return $value;
}
};

$this->assertSame('test', $obj->getNotes());
Expand All @@ -41,11 +31,6 @@ public function testGetNameReturnsValueWhenConstantIsSet()
{
$obj = new class extends AbstractSettingDefinition {
public const NAME = 'test';

public function processValue($value)
{
return $value;
}
};

$this->assertSame('test', $obj->getName());
Expand All @@ -57,10 +42,7 @@ public function testGetNameThrowsExceptionWhenConstantNotSet()
$this->expectExceptionMessage('must implement constant');

$obj = new class extends AbstractSettingDefinition {
public function processValue($value)
{
return $value;
}
// NAME constant purposefully omitted
};

$obj->getName();
Expand All @@ -70,11 +52,6 @@ public function testGetDefault()
{
$obj = new class extends AbstractSettingDefinition {
public const DEFAULT = 'test';

public function processValue($value)
{
return $value;
}
};

$this->assertSame('test', $obj->getDefault());
Expand All @@ -84,11 +61,6 @@ public function testGetDisplayNameReturnsValueWhenConstantIsSet()
{
$obj = new class extends AbstractSettingDefinition {
public const DISPLAY_NAME = 'test';

public function processValue($value)
{
return $value;
}
};

$this->assertSame('test', $obj->getDisplayName());
Expand All @@ -100,12 +72,18 @@ public function testGetDisplayNameThrowsExceptionWhenConstantIsNotSet()
$this->expectExceptionMessage('must implement constant');

$obj = new class extends AbstractSettingDefinition {
public function processValue($value)
{
return $value;
}
// DISPLAY_NAME constant purposefully omitted
};

$obj->getDisplayName();
}

public function testDefaultImplementationReturnsTheSameValueAsWasPassed()
{
$obj = new class extends AbstractSettingDefinition {
public const NAME = 'test';
public const DISPLAY_NAME = 'Test setting';
};
$this->assertSame('test', $obj->processValue('test'));
}
}
2 changes: 1 addition & 1 deletion tests/Registry/SettingDefinitionRegistryTest.php
Expand Up @@ -61,7 +61,7 @@ public function testGetReturnsDefinitionWhenExists()
public function testGetThrowsExceptionWhenDefinitionDoesNotExist()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage("Setting not found: ");
$this->expectExceptionMessage("Setting definition not found: ");
$registry = new SettingDefinitionRegistry();
$registry->get(StringSetting::NAME);
}
Expand Down

0 comments on commit 545c175

Please sign in to comment.