Skip to content

Commit

Permalink
Added tests for Commands Attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
WendellAdriel committed May 7, 2024
1 parent 9936f4c commit a9f999c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
],
"require": {
"php": "^8.2",
"illuminate/console": "^11.0",
"illuminate/database": "^11.0",
"illuminate/support": "^11.0"
},
Expand Down
44 changes: 44 additions & 0 deletions tests/Datasets/TestCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Tests\Datasets;

use Illuminate\Console\Command;
use WendellAdriel\Virtue\Commands\Attributes\FlagOption;
use WendellAdriel\Virtue\Commands\Attributes\OptionalArgument;
use WendellAdriel\Virtue\Commands\Attributes\RequiredArgument;
use WendellAdriel\Virtue\Commands\Attributes\ValueOption;
use WendellAdriel\Virtue\Commands\Concerns\Virtue;

#[RequiredArgument(name: 'name')]
#[OptionalArgument(name: 'age', default: 18)]
#[FlagOption(name: 'negative', shortcut: 'm', negatable: true)]
#[ValueOption(name: 'year', description: 'The year')]
#[ValueOption(name: 'scores', array: true, default: [1, 2, 3])]
class TestCommand extends Command
{
use Virtue;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'app:test';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*/
public function handle()
{
//
}
}
24 changes: 24 additions & 0 deletions tests/Feature/CommandAttributesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Collection;
use Tests\Datasets\TestCommand;

it('load attributes and options correctly', function () {
$command = resolve(TestCommand::class);
$arguments = Collection::make($command->getArguments())->keyBy('name');
$options = Collection::make($command->getOptions())->keyBy('name');

expect($arguments)->toHaveCount(2)
->and($arguments->get('name'))->toBeArray()
->and($arguments->get('age'))->toBeArray()
->and($arguments->get('age')['default'])->toBe(18)
->and($options)->toHaveCount(3)
->and($options->get('year'))->toBeArray()
->and($options->get('year')['description'])->toBe('The year')
->and($options->get('negative'))->toBeArray()
->and($options->get('negative')['shortcut'])->toBe('m')
->and($options->get('scores'))->toBeArray()
->and($options->get('scores')['default'])->toBe([1, 2, 3]);
});

0 comments on commit a9f999c

Please sign in to comment.