use 5.32.1; use Test::Most tests => 5; use Test::MockModule; use strict; use warnings; use Data::Dumper; use Const::Fast; use Crypt::Bcrypt::Easy; use BeastBB::Mock::Controller; use BeastBB::Model::Group; { use_ok 'BeastBB::DAO::UserManager'; } const my $APP => BeastBB::Mock::Controller->new; const my $USER_MANAGER => BeastBB::DAO::UserManager->new( app => $APP ); const my %USER_PARAMS => ( id_user => 1, username => 'example_username', matrix_address => '@example_username:example.com', password_bcrypt => bcrypt->crypt('example_password'), is_confirmed => 1, creation_date => DateTime->new( year => 2021, month => 06, day => 3 ), last_connection => DateTime->now, ); { my @results; my @user_keys = keys %USER_PARAMS; push @results, [@user_keys]; push @results, [ map { $USER_PARAMS{$_} } @user_keys ]; $APP->db->Mock( sql => 'SELECT "id_user", "username", "matrix_address", "password_bcrypt", "is_confirmed", "creation_date", "id_group", "last_connection" FROM "user" WHERE "username" = ?', results => \@results, ); my $maybe_user = $USER_MANAGER->Get( username => 'example_user' ); die $maybe_user->ErrorMessage if $maybe_user->IsError; my $user = $maybe_user->Content; ok $user->isa('BeastBB::Model::User'), 'User is made of BeastBB::Model::User'; is_deeply $user->Hash, \%USER_PARAMS, 'User to hash conversion works ok.'; } { my $test_mock_module_group_manager = Test::MockModule->new('BeastBB::DAO::GroupManager'); my $group = BeastBB::Model::Group->new( id_group => 1, groupname => 'example_group' ); $test_mock_module_group_manager->mock( Get => sub { my $self = shift; return BeastBB::Response->new( content => $group ); } ); my @results; my %user_params = %USER_PARAMS; my %user_hash_expected = ( %user_params, group => $group->Hash ); $user_params{id_group} = 1; my @user_keys = keys %user_params; push @results, [@user_keys]; push @results, [ map { $user_params{$_} } @user_keys ]; $APP->db->Mock( sql => 'SELECT "id_user", "username", "matrix_address", "password_bcrypt", "is_confirmed", "creation_date", "id_group", "last_connection" FROM "user" WHERE "username" = ?', results => \@results, ); my $maybe_user = $USER_MANAGER->Get( username => 'example_user', recover_group => 1 ); die $maybe_user->ErrorMessage if $maybe_user->IsError; my $user = $maybe_user->Content; ok $user->isa('BeastBB::Model::User'), 'User is made of BeastBB::Model::User when trying to recover also a group.'; is_deeply $user->Hash, \%user_hash_expected, 'User to hash conversion works ok.'; }