can you get some inspiration here? https://github.com/laravel/prompts/blob/main/tests/Feature/SearchPromptTest.php
Jan 8, 2024
7
Level 1
Test for console command
Hi,
I want make a integration test for this console command with Laravel Prompts
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use function Laravel\Prompts\search;
use function Laravel\Prompts\select;
use function Laravel\Prompts\confirm;
use LdapRecord\Models\OpenLDAP\User as LdapUser;
use App\Models\User;
use App\Models\Role;
class RegisterUser extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:register-user';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Users info array
*
* @var array
*/
private $usersInfo = [];
/**
* Execute the console command.
*/
public function handle()
{
$selection = search(
label: 'Search for the user that should receive the mail',
options: function (string $value) {
if (strlen($value) > 0) {
// Realiza la búsqueda y obtiene los resultados
$results = LdapUser::select(['cn', 'mail'])
->whereHas('mail')
->whereContains('mail', $value)
->OrderBy('mail')
->get();
$this->usersInfo = [];
foreach ($results as $user) {
$cn = $user->getFirstAttribute('cn');
$mail = $user->getFirstAttribute('mail');
$displayString = $cn . ' (' . $mail . ')';
$this->usersInfo[$displayString] = ['name' => $cn, 'email' => $mail];
}
return array_keys($this->usersInfo);
return $results->map(function ($user) {
return $user->getFirstAttribute('cn') . ' (' . $user->getFirstAttribute('mail') . ')';
})->toArray();
}
return [];
},
validate: function (int|string $value) {
$user = User::where('email', $this->usersInfo[$value]['email'])->first();
if ($user) {
return 'This user is already registered.';
}
}
);
if ($selection && isset($this->usersInfo[$selection])) {
$name = $this->usersInfo[$selection]['name'];
$email = $this->usersInfo[$selection]['email'];
$role = select(
label: '¿Que rol tendrá el usuario?',
options: [
'admin' => 'Administrador',
'gestor' => 'Gestor',
'cau' => 'CAU'
],
default: 'admin'
);
$confirm = confirm(
label: '¿Esta seguro de crear el usuario ' . $name . ' (' . $email . ') con el ' . $role . '?',
default: false,
yes: 'Crear usuario',
no: 'Cancelar',
);
if ($confirm) {
$user = User::create([
'name' => $name,
'email' => $email,
]);
$role = Role::where('slug', $role)->first();
$user->roles()->sync($role);
$this->info('Usuario creado correctamente');
} else {
$this->info('Operación cancelada');
}
}
}
}
I search in documentation
But I don't show to make test for the search component.
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use LdapRecord\Models\OpenLDAP\User;
use LdapRecord\Laravel\Testing\DirectoryEmulator;
class RegisterUserCommandTest extends TestCase
{
use RefreshDatabase, WithFaker;
protected function setUp(): void
{
parent::setUp();
$this->seed(\Database\Seeders\PermissionsSeeder::class);
}
public function test_add_admin(): void
{
$fake = DirectoryEmulator::setup('default');
$userData = [
'cn' => $this->faker->name,
'mail' => $this->faker->email,
'guid' => $this->faker->uuid,
];
$ldapUser = User::create($userData);
$this->artisan('app:register-user');
}
}
Thanks for the help
Please or to participate in this conversation.