-
Notifications
You must be signed in to change notification settings - Fork 1
/
JokeCommandTest.php
72 lines (60 loc) · 2.09 KB
/
JokeCommandTest.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
<?php
declare(strict_types=1);
namespace YourNamespace\App\Tests\Functional;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use YourNamespace\App\Command\JokeCommand;
use YourNamespace\App\Tests\Traits\ConsoleTrait;
use YourNamespace\App\Tests\Traits\MockTrait;
/**
* Class JokeCommandTest.
*
* This is a unit test for the JokeCommand class.
*/
#[CoversMethod(JokeCommand::class, 'execute')]
#[CoversMethod(JokeCommand::class, 'configure')]
#[CoversMethod(JokeCommand::class, 'getJoke')]
#[Group('command')]
class JokeCommandTest extends ApplicationFunctionalTestCase {
use ConsoleTrait;
use MockTrait;
#[DataProvider('dataProviderExecute')]
public function testExecute(string $content, array $expected_output, bool $expected_fail = FALSE): void {
/** @var \YourNamespace\App\Command\JokeCommand $mock */
// @phpstan-ignore varTag.nativeType
$mock = $this->prepareMock(JokeCommand::class, [
'getContent' => $content,
]);
$mock->setName('joke');
$this->consoleInitApplicationTester($mock);
$output = $this->consoleApplicationRun([], [], $expected_fail);
foreach ($expected_output as $expected_output_string) {
$this->assertStringContainsString($expected_output_string, $output);
}
}
public static function dataProviderExecute(): array {
return [
[static::fixturePayload(['setup' => 'Test setup', 'punchline' => 'Test punchline']), ['Test setup', 'Test punchline']],
['', ['Unable to retrieve a joke.'], TRUE],
['non-json', ['Unable to retrieve a joke.'], TRUE],
[static::fixturePayload(['setup' => 'Test setup']), ['Unable to retrieve a joke.'], TRUE],
];
}
/**
* Get a fixture payload.
*
* @param array<string, string> $data
* Data to be encoded.
*
* @return string
* Encoded data.
*/
protected static function fixturePayload(array $data): string {
$json = json_encode([(object) $data]);
if ($json === FALSE) {
throw new \Exception('Unable to encode test data.');
}
return $json;
}
}