Skip to content

test: add failing test #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 91 additions & 9 deletions tests/IgnoreWhitespaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
*/
final class IgnoreWhitespaceTest extends TestCase
{
public function testIgnoreWhitespaces(): void

/**
* @return string[][]
*/
public function provideIgnoreWhitespaces(): array
{
$old = <<<'PHP'
return [
[
<<<'OLD'
<?php

function foo(\DateTimeImmutable $date)
Expand All @@ -29,18 +35,17 @@ function foo(\DateTimeImmutable $date)
}
}

PHP;
$new = <<<'PHP'
OLD,
<<<'NEW'
<?php

function foo(\DateTimeImmutable $date)
{
echo 'foo';
}

PHP;

$expected = <<<'DIFF'
NEW,
<<<'DIFF'
@@ -2,9 +2,5 @@

function foo(\DateTimeImmutable $date)
Expand All @@ -52,14 +57,91 @@ function foo(\DateTimeImmutable $date)
- }
}

DIFF;
DIFF
],
[
<<<'OLD'
<?php

class Foo
{
function foo()
{
echo 'haha';
return;

echo 'blabla';
if (false) {

}
}

}

OLD,
<<<'NEW'
<?php

class Foo
{
function foo()
{
echo 'haha';
return;
}

}

NEW,
<<<'DIFF'
@@ -6,11 +6,6 @@
{
echo 'haha';
return;
-
- echo 'blabla';
- if (false) {
-
- }
}

}

DIFF
],
[
file_get_contents(__DIR__ . '/data/WorkerCommandA.php'),
file_get_contents(__DIR__ . '/data/WorkerCommandB.php'),
<<<'DIFF'
@@ -215,11 +215,6 @@
{
echo 'haha';
return;
-
- echo 'blabla';
- if (false) {
-
- }
}

}

DIFF
],
];
}

/**
* @dataProvider provideIgnoreWhitespaces
*/
public function testIgnoreWhitespaces(string $old, string $new, string $expectedDiff): void
{
$diff = DiffHelper::calculate($old, $new, 'Unified', [
'ignoreWhitespace' => true,
], [
'cliColorization' => RendererConstant::CLI_COLOR_DISABLE,
]);

static::assertSame($expected, $diff);
static::assertSame($expectedDiff, $diff);
}
}
225 changes: 225 additions & 0 deletions tests/data/WorkerCommandA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
<?php declare(strict_types = 1);

namespace PHPStan\Command;

use Clue\React\NDJson\Decoder;
use Clue\React\NDJson\Encoder;
use PHPStan\Analyser\FileAnalyser;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\DependencyInjection\Container;
use PHPStan\Rules\Registry;
use React\EventLoop\StreamSelectLoop;
use React\Socket\ConnectionInterface;
use React\Socket\TcpConnector;
use React\Stream\ReadableStreamInterface;
use React\Stream\WritableStreamInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class WorkerCommand extends Command
{

private const NAME = 'worker';

/** @var string[] */
private array $composerAutoloaderProjectPaths;

/**
* @param string[] $composerAutoloaderProjectPaths
*/
public function __construct(
array $composerAutoloaderProjectPaths
)
{
parent::__construct();
$this->composerAutoloaderProjectPaths = $composerAutoloaderProjectPaths;
}

protected function configure(): void
{
$this->setName(self::NAME)
->setDescription('(Internal) Support for parallel analysis.')
->setDefinition([
new InputArgument('paths', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Paths with source code to run analysis on'),
new InputOption('paths-file', null, InputOption::VALUE_REQUIRED, 'Path to a file with a list of paths to run analysis on'),
new InputOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'Path to project configuration file'),
new InputOption(AnalyseCommand::OPTION_LEVEL, 'l', InputOption::VALUE_REQUIRED, 'Level of rule options - the higher the stricter'),
new InputOption('autoload-file', 'a', InputOption::VALUE_REQUIRED, 'Project\'s additional autoload file path'),
new InputOption('memory-limit', null, InputOption::VALUE_REQUIRED, 'Memory limit for analysis'),
new InputOption('xdebug', null, InputOption::VALUE_NONE, 'Allow running with XDebug for debugging purposes'),
new InputOption('port', null, InputOption::VALUE_REQUIRED),
new InputOption('identifier', null, InputOption::VALUE_REQUIRED),
]);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$paths = $input->getArgument('paths');
$memoryLimit = $input->getOption('memory-limit');
$autoloadFile = $input->getOption('autoload-file');
$configuration = $input->getOption('configuration');
$level = $input->getOption(AnalyseCommand::OPTION_LEVEL);
$pathsFile = $input->getOption('paths-file');
$allowXdebug = $input->getOption('xdebug');
$port = $input->getOption('port');
$identifier = $input->getOption('identifier');

if (
!is_array($paths)
|| (!is_string($memoryLimit) && $memoryLimit !== null)
|| (!is_string($autoloadFile) && $autoloadFile !== null)
|| (!is_string($configuration) && $configuration !== null)
|| (!is_string($level) && $level !== null)
|| (!is_string($pathsFile) && $pathsFile !== null)
|| (!is_bool($allowXdebug))
|| !is_string($port)
|| !is_string($identifier)
) {
throw new \PHPStan\ShouldNotHappenException();
}

try {
$inceptionResult = CommandHelper::begin(
$input,
$output,
$paths,
$pathsFile,
$memoryLimit,
$autoloadFile,
$this->composerAutoloaderProjectPaths,
$configuration,
null,
$level,
$allowXdebug,
false
);
} catch (\PHPStan\Command\InceptionNotSuccessfulException $e) {
return 1;
}
$loop = new StreamSelectLoop();

$container = $inceptionResult->getContainer();

$analysedFiles = $inceptionResult->getFiles();

/** @var NodeScopeResolver $nodeScopeResolver */
$nodeScopeResolver = $container->getByType(NodeScopeResolver::class);
$nodeScopeResolver->setAnalysedFiles($analysedFiles);

$analysedFiles = array_fill_keys($analysedFiles, true);

$tcpConector = new TcpConnector($loop);
$tcpConector->connect(sprintf('127.0.0.1:%d', $port))->done(function (ConnectionInterface $connection) use ($container, $identifier, $analysedFiles): void {
$out = new Encoder($connection);
$in = new Decoder($connection, true, 512, 0, $container->getParameter('parallel')['buffer']);
$out->write(['action' => 'hello', 'identifier' => $identifier]);
$this->runWorker($container, $out, $in, $analysedFiles);
});

$loop->run();

return 0;
}

/**
* @param Container $container
* @param WritableStreamInterface $out
* @param ReadableStreamInterface $in
* @param array<string, true> $analysedFiles
*/
private function runWorker(
Container $container,
WritableStreamInterface $out,
ReadableStreamInterface $in,
array $analysedFiles
): void
{
$handleError = static function (\Throwable $error) use ($out): void {
$out->write([
'action' => 'result',
'result' => [
'errors' => [$error->getMessage()],
'dependencies' => [],
'filesCount' => 0,
'internalErrorsCount' => 1,
],
]);
$out->end();
};
$out->on('error', $handleError);

/** @var FileAnalyser $fileAnalyser */
$fileAnalyser = $container->getByType(FileAnalyser::class);

/** @var Registry $registry */
$registry = $container->getByType(Registry::class);

// todo collectErrors (from Analyser)
$in->on('data', static function (array $json) use ($fileAnalyser, $registry, $out, $analysedFiles): void {
$action = $json['action'];
if ($action !== 'analyse') {
return;
}

$internalErrorsCount = 0;
$files = $json['files'];
$errors = [];
$dependencies = [];
foreach ($files as $file) {
try {
$fileAnalyserResult = $fileAnalyser->analyseFile($file, $analysedFiles, $registry, null);
$fileErrors = $fileAnalyserResult->getErrors();
$dependencies[$file] = $fileAnalyserResult->getDependencies();
foreach ($fileErrors as $fileError) {
$errors[] = $fileError;
}
} catch (\Throwable $t) {
$internalErrorsCount++;
$internalErrorMessage = sprintf('Internal error: %s in file %s', $t->getMessage(), $file);
$internalErrorMessage .= sprintf(
'%sRun PHPStan with --debug option and post the stack trace to:%s%s',
"\n",
"\n",
'https://github.com/phpstan/phpstan/issues/new'
);
$errors[] = $internalErrorMessage;
}
}

$out->write([
'action' => 'result',
'result' => [
'errors' => $errors,
'dependencies' => $dependencies,
'filesCount' => count($files),
'internalErrorsCount' => $internalErrorsCount,
]]);
});
$in->on('error', $handleError);
}

public function haha(int $i): void
{
function () use ($i) {

};
}

/**
* @param int $b
*/
public function doLorem(int $a): void
{
echo 'haha';
return;

echo 'blabla';
if (false) {

}
}

}
Loading