<commit_before><?php

namespace CodeIgniter\Commands;

use CodeIgniter\CLI\CommandRunner;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Services;

class GenerateKeyTest extends CIUnitTestCase
{
	private $streamFilter;
	private $envPath;
	private $backupEnvPath;

	/**
	 * @var CommandRunner
	 */
	private $runner;

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

		CITestStreamFilter::$buffer = '';
		$this->streamFilter         = stream_filter_append(STDOUT, 'CITestStreamFilter');
		$this->streamFilter         = stream_filter_append(STDERR, 'CITestStreamFilter');

		$this->runner = new CommandRunner();
		$this->runner->initController(Services::request(), Services::response(), Services::logger());

		$this->envPath       = ROOTPATH . '.env';
		$this->backupEnvPath = ROOTPATH . '.env.backup';

		if (file_exists($this->envPath))
		{
			rename($this->envPath, $this->backupEnvPath);
		}
	}

	protected function tearDown(): void
	{
		stream_filter_remove($this->streamFilter);

		if (file_exists($this->envPath))
		{
			unlink($this->envPath);
		}

		rename($this->backupEnvPath, $this->envPath);
	}

	/**
	 * Gets buffer contents then releases it.
	 *
	 * @return string
	 */
	protected function getBuffer(): string
	{
		$result                     = CITestStreamFilter::$buffer;
		CITestStreamFilter::$buffer = '';

		return $result;
	}

	public function testGenerateKeyShowsEncodedKey()
	{
		$this->runner->index(['key:generate', '-encoding' => 'hex', '-show' => null]);
		$this->assertStringContainsString('hex2bin:', $this->getBuffer());

		$this->runner->index(['key:generate', '-encoding' => 'base64', '-show' => null]);
		$this->assertStringContainsString('base64:', $this->getBuffer());
	}

	public function testGenerateKeyCreatesNewKey()
	{
		// use the 'force' option to bypass CLI::prompt
		$this->runner->index(['key:generate', '-encoding' => 'hex', '-force' => null]);
		$this->assertStringContainsString('successfully set.', $this->getBuffer());
		$this->assertStringContainsString(env('encryption.key'), file_get_contents($this->envPath));
	}
}<commit_msg>Update GenerateKeyTest<commit_after><?php

namespace CodeIgniter\Commands;

use CodeIgniter\CLI\CommandRunner;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Services;

class GenerateKeyTest extends CIUnitTestCase
{
	private $streamFilter;
	private $envPath;
	private $backupEnvPath;

	/**
	 * @var CommandRunner
	 */
	private $runner;

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

		CITestStreamFilter::$buffer = '';
		$this->streamFilter         = stream_filter_append(STDOUT, 'CITestStreamFilter');
		$this->streamFilter         = stream_filter_append(STDERR, 'CITestStreamFilter');

		$this->runner = new CommandRunner();
		$this->runner->initController(Services::request(), Services::response(), Services::logger());

		$this->envPath       = ROOTPATH . '.env';
		$this->backupEnvPath = ROOTPATH . '.env.backup';

		if (file_exists($this->envPath))
		{
			rename($this->envPath, $this->backupEnvPath);
		}
	}

	protected function tearDown(): void
	{
		stream_filter_remove($this->streamFilter);

		if (file_exists($this->envPath))
		{
			unlink($this->envPath);
			rename($this->backupEnvPath, $this->envPath);
		}

		putenv('encryption.key');
		unset($_ENV['encryption.key'], $_SERVER['encryption.key']);
	}

	/**
	 * Gets buffer contents then releases it.
	 *
	 * @return string
	 */
	protected function getBuffer(): string
	{
		$result                     = CITestStreamFilter::$buffer;
		CITestStreamFilter::$buffer = '';

		return $result;
	}

	public function testGenerateKeyShowsEncodedKey()
	{
		$this->runner->index(['key:generate', '-encoding' => 'hex', '-show' => null]);
		$this->assertStringContainsString('hex2bin:', $this->getBuffer());

		$this->runner->index(['key:generate', '-encoding' => 'base64', '-show' => null]);
		$this->assertStringContainsString('base64:', $this->getBuffer());
	}

	/**
	 * @runInSeparateProcess
	 * @preserveGlobalState  disabled
	 */
	public function testGenerateKeyCreatesNewKey()
	{
		// use the 'force' option to bypass CLI::prompt
		$this->runner->index(['key:generate', '-encoding' => 'hex', '-force' => null]);
		$this->assertStringContainsString('successfully set.', $this->getBuffer());
		$this->assertStringContainsString(env('encryption.key'), file_get_contents($this->envPath));
	}
}
{'fraction_non_alphanumeric': 0.17038037362142697, 'fraction_numerical': 0.0022507314877335135, 'mean_word_length': 4.786458333333333, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 73, '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': '3301199', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1375, 'n_words': 241}