<commit_before><?php

namespace CodeIgniter\Config;

use org\bovigo\vfs\vfsStream;

/**
 * @backupGlobals enabled
 */
class DotEnvTest extends \CodeIgniter\Test\CIUnitTestCase
{

	protected $fixturesFolder;

	//--------------------------------------------------------------------

	protected function setUp(): void
	{
		parent::setUp();

		$this->root           = vfsStream::setup();
		$this->fixturesFolder = $this->root->url();
		$this->path           = TESTPATH . 'system/Config/fixtures';
		vfsStream::copyFromFileSystem($this->path, $this->root);

		$file = 'unreadable.env';
		$path = rtrim($this->fixturesFolder, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $file;
		chmod($path, 0644);
	}

	protected function tearDown(): void
	{
		parent::tearDown();

		$this->root = null;
	}

	//--------------------------------------------------------------------

	public function testReturnsFalseIfCannotFindFile()
	{
		$dotenv = new DotEnv($this->fixturesFolder, 'bogus');
		$this->assertFalse($dotenv->load());
	}

	//--------------------------------------------------------------------

	public function testLoadsVars()
	{
		$dotenv = new DotEnv($this->fixturesFolder);
		$dotenv->load();
		$this->assertEquals('bar', getenv('FOO'));
		$this->assertEquals('baz', getenv('BAR'));
		$this->assertEquals('with spaces', getenv('SPACED'));
		$this->assertEquals('', getenv('NULL'));
	}

	//--------------------------------------------------------------------

	public function testLoadsHex2Bin()
	{
		$dotenv = new DotEnv($this->fixturesFolder, 'encryption.env');
		$dotenv->load();

		$value = $_ENV['encryption.key'] ?? getenv('encryption.key');

		$this->assertTrue(! empty($value));
		$this->assertEquals('f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6', bin2hex($value));
		$this->assertEquals('hex2bin:f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6', getenv('different.key'));
		$this->assertEquals('OpenSSL', getenv('encryption.driver'));
	}

	//--------------------------------------------------------------------

	public function testLoadsBase64()
	{
		$dotenv = new DotEnv($this->fixturesFolder, 'base64encryption.env');
		$dotenv->load();

		$value = $_ENV['encryption.key'] ?? getenv('encryption.key');

		$this->assertFalse(empty($value));
		$this->assertEquals('L40bKo6b8Nu541LeVeZ1i5RXfGgnkar42CPTfukhGhw=', base64_encode($value));
		$this->assertEquals('OpenSSL', getenv('encryption.driver'));
	}

	//--------------------------------------------------------------------

	public function testLoadsNoneStringFiles()
	{
		$dotenv = new DotEnv($this->fixturesFolder, 2);
		$dotenv->load();
		$this->assertEquals('bar', getenv('FOO'));<commit_msg>Use putenv first and refuse use of $_ENV<commit_after><?php

namespace CodeIgniter\Config;

use org\bovigo\vfs\vfsStream;

/**
 * @backupGlobals enabled
 */
class DotEnvTest extends \CodeIgniter\Test\CIUnitTestCase
{

	protected $fixturesFolder;

	//--------------------------------------------------------------------

	protected function setUp(): void
	{
		parent::setUp();

		$this->root           = vfsStream::setup();
		$this->fixturesFolder = $this->root->url();
		$this->path           = TESTPATH . 'system/Config/fixtures';
		vfsStream::copyFromFileSystem($this->path, $this->root);

		$file = 'unreadable.env';
		$path = rtrim($this->fixturesFolder, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $file;
		chmod($path, 0644);
	}

	protected function tearDown(): void
	{
		parent::tearDown();

		$this->root = null;
	}

	//--------------------------------------------------------------------

	public function testReturnsFalseIfCannotFindFile()
	{
		$dotenv = new DotEnv($this->fixturesFolder, 'bogus');
		$this->assertFalse($dotenv->load());
	}

	//--------------------------------------------------------------------

	public function testLoadsVars()
	{
		$dotenv = new DotEnv($this->fixturesFolder);
		$dotenv->load();
		$this->assertEquals('bar', getenv('FOO'));
		$this->assertEquals('baz', getenv('BAR'));
		$this->assertEquals('with spaces', getenv('SPACED'));
		$this->assertEquals('', getenv('NULL'));
	}

	//--------------------------------------------------------------------

	public function testLoadsHex2Bin()
	{
		putenv('encryption.key');

		$dotenv = new DotEnv($this->fixturesFolder, 'encryption.env');
		$dotenv->load();

		$value = getenv('encryption.key');

		$this->assertTrue(! empty($value));
		$this->assertEquals('f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6', bin2hex($value));
		$this->assertEquals('hex2bin:f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6', getenv('different.key'));
		$this->assertEquals('OpenSSL', getenv('encryption.driver'));
	}

	//--------------------------------------------------------------------

	public function testLoadsBase64()
	{
		putenv('encryption.key');

		$dotenv = new DotEnv($this->fixturesFolder, 'base64encryption.env');
		$dotenv->load();

		$value = getenv('encryption.key');

		$this->assertFalse(empty($value));
		$this->assertEquals('L40bKo6b8Nu541LeVeZ1i5RXfGgnkar42CPTfukhGhw=', base64_encode($value));
		$this->assertEquals('OpenSSL', getenv('encryption.driver'));
	}

	//--------------------------------------------------------------------

	public function testLoadsNoneStringFiles()
	{
		$dotenv = new DotEnv($this->fixturesFolder, 2);
		$dotenv->load();
		$this->assertEquals('bar', getenv('FOO'));
{'fraction_non_alphanumeric': 0.3042675041566599, 'fraction_numerical': 0.03583964529835581, 'mean_word_length': 2.4929032258064514, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4250426', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1669, 'n_words': 230}