| 1 | package Test::BDD::Cucumber::Definitions::Base; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use DBI; | ||
| 7 | use DDP ( show_unicode => 1 ); | ||
| 8 | use Exporter qw(import); | ||
| 9 | use Test::BDD::Cucumber::Definitions qw(S :validator); | ||
| 10 | use Test::More; | ||
| 11 | |||
| 12 | our $VERSION = '0.41'; | ||
| 13 | |||
| 14 | our @EXPORT_OK = qw(Base); | ||
| 15 | |||
| 16 | ## no critic [Subroutines::RequireArgUnpacking] | ||
| 17 | |||
| 18 | sub Base { | ||
| 19 | return __PACKAGE__; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub param_set { | ||
| 23 | my $self = shift; | ||
| 24 | my ( $param, $value ) = validator_ns->(@_); | ||
| 25 | |||
| 26 | S->{Base} = __PACKAGE__; | ||
| 27 | |||
| 28 | S->{_Base}->{params}->{$param} = $value; | ||
| 29 | |||
| 30 | return 1; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub request_send { | ||
| 34 | my $self = shift; | ||
| 35 | my ($request) = validator_n->(@_); | ||
| 36 | |||
| 37 | my $dsn = sprintf( | ||
| 38 | 'DBI:%s:database=%s;host=%s;port=%s', | ||
| 39 | S->{_Base}->{params}->{driver}, S->{_Base}->{params}->{base}, | ||
| 40 | S->{_Base}->{params}->{host}, S->{_Base}->{params}->{port}, | ||
| 41 | ); | ||
| 42 | |||
| 43 | my $dbh | ||
| 44 | = DBI->connect( $dsn, S->{_Base}->{params}->{user}, S->{_Base}->{params}->{password}, { PrintError => 0 } ); | ||
| 45 | |||
| 46 | if ( !ok( $dbh, 'Connection to the database was established' ) ) { | ||
| 47 | diag("Connect failed: $DBI::err $DBI::errstr"); | ||
| 48 | diag( 'Params: ' . np S->{_Base}->{params} ); | ||
| 49 | } | ||
| 50 | |||
| 51 | S->{_Base}->{response} = $dbh->selectall_arrayref( $request, { Slice => {} } ); | ||
| 52 | |||
| 53 | if ( !ok( S->{_Base}->{response}, 'Base request was sent' ) ) { | ||
| 54 | diag("Request failed: $DBI::err $DBI::errstr"); | ||
| 55 | diag( 'SQL: ' . np $request ); | ||
| 56 | } | ||
| 57 | |||
| 58 | return 1; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub response { | ||
| 62 | my $self = shift; | ||
| 63 | |||
| 64 | return S->{_Base}->{response}; | ||
| 65 | } | ||
| 66 | |||
| 67 | 1; |