Skip to content

Commit

Permalink
fix: updated tests and refactored commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Tbaile committed May 10, 2024
1 parent ed4758c commit 1390aa4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 24 deletions.
3 changes: 1 addition & 2 deletions app/Console/Commands/CreateRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
use App\Jobs\SyncRepository;
use App\Models\Repository;
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;

use function Laravel\Prompts\info;
use function Laravel\Prompts\text;
use function Laravel\Prompts\textarea;

class CreateRepository extends Command implements PromptsForMissingInput
class CreateRepository extends Command
{
/**
* The name and signature of the console command.
Expand Down
38 changes: 29 additions & 9 deletions app/Console/Commands/SyncRepositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
use App\Jobs\SyncRepository;
use App\Models\Repository;
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use function Laravel\Prompts\multiselect;

class SyncRepositories extends Command
class SyncRepositories extends Command implements PromptsForMissingInput
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'repository:sync';
protected $signature = 'repository:sync {repository*}';

/**
* The console command description.
Expand All @@ -33,13 +35,31 @@ class SyncRepositories extends Command
*/
public function handle(): void
{
$repositories = multiselect(
label: 'Select the repositories to sync',
options: Repository::pluck('name', 'id'),
required: true
);
foreach ($repositories as $repository) {
SyncRepository::dispatch(Repository::find($repository));
$repositories = $this->argument('repository');
foreach ($repositories as $repoName) {
try {
$repository = Repository::where('name', $repoName)->firstOrFail();
$this->info("Dispatching sync for '$repoName'.");
SyncRepository::dispatch($repository);
} catch (ModelNotFoundException) {
$this->warn("Repository '$repoName' not found.");
}
}
}

/**
* Prompt for missing input arguments using the returned questions.
*
* @return array<string, string>
*/
protected function promptForMissingArgumentsUsing(): array
{
return [
'repository' => fn () => multiselect(
label: 'Choose repositories to sync',
options: Repository::pluck('name'),
required: true,
),
];
}
}
32 changes: 32 additions & 0 deletions tests/Feature/CommandLineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use App\Jobs\SyncRepository;
use App\Models\Repository;
use Illuminate\Support\Facades\Queue;

use function Pest\Laravel\artisan;
use function Pest\Laravel\assertDatabaseHas;

it('can sync repositories as parameter', function () {
$repositories = Repository::factory()->count(2)->create()->pluck('name');
Queue::fake();
artisan("repository:sync {$repositories->join(' ')} test")
->expectsOutput("Dispatching sync for '{$repositories->get(0)}'.")
->expectsOutput("Dispatching sync for '{$repositories->get(1)}'.")
->expectsOutput("Repository 'test' not found.")
->assertExitCode(0);
Queue::assertPushed(SyncRepository::class, 2);
});

it('can create a repository through cli', function () {
$repository = Repository::factory()->make();
Queue::fake();
artisan('repository:create')
->expectsQuestion('What is the name of the repository?', $repository->name)
->expectsQuestion('Provide the command to be ran to sync this repository.', $repository->command)
->expectsQuestion('Provide the folder where the data is.', $repository->source_folder)
->expectsQuestion('Please provide how much time the repository must be kept back from upstream.', $repository->delay)
->assertExitCode(0);
assertDatabaseHas('repositories', $repository->toArray());
Queue::assertPushed(SyncRepository::class);
});
2 changes: 1 addition & 1 deletion tests/Feature/FreezeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$repository = Repository::factory()->create([
'delay' => 7,
]);
$snapshotPath = config('repositories.directory').'/'.$repository->name;
$snapshotPath = config('repositories.snapshots').'/'.$repository->name;
Storage::createDirectory($snapshotPath.'/'.now()->subDays(6)->toAtomString());
expect($repository->getStablePath())
->toBe($snapshotPath.'/'.now()->subDays(6)->toAtomString());
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
});

beforeEach(function () {
Storage::fake('local');
Storage::fake();
});

it('returns not found if wrong repo')
Expand Down
24 changes: 13 additions & 11 deletions tests/Feature/SyncRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use function Pest\Laravel\artisan;
use function Pest\Laravel\freezeTime;

dataset('command', ['app:sync-repositories', 'app:sync-repositories -Q', 'app:sync-repositories --queued']);
dataset('command', ['repository:sync']);

test('runs command correctly', function () {
Process::fake();
Expand All @@ -33,24 +33,26 @@
test('can sync repository from command line', function ($command) {
$repositories = Repository::factory()->count(3)->create();
Queue::fake();
artisan($command)
artisan("$command {$repositories->pluck('name')->join(' ')}")
->assertSuccessful();
Queue::assertCount($repositories->count());
Queue::assertPushed(SyncRepository::class, $repositories->count());
})->with('command');

test('repository upstream processing', function () {
$repository = Repository::factory()->create(['source_folder' => 'example']);
$repository = Repository::factory()->create(['sub_dir' => 'example']);
$listener = new ProcessRepositoryUpstream();
$sourcePath = config('repositories.source_folder').'/'.$repository->name;
$snapshotPath = config('repositories.snapshots').'/'.$repository->name;
Event::fake();
Storage::fake();
UploadedFile::fake()->create('example/file1.txt')->storeAs('example', 'file1.txt');
UploadedFile::fake()->create('example/file2.txt')->storeAs('example', 'file2.txt');
UploadedFile::fake()->create('example/file3.txt')->storeAs('example', 'file3.txt');
UploadedFile::fake()->create('example/file1.txt')->storeAs("$sourcePath/example", 'file1.txt');
UploadedFile::fake()->create('example/file2.txt')->storeAs("$sourcePath/example", 'file2.txt');
UploadedFile::fake()->create('example/file3.txt')->storeAs("$sourcePath/example", 'file3.txt');
$listener->handle(new RepositorySynced($repository));
Storage::assertExists('repositories/'.$repository->name.'/'.now()->toAtomString().'/file1.txt');
Storage::assertExists('repositories/'.$repository->name.'/'.now()->toAtomString().'/file2.txt');
Storage::assertExists('repositories/'.$repository->name.'/'.now()->toAtomString().'/file3.txt');
Storage::assertExists("$snapshotPath/".now()->toAtomString().'/file1.txt');
Storage::assertExists("$snapshotPath/".now()->toAtomString().'/file2.txt');
Storage::assertExists("$snapshotPath/".now()->toAtomString().'/file3.txt');
Event::assertDispatched(function (SnapshotCreated $event) use ($repository): bool {
return $event->repository->is($repository);
});
Expand All @@ -62,7 +64,7 @@
'delay' => 2,
]);
Storage::fake();
$snapshotPath = config('repositories.directory').'/'.$repository->name;
$snapshotPath = config('repositories.snapshots').'/'.$repository->name;
Storage::createDirectory($snapshotPath.'/'.now()->subDays(3)->toAtomString());
Storage::createDirectory($snapshotPath.'/'.now()->subDays(2)->toAtomString());
Storage::createDirectory($snapshotPath.'/'.now()->subDay()->toAtomString());
Expand All @@ -81,7 +83,7 @@
'freeze' => 'frozen_dir',
]);
Storage::fake();
$snapshotPath = config('repositories.directory').'/'.$repository->name;
$snapshotPath = config('repositories.snapshots').'/'.$repository->name;
Storage::createDirectory($snapshotPath.'/'.now()->subDays(3)->toAtomString());
Storage::createDirectory($snapshotPath.'/'.now()->subDays(2)->toAtomString());
Storage::createDirectory($snapshotPath.'/'.now()->subDay()->toAtomString());
Expand Down

0 comments on commit 1390aa4

Please sign in to comment.