Skip to content

Commit

Permalink
Override option values by constants
Browse files Browse the repository at this point in the history
Close #67
  • Loading branch information
tangrufus committed Apr 11, 2017
1 parent c0478e5 commit 311a66d
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 40 deletions.
58 changes: 47 additions & 11 deletions src/OptionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,70 @@
* This is a very basic adapter for the WordPress get_option()
* function that can be configured to supply consistent default
* values for particular options.
*
* @since 0.5.0
*/
class OptionStore implements OptionStoreInterface
{
/**
* Get an option value from database.
* Get an option value from constant or database.
*
* Wrapper around the WordPress function `get_option`.
*
* @since 0.5.0
* Can be overridden by constant `OPTION_NAME_KEY`.
*
* @param string $optionName Name of option to retrieve.
* Expected to not be SQL-escaped.
* @param string $key Array key of the option element.
* @param string $key Optional. Array key of the option element.
* Also, the field ID.
*
* @return mixed
*/
public function get(string $optionName, string $key)
public function get(string $optionName, string $key = null)
{
$option = get_option($optionName, []);
$constantName = $this->constantNameFor($optionName, $key);

if (! is_array($option)) {
return $option;
if (defined($constantName)) {
return constant($constantName);
}

return $this->getFromDatabase($optionName, $key);

// TODO: Add filters and hooks.
return $option[ $key ] ?? false;
}

/**
* Normalize option name and key to SCREAMING_SNAKE_CASE constant name.
*
* @param string $optionName Name of option to retrieve.
* Expected to not be SQL-escaped.
* @param string $key Optional. Array key of the option element.
* Also, the field ID.
*
* @return string
*/
private function constantNameFor(string $optionName, string $key = null): string
{
$name = empty($key) ? $optionName : $optionName . '_' . $key;

return strtoupper($name);
}

/**
* Get option from database.
*
* @param string $optionName Name of option to retrieve.
* Expected to not be SQL-escaped.
* @param string $key Optional. Array key of the option element.
* Also, the field ID.
*
* @return mixed
*/
private function getFromDatabase(string $optionName, string $key = null)
{
$option = get_option($optionName);

if (! empty($key) && is_array($option)) {
return $option[ $key ] ?? false;
}

return $option;
}
}
8 changes: 2 additions & 6 deletions src/OptionStoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,18 @@
* This is a very basic adapter for the WordPress get_option()
* function that can be configured to supply consistent default
* values for particular options.
*
* @since 0.5.0
*/
interface OptionStoreInterface
{
/**
* Get an option value.
*
* @since 0.5.0
*
* @param string $optionName Name of option to retrieve.
* Expected to not be SQL-escaped.
* @param string $key Array key of the option element.
* @param string $key Optional. Array key of the option element.
* Also, the field ID.
*
* @return mixed
*/
public function get(string $optionName, string $key);
public function get(string $optionName, string $key = null);
}
112 changes: 89 additions & 23 deletions tests/unit/OptionStoreTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace TypistTech\WPBetterSettings;

use AspectMock\Test;
Expand All @@ -9,71 +11,135 @@
*/
class OptionStoreTest extends \Codeception\Test\Unit
{

/**
* @var \TypistTech\WPBetterSettings\OptionStore
*/
private $optionStore;

/**
* @covers ::get
* @covers \TypistTech\WPBetterSettings\OptionStore
*/
public function testGet()
public function testGetCheckboxFromArray()
{
$actual = $this->optionStore->get('my_option_array', 'my_text');
$this->assertSame('long long text.', $actual);
$actual = $this->optionStore->get('my_option_array', 'my_checkbox');
$this->assertTrue($actual);
}

/**
* @covers ::get
* @covers \TypistTech\WPBetterSettings\OptionStore
*/
public function testGetCheckbox()
public function testGetCheckedCheckboxFromArrayConstant()
{
$actual = $this->optionStore->get('my_option_array', 'my_checkbox');
$this->assertSame('1', $actual);
$actual = $this->optionStore->get('my_option_array', 'my_checked_checkbox_constant');
$this->assertTrue($actual);
}

/**
* @covers ::get
* @covers \TypistTech\WPBetterSettings\OptionStore
*/
public function testGetNonArray()
public function testGetNonExistKeyFromArray()
{
$actual = $this->optionStore->get('my_option_string', 'my_text');
$this->assertSame('i live in wp_option', $actual);
$actual = $this->optionStore->get('my_option_array', 'non_exist_key');
$this->assertFalse($actual);
}

/**
* @covers ::get
* @covers \TypistTech\WPBetterSettings\OptionStore
*/
public function testGetNonExistKey()
public function testGetNonExistOption()
{
$actual = $this->optionStore->get('my_option_array', 'non_exist_key');
$actual = $this->optionStore->get('non_exist_option', 'my_text');
$this->assertFalse($actual);
}

/**
* @covers ::get
* @covers \TypistTech\WPBetterSettings\OptionStore
*/
public function testGetNonExistOption()
public function testGetString()
{
$actual = $this->optionStore->get('non_exist_option', 'my_text');
$actual = $this->optionStore->get('my_option_string');
$this->assertSame('i live in wp_option', $actual);
}

/**
* @covers \TypistTech\WPBetterSettings\OptionStore
*/
public function testGetStringFromArray()
{
$actual = $this->optionStore->get('my_option_array', 'my_text');
$this->assertSame('long long text.', $actual);
}

/**
* @covers \TypistTech\WPBetterSettings\OptionStore
*/
public function testGetStringFromArrayConstant()
{
$actual = $this->optionStore->get('my_option_array', 'my_text_constant');
$this->assertSame('i am constant', $actual);
}

/**
* @covers \TypistTech\WPBetterSettings\OptionStore
*/
public function testGetStringFromConstant()
{
$actual = $this->optionStore->get('my_option_string_constant');
$this->assertSame('i am string constant', $actual);
}

/**
* @covers \TypistTech\WPBetterSettings\OptionStore
*/
public function testGetUncheckedCheckboxFromArrayConstant()
{
$actual = $this->optionStore->get('my_option_array', 'my_unchecked_checkbox_constant');
$this->assertFalse($actual);
}

protected function _before()
{
Test::func(__NAMESPACE__, 'defined', function (string $name) {
switch ($name) {
case 'MY_OPTION_STRING_CONSTANT':
case 'MY_OPTION_ARRAY_MY_TEXT_CONSTANT':
case 'MY_OPTION_ARRAY_MY_CHECKED_CHECKBOX_CONSTANT':
case 'MY_OPTION_ARRAY_MY_UNCHECKED_CHECKBOX_CONSTANT':
return true;
}

return false;
});

Test::func(__NAMESPACE__, 'constant', function (string $name) {
switch ($name) {
case 'MY_OPTION_STRING_CONSTANT':
return 'i am string constant';
case 'MY_OPTION_ARRAY_MY_TEXT_CONSTANT':
return 'i am constant';
case 'MY_OPTION_ARRAY_MY_CHECKED_CHECKBOX_CONSTANT':
return true;
case 'MY_OPTION_ARRAY_MY_UNCHECKED_CHECKBOX_CONSTANT':
return false;
}

$this->fail($name . ' is not defined');
});

Test::func(__NAMESPACE__, 'get_option', function (string $key) {
switch ($key) {
case 'my_option_array':
return [
'my_text' => 'long long text.',
'my_checkbox' => '1',
'my_text' => 'long long text.',
'my_text_constant' => 'i am not constant',
'my_checkbox' => true,
'my_unchecked_checkbox_constant' => true,
];
case 'my_option_string':
return 'i live in wp_option';
default:
return false;
}

return false;
});

$this->optionStore = new OptionStore;
Expand Down

0 comments on commit 311a66d

Please sign in to comment.