Init
This commit is contained in:
10
tests/.gitignore
vendored
Normal file
10
tests/.gitignore
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
# Folders - recursive
|
||||
*.expected
|
||||
*.actual
|
||||
|
||||
# Folders
|
||||
/tmp
|
||||
|
||||
# Files
|
||||
/*.log
|
||||
/*.html
|
||||
28
tests/bootstrap.container.php
Normal file
28
tests/bootstrap.container.php
Normal 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
12
tests/bootstrap.php
Normal 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__);
|
||||
56
tests/cases/Integration/Container/ContainerBuild.phpt
Normal file
56
tests/cases/Integration/Container/ContainerBuild.phpt
Normal 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()));
|
||||
}
|
||||
});
|
||||
27
tests/cases/Integration/Database/Entity/MappingTest.phpt
Normal file
27
tests/cases/Integration/Database/Entity/MappingTest.phpt
Normal 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);
|
||||
});
|
||||
31
tests/cases/Integration/Database/TRepositoriesTest.php
Normal file
31
tests/cases/Integration/Database/TRepositoriesTest.php
Normal 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());
|
||||
}
|
||||
});
|
||||
34
tests/cases/Integration/Latte/CompilerTest.phpt
Normal file
34
tests/cases/Integration/Latte/CompilerTest.phpt
Normal 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()));
|
||||
}
|
||||
});
|
||||
16
tests/cases/Unit/Model/Utils/Strings.phpt
Normal file
16
tests/cases/Unit/Model/Utils/Strings.phpt
Normal 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);
|
||||
});
|
||||
120
tests/toolkit/Environment.php
Normal file
120
tests/toolkit/Environment.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
38
tests/toolkit/Nette/DummyUserStorage.php
Normal file
38
tests/toolkit/Nette/DummyUserStorage.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
28
tests/toolkit/TestCase/BaseContainerTestCase.php
Normal file
28
tests/toolkit/TestCase/BaseContainerTestCase.php
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
10
tests/toolkit/TestCase/BaseTestCase.php
Normal file
10
tests/toolkit/TestCase/BaseTestCase.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace Tests\Toolkit\TestCase;
|
||||
|
||||
use Tester\TestCase;
|
||||
|
||||
abstract class BaseTestCase extends TestCase
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user