Skip to content

Commit

Permalink
Run PHPStan on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurk committed Aug 17, 2018
1 parent 5d6a621 commit f71ea74
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Expand Up @@ -35,7 +35,8 @@ install:
script:
- /tmp/php-parallel-lint/parallel-lint -e php,phpt --exclude tests/temp src tests
- vendor/bin/tester -s -p php -c tests/php.ini tests
- /tmp/phpstan/phpstan.phar analyse -c tests/phpstan.neon -l 7 src tests
- /tmp/phpstan/phpstan.phar analyse -c tests/phpstan.neon -l 7 src
- /tmp/phpstan/phpstan.phar analyse -c tests/phpstan.tests.neon -l 7 tests

after_failure:
- for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done
Expand Down
4 changes: 2 additions & 2 deletions tests/EmailAddress/EmailAddressTest.phpt
Expand Up @@ -5,7 +5,7 @@ namespace NepadaTests\EmailAddress;

use Nepada\EmailAddress\EmailAddress;
use Nepada\EmailAddress\InvalidEmailAddressException;
use Tester;
use NepadaTests\TestCase;
use Tester\Assert;

require_once __DIR__ . '/../bootstrap.php';
Expand All @@ -14,7 +14,7 @@ require_once __DIR__ . '/../bootstrap.php';
/**
* @testCase
*/
class EmailAddressTest extends Tester\TestCase
class EmailAddressTest extends TestCase
{

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/TestCase.php
@@ -0,0 +1,20 @@
<?php
declare(strict_types = 1);

namespace NepadaTests;

use Tester;

abstract class TestCase extends Tester\TestCase
{

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

parent::run();
}

}
32 changes: 14 additions & 18 deletions tests/bootstrap.php
@@ -1,30 +1,26 @@
<?php
declare(strict_types = 1);

// The Nette Tester command-line runner can be
// invoked through the command: ../vendor/bin/tester .
require __DIR__ . '/../vendor/autoload.php';

if (@!include __DIR__ . '/../vendor/autoload.php') {
echo 'Install Nette Tester using `composer install`';
exit(1);
// detect PHPStan
if (!isset($_ENV['IS_PHPSTAN'])) {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$_ENV['IS_PHPSTAN'] = (bool) preg_match('~[/\\\\]phpstan(?:\.phar)?$~', end($trace)['file'] ?? '');
}


// configure environment
Tester\Environment::setup();
date_default_timezone_set('Europe/Prague');
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()));
@mkdir(dirname(TEMP_DIR)); // @ - directory may already exist
Tester\Helpers::purge(TEMP_DIR);
ini_set('session.save_path', TEMP_DIR);


$_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 = [];
if (!$_ENV['IS_PHPSTAN']) {
@mkdir(dirname(TEMP_DIR)); // @ - directory may already exist
Tester\Helpers::purge(TEMP_DIR);
ini_set('session.save_path', TEMP_DIR);
}
6 changes: 1 addition & 5 deletions tests/phpstan.neon
@@ -1,8 +1,8 @@
includes:
- phar://phpstan.phar/vendor/pepakriz/phpstan-exception-rules/extension.neon
- phar://phpstan.phar/vendor/phpstan/phpstan-nette/extension.neon
- phar://phpstan.phar/vendor/phpstan/phpstan-nette/rules.neon
- phar://phpstan.phar/vendor/phpstan/phpstan-strict-rules/rules.neon
- phar://phpstan.phar/vendor/pepakriz/phpstan-exception-rules/extension.neon

parameters:
exceptionRules:
Expand All @@ -12,7 +12,3 @@ parameters:
ignoredExceptions:
- LogicException
- Nette\InvalidStateException

excludes_analyse:
- %currentWorkingDirectory%/tests/bootstrap.php
- %currentWorkingDirectory%/tests/temp/*
16 changes: 16 additions & 0 deletions tests/phpstan.tests.neon
@@ -0,0 +1,16 @@
includes:
- phar://phpstan.phar/vendor/phpstan/phpstan-nette/extension.neon
- phar://phpstan.phar/vendor/phpstan/phpstan-nette/rules.neon
- phar://phpstan.phar/vendor/phpstan/phpstan-strict-rules/rules.neon

parameters:
fileExtensions:
- php
- phpt

autoload_directories:
- %currentWorkingDirectory%/tests

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

0 comments on commit f71ea74

Please sign in to comment.