Skip to content

Commit

Permalink
add base tests for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsn committed Dec 20, 2023
1 parent 047f77d commit 79af5eb
Show file tree
Hide file tree
Showing 14 changed files with 540 additions and 7 deletions.
3 changes: 2 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
<directory suffix=".php">./src/</directory>
</include>
<exclude>
<directory suffix=".php">./src/Commands</directory>
<directory suffix=".php">./src/Commands/Generators</directory>
<directory suffix=".php">./src/Commands/Utils</directory>
<directory suffix=".php">./src/Config</directory>
</exclude>
<report>
Expand Down
7 changes: 4 additions & 3 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@
realpath(getcwd()) . '/vendor/codeigniter4/framework/system/Test/bootstrap.php',
]);

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

// Set the target version for refactoring
$rectorConfig->phpVersion(PhpVersion::PHP_81);
Expand Down
8 changes: 7 additions & 1 deletion src/Commands/QueueFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ public function run(array $params)
$tbody = [];

foreach ($results as $result) {
$tbody[] = [$result->id, $result->connection, $result->queue, $this->getClassName($result->payload['job'], $config), $result->failed_at];
$tbody[] = [
$result->id,
$result->connection,
$result->queue,
$this->getClassName($result->payload['job'], $config),
$result->failed_at,
];
}

CLI::table($tbody, $thead);
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/QueuePublish.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class QueuePublish extends BaseCommand
{
protected $group = 'Queue';
protected $name = 'queue:publish';
protected $description = 'Publish QueueJob config file into the current application.';
protected $description = 'Publish Queue config file into the current application.';

public function run(array $params): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/QueueStop.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function run(array $params)

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

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

return EXIT_SUCCESS;
}
Expand Down
40 changes: 40 additions & 0 deletions tests/Commands/QueueClearTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Tests\Commands;

use CodeIgniter\Test\Filters\CITestStreamFilter;
use Tests\Support\CLITestCase;

/**
* @internal
*/
final class QueueClearTest extends CLITestCase
{
public function testRunWithNoQueueName(): void
{
CITestStreamFilter::registration();
CITestStreamFilter::addErrorFilter();

$this->assertNotFalse(command('queue:clear'));
$output = $this->parseOutput(CITestStreamFilter::$buffer);

CITestStreamFilter::removeErrorFilter();

$this->assertSame('The queueName is not specified.', $output);
}

public function testRun(): void
{
CITestStreamFilter::registration();
CITestStreamFilter::addOutputFilter();

$this->assertNotFalse(command('queue:clear test'));
$output = $this->parseOutput(CITestStreamFilter::$buffer);

CITestStreamFilter::removeOutputFilter();

$this->assertSame('Queue test has been cleared.', $output);
}
}
51 changes: 51 additions & 0 deletions tests/Commands/QueueFailedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Tests\Commands;

use CodeIgniter\I18n\Time;
use CodeIgniter\Queue\Models\QueueJobFailedModel;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use Exception;
use Tests\Support\CLITestCase;

/**
* @internal
*/
final class QueueFailedTest extends CLITestCase
{
/**
* @throws Exception
*/
public function testRun(): void
{
Time::setTestNow('2023-12-19 14:15:16');

fake(QueueJobFailedModel::class, [
'connection' => 'database',
'queue' => 'test',
'payload' => ['job' => 'failure', 'data' => ['key' => 'value']],
'priority' => 'default',
'exception' => 'Exception: Test error',
]);

CITestStreamFilter::registration();
CITestStreamFilter::addOutputFilter();

$this->assertNotFalse(command('queue:failed'));
$output = $this->parseOutput(CITestStreamFilter::$buffer);

CITestStreamFilter::removeOutputFilter();

$expect = <<<'EOT'
+----+------------+-------+----------------------------+---------------------+
| ID | Connection | Queue | Class | Failed At |
+----+------------+-------+----------------------------+---------------------+
| 1 | database | test | Tests\Support\Jobs\Failure | 2023-12-19 14:15:16 |
+----+------------+-------+----------------------------+---------------------+
EOT;

$this->assertSame($expect, $output);
}
}
89 changes: 89 additions & 0 deletions tests/Commands/QueueFlushTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace Tests\Commands;

use CodeIgniter\I18n\Time;
use CodeIgniter\Queue\Models\QueueJobFailedModel;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use Exception;
use Tests\Support\CLITestCase;

/**
* @internal
*/
final class QueueFlushTest extends CLITestCase
{
/**
* @throws Exception
*/
public function testRun(): void
{
Time::setTestNow('2023-12-19 14:15:16');

fake(QueueJobFailedModel::class, [
'connection' => 'database',
'queue' => 'test',
'payload' => ['job' => 'failure', 'data' => ['key' => 'value']],
'priority' => 'default',
'exception' => 'Exception: Test error',
]);

CITestStreamFilter::registration();
CITestStreamFilter::addOutputFilter();

$this->assertNotFalse(command('queue:flush'));
$output = $this->parseOutput(CITestStreamFilter::$buffer);

CITestStreamFilter::removeOutputFilter();

$this->assertSame('All failed jobs has been removed from the queue ', $output);
}

public function testRunWithQueue(): void
{
Time::setTestNow('2023-12-19 14:15:16');

fake(QueueJobFailedModel::class, [
'connection' => 'database',
'queue' => 'test',
'payload' => ['job' => 'failure', 'data' => ['key' => 'value']],
'priority' => 'default',
'exception' => 'Exception: Test error',
]);

CITestStreamFilter::registration();
CITestStreamFilter::addOutputFilter();

$this->assertNotFalse(command('queue:flush -queue default'));
$output = $this->parseOutput(CITestStreamFilter::$buffer);

CITestStreamFilter::removeOutputFilter();

$this->assertSame('All failed jobs has been removed from the queue default', $output);
}

public function testRunWithQueueAndHour(): void
{
Time::setTestNow('2023-12-19 14:15:16');

fake(QueueJobFailedModel::class, [
'connection' => 'database',
'queue' => 'test',
'payload' => ['job' => 'failure', 'data' => ['key' => 'value']],
'priority' => 'default',
'exception' => 'Exception: Test error',
]);

CITestStreamFilter::registration();
CITestStreamFilter::addOutputFilter();

$this->assertNotFalse(command('queue:flush -queue default -hours 2'));
$output = $this->parseOutput(CITestStreamFilter::$buffer);

CITestStreamFilter::removeOutputFilter();

$this->assertSame('All failed jobs older than 2 hours has been removed from the queue default', $output);
}
}
62 changes: 62 additions & 0 deletions tests/Commands/QueueForgetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Tests\Commands;

use CodeIgniter\Queue\Models\QueueJobFailedModel;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use Tests\Support\CLITestCase;

/**
* @internal
*/
final class QueueForgetTest extends CLITestCase
{
public function testRunWithNoQueueName(): void
{
CITestStreamFilter::registration();
CITestStreamFilter::addErrorFilter();

$this->assertNotFalse(command('queue:forget'));
$output = $this->parseOutput(CITestStreamFilter::$buffer);

CITestStreamFilter::removeErrorFilter();

$this->assertSame('The ID of the failed job is not specified.', $output);
}

public function testRunFailed(): void
{
CITestStreamFilter::registration();
CITestStreamFilter::addOutputFilter();

$this->assertNotFalse(command('queue:forget 123'));
$output = $this->parseOutput(CITestStreamFilter::$buffer);

CITestStreamFilter::removeOutputFilter();

$this->assertSame('Could not find the failed job with ID 123', $output);
}

public function testRun(): void
{
fake(QueueJobFailedModel::class, [
'connection' => 'database',
'queue' => 'test',
'payload' => ['job' => 'failure', 'data' => ['key' => 'value']],
'priority' => 'default',
'exception' => 'Exception: Test error',
]);

CITestStreamFilter::registration();
CITestStreamFilter::addOutputFilter();

$this->assertNotFalse(command('queue:forget 1'));
$output = $this->parseOutput(CITestStreamFilter::$buffer);

CITestStreamFilter::removeOutputFilter();

$this->assertSame('Failed job with ID 1 has been removed.', $output);
}
}
27 changes: 27 additions & 0 deletions tests/Commands/QueuePublishTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Tests\Commands;

use CodeIgniter\Test\Filters\CITestStreamFilter;
use Tests\Support\CLITestCase;

/**
* @internal
*/
final class QueuePublishTest extends CLITestCase
{
public function testRun(): void
{
CITestStreamFilter::registration();
CITestStreamFilter::addOutputFilter();

$this->assertNotFalse(command('queue:publish'));
$output = $this->parseOutput(CITestStreamFilter::$buffer);

CITestStreamFilter::removeOutputFilter();

$this->assertSame(' Published! You can customize the configuration by editing the "app/Config/Queue.php" file.', $output);
}
}
62 changes: 62 additions & 0 deletions tests/Commands/QueueRetryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Tests\Commands;

use CodeIgniter\Queue\Models\QueueJobFailedModel;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use Tests\Support\CLITestCase;

/**
* @internal
*/
final class QueueRetryTest extends CLITestCase
{
public function testRunWithNoQueueName(): void
{
CITestStreamFilter::registration();
CITestStreamFilter::addErrorFilter();

$this->assertNotFalse(command('queue:retry'));
$output = $this->parseOutput(CITestStreamFilter::$buffer);

CITestStreamFilter::removeErrorFilter();

$this->assertSame('The ID of the failed job is not specified.', $output);
}

public function testRunFailed(): void
{
CITestStreamFilter::registration();
CITestStreamFilter::addOutputFilter();

$this->assertNotFalse(command('queue:retry all -queue test'));
$output = $this->parseOutput(CITestStreamFilter::$buffer);

CITestStreamFilter::removeOutputFilter();

$this->assertSame('No failed jobs has been restored to the queue test', $output);
}

public function testRun(): void
{
fake(QueueJobFailedModel::class, [
'connection' => 'database',
'queue' => 'test',
'payload' => ['job' => 'failure', 'data' => ['key' => 'value']],
'priority' => 'default',
'exception' => 'Exception: Test error',
]);

CITestStreamFilter::registration();
CITestStreamFilter::addOutputFilter();

$this->assertNotFalse(command('queue:retry 1'));
$output = $this->parseOutput(CITestStreamFilter::$buffer);

CITestStreamFilter::removeOutputFilter();

$this->assertSame('1 failed job(s) has been restored to the queue ', $output);
}
}
Loading

0 comments on commit 79af5eb

Please sign in to comment.