Skip to content

Commit

Permalink
tests: add base tests for commands (#27)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
michalsn authored Dec 21, 2023
1 parent 047f77d commit b289d14
Show file tree
Hide file tree
Showing 15 changed files with 555 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ jobs:
- name: Analyze for refactoring
run: |
composer global require --dev rector/rector:^0.15.1
composer global require --dev rector/rector:^0.18.12
rector process --dry-run --no-progress-bar
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
10 changes: 8 additions & 2 deletions src/Commands/QueueFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,21 @@ public function run(array $params)
$queue = $params['queue'] ?? CLI::getOption('queue');

/** @var QueueConfig $config */
$config = config('queue');
$config = config('Queue');

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

$thead = ['ID', 'Connection', 'Queue', 'Class', 'Failed At'];
$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);
}
}
Loading

0 comments on commit b289d14

Please sign in to comment.