Skip to content

Commit b289d14

Browse files
authored
tests: add base tests for commands (#27)
* add base tests for commands * fix config load * disable phpstan-strict-rules in rector.php * Revert "disable phpstan-strict-rules in rector.php" This reverts commit 5c5d115. * add rector separately * update rector version in the workflow * add suggestions from the the code review
1 parent 047f77d commit b289d14

15 files changed

Lines changed: 555 additions & 9 deletions

.github/workflows/rector.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ jobs:
6262
6363
- name: Analyze for refactoring
6464
run: |
65-
composer global require --dev rector/rector:^0.15.1
65+
composer global require --dev rector/rector:^0.18.12
6666
rector process --dry-run --no-progress-bar

phpunit.xml.dist

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
<directory suffix=".php">./src/</directory>
2525
</include>
2626
<exclude>
27-
<directory suffix=".php">./src/Commands</directory>
27+
<directory suffix=".php">./src/Commands/Generators</directory>
28+
<directory suffix=".php">./src/Commands/Utils</directory>
2829
<directory suffix=".php">./src/Config</directory>
2930
</exclude>
3031
<report>

rector.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@
6363
realpath(getcwd()) . '/vendor/codeigniter4/framework/system/Test/bootstrap.php',
6464
]);
6565

66-
if (is_file(__DIR__ . '/phpstan.neon.dist')) {
67-
$rectorConfig->phpstanConfig(__DIR__ . '/phpstan.neon.dist');
68-
}
66+
$rectorConfig->phpstanConfigs([
67+
__DIR__ . '/phpstan.neon.dist',
68+
__DIR__ . '/vendor/phpstan/phpstan-strict-rules/rules.neon',
69+
]);
6970

7071
// Set the target version for refactoring
7172
$rectorConfig->phpVersion(PhpVersion::PHP_81);

src/Commands/QueueFailed.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,21 @@ public function run(array $params)
5656
$queue = $params['queue'] ?? CLI::getOption('queue');
5757

5858
/** @var QueueConfig $config */
59-
$config = config('queue');
59+
$config = config('Queue');
6060

6161
$results = service('queue')->listFailed($queue);
6262

6363
$thead = ['ID', 'Connection', 'Queue', 'Class', 'Failed At'];
6464
$tbody = [];
6565

6666
foreach ($results as $result) {
67-
$tbody[] = [$result->id, $result->connection, $result->queue, $this->getClassName($result->payload['job'], $config), $result->failed_at];
67+
$tbody[] = [
68+
$result->id,
69+
$result->connection,
70+
$result->queue,
71+
$this->getClassName($result->payload['job'], $config),
72+
$result->failed_at,
73+
];
6874
}
6975

7076
CLI::table($tbody, $thead);

src/Commands/QueuePublish.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class QueuePublish extends BaseCommand
1313
{
1414
protected $group = 'Queue';
1515
protected $name = 'queue:publish';
16-
protected $description = 'Publish QueueJob config file into the current application.';
16+
protected $description = 'Publish Queue config file into the current application.';
1717

1818
public function run(array $params): void
1919
{

src/Commands/QueueStop.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function run(array $params)
7272

7373
cache()->save($cacheName, $startTime, MINUTE * 10);
7474

75-
CLI::write('QueueJob will be stopped after the current job finish', 'yellow');
75+
CLI::write('Queue will be stopped after the current job finish', 'yellow');
7676

7777
return EXIT_SUCCESS;
7878
}

tests/Commands/QueueClearTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Commands;
6+
7+
use CodeIgniter\Test\Filters\CITestStreamFilter;
8+
use Tests\Support\CLITestCase;
9+
10+
/**
11+
* @internal
12+
*/
13+
final class QueueClearTest extends CLITestCase
14+
{
15+
public function testRunWithNoQueueName(): void
16+
{
17+
CITestStreamFilter::registration();
18+
CITestStreamFilter::addErrorFilter();
19+
20+
$this->assertNotFalse(command('queue:clear'));
21+
$output = $this->parseOutput(CITestStreamFilter::$buffer);
22+
23+
CITestStreamFilter::removeErrorFilter();
24+
25+
$this->assertSame('The queueName is not specified.', $output);
26+
}
27+
28+
public function testRun(): void
29+
{
30+
CITestStreamFilter::registration();
31+
CITestStreamFilter::addOutputFilter();
32+
33+
$this->assertNotFalse(command('queue:clear test'));
34+
$output = $this->parseOutput(CITestStreamFilter::$buffer);
35+
36+
CITestStreamFilter::removeOutputFilter();
37+
38+
$this->assertSame('Queue test has been cleared.', $output);
39+
}
40+
}

tests/Commands/QueueFailedTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Commands;
6+
7+
use CodeIgniter\I18n\Time;
8+
use CodeIgniter\Queue\Models\QueueJobFailedModel;
9+
use CodeIgniter\Test\Filters\CITestStreamFilter;
10+
use Exception;
11+
use Tests\Support\CLITestCase;
12+
13+
/**
14+
* @internal
15+
*/
16+
final class QueueFailedTest extends CLITestCase
17+
{
18+
/**
19+
* @throws Exception
20+
*/
21+
public function testRun(): void
22+
{
23+
Time::setTestNow('2023-12-19 14:15:16');
24+
25+
fake(QueueJobFailedModel::class, [
26+
'connection' => 'database',
27+
'queue' => 'test',
28+
'payload' => ['job' => 'failure', 'data' => ['key' => 'value']],
29+
'priority' => 'default',
30+
'exception' => 'Exception: Test error',
31+
]);
32+
33+
CITestStreamFilter::registration();
34+
CITestStreamFilter::addOutputFilter();
35+
36+
$this->assertNotFalse(command('queue:failed'));
37+
$output = $this->parseOutput(CITestStreamFilter::$buffer);
38+
39+
CITestStreamFilter::removeOutputFilter();
40+
41+
$expect = <<<'EOT'
42+
+----+------------+-------+----------------------------+---------------------+
43+
| ID | Connection | Queue | Class | Failed At |
44+
+----+------------+-------+----------------------------+---------------------+
45+
| 1 | database | test | Tests\Support\Jobs\Failure | 2023-12-19 14:15:16 |
46+
+----+------------+-------+----------------------------+---------------------+
47+
EOT;
48+
49+
$this->assertSame($expect, $output);
50+
}
51+
}

tests/Commands/QueueFlushTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Commands;
6+
7+
use CodeIgniter\I18n\Time;
8+
use CodeIgniter\Queue\Models\QueueJobFailedModel;
9+
use CodeIgniter\Test\Filters\CITestStreamFilter;
10+
use Exception;
11+
use Tests\Support\CLITestCase;
12+
13+
/**
14+
* @internal
15+
*/
16+
final class QueueFlushTest extends CLITestCase
17+
{
18+
/**
19+
* @throws Exception
20+
*/
21+
public function testRun(): void
22+
{
23+
Time::setTestNow('2023-12-19 14:15:16');
24+
25+
fake(QueueJobFailedModel::class, [
26+
'connection' => 'database',
27+
'queue' => 'test',
28+
'payload' => ['job' => 'failure', 'data' => ['key' => 'value']],
29+
'priority' => 'default',
30+
'exception' => 'Exception: Test error',
31+
]);
32+
33+
CITestStreamFilter::registration();
34+
CITestStreamFilter::addOutputFilter();
35+
36+
$this->assertNotFalse(command('queue:flush'));
37+
$output = $this->parseOutput(CITestStreamFilter::$buffer);
38+
39+
CITestStreamFilter::removeOutputFilter();
40+
41+
$this->assertSame('All failed jobs has been removed from the queue ', $output);
42+
}
43+
44+
public function testRunWithQueue(): void
45+
{
46+
Time::setTestNow('2023-12-19 14:15:16');
47+
48+
fake(QueueJobFailedModel::class, [
49+
'connection' => 'database',
50+
'queue' => 'test',
51+
'payload' => ['job' => 'failure', 'data' => ['key' => 'value']],
52+
'priority' => 'default',
53+
'exception' => 'Exception: Test error',
54+
]);
55+
56+
CITestStreamFilter::registration();
57+
CITestStreamFilter::addOutputFilter();
58+
59+
$this->assertNotFalse(command('queue:flush -queue default'));
60+
$output = $this->parseOutput(CITestStreamFilter::$buffer);
61+
62+
CITestStreamFilter::removeOutputFilter();
63+
64+
$this->assertSame('All failed jobs has been removed from the queue default', $output);
65+
}
66+
67+
public function testRunWithQueueAndHour(): void
68+
{
69+
Time::setTestNow('2023-12-19 14:15:16');
70+
71+
fake(QueueJobFailedModel::class, [
72+
'connection' => 'database',
73+
'queue' => 'test',
74+
'payload' => ['job' => 'failure', 'data' => ['key' => 'value']],
75+
'priority' => 'default',
76+
'exception' => 'Exception: Test error',
77+
]);
78+
79+
CITestStreamFilter::registration();
80+
CITestStreamFilter::addOutputFilter();
81+
82+
$this->assertNotFalse(command('queue:flush -queue default -hours 2'));
83+
$output = $this->parseOutput(CITestStreamFilter::$buffer);
84+
85+
CITestStreamFilter::removeOutputFilter();
86+
87+
$this->assertSame('All failed jobs older than 2 hours has been removed from the queue default', $output);
88+
}
89+
}

tests/Commands/QueueForgetTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Commands;
6+
7+
use CodeIgniter\Queue\Models\QueueJobFailedModel;
8+
use CodeIgniter\Test\Filters\CITestStreamFilter;
9+
use Tests\Support\CLITestCase;
10+
11+
/**
12+
* @internal
13+
*/
14+
final class QueueForgetTest extends CLITestCase
15+
{
16+
public function testRunWithNoQueueName(): void
17+
{
18+
CITestStreamFilter::registration();
19+
CITestStreamFilter::addErrorFilter();
20+
21+
$this->assertNotFalse(command('queue:forget'));
22+
$output = $this->parseOutput(CITestStreamFilter::$buffer);
23+
24+
CITestStreamFilter::removeErrorFilter();
25+
26+
$this->assertSame('The ID of the failed job is not specified.', $output);
27+
}
28+
29+
public function testRunFailed(): void
30+
{
31+
CITestStreamFilter::registration();
32+
CITestStreamFilter::addOutputFilter();
33+
34+
$this->assertNotFalse(command('queue:forget 123'));
35+
$output = $this->parseOutput(CITestStreamFilter::$buffer);
36+
37+
CITestStreamFilter::removeOutputFilter();
38+
39+
$this->assertSame('Could not find the failed job with ID 123', $output);
40+
}
41+
42+
public function testRun(): void
43+
{
44+
fake(QueueJobFailedModel::class, [
45+
'connection' => 'database',
46+
'queue' => 'test',
47+
'payload' => ['job' => 'failure', 'data' => ['key' => 'value']],
48+
'priority' => 'default',
49+
'exception' => 'Exception: Test error',
50+
]);
51+
52+
CITestStreamFilter::registration();
53+
CITestStreamFilter::addOutputFilter();
54+
55+
$this->assertNotFalse(command('queue:forget 1'));
56+
$output = $this->parseOutput(CITestStreamFilter::$buffer);
57+
58+
CITestStreamFilter::removeOutputFilter();
59+
60+
$this->assertSame('Failed job with ID 1 has been removed.', $output);
61+
}
62+
}

0 commit comments

Comments
 (0)