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

10
tests/.gitignore vendored Normal file
View File

@@ -0,0 +1,10 @@
# Folders - recursive
*.expected
*.actual
# Folders
/tmp
# Files
/*.log
/*.html

View File

@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);
use Nette\Configurator;
$rootDir = __DIR__ . '/..';
// Require base bootstrap
require_once __DIR__ . '/bootstrap.php';
// Create container
$configurator = new Configurator();
$configurator->setTempDirectory(TEMP_DIR);
$configurator->addConfig($rootDir . '/config/env/test.neon');
$configurator->addConfig($rootDir . '/config/local.neon');
// Setup debugMode of course!
$configurator->setDebugMode(true);
// Override to original wwwDir
$configurator->addParameters([
'rootDir' => $rootDir,
'appDir' => $rootDir . '/app',
'wwwDir' => $rootDir . '/www',
]);
// Create test container
return $configurator->createContainer();

12
tests/bootstrap.php Normal file
View File

@@ -0,0 +1,12 @@
<?php declare(strict_types = 1);
use Ninjify\Nunjuck\Environment;
// Check composer && tester
if (@!include __DIR__ . '/../vendor/autoload.php') {
echo 'Install Nette Tester using `composer update --dev`';
exit(1);
}
// Configure test environment
Environment::setup(__DIR__);

View File

@@ -0,0 +1,56 @@
<?php declare(strict_types = 1);
namespace Tests\Integration\Container;
use Nette\Configurator;
use Nette\DI\Container;
use Tester\Assert;
use Throwable;
require_once __DIR__ . '/../../../bootstrap.php';
$rootDir = realpath(__DIR__ . '/../../../..');
$parameters = [
'rootDir' => $rootDir,
'appDir' => $rootDir . '/app',
'wwwDir' => $rootDir . '/www',
'database' => [
'host' => 'fake',
'user' => 'fake',
'password' => 'fake',
'dbname' => 'fake',
],
];
// Production container build
test(function () use ($parameters): void {
$configurator = new Configurator();
$configurator->setTempDirectory(TEMP_DIR);
$configurator->addConfig($parameters['rootDir'] . '/config/env/prod.neon');
$configurator->addParameters($parameters);
try {
$configurator->setDebugMode(false);
$container = $configurator->createContainer();
Assert::type(Container::class, $container);
} catch (Throwable $t) {
Assert::fail(sprintf('Building production container failed. Exception: %s.', $t->getMessage()));
}
});
// Development container build
test(function () use ($parameters): void {
$configurator = new Configurator();
$configurator->setTempDirectory(TEMP_DIR);
$configurator->addConfig($parameters['rootDir'] . '/config/env/dev.neon');
$configurator->addParameters($parameters);
try {
$configurator->setDebugMode(false);
$container = $configurator->createContainer();
Assert::type(Container::class, $container);
} catch (Throwable $t) {
Assert::fail(sprintf('Building development container failed. Exception: %s.', $t->getMessage()));
}
});

View File

@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);
namespace Tests\Integration\Database\Entity;
use App\Model\Database\EntityManager;
use Doctrine\ORM\Tools\SchemaValidator;
use Nette\DI\Container;
use Tester\Assert;
/** @var Container $container */
$container = require_once __DIR__ . '/../../../../bootstrap.container.php';
test(function () use ($container): void {
/** @var EntityManager $em */
$em = $container->getByType(EntityManager::class);
// Validation
$validator = new SchemaValidator($em);
$validations = $validator->validateMapping();
foreach ($validations as $fails) {
foreach ((array) $fails as $fail) {
Assert::fail($fail);
}
}
Assert::count(0, $validations);
});

View File

@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);
namespace Tests\Integration\Database;
use App\Model\Database\EntityManager;
use App\Model\Database\TRepositories;
use Doctrine\ORM\Mapping\ClassMetadata;
use Nette\DI\Container;
use ReflectionClass;
use Tester\Assert;
/** @var Container $container */
$container = require_once __DIR__ . '/../../../bootstrap.container.php';
test(function () use ($container): void {
/** @var EntityManager $em */
$em = $container->getByType(EntityManager::class);
/** @var ClassMetadata[] $metadata */
$metadata = $em->getMetadataFactory()->getAllMetadata();
foreach ($metadata as $item) {
$entityClass = $item->getName();
$methodName = 'get' . (new ReflectionClass($entityClass))->getShortName() . 'Repository';
Assert::true(
method_exists($em, $methodName),
sprintf('Method %s() not exist in %s or %s', $methodName, TRepositories::class, EntityManager::class)
);
Assert::same($em->getRepository($entityClass), $em->$methodName());
}
});

View File

@@ -0,0 +1,34 @@
<?php declare(strict_types = 1);
namespace Tests\Integration\Latte;
use App\Model\Latte\TemplateFactory;
use Nette\Application\UI\ITemplateFactory;
use Nette\Bridges\ApplicationLatte\Template;
use Nette\DI\Container;
use Nette\Utils\Finder;
use SplFileInfo;
use Tester\Assert;
use Throwable;
/** @var Container $container */
$container = require_once __DIR__ . '/../../../bootstrap.container.php';
test(function () use ($container): void {
/** @var ITemplateFactory $templateFactory */
$templateFactory = $container->getByType(ITemplateFactory::class);
Assert::type(TemplateFactory::class, $templateFactory);
/** @var Template $template */
$template = $templateFactory->createTemplate();
$finder = Finder::findFiles('*.latte')->from(APP_DIR);
try {
/** @var SplFileInfo $file */
foreach ($finder as $file) {
$template->getLatte()->warmupCache($file->getRealPath());
}
} catch (Throwable $e) {
Assert::fail(sprintf('Template compilation failed ([%s] %s)', get_class($e), $e->getMessage()));
}
});

View File

@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);
use App\Model\Utils\Strings;
use Tester\Assert;
require_once __DIR__ . '/../../../../bootstrap.php';
// Strings::slashless
test(function (): void {
$input = 'foo//bar/////test/asd';
$expected = 'foo/bar/test/asd';
$result = Strings::slashless($input);
Assert::same($expected, $result);
});

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
{
}