Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions src/Commands/MakeJobCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,21 @@ class MakeJobCommand extends Command
public function handle(): int
{
return $this->executeWithTiming(function () {
$name = $this->argument('name');
$parts = explode('/', $name);
$className = array_pop($parts);
[$name, $parts, $className] = $this->splitGeneratedName((string) $this->argument('name'));

// Ensure class name ends with Job
if (!str_ends_with($className, 'Job')) {
$className .= 'Job';
}

$namespace = 'App\\Jobs' . (count($parts) > 0 ? '\\' . implode('\\', $parts) : '');
$parts[] = $className;

$filePath = base_path(
'app/Jobs/' . implode(DIRECTORY_SEPARATOR, $parts) . '.php'
);
$fileName = count($parts) > 0 ? implode('/', $parts) . '/' . $className : $className;
$filePath = $this->generatedFilePath('app/Jobs', $fileName);

// Check if Job already exists
if (file_exists($filePath)) {
$this->displayError('Job already exists at:');
$this->line('<fg=white>' . str_replace(base_path(), '', $filePath) . '</>');
$this->line('<fg=white>' . $this->relativePath($filePath) . '</>');
return Command::FAILURE;
}

Expand All @@ -62,7 +57,7 @@ public function handle(): int
file_put_contents($filePath, $content);

$this->displaySuccess('Job created successfully');
$this->line('<fg=yellow>📦 File:</> <fg=white>' . str_replace(base_path(), '', $filePath) . '</>');
$this->line('<fg=yellow>📦 File:</> <fg=white>' . $this->relativePath($filePath) . '</>');
$this->newLine();
$this->line('<fg=yellow>⚙️ Class:</> <fg=white>' . $className . '</>');

Expand Down
125 changes: 125 additions & 0 deletions tests/Unit/MakeJobCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

declare(strict_types=1);

namespace Doppar\Queue\Tests\Unit;

use Doppar\Queue\Commands\MakeJobCommand;
use PHPUnit\Framework\TestCase;

class MakeJobCommandTest extends TestCase
{
private string $tempRoot;

protected function setUp(): void
{
parent::setUp();

$this->tempRoot = sys_get_temp_dir() . '/doppar-queue-command-' . bin2hex(random_bytes(5));
mkdir($this->tempRoot, 0755, true);
}

protected function tearDown(): void
{
$this->deleteDirectory($this->tempRoot);

parent::tearDown();
}

public function testMakeJobCommandSupportsBackslashesAndRelativeOutput(): void
{
$command = new class($this->tempRoot) extends MakeJobCommand
{
public array $capturedLines = [];
public array $capturedSuccesses = [];

public function __construct(private string $tempRoot)
{
parent::__construct();
}

protected function argument($key = null)
{
return $key === 'name' ? 'Reports\\GenerateDaily' : null;
}

protected function line(string $string, ?string $style = null): void
{
$this->capturedLines[] = $string;
}

protected function newLine($count = 1): void
{
}

protected function displaySuccess(string $message): void
{
$this->capturedSuccesses[] = $message;
}

protected function executeWithTiming(callable $callback): int
{
$result = $callback();

return is_int($result) ? $result : 0;
}

protected function generatedFilePath(string $baseDirectory, string $name, string $extension = '.php'): string
{
$normalizedName = $this->normalizeGeneratedName($name);
$relativePath = trim($baseDirectory, '/\\');

if ($normalizedName !== '') {
$relativePath .= '/' . $normalizedName;
}

return $this->tempRoot . '/' . $relativePath . $extension;
}

protected function relativePath(string $path, ?string $basePath = null): string
{
$normalizedPath = str_replace('\\', '/', $path);
$normalizedBase = rtrim(str_replace('\\', '/', $this->tempRoot), '/');

return substr($normalizedPath, strlen($normalizedBase) + 1);
}
};

$result = $command->handle();
$file = $this->tempRoot . '/app/Jobs/Reports/GenerateDailyJob.php';
$contents = (string) file_get_contents($file);

$this->assertSame(0, $result);
$this->assertFileExists($file);
$this->assertStringContainsString('namespace App\\Jobs\\Reports;', $contents);
$this->assertStringContainsString('class GenerateDailyJob extends Job', $contents);
$this->assertContains('Job created successfully', $command->capturedSuccesses);
$this->assertContains(
'<fg=yellow>📦 File:</> <fg=white>app/Jobs/Reports/GenerateDailyJob.php</>',
$command->capturedLines
);
}

private function deleteDirectory(string $directory): void
{
if (!is_dir($directory)) {
return;
}

$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);

foreach ($iterator as $item) {
if ($item->isDir()) {
rmdir($item->getPathname());
continue;
}

unlink($item->getPathname());
}

rmdir($directory);
}
}
Loading