-
-
Notifications
You must be signed in to change notification settings - Fork 981
[ticket/10931] Wrapper class for ini_get() function #834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ca974e2
[ticket/10931] Add wrapper class for ini_get function.
bantu afd6f86
[ticket/10931] Unit tests for phpbb_php_ini class.
bantu 5bea6ed
[ticket/10931] Let us try ini_get() without error suppression.
bantu 63b2e36
[ticket/10931] Correct method description of get_string().
bantu 7501ea8
[ticket/10931] Document that false is also returned if value is not w…
bantu 5086366
[ticket/10931] Make it clear that we are mocking the ini_get() function.
bantu 80dfa53
[ticket/10931] Correctly use GNU GPL version 2.
bantu fb279c9
[ticket/10931] Also test lower case units in test_get_bytes().
bantu 3872abd
[ticket/10931] Also test for negative values.
bantu 44287e5
[ticket/10931] Use strict assertSame() instead of assertEquals().
bantu e9348b1
[ticket/10931] Correctly handle inputs such as '-k' as invalid in get…
bantu 09fb9a9
[ticket/10931] Make sure get_bytes() always returns either an int or …
bantu cbff02d
[ticket/10931] Make to_numeric function globally available.
bantu 7221207
[ticket/10931] Also test get_bytes() and get_string() with false.
bantu 4468847
[ticket/10931] Apply strtolower() correctly, i.e. not on false.
bantu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
<?php | ||
/** | ||
* | ||
* @package phpBB | ||
* @copyright (c) 2011 phpBB Group | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 | ||
* | ||
*/ | ||
|
||
/** | ||
* @ignore | ||
*/ | ||
if (!defined('IN_PHPBB')) | ||
{ | ||
exit; | ||
} | ||
|
||
/** | ||
* Wrapper class for ini_get function. | ||
* | ||
* Provides easier handling of the different interpretations of ini values. | ||
* | ||
* @package phpBB | ||
*/ | ||
class phpbb_php_ini | ||
{ | ||
/** | ||
* Simple wrapper for ini_get() | ||
* See http://php.net/manual/en/function.ini-get.php | ||
* | ||
* @param string $varname The configuration option name. | ||
* @return bool|string False if configuration option does not exist, | ||
* the configuration option value (string) otherwise. | ||
*/ | ||
public function get($varname) | ||
{ | ||
return ini_get($varname); | ||
} | ||
|
||
/** | ||
* Gets the configuration option value as a trimmed string. | ||
* | ||
* @param string $varname The configuration option name. | ||
* @return bool|string False if configuration option does not exist, | ||
* the configuration option value (string) otherwise. | ||
*/ | ||
public function get_string($varname) | ||
{ | ||
$value = $this->get($varname); | ||
|
||
if ($value === false) | ||
{ | ||
return false; | ||
} | ||
|
||
return trim($value); | ||
} | ||
|
||
/** | ||
* Gets configuration option value as a boolean. | ||
* Interprets the string value 'off' as false. | ||
* | ||
* @param string $varname The configuration option name. | ||
* @return bool False if configuration option does not exist. | ||
* False if configuration option is disabled. | ||
* True otherwise. | ||
*/ | ||
public function get_bool($varname) | ||
{ | ||
$value = $this->get_string($varname); | ||
|
||
if (empty($value) || strtolower($value) == 'off') | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Gets configuration option value as an integer. | ||
* | ||
* @param string $varname The configuration option name. | ||
* @return bool|int False if configuration option does not exist, | ||
* false if configuration option value is not numeric, | ||
* the configuration option value (integer) otherwise. | ||
*/ | ||
public function get_int($varname) | ||
{ | ||
$value = $this->get_string($varname); | ||
|
||
if (!is_numeric($value)) | ||
{ | ||
return false; | ||
} | ||
|
||
return (int) $value; | ||
} | ||
|
||
/** | ||
* Gets configuration option value as a float. | ||
* | ||
* @param string $varname The configuration option name. | ||
* @return bool|float False if configuration option does not exist, | ||
* false if configuration option value is not numeric, | ||
* the configuration option value (float) otherwise. | ||
*/ | ||
public function get_float($varname) | ||
{ | ||
$value = $this->get_string($varname); | ||
|
||
if (!is_numeric($value)) | ||
{ | ||
return false; | ||
} | ||
|
||
return (float) $value; | ||
} | ||
|
||
/** | ||
* Gets configuration option value in bytes. | ||
* Converts strings like '128M' to bytes (integer or float). | ||
* | ||
* @param string $varname The configuration option name. | ||
* @return bool|int|float False if configuration option does not exist, | ||
* false if configuration option value is not well-formed, | ||
* the configuration option value otherwise. | ||
*/ | ||
public function get_bytes($varname) | ||
{ | ||
$value = $this->get_string($varname); | ||
|
||
if ($value === false) | ||
{ | ||
return false; | ||
} | ||
|
||
if (is_numeric($value)) | ||
{ | ||
// Already in bytes. | ||
return phpbb_to_numeric($value); | ||
} | ||
else if (strlen($value) < 2) | ||
{ | ||
// Single character. | ||
return false; | ||
} | ||
else if (strlen($value) < 3 && $value[0] === '-') | ||
{ | ||
// Two characters but the first one is a minus. | ||
return false; | ||
} | ||
|
||
$value_lower = strtolower($value); | ||
$value_numeric = phpbb_to_numeric($value); | ||
|
||
switch ($value_lower[strlen($value_lower) - 1]) | ||
{ | ||
case 'g': | ||
$value_numeric *= 1024; | ||
case 'm': | ||
$value_numeric *= 1024; | ||
case 'k': | ||
$value_numeric *= 1024; | ||
break; | ||
|
||
default: | ||
// It's not already in bytes (and thus numeric) | ||
// and does not carry a unit. | ||
return false; | ||
} | ||
|
||
return $value_numeric; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
/** | ||
* | ||
* @package testing | ||
* @copyright (c) 2011 phpBB Group | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 | ||
* | ||
*/ | ||
|
||
class phpbb_php_ini_fake extends phpbb_php_ini | ||
{ | ||
function get($varname) | ||
{ | ||
return $varname; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
/** | ||
* | ||
* @package testing | ||
* @copyright (c) 2011 phpBB Group | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 | ||
* | ||
*/ | ||
|
||
require_once dirname(__FILE__) . '/phpbb_php_ini_fake.php'; | ||
require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; | ||
|
||
class phpbb_wrapper_phpbb_php_ini_test extends phpbb_test_case | ||
{ | ||
protected $php_ini; | ||
|
||
public function setUp() | ||
{ | ||
$this->php_ini = new phpbb_php_ini_fake; | ||
} | ||
|
||
public function test_get_string() | ||
{ | ||
$this->assertSame(false, $this->php_ini->get_string(false)); | ||
$this->assertSame('phpbb', $this->php_ini->get_string(' phpbb ')); | ||
} | ||
|
||
public function test_get_bool() | ||
{ | ||
$this->assertSame(true, $this->php_ini->get_bool('ON')); | ||
$this->assertSame(true, $this->php_ini->get_bool('on')); | ||
$this->assertSame(true, $this->php_ini->get_bool('1')); | ||
|
||
$this->assertSame(false, $this->php_ini->get_bool('OFF')); | ||
$this->assertSame(false, $this->php_ini->get_bool('off')); | ||
$this->assertSame(false, $this->php_ini->get_bool('0')); | ||
$this->assertSame(false, $this->php_ini->get_bool('')); | ||
} | ||
|
||
public function test_get_int() | ||
{ | ||
$this->assertSame(1234, $this->php_ini->get_int('1234')); | ||
$this->assertSame(-12345, $this->php_ini->get_int('-12345')); | ||
$this->assertSame(false, $this->php_ini->get_int('phpBB')); | ||
} | ||
|
||
public function test_get_float() | ||
{ | ||
$this->assertSame(1234.0, $this->php_ini->get_float('1234')); | ||
$this->assertSame(-12345.0, $this->php_ini->get_float('-12345')); | ||
$this->assertSame(false, $this->php_ini->get_float('phpBB')); | ||
} | ||
|
||
public function test_get_bytes_invalid() | ||
{ | ||
$this->assertSame(false, $this->php_ini->get_bytes(false)); | ||
$this->assertSame(false, $this->php_ini->get_bytes('phpBB')); | ||
$this->assertSame(false, $this->php_ini->get_bytes('k')); | ||
$this->assertSame(false, $this->php_ini->get_bytes('-k')); | ||
$this->assertSame(false, $this->php_ini->get_bytes('M')); | ||
$this->assertSame(false, $this->php_ini->get_bytes('-M')); | ||
} | ||
|
||
/** | ||
* @dataProvider get_bytes_data | ||
*/ | ||
public function test_get_bytes($expected, $value) | ||
{ | ||
$actual = $this->php_ini->get_bytes($value); | ||
|
||
$this->assertTrue(is_float($actual) || is_int($actual)); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
static public function get_bytes_data() | ||
{ | ||
return array( | ||
array(32 * pow(2, 20), '32m'), | ||
array(- 32 * pow(2, 20), '-32m'), | ||
array(8 * pow(2, 30), '8G'), | ||
array(- 8 * pow(2, 30), '-8G'), | ||
array(1234, '1234'), | ||
array(-12345, '-12345'), | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not match the behavior - is_numeric check will return false for options that exist.