forked from phpro/grumphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractTaskTestCase.php
183 lines (158 loc) · 5.64 KB
/
AbstractTaskTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
declare(strict_types=1);
namespace GrumPHP\Test\Task;
use GrumPHP\Collection\FilesCollection;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Task\Config\EmptyTaskConfig;
use GrumPHP\Task\Config\Metadata;
use GrumPHP\Task\Config\TaskConfig;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\TaskInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
abstract class AbstractTaskTestCase extends TestCase
{
use ProphecyTrait;
/**
* @var TaskInterface
*/
protected $task;
abstract protected function provideTask(): TaskInterface;
abstract public function provideConfigurableOptions(): iterable;
abstract public function provideRunContexts(): iterable;
abstract public function provideFailsOnStuff(): iterable;
abstract public function providePassesOnStuff(): iterable;
abstract public function provideSkipsOnStuff(): iterable;
protected function setUp(): void
{
parent::setUp();
$this->task = $this->provideTask();
}
/**
* @test
* @dataProvider provideConfigurableOptions
*/
public function it_contains_configurable_options(array $input, ?array $output): void
{
if (!$output) {
$this->expectException(ExceptionInterface::class);
}
$resolver = $this->task::getConfigurableOptions();
self::assertSame(
$output,
$resolver->resolve($input)
);
}
/**
* @test
* @dataProvider provideRunContexts
*/
public function it_runs_in_specific_contexts(bool $expected, ContextInterface $context): void
{
self::assertSame(
$expected,
$this->task->canRunInContext($context)
);
}
/**
* @test
*/
public function it_can_contain_config(): void
{
// All tasks require to return an empty config during construction.
self::assertEquals(new EmptyTaskConfig(), $this->task->getConfig());
$config = new TaskConfig('newConfig', [], new Metadata([]));
// Validate if task has immutable withConfig method.
$newVersion = $this->task->withConfig($config);
self::assertNotSame($newVersion, $this->task);
self::assertEquals(new EmptyTaskConfig(), $this->task->getConfig());
self::assertSame($config, $newVersion->getConfig());
}
/**
* @test
* @dataProvider provideFailsOnStuff
*/
public function it_fails_on_stuff(
array $config,
ContextInterface $context,
callable $configurator,
string $expectedErrorMessage,
string $resultClass = TaskResult::class
): void {
$task = $this->configureTask($config);
\Closure::bind($configurator, $this)($task->getConfig()->getOptions(), $context);
$result = $task->run($context);
self::assertInstanceOf($resultClass, $result);
self::assertSame(TaskResult::FAILED, $result->getResultCode());
self::assertSame($task, $result->getTask());
self::assertSame($context, $result->getContext());
self::assertNotSame('', $expectedErrorMessage, 'Please specify (partial) expected error message!');
self::assertStringContainsString($expectedErrorMessage, $result->getMessage());
}
/**
* @test
* @dataProvider providePassesOnStuff
*/
public function it_passes_on_stuff(
array $config,
ContextInterface $context,
callable $configurator
): void {
$task = $this->configureTask($config);
\Closure::bind($configurator, $this)($task->getConfig()->getOptions(), $context);
$result = $task->run($context);
self::assertInstanceOf(TaskResult::class, $result);
self::assertSame(TaskResult::PASSED, $result->getResultCode());
self::assertSame($task, $result->getTask());
self::assertSame($context, $result->getContext());
self::assertSame('', $result->getMessage());
}
/**
* @test
* @dataProvider provideSkipsOnStuff
*/
public function it_skips_on_stuff(
array $config,
ContextInterface $context,
callable $configurator
): void
{
$task = $this->configureTask($config);
\Closure::bind($configurator, $this)($task->getConfig()->getOptions());
$result = $task->run($context);
self::assertInstanceOf(TaskResult::class, $result);
self::assertSame(TaskResult::SKIPPED, $result->getResultCode());
self::assertSame($task, $result->getTask());
self::assertSame($context, $result->getContext());
self::assertSame('', $result->getMessage());
}
protected function configureTask(array $options = []): TaskInterface
{
return $this->task->withConfig(
new TaskConfig(
'',
$this->task::getConfigurableOptions()->resolve($options),
new Metadata([])
)
);
}
protected function mockContext(string $class = ContextInterface::class, array $files = []): ContextInterface
{
/** @var ContextInterface|ObjectProphecy $context */
$context = $this->prophesize($class);
$context->getFiles()->willReturn(
new FilesCollection(
array_map(
static function ($file): SplFileInfo {
return $file instanceof SplFileInfo ? $file : new SplFileInfo($file, $file, $file);
},
$files
)
)
);
return $context->reveal();
}
}