2022-03-14 19:14:05 +01:00
|
|
|
use v5.30.0;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2022-03-14 19:47:22 +01:00
|
|
|
use Test::Most tests => 6;
|
2022-03-14 19:14:05 +01:00
|
|
|
|
|
|
|
use DBI;
|
2022-03-14 19:47:22 +01:00
|
|
|
use DateTime;
|
2022-03-14 19:14:05 +01:00
|
|
|
|
|
|
|
use Peace::Model::Customer;
|
2022-03-15 17:34:14 +01:00
|
|
|
use DateTime::Format::Pg;
|
|
|
|
use DateTime;
|
2022-03-14 19:14:05 +01:00
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
use_ok 'Peace::DAO::Customer';
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
## GIVEN
|
|
|
|
my $sql = <<'EOF';
|
2022-03-15 02:45:16 +01:00
|
|
|
INSERT INTO customers (secret_bcrypt) VALUES (?) RETURNING uuid;
|
2022-03-14 19:14:05 +01:00
|
|
|
EOF
|
2022-03-14 19:47:22 +01:00
|
|
|
my $uuid = 'hola';
|
2022-03-14 19:14:05 +01:00
|
|
|
my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
|
2022-03-15 02:45:16 +01:00
|
|
|
my $customer = Peace::Model::Customer->new( secret_bcrypt => 'hola' );
|
2022-03-14 19:14:05 +01:00
|
|
|
my $customer_dao = Peace::DAO::Customer->new( dbh => $dbh );
|
|
|
|
|
|
|
|
$dbh->{mock_add_resultset} = {
|
2022-03-14 19:47:22 +01:00
|
|
|
sql => $sql,
|
|
|
|
results => [ ['uuid'], [$uuid], ]
|
|
|
|
};
|
|
|
|
$sql = <<'EOF';
|
2022-03-15 17:34:14 +01:00
|
|
|
SELECT uuid, date_creation, secret_bcrypt, stripe_id FROM customers WHERE uuid = ?;
|
2022-03-14 19:47:22 +01:00
|
|
|
EOF
|
2022-03-15 17:34:14 +01:00
|
|
|
my $datetime = DateTime->now;
|
|
|
|
my $formatter = DateTime::Format::Pg->new;
|
2022-03-14 19:47:22 +01:00
|
|
|
$dbh->{mock_add_resultset} = {
|
|
|
|
sql => $sql,
|
2022-03-15 02:45:16 +01:00
|
|
|
results => [
|
|
|
|
[ 'uuid', 'secret_bcrypt', 'date_creation', 'stripe_id' ],
|
2022-03-15 17:34:14 +01:00
|
|
|
[ $uuid, 'hola', $formatter->format_datetime($datetime), undef ]
|
2022-03-15 02:45:16 +01:00
|
|
|
],
|
2022-03-14 19:47:22 +01:00
|
|
|
};
|
|
|
|
## WHEN
|
|
|
|
$customer_dao->create( customer => $customer );
|
|
|
|
|
|
|
|
## THEN
|
|
|
|
is $customer->uuid, $uuid, 'Customer id correctly set after user creation.';
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
## GIVEN
|
2022-03-15 02:45:16 +01:00
|
|
|
my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
|
2022-03-14 19:47:22 +01:00
|
|
|
my $customer_dao = Peace::DAO::Customer->new( dbh => $dbh );
|
2022-03-15 02:45:16 +01:00
|
|
|
my $sql = <<'EOF';
|
2022-03-15 17:34:14 +01:00
|
|
|
SELECT uuid, date_creation, secret_bcrypt, stripe_id FROM customers WHERE uuid = ?;
|
2022-03-14 19:47:22 +01:00
|
|
|
EOF
|
|
|
|
my $uuid = 'hola';
|
2022-03-15 17:34:14 +01:00
|
|
|
my $formatter = DateTime::Format::Pg->new;
|
2022-03-14 19:47:22 +01:00
|
|
|
my $date_creation = DateTime->now;
|
2022-03-15 02:45:16 +01:00
|
|
|
my $secret_bcrypt = 'hola';
|
2022-03-14 19:47:22 +01:00
|
|
|
my $stripe_id = undef;
|
|
|
|
|
|
|
|
$dbh->{mock_add_resultset} = {
|
|
|
|
sql => $sql,
|
2022-03-14 19:14:05 +01:00
|
|
|
results => [
|
2022-03-15 02:45:16 +01:00
|
|
|
[ 'uuid', 'date_creation', 'secret_bcrypt', 'stripe_id' ],
|
2022-03-15 17:34:14 +01:00
|
|
|
[ $uuid, $formatter->format_datetime($date_creation), $secret_bcrypt, $stripe_id ],
|
2022-03-14 19:14:05 +01:00
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
## WHEN
|
2022-03-14 19:47:22 +01:00
|
|
|
my $customer = $customer_dao->recover_by_uuid( uuid => $uuid );
|
2022-03-14 19:14:05 +01:00
|
|
|
|
|
|
|
## THEN
|
2022-03-14 19:47:22 +01:00
|
|
|
is $customer->uuid, $uuid, 'Uuid is recovered correctly';
|
|
|
|
is $customer->date_creation, $date_creation,
|
|
|
|
'Date creation is recovered correctly';
|
2022-03-15 02:45:16 +01:00
|
|
|
is $customer->secret_bcrypt, $secret_bcrypt,
|
|
|
|
'Secret is recovered correctly';
|
2022-03-14 19:47:22 +01:00
|
|
|
is $customer->stripe_id, $stripe_id, 'Secret is recovered correctly';
|
|
|
|
|
2022-03-14 19:14:05 +01:00
|
|
|
}
|