Skip to content

[2.5] Added the Valiator method isBoolean #197

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 11 commits into from Jan 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@ If the environment variable is not an integer, you'd get an Exception:
One or more environment variables failed assertions: FOO is not an integer
```

### Boolean Variables

You may need to ensure a variable is in the form of a boolean, accepting "On", "1", "Yes", "Off", "0" and "No". You may do the following:

```php
$dotenv->required('FOO')->isBoolean();
```

If the environment variable is not a boolean, you'd get an Exception:

```
One or more environment variables failed assertions: FOO is not a boolean
```

### Allowed Values

It is also possible to define a set of values that your environment variable
Expand Down
19 changes: 19 additions & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ function ($value) {
);
}

/**
* Assert that each specified variable is a boolean.
*
* @return \Dotenv\Validator
*/
public function isBoolean()
{
return $this->assertCallback(
function ($value) {
if ($value === '') {
return false;
}

return (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== NULL);
},
'is not a boolean'
);
}

/**
* Assert that each variable is amongst the given choices.
*
Expand Down
95 changes: 95 additions & 0 deletions tests/Dotenv/ValidatorBooleanTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

use Dotenv\Dotenv;

class ValidatorBooleanTest extends PHPUnit_Framework_TestCase
{
/**
* @var string
*/
private $fixturesFolder;

public function setUp()
{
$this->fixturesFolder = dirname(__DIR__).'/fixtures/env';
}

/**
* List of valid boolean values in fixtures/env/booleans.env
*
* @return array
*/
public function validBooleanValuesDataProvider()
{
return array(
array('VALID_EXPLICIT_LOWERCASE_TRUE'),
array('VALID_EXPLICIT_LOWERCASE_FALSE'),
array('VALID_EXPLICIT_UPPERCASE_TRUE'),
array('VALID_EXPLICIT_UPPERCASE_FALSE'),
array('VALID_EXPLICIT_MIXEDCASE_TRUE'),
array('VALID_EXPLICIT_MIXEDCASE_FALSE'),

array('VALID_NUMBER_TRUE'),
array('VALID_NUMBER_FALSE'),

array('VALID_ONOFF_LOWERCASE_TRUE'),
array('VALID_ONOFF_LOWERCASE_FALSE'),
array('VALID_ONOFF_UPPERCASE_TRUE'),
array('VALID_ONOFF_UPPERCASE_FALSE'),
array('VALID_ONOFF_MIXEDCASE_TRUE'),
array('VALID_ONOFF_MIXEDCASE_FALSE'),

array('VALID_YESNO_LOWERCASE_TRUE'),
array('VALID_YESNO_LOWERCASE_FALSE'),
array('VALID_YESNO_UPPERCASE_TRUE'),
array('VALID_YESNO_UPPERCASE_FALSE'),
array('VALID_YESNO_MIXEDCASE_TRUE'),
array('VALID_YESNO_MIXEDCASE_FALSE'),
);
}

/**
* @dataProvider validBooleanValuesDataProvider
*/
public function testCanValidateBooleans($boolean)
{
$dotenv = new Dotenv($this->fixturesFolder, 'booleans.env');
$dotenv->load();

$dotenv->required($boolean)->isBoolean();

$this->assertTrue(true); // anything wrong - an exception will be thrown
}

/**
* List of non-boolean values in fixtures/env/booleans.env
*
* @return array
*/
public function invalidBooleanValuesDataProvider()
{
return array(
array('INVALID_SOMETHING'),
array('INVALID_EMPTY'),
array('INVALID_EMPTY_STRING'),
array('INVALID_NULL'),
array('INVALID_NUMBER_POSITIVE'),
array('INVALID_NUMBER_NEGATIVE'),
array('INVALID_MINUS'),
array('INVALID_TILDA'),
array('INVALID_EXCLAMATION'),
);
}

/**
* @dataProvider invalidBooleanValuesDataProvider
* @expectedException Dotenv\Exception\ValidationException
*/
public function testCanInvalidateNonBooleans($boolean)
{
$dotenv = new Dotenv($this->fixturesFolder, 'booleans.env');
$dotenv->load();

$dotenv->required($boolean)->isBoolean();
}
}
33 changes: 33 additions & 0 deletions tests/fixtures/env/booleans.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
VALID_EXPLICIT_LOWERCASE_TRUE=true
VALID_EXPLICIT_LOWERCASE_FALSE=false
VALID_EXPLICIT_UPPERCASE_TRUE=TRUE
VALID_EXPLICIT_UPPERCASE_FALSE=FALSE
VALID_EXPLICIT_MIXEDCASE_TRUE=True
VALID_EXPLICIT_MIXEDCASE_FALSE=False

VALID_NUMBER_TRUE=1
VALID_NUMBER_FALSE=0

VALID_ONOFF_LOWERCASE_TRUE=on
VALID_ONOFF_LOWERCASE_FALSE=off
VALID_ONOFF_UPPERCASE_TRUE=ON
VALID_ONOFF_UPPERCASE_FALSE=OFF
VALID_ONOFF_MIXEDCASE_TRUE=On
VALID_ONOFF_MIXEDCASE_FALSE=Off

VALID_YESNO_LOWERCASE_TRUE=yes
VALID_YESNO_LOWERCASE_FALSE=no
VALID_YESNO_UPPERCASE_TRUE=YES
VALID_YESNO_UPPERCASE_FALSE=NO
VALID_YESNO_MIXEDCASE_TRUE=Yes
VALID_YESNO_MIXEDCASE_FALSE=No

INVALID_SOMETHING=something
INVALID_EMPTY=
INVALID_EMPTY_STRING=""
INVALID_NULL=null
INVALID_NUMBER_POSITIVE=2
INVALID_NUMBER_NEGATIVE=-2
INVALID_MINUS=-
INVALID_TILDA=~
INVALID_EXCLAMATION=!