Skip to content

Commit 3b2f561

Browse files
Implemented ifPresent validation
1 parent 9092874 commit 3b2f561

File tree

3 files changed

+134
-12
lines changed

3 files changed

+134
-12
lines changed

src/Dotenv.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,18 @@ public function required($variables)
135135
return new Validator((array) $variables, $this->loader);
136136
}
137137

138+
/**
139+
* Returns a new validator object that won't check if the specified variables exist.
140+
*
141+
* @param string|string[] $variables
142+
*
143+
* @return \Dotenv\Validator
144+
*/
145+
public function ifPresent($variables)
146+
{
147+
return new Validator((array) $variables, $this->loader, false);
148+
}
149+
138150
/**
139151
* Get the list of environment variables declared inside the 'env' file.
140152
*

src/Validator.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,26 @@ class Validator
3030
*
3131
* @param string[] $variables
3232
* @param \Dotenv\Loader $loader
33+
* @param bool $required
3334
*
3435
* @throws \Dotenv\Exception\ValidationException
3536
*
3637
* @return void
3738
*/
38-
public function __construct(array $variables, Loader $loader)
39+
public function __construct(array $variables, Loader $loader, $required = true)
3940
{
4041
$this->variables = $variables;
4142
$this->loader = $loader;
4243

43-
$this->assertCallback(
44-
function ($value) {
45-
return $value !== null;
46-
},
47-
'is missing'
48-
);
44+
if ($required) {
45+
$this->assertCallback(
46+
function ($value) {
47+
return $value !== null;
48+
},
49+
'is missing'
50+
);
51+
}
52+
4953
}
5054

5155
/**
@@ -59,7 +63,11 @@ public function notEmpty()
5963
{
6064
return $this->assertCallback(
6165
function ($value) {
62-
return $value !== null && strlen(trim($value)) > 0;
66+
if ($value === null) {
67+
return true;
68+
}
69+
70+
return strlen(trim($value)) > 0;
6371
},
6472
'is empty'
6573
);
@@ -76,7 +84,11 @@ public function isInteger()
7684
{
7785
return $this->assertCallback(
7886
function ($value) {
79-
return $value !== null && ctype_digit($value);
87+
if ($value === null) {
88+
return true;
89+
}
90+
91+
return ctype_digit($value);
8092
},
8193
'is not an integer'
8294
);
@@ -93,7 +105,11 @@ public function isBoolean()
93105
{
94106
return $this->assertCallback(
95107
function ($value) {
96-
if ($value === null || $value === '') {
108+
if ($value === null) {
109+
return true;
110+
}
111+
112+
if ($value === '') {
97113
return false;
98114
}
99115

tests/Dotenv/ValidatorTest.php

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public function testDotenvAllowedValues()
3131
$this->assertTrue(true);
3232
}
3333

34+
public function testDotenvAllowedValuesIfPresent()
35+
{
36+
$dotenv = Dotenv::create($this->fixturesFolder);
37+
$dotenv->load();
38+
$dotenv->ifPresent('FOO')->allowedValues(['bar', 'baz']);
39+
$this->assertTrue(true);
40+
}
41+
3442
/**
3543
* @expectedException \Dotenv\Exception\ValidationException
3644
* @expectedExceptionMessage One or more environment variables failed assertions: FOO is not one of [buzz, buz].
@@ -42,6 +50,17 @@ public function testDotenvProhibitedValues()
4250
$dotenv->required('FOO')->allowedValues(['buzz', 'buz']);
4351
}
4452

53+
/**
54+
* @expectedException \Dotenv\Exception\ValidationException
55+
* @expectedExceptionMessage One or more environment variables failed assertions: FOO is not one of [buzz, buz].
56+
*/
57+
public function testDotenvProhibitedValuesIfPresent()
58+
{
59+
$dotenv = Dotenv::create($this->fixturesFolder);
60+
$dotenv->load();
61+
$dotenv->ifPresent('FOO')->allowedValues(['buzz', 'buz']);
62+
}
63+
4564
/**
4665
* @expectedException \Dotenv\Exception\ValidationException
4766
* @expectedExceptionMessage One or more environment variables failed assertions: FOOX is missing, NOPE is missing.
@@ -118,6 +137,15 @@ public function testDotenvEmptyThrowsRuntimeException()
118137
$dotenv->required('ASSERTVAR2')->notEmpty();
119138
}
120139

140+
public function testDotenvEmptyWhenNotPresent()
141+
{
142+
$dotenv = Dotenv::create($this->fixturesFolder, 'assertions.env');
143+
$dotenv->load();
144+
145+
$dotenv->ifPresent('ASSERTVAR2_NO_SUCH_VARIABLE')->notEmpty();
146+
$this->assertTrue(true);
147+
}
148+
121149
/**
122150
* @expectedException \Dotenv\Exception\ValidationException
123151
* @expectedExceptionMessage One or more environment variables failed assertions: ASSERTVAR9 is empty.
@@ -190,8 +218,19 @@ public function testCanValidateBooleans($boolean)
190218
$dotenv->load();
191219

192220
$dotenv->required($boolean)->isBoolean();
221+
$this->assertTrue(true);
222+
}
193223

194-
$this->assertTrue(true); // anything wrong - an exception will be thrown
224+
/**
225+
* @dataProvider validBooleanValuesDataProvider
226+
*/
227+
public function testCanValidateBooleansIfPresent($boolean)
228+
{
229+
$dotenv = Dotenv::create($this->fixturesFolder, 'booleans.env');
230+
$dotenv->load();
231+
232+
$dotenv->ifPresent($boolean)->isBoolean();
233+
$this->assertTrue(true);
195234
}
196235

197236
/**
@@ -227,6 +266,19 @@ public function testCanInvalidateNonBooleans($boolean)
227266
$dotenv->required($boolean)->isBoolean();
228267
}
229268

269+
/**
270+
* @dataProvider invalidBooleanValuesDataProvider
271+
* @expectedException \Dotenv\Exception\ValidationException
272+
* @expectedExceptionMessage One or more environment variables failed assertions: INVALID_
273+
*/
274+
public function testCanInvalidateNonBooleansIfPresent($boolean)
275+
{
276+
$dotenv = Dotenv::create($this->fixturesFolder, 'booleans.env');
277+
$dotenv->load();
278+
279+
$dotenv->ifPresent($boolean)->isBoolean();
280+
}
281+
230282
/**
231283
* @expectedException \Dotenv\Exception\ValidationException
232284
* @expectedExceptionMessage One or more environment variables failed assertions: VAR_DOES_NOT_EXIST_234782462764
@@ -239,6 +291,15 @@ public function testCanInvalidateBooleanNonExist()
239291
$dotenv->required(['VAR_DOES_NOT_EXIST_234782462764'])->isBoolean();
240292
}
241293

294+
public function testIfPresentBooleanNonExist()
295+
{
296+
$dotenv = Dotenv::create($this->fixturesFolder, 'booleans.env');
297+
$dotenv->load();
298+
299+
$dotenv->ifPresent(['VAR_DOES_NOT_EXIST_234782462764'])->isBoolean();
300+
$this->assertTrue(true);
301+
}
302+
242303
/**
243304
* List of valid integer values in fixtures/env/integers.env.
244305
*
@@ -265,8 +326,19 @@ public function testCanValidateIntegers($integer)
265326
$dotenv->load();
266327

267328
$dotenv->required($integer)->isInteger();
329+
$this->assertTrue(true);
330+
}
331+
332+
/**
333+
* @dataProvider validIntegerValuesDataProvider
334+
*/
335+
public function testCanValidateIntegersIfPresent($integer)
336+
{
337+
$dotenv = Dotenv::create($this->fixturesFolder, 'integers.env');
338+
$dotenv->load();
268339

269-
$this->assertTrue(true); // anything wrong - an exception will be thrown
340+
$dotenv->ifPresent($integer)->isInteger();
341+
$this->assertTrue(true);
270342
}
271343

272344
/**
@@ -303,6 +375,19 @@ public function testCanInvalidateNonIntegers($integer)
303375
$dotenv->required($integer)->isInteger();
304376
}
305377

378+
/**
379+
* @dataProvider invalidIntegerValuesDataProvider
380+
* @expectedException \Dotenv\Exception\ValidationException
381+
* @expectedExceptionMessage One or more environment variables failed assertions: INVALID_
382+
*/
383+
public function testCanInvalidateNonIntegersIfExist($integer)
384+
{
385+
$dotenv = Dotenv::create($this->fixturesFolder, 'integers.env');
386+
$dotenv->load();
387+
388+
$dotenv->ifPresent($integer)->isInteger();
389+
}
390+
306391
/**
307392
* @expectedException \Dotenv\Exception\ValidationException
308393
* @expectedExceptionMessage One or more environment variables failed assertions: VAR_DOES_NOT_EXIST_234782462764
@@ -314,4 +399,13 @@ public function testCanInvalidateIntegerNonExist()
314399

315400
$dotenv->required(['VAR_DOES_NOT_EXIST_234782462764'])->isInteger();
316401
}
402+
403+
public function testIfPresentIntegerNonExist()
404+
{
405+
$dotenv = Dotenv::create($this->fixturesFolder, 'integers.env');
406+
$dotenv->load();
407+
408+
$dotenv->ifPresent(['VAR_DOES_NOT_EXIST_234782462764'])->isInteger();
409+
$this->assertTrue(true);
410+
}
317411
}

0 commit comments

Comments
 (0)