Skip to content

Commit

Permalink
#1226 update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
bortefi committed Jun 3, 2024
1 parent 4d34896 commit 5a700bd
Showing 1 changed file with 67 additions and 70 deletions.
137 changes: 67 additions & 70 deletions test/Unit/Task/SymfonyConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@

namespace GrumPHPTest\Unit\Task;

use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\Context\GitPreCommitContext;
use GrumPHP\Task\Context\RunContext;
use GrumPHP\Task\SymfonyConsole;
use GrumPHP\Task\TaskInterface;
use GrumPHP\Test\Task\AbstractExternalTaskTestCase;
use Symfony\Component\Process\PhpExecutableFinder;

final class SymfonyConsoleTest extends AbstractExternalTaskTestCase
{
private static string|false $php;

private static function php(): string|false
{
return self::$php ??= (new PhpExecutableFinder())->find();
}

protected function provideTask(): TaskInterface
{
return new SymfonyConsole(
Expand All @@ -25,26 +31,20 @@ protected function provideTask(): TaskInterface
public function provideConfigurableOptions(): iterable
{
yield 'default' => [
[],
[
'bin' => './bin/console',
'command' => [],
]
];

yield 'single-command' => [
[
'command' => ['task:run'],
],
[
'bin' => './bin/console',
'command' => [
'task:run'
],
'command' => ['task:run'],
'ignore_patterns' => [],
'whitelist_patterns' => [],
'triggered_by' => ['php', 'yml', 'xml'],
'run_always' => false,
]
];

yield 'array-command' => [
yield 'with-array-command' => [
[
'command' => ['task:run', '--env', 'dev', '-vvv'],
],
Expand All @@ -56,6 +56,10 @@ public function provideConfigurableOptions(): iterable
'dev',
'-vvv'
],
'ignore_patterns' => [],
'whitelist_patterns' => [],
'triggered_by' => ['php', 'yml', 'xml'],
'run_always' => false,
]
];
}
Expand Down Expand Up @@ -87,7 +91,7 @@ public function provideFailsOnStuff(): iterable
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
function() {
$process = $this->mockProcess(1);
$this->mockProcessBuilder('./bin/console', $process);
$this->mockProcessBuilder(self::php(), $process);
$this->formatter->format($process)->willReturn('nope');
},
'nope'
Expand All @@ -102,19 +106,61 @@ public function providePassesOnStuff(): iterable
],
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
function() {
$this->mockProcessBuilder('./bin/console', $this->mockProcess());
$this->mockProcessBuilder(self::php(), $this->mockProcess());
}
];

yield 'exitCode0WhenRunAlways' => [
[
'command' => ['--version'],
'run_always' => true,
],
$this->mockContext(RunContext::class, ['non-related.log']),
function() {
$this->mockProcessBuilder(self::php(), $this->mockProcess());
}
];
}

public function provideSkipsOnStuff(): iterable
{
yield 'no-files' => [
[],
[
'command' => ['task:run']
],
$this->mockContext(RunContext::class),
function() {
}
];

yield 'no-files-after-ignore-patterns' => [
[
'command' => ['task:run'],
'ignore_patterns' => ['test/'],
],
$this->mockContext(RunContext::class, ['test/file.php']),
function() {
}
];

yield 'no-files-after-whitelist-patterns' => [
[
'command' => ['task:run'],
'whitelist_patterns' => ['src/'],
],
$this->mockContext(RunContext::class, ['config/file.php']),
function() {
}
];

yield 'no-files-after-triggered-by' => [
[
'command' => ['task:run'],
],
$this->mockContext(RunContext::class, ['non-trigger-extension.log']),
function() {
}
];
}

public function provideExternalTaskRuns(): iterable
Expand All @@ -124,8 +170,9 @@ public function provideExternalTaskRuns(): iterable
'command' => ['lint:container']
],
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
'./bin/console',
self::php(),
[
'./bin/console',
'lint:container',
]
];
Expand All @@ -140,64 +187,14 @@ public function provideExternalTaskRuns(): iterable
]
],
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
'./bin/console',
self::php(),
[
'./bin/console',
'task:run',
'--env',
'dev',
'-vvv'
]
];
}

/**
* @test
* @dataProvider provideFailsNonBlockingOnStuff
*/
public function it_fails_non_blocking_on_stuff(
array $config,
ContextInterface $context,
callable $configurator,
string $expectedErrorMessage,
): void {
$task = $this->configureTask($config);
\Closure::bind($configurator, $this)($task->getConfig()->getOptions(), $context);
$result = $task->run($context);

self::assertInstanceOf(TaskResultInterface::class, $result);
self::assertSame(TaskResultInterface::NONBLOCKING_FAILED, $result->getResultCode());
self::assertSame($task, $result->getTask());
self::assertSame($context, $result->getContext());
self::assertSame($expectedErrorMessage, $result->getMessage());
}

public function provideFailsNonBlockingOnStuff(): iterable
{
yield 'missing-command' => [
[
// missing command
],
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
function() {},
'Missing "command" configuration for task "symfony_console".'
];

yield 'empty-command-array' => [
[
'command' => [], // missing command config
],
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
function() {},
'Missing "command" configuration for task "symfony_console".'
];

yield 'empty-command-data' => [
[
'command' => [""], // empty command config
],
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
function() {},
'Missing "command" configuration for task "symfony_console".'
];
}
}

0 comments on commit 5a700bd

Please sign in to comment.