This commit is contained in:
2022-01-13 18:41:03 +01:00
commit 0fb9f639da
159 changed files with 13183 additions and 0 deletions

View File

@@ -0,0 +1,120 @@
<?php declare(strict_types = 1);
namespace Tests\Toolkit;
use RuntimeException;
use Tester\Environment as TEnvironment;
use Tester\Helpers as THelpers;
class Environment
{
public const TEMP_DIR = 'TEMP_DIR';
public const CACHE_DIR = 'CACHE_DIR';
/**
* Magic setup method
*/
public static function setup(string $dir): void
{
self::setupTester();
self::setupTimezone();
self::setupVariables($dir);
self::setupGlobalVariables();
}
/**
* Configure environment
*/
public static function setupTester(): void
{
TEnvironment::setup();
}
/**
* Configure timezone
*/
public static function setupTimezone(string $timezone = 'Europe/Prague'): void
{
date_default_timezone_set($timezone);
}
/**
* Configure variables
*/
public static function setupVariables(string $rootDir): void
{
if (!is_dir($rootDir)) {
die(sprintf('Provide existing folder, "%s" does not exist.', $rootDir));
}
$tmpDir = realpath($rootDir) . '/tmp';
// Temp, cache directories
define('TEMP_DIR', $tmpDir . '/tests/' . getmypid() . '/' . md5(uniqid((string) microtime(true), true) . lcg_value() . mt_rand(0, 20) . microtime()));
define('CACHE_DIR', $tmpDir . '/cache');
ini_set('session.save_path', $tmpDir . '/sessions');
// Create folders
self::purge($tmpDir);
}
/**
* @param mixed $value
*/
public static function setupVariable(string $variable, $value): void
{
define($variable, $value);
}
/**
* Configure global variables
*/
public static function setupGlobalVariables(): void
{
$_SERVER = array_intersect_key($_SERVER, array_flip([
'PHP_SELF',
'SCRIPT_NAME',
'SERVER_ADDR',
'SERVER_SOFTWARE',
'HTTP_HOST',
'DOCUMENT_ROOT',
'OS',
'argc',
'argv',
]));
$_SERVER['REQUEST_TIME'] = 1234567890;
$_ENV = $_GET = $_POST = [];
}
public static function mkdir(string $dir, int $mode = 0777, bool $recursive = true): void
{
if (is_dir($dir) === false && @mkdir($dir, $mode, $recursive) === false) {
clearstatcache(true, $dir);
$error = error_get_last();
if (is_dir($dir) === false && !file_exists($dir) === false) {
throw new RuntimeException(sprintf("Unable to create directory '%s'. " . ($error['message'] ?? null), $dir));
}
}
}
public static function rmdir(string $dir): void
{
if (!is_dir($dir)) {
return;
}
self::purge($dir);
@rmdir($dir);
}
private static function purge(string $dir): void
{
if (!is_dir($dir)) {
self::mkdir($dir);
}
THelpers::purge($dir);
}
}

View File

@@ -0,0 +1,38 @@
<?php declare(strict_types = 1);
namespace Tests\Toolkit\Nette;
use Nette\Security\IIdentity;
use Nette\Security\UserStorage;
final class DummyUserStorage implements UserStorage
{
/** @var IIdentity|NULL */
private $identity;
public function saveAuthentication(IIdentity $identity): void
{
$this->identity = $identity;
}
public function clearAuthentication(bool $clearIdentity): void
{
$this->identity = null;
}
public function getState(): array
{
return [$this->identity !== null, $this->identity, null];
}
public function setExpiration(?string $expire, bool $clearIdentity): void
{
}
public function setNamespace(string $namespace): self
{
return $this;
}
}

View File

@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);
namespace Tests\Toolkit\TestCase;
use Nette\DI\Container;
abstract class BaseContainerTestCase extends BaseTestCase
{
/** @var Container */
protected $container;
public function __construct(Container $container)
{
$this->container = $container;
}
protected function getService(string $class): object
{
if (strpos($class, '\\')) {
/** @phpstan-var class-string<mixed> $class */
return $this->container->getByType($class);
} else {
return $this->container->getService($class);
}
}
}

View File

@@ -0,0 +1,10 @@
<?php declare(strict_types = 1);
namespace Tests\Toolkit\TestCase;
use Tester\TestCase;
abstract class BaseTestCase extends TestCase
{
}