setPersistKeys(['1/', 2]); $result = ConfigGenerator::getConfigFile($cf); self::assertStringContainsString("getMethod('getVarExport'); if (PHP_VERSION_ID < 80100) { $method->setAccessible(true); } self::assertSame('$cfg[\'var_name\'] = 1;' . "\n", $method->invoke(null, 'var_name', 1, "\n")); self::assertSame('$cfg[\'var_name\'] = array (' . "\n);\n", $method->invoke(null, 'var_name', [], "\n")); self::assertSame('$cfg[\'var_name\'] = [1, 2, 3];' . "\n", $method->invoke( null, 'var_name', [ 1, 2, 3, ], "\n" )); self::assertSame('$cfg[\'var_name\'][\'1a\'] = \'foo\';' . "\n" . '$cfg[\'var_name\'][\'b\'] = \'bar\';' . "\n", $method->invoke( null, 'var_name', [ '1a' => 'foo', 'b' => 'bar', ], "\n" )); } public function testGetVarExportForBlowfishSecret(): void { $reflection = new ReflectionClass(ConfigGenerator::class); $method = $reflection->getMethod('getVarExport'); if (PHP_VERSION_ID < 80100) { $method->setAccessible(true); } self::assertSame( '$cfg[\'blowfish_secret\'] = \sodium_hex2bin(\'' . '6161616161616161616161616161616161616161616161616161616161616161\');' . "\n", $method->invoke(null, 'blowfish_secret', str_repeat('a', SODIUM_CRYPTO_SECRETBOX_KEYBYTES), "\n") ); /** @var string $actual */ $actual = $method->invoke(null, 'blowfish_secret', 'invalid secret', "\n"); self::assertStringStartsWith('$cfg[\'blowfish_secret\'] = \sodium_hex2bin(\'', $actual); self::assertStringEndsWith('\');' . "\n", $actual); $pieces = explode('\'', $actual); self::assertCount(5, $pieces); $binaryString = hex2bin($pieces[3]); self::assertIsString($binaryString); self::assertSame(SODIUM_CRYPTO_SECRETBOX_KEYBYTES, mb_strlen($binaryString, '8bit')); } /** * Test for ConfigGenerator::isZeroBasedArray */ public function testIsZeroBasedArray(): void { $reflection = new ReflectionClass(ConfigGenerator::class); $method = $reflection->getMethod('isZeroBasedArray'); if (PHP_VERSION_ID < 80100) { $method->setAccessible(true); } self::assertFalse($method->invoke( null, [ 'a' => 1, 'b' => 2, ] )); self::assertFalse($method->invoke( null, [ 0 => 1, 1 => 2, 3 => 3, ] )); self::assertTrue($method->invoke( null, [] )); self::assertTrue($method->invoke( null, [ 1, 2, 3, ] )); } /** * Test for ConfigGenerator::exportZeroBasedArray */ public function testExportZeroBasedArray(): void { $reflection = new ReflectionClass(ConfigGenerator::class); $method = $reflection->getMethod('exportZeroBasedArray'); if (PHP_VERSION_ID < 80100) { $method->setAccessible(true); } $arr = [ 1, 2, 3, 4, ]; $result = $method->invoke(null, $arr, "\n"); self::assertSame('[1, 2, 3, 4]', $result); $arr = [ 1, 2, 3, 4, 7, 'foo', ]; $result = $method->invoke(null, $arr, "\n"); self::assertSame('[' . "\n" . ' 1,' . "\n" . ' 2,' . "\n" . ' 3,' . "\n" . ' 4,' . "\n" . ' 7,' . "\n" . ' \'foo\']', $result); } }