Skip to content

Commit

Permalink
tests: remove obsolete PHPStan workarounds, new environment setup
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurk committed Jun 8, 2020
1 parent 943af8f commit f239c9e
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 39 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"require-dev": {
"nette/tester": "^2.3.2",
"mockery/mockery": "^1.3.1",
"phpstan/phpstan": "^0.12.19",
"phpstan/phpstan": "^0.12.26",
"phpstan/phpstan-strict-rules": "^0.12.2",
"pepakriz/phpstan-exception-rules": "^0.10.1",
"pepakriz/phpstan-exception-rules": "^0.11.1",
"phpstan/phpstan-nette": "^0.12.6",
"nepada/phpstan-nette-tester": "^0.3.0",
"spaze/phpstan-disallowed-calls": "^0.12.1",
Expand Down
4 changes: 0 additions & 4 deletions phpstan.tests.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ parameters:
- php
- phpt

autoload_directories:
- tests

excludes_analyse:
- tests/bootstrap.php
- tests/temp/*

ignoreErrors:
Expand Down
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/temp
/.lock
/*.log
output/
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare(strict_types = 1);
namespace NepadaTests\Bridges\BirthNumberInputDI;

use Nepada\BirthNumberInput\BirthNumberInput;
use NepadaTests\Environment;
use NepadaTests\TestCase;
use Nette;
use Nette\Forms\Form;
Expand All @@ -21,7 +22,7 @@ class BirthNumberInputExtensionTest extends TestCase
public function testBirthNumberInput(): void
{
$configurator = new Nette\Configurator();
$configurator->setTempDirectory(TEMP_DIR);
$configurator->setTempDirectory(Environment::getTempDir());
$configurator->setDebugMode(true);
$configurator->addConfig(__DIR__ . '/fixtures/config.neon');
$configurator->createContainer();
Expand Down
65 changes: 65 additions & 0 deletions tests/Environment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
declare(strict_types = 1);

namespace NepadaTests;

use Nette;
use Tester;

final class Environment
{

use Nette\StaticClass;

public const TEMP_BASE_DIR = __DIR__ . '/temp';
private const LOCK_FILE = __DIR__ . '/.lock';
private const GC_DIVISOR = 100;

private static ?string $tempDir = null;

/** @var resource */
private static $lock;

public static function setup(): void
{
Tester\Environment::setup();
date_default_timezone_set('Europe/Prague');
ini_set('session.save_path', self::getTempDir());
}

public static function getTempDir(): string
{
if (self::$tempDir !== null) {
return self::$tempDir;
}

self::prepareTempBaseDir();

$tempDir = self::TEMP_BASE_DIR . '/' . getmypid();
Tester\Helpers::purge($tempDir);
self::$tempDir = $tempDir;

return self::$tempDir;
}

private static function prepareTempBaseDir(): void
{
$lock = fopen(self::LOCK_FILE, 'w');
assert(is_resource($lock));
self::$lock = $lock;

if (self::shouldCollectGarbage() && flock(self::$lock, LOCK_EX)) {
Tester\Helpers::purge(self::TEMP_BASE_DIR);

} else {
flock(self::$lock, LOCK_SH);
@mkdir(self::TEMP_BASE_DIR);
}
}

private static function shouldCollectGarbage(): bool
{
return rand(0, self::GC_DIVISOR) === 0;
}

}
9 changes: 0 additions & 9 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@
abstract class TestCase extends Tester\TestCase
{

public function run(): void
{
if ($_ENV['IS_PHPSTAN'] ?? false) {
return;
}

parent::run();
}

protected function tearDown(): void
{
parent::tearDown();
Expand Down
24 changes: 1 addition & 23 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,4 @@

require __DIR__ . '/../vendor/autoload.php';

// detect PHPStan
if (getenv('IS_PHPSTAN') !== false) {
$_ENV['IS_PHPSTAN'] = in_array(strtolower(getenv('IS_PHPSTAN')), ['1', 'true', 'yes', 'on'], true);
} else {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$_ENV['IS_PHPSTAN'] = (bool) preg_match('~[/\\\\]phpstan(?:\.phar)?$~', end($trace)['file'] ?? '');
}


// configure environment
if (! $_ENV['IS_PHPSTAN']) {
Tester\Environment::setup();
date_default_timezone_set('Europe/Prague');
}


// create temporary directory
define('TEMP_DIR', __DIR__ . '/temp/' . (isset($_SERVER['argv']) ? md5(serialize($_SERVER['argv'])) : getmypid()));
if (! $_ENV['IS_PHPSTAN']) {
@mkdir(dirname(TEMP_DIR)); // @ - directory may already exist
Tester\Helpers::purge(TEMP_DIR);
ini_set('session.save_path', TEMP_DIR);
}
NepadaTests\Environment::setup();

0 comments on commit f239c9e

Please sign in to comment.