ValidatorTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. namespace Dotenv\Tests;
  3. use Dotenv\Dotenv;
  4. use PHPUnit\Framework\TestCase;
  5. class ValidatorTest extends TestCase
  6. {
  7. /**
  8. * @var string
  9. */
  10. private $fixturesFolder;
  11. /**
  12. * @before
  13. */
  14. public function setUpTest()
  15. {
  16. $this->fixturesFolder = dirname(__DIR__).'/fixtures/env';
  17. }
  18. public function testDotenvRequiredStringEnvironmentVars()
  19. {
  20. $dotenv = Dotenv::createImmutable($this->fixturesFolder);
  21. $dotenv->load();
  22. $dotenv->required('FOO');
  23. self::assertTrue(true);
  24. }
  25. public function testDotenvAllowedValues()
  26. {
  27. $dotenv = Dotenv::createImmutable($this->fixturesFolder);
  28. $dotenv->load();
  29. $dotenv->required('FOO')->allowedValues(['bar', 'baz']);
  30. self::assertTrue(true);
  31. }
  32. public function testDotenvAllowedValuesIfPresent()
  33. {
  34. $dotenv = Dotenv::createImmutable($this->fixturesFolder);
  35. $dotenv->load();
  36. $dotenv->ifPresent('FOO')->allowedValues(['bar', 'baz']);
  37. self::assertTrue(true);
  38. }
  39. public function testDotenvAllowedValuesIfNotPresent()
  40. {
  41. $dotenv = Dotenv::createImmutable($this->fixturesFolder);
  42. $dotenv->load();
  43. $dotenv->ifPresent('FOOQWERTYOOOOOO')->allowedValues(['bar', 'baz']);
  44. self::assertTrue(true);
  45. }
  46. /**
  47. * @expectedException \Dotenv\Exception\ValidationException
  48. * @expectedExceptionMessage One or more environment variables failed assertions: FOO is not one of [buzz, buz].
  49. */
  50. public function testDotenvProhibitedValues()
  51. {
  52. $dotenv = Dotenv::createImmutable($this->fixturesFolder);
  53. $dotenv->load();
  54. $dotenv->required('FOO')->allowedValues(['buzz', 'buz']);
  55. }
  56. /**
  57. * @expectedException \Dotenv\Exception\ValidationException
  58. * @expectedExceptionMessage One or more environment variables failed assertions: FOO is not one of [buzz, buz].
  59. */
  60. public function testDotenvProhibitedValuesIfPresent()
  61. {
  62. $dotenv = Dotenv::createImmutable($this->fixturesFolder);
  63. $dotenv->load();
  64. $dotenv->ifPresent('FOO')->allowedValues(['buzz', 'buz']);
  65. }
  66. /**
  67. * @expectedException \Dotenv\Exception\ValidationException
  68. * @expectedExceptionMessage One or more environment variables failed assertions: FOOX is missing, NOPE is missing.
  69. */
  70. public function testDotenvRequiredThrowsRuntimeException()
  71. {
  72. $dotenv = Dotenv::createImmutable($this->fixturesFolder);
  73. $dotenv->load();
  74. self::assertFalse(getenv('FOOX'));
  75. self::assertFalse(getenv('NOPE'));
  76. $dotenv->required(['FOOX', 'NOPE']);
  77. }
  78. public function testDotenvRequiredArrayEnvironmentVars()
  79. {
  80. $dotenv = Dotenv::createImmutable($this->fixturesFolder);
  81. $dotenv->load();
  82. $dotenv->required(['FOO', 'BAR']);
  83. self::assertTrue(true);
  84. }
  85. public function testDotenvAssertions()
  86. {
  87. $dotenv = Dotenv::createImmutable($this->fixturesFolder, 'assertions.env');
  88. $dotenv->load();
  89. self::assertSame('val1', getenv('ASSERTVAR1'));
  90. self::assertEmpty(getenv('ASSERTVAR2'));
  91. self::assertSame('val3 ', getenv('ASSERTVAR3'));
  92. self::assertSame('0', getenv('ASSERTVAR4'));
  93. self::assertSame('#foo', getenv('ASSERTVAR5'));
  94. self::assertSame("val1\nval2", getenv('ASSERTVAR6'));
  95. self::assertSame("\nval3", getenv('ASSERTVAR7'));
  96. self::assertSame("val3\n", getenv('ASSERTVAR8'));
  97. $dotenv->required([
  98. 'ASSERTVAR1',
  99. 'ASSERTVAR2',
  100. 'ASSERTVAR3',
  101. 'ASSERTVAR4',
  102. 'ASSERTVAR5',
  103. 'ASSERTVAR6',
  104. 'ASSERTVAR7',
  105. 'ASSERTVAR8',
  106. 'ASSERTVAR9',
  107. ]);
  108. $dotenv->required([
  109. 'ASSERTVAR1',
  110. 'ASSERTVAR3',
  111. 'ASSERTVAR4',
  112. 'ASSERTVAR5',
  113. 'ASSERTVAR6',
  114. 'ASSERTVAR7',
  115. 'ASSERTVAR8',
  116. ])->notEmpty();
  117. $dotenv->required([
  118. 'ASSERTVAR1',
  119. 'ASSERTVAR4',
  120. 'ASSERTVAR5',
  121. ])->notEmpty()->allowedValues(['0', 'val1', '#foo']);
  122. }
  123. /**
  124. * @expectedException \Dotenv\Exception\ValidationException
  125. * @expectedExceptionMessage One or more environment variables failed assertions: ASSERTVAR2 is empty.
  126. */
  127. public function testDotenvEmptyThrowsRuntimeException()
  128. {
  129. $dotenv = Dotenv::createImmutable($this->fixturesFolder, 'assertions.env');
  130. $dotenv->load();
  131. self::assertEmpty(getenv('ASSERTVAR2'));
  132. $dotenv->required('ASSERTVAR2')->notEmpty();
  133. }
  134. public function testDotenvEmptyWhenNotPresent()
  135. {
  136. $dotenv = Dotenv::createImmutable($this->fixturesFolder, 'assertions.env');
  137. $dotenv->load();
  138. $dotenv->ifPresent('ASSERTVAR2_NO_SUCH_VARIABLE')->notEmpty();
  139. self::assertTrue(true);
  140. }
  141. /**
  142. * @expectedException \Dotenv\Exception\ValidationException
  143. * @expectedExceptionMessage One or more environment variables failed assertions: ASSERTVAR9 is empty.
  144. */
  145. public function testDotenvStringOfSpacesConsideredEmpty()
  146. {
  147. $dotenv = Dotenv::createImmutable($this->fixturesFolder, 'assertions.env');
  148. $dotenv->load();
  149. $dotenv->required('ASSERTVAR9')->notEmpty();
  150. }
  151. /**
  152. * @expectedException \Dotenv\Exception\ValidationException
  153. * @expectedExceptionMessage One or more environment variables failed assertions: foo is missing.
  154. */
  155. public function testDotenvValidateRequiredWithoutLoading()
  156. {
  157. $dotenv = Dotenv::createImmutable($this->fixturesFolder, 'assertions.env');
  158. $dotenv->required('foo');
  159. }
  160. public function testDotenvRequiredCanBeUsedWithoutLoadingFile()
  161. {
  162. putenv('REQUIRED_VAR=1');
  163. $dotenv = Dotenv::createImmutable($this->fixturesFolder);
  164. $dotenv->required('REQUIRED_VAR')->notEmpty();
  165. self::assertTrue(true);
  166. }
  167. /**
  168. * List of valid boolean values in fixtures/env/booleans.env.
  169. *
  170. * @return array
  171. */
  172. public function validBooleanValuesDataProvider()
  173. {
  174. return [
  175. ['VALID_EXPLICIT_LOWERCASE_TRUE'],
  176. ['VALID_EXPLICIT_LOWERCASE_FALSE'],
  177. ['VALID_EXPLICIT_UPPERCASE_TRUE'],
  178. ['VALID_EXPLICIT_UPPERCASE_FALSE'],
  179. ['VALID_EXPLICIT_MIXEDCASE_TRUE'],
  180. ['VALID_EXPLICIT_MIXEDCASE_FALSE'],
  181. ['VALID_NUMBER_TRUE'],
  182. ['VALID_NUMBER_FALSE'],
  183. ['VALID_ONOFF_LOWERCASE_TRUE'],
  184. ['VALID_ONOFF_LOWERCASE_FALSE'],
  185. ['VALID_ONOFF_UPPERCASE_TRUE'],
  186. ['VALID_ONOFF_UPPERCASE_FALSE'],
  187. ['VALID_ONOFF_MIXEDCASE_TRUE'],
  188. ['VALID_ONOFF_MIXEDCASE_FALSE'],
  189. ['VALID_YESNO_LOWERCASE_TRUE'],
  190. ['VALID_YESNO_LOWERCASE_FALSE'],
  191. ['VALID_YESNO_UPPERCASE_TRUE'],
  192. ['VALID_YESNO_UPPERCASE_FALSE'],
  193. ['VALID_YESNO_MIXEDCASE_TRUE'],
  194. ['VALID_YESNO_MIXEDCASE_FALSE'],
  195. ];
  196. }
  197. /**
  198. * @dataProvider validBooleanValuesDataProvider
  199. */
  200. public function testCanValidateBooleans($boolean)
  201. {
  202. $dotenv = Dotenv::createImmutable($this->fixturesFolder, 'booleans.env');
  203. $dotenv->load();
  204. $dotenv->required($boolean)->isBoolean();
  205. self::assertTrue(true);
  206. }
  207. /**
  208. * @dataProvider validBooleanValuesDataProvider
  209. */
  210. public function testCanValidateBooleansIfPresent($boolean)
  211. {
  212. $dotenv = Dotenv::createImmutable($this->fixturesFolder, 'booleans.env');
  213. $dotenv->load();
  214. $dotenv->ifPresent($boolean)->isBoolean();
  215. self::assertTrue(true);
  216. }
  217. /**
  218. * List of non-boolean values in fixtures/env/booleans.env.
  219. *
  220. * @return array
  221. */
  222. public function invalidBooleanValuesDataProvider()
  223. {
  224. return [
  225. ['INVALID_SOMETHING'],
  226. ['INVALID_EMPTY'],
  227. ['INVALID_EMPTY_STRING'],
  228. ['INVALID_NULL'],
  229. ['INVALID_NUMBER_POSITIVE'],
  230. ['INVALID_NUMBER_NEGATIVE'],
  231. ['INVALID_MINUS'],
  232. ['INVALID_TILDA'],
  233. ['INVALID_EXCLAMATION'],
  234. ];
  235. }
  236. /**
  237. * @dataProvider invalidBooleanValuesDataProvider
  238. * @expectedException \Dotenv\Exception\ValidationException
  239. * @expectedExceptionMessage One or more environment variables failed assertions: INVALID_
  240. */
  241. public function testCanInvalidateNonBooleans($boolean)
  242. {
  243. $dotenv = Dotenv::createImmutable($this->fixturesFolder, 'booleans.env');
  244. $dotenv->load();
  245. $dotenv->required($boolean)->isBoolean();
  246. }
  247. /**
  248. * @dataProvider invalidBooleanValuesDataProvider
  249. * @expectedException \Dotenv\Exception\ValidationException
  250. * @expectedExceptionMessage One or more environment variables failed assertions: INVALID_
  251. */
  252. public function testCanInvalidateNonBooleansIfPresent($boolean)
  253. {
  254. $dotenv = Dotenv::createImmutable($this->fixturesFolder, 'booleans.env');
  255. $dotenv->load();
  256. $dotenv->ifPresent($boolean)->isBoolean();
  257. }
  258. /**
  259. * @expectedException \Dotenv\Exception\ValidationException
  260. * @expectedExceptionMessage One or more environment variables failed assertions: VAR_DOES_NOT_EXIST_234782462764
  261. */
  262. public function testCanInvalidateBooleanNonExist()
  263. {
  264. $dotenv = Dotenv::createImmutable($this->fixturesFolder, 'booleans.env');
  265. $dotenv->load();
  266. $dotenv->required(['VAR_DOES_NOT_EXIST_234782462764'])->isBoolean();
  267. }
  268. public function testIfPresentBooleanNonExist()
  269. {
  270. $dotenv = Dotenv::createImmutable($this->fixturesFolder, 'booleans.env');
  271. $dotenv->load();
  272. $dotenv->ifPresent(['VAR_DOES_NOT_EXIST_234782462764'])->isBoolean();
  273. self::assertTrue(true);
  274. }
  275. /**
  276. * List of valid integer values in fixtures/env/integers.env.
  277. *
  278. * @return array
  279. */
  280. public function validIntegerValuesDataProvider()
  281. {
  282. return [
  283. ['VALID_ZERO'],
  284. ['VALID_ONE'],
  285. ['VALID_TWO'],
  286. ['VALID_LARGE'],
  287. ['VALID_HUGE'],
  288. ];
  289. }
  290. /**
  291. * @dataProvider validIntegerValuesDataProvider
  292. */
  293. public function testCanValidateIntegers($integer)
  294. {
  295. $dotenv = Dotenv::createImmutable($this->fixturesFolder, 'integers.env');
  296. $dotenv->load();
  297. $dotenv->required($integer)->isInteger();
  298. self::assertTrue(true);
  299. }
  300. /**
  301. * @dataProvider validIntegerValuesDataProvider
  302. */
  303. public function testCanValidateIntegersIfPresent($integer)
  304. {
  305. $dotenv = Dotenv::createImmutable($this->fixturesFolder, 'integers.env');
  306. $dotenv->load();
  307. $dotenv->ifPresent($integer)->isInteger();
  308. self::assertTrue(true);
  309. }
  310. /**
  311. * List of non-integer values in fixtures/env/integers.env.
  312. *
  313. * @return array
  314. */
  315. public function invalidIntegerValuesDataProvider()
  316. {
  317. return [
  318. ['INVALID_SOMETHING'],
  319. ['INVALID_EMPTY'],
  320. ['INVALID_EMPTY_STRING'],
  321. ['INVALID_NULL'],
  322. ['INVALID_NEGATIVE'],
  323. ['INVALID_MINUS'],
  324. ['INVALID_TILDA'],
  325. ['INVALID_EXCLAMATION'],
  326. ['INVALID_SPACES'],
  327. ['INVALID_COMMAS'],
  328. ];
  329. }
  330. /**
  331. * @dataProvider invalidIntegerValuesDataProvider
  332. * @expectedException \Dotenv\Exception\ValidationException
  333. * @expectedExceptionMessage One or more environment variables failed assertions: INVALID_
  334. */
  335. public function testCanInvalidateNonIntegers($integer)
  336. {
  337. $dotenv = Dotenv::createImmutable($this->fixturesFolder, 'integers.env');
  338. $dotenv->load();
  339. $dotenv->required($integer)->isInteger();
  340. }
  341. /**
  342. * @dataProvider invalidIntegerValuesDataProvider
  343. * @expectedException \Dotenv\Exception\ValidationException
  344. * @expectedExceptionMessage One or more environment variables failed assertions: INVALID_
  345. */
  346. public function testCanInvalidateNonIntegersIfExist($integer)
  347. {
  348. $dotenv = Dotenv::createImmutable($this->fixturesFolder, 'integers.env');
  349. $dotenv->load();
  350. $dotenv->ifPresent($integer)->isInteger();
  351. }
  352. /**
  353. * @expectedException \Dotenv\Exception\ValidationException
  354. * @expectedExceptionMessage One or more environment variables failed assertions: VAR_DOES_NOT_EXIST_234782462764
  355. */
  356. public function testCanInvalidateIntegerNonExist()
  357. {
  358. $dotenv = Dotenv::createImmutable($this->fixturesFolder, 'integers.env');
  359. $dotenv->load();
  360. $dotenv->required(['VAR_DOES_NOT_EXIST_234782462764'])->isInteger();
  361. }
  362. public function testIfPresentIntegerNonExist()
  363. {
  364. $dotenv = Dotenv::createImmutable($this->fixturesFolder, 'integers.env');
  365. $dotenv->load();
  366. $dotenv->ifPresent(['VAR_DOES_NOT_EXIST_234782462764'])->isInteger();
  367. self::assertTrue(true);
  368. }
  369. public function testDotenvRegexMatchPass()
  370. {
  371. $dotenv = Dotenv::createImmutable($this->fixturesFolder);
  372. $dotenv->load();
  373. $dotenv->required('FOO')->allowedRegexValues('([[:lower:]]{3})');
  374. self::assertTrue(true);
  375. }
  376. /**
  377. * @expectedException \Dotenv\Exception\ValidationException
  378. * @expectedExceptionMessage One or more environment variables failed assertions: FOO does not match "/^([[:lower:]]{1})$/".
  379. */
  380. public function testDotenvRegexMatchFail()
  381. {
  382. $dotenv = Dotenv::createImmutable($this->fixturesFolder);
  383. $dotenv->load();
  384. $dotenv->required('FOO')->allowedRegexValues('/^([[:lower:]]{1})$/');
  385. }
  386. /**
  387. * @expectedException \Dotenv\Exception\ValidationException
  388. * @expectedExceptionMessage One or more environment variables failed assertions: FOO does not match "/([[:lower:]{1{".
  389. */
  390. public function testDotenvRegexMatchError()
  391. {
  392. $dotenv = Dotenv::createImmutable($this->fixturesFolder);
  393. $dotenv->load();
  394. $dotenv->required('FOO')->allowedRegexValues('/([[:lower:]{1{');
  395. }
  396. public function testDotenvRegexMatchNotPresent()
  397. {
  398. $dotenv = Dotenv::createImmutable($this->fixturesFolder);
  399. $dotenv->load();
  400. $dotenv->ifPresent('FOOOOOOOOOOO')->allowedRegexValues('([[:lower:]]{3})');
  401. self::assertTrue(true);
  402. }
  403. }