| 1 | package DBD::Mock::dr; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 41 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 41 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 41 | ▶ | use List::Util qw(reduce); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 41 | our $imp_data_size = 0; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 41 | my @connect_callbacks; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 91 | ▶ | sub connect { | ||||||||||||||||||||||||||
|
Subroutine
connect: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 91 | my ( $drh, $dbname, $user, $auth, $attributes ) = @_; | |||||||||||||||||||||||||||
| 15 | 91 | ▶ | if ( $drh->{'mock_connect_fail'} == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 1 | $drh->set_err( 1, "Could not connect to mock database" ); | |||||||||||||||||||||||||||
| 17 | 1 | return; | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | 90 | ▶ | $attributes ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$attributes ||= {}
MC/DC100%1/1
$attributes ||= {}
$attributes
Truth table100%2/2
A
A= $attributes
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 90 | my %driverParameters = _parse_driver_dsn( $dbname ); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 90 | ▶ | if ( $dbname && $DBD::Mock::AttributeAliasing ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$dbname and $DBD::Mock::AttributeAliasing
MC/DC100%2/2
$dbname and $DBD::Mock::AttributeAliasing
$dbname
$DBD::Mock::AttributeAliasing
Truth table100%3/3
A and B
A= $dbnameB= $DBD::Mock::AttributeAliasing
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # this is the DB we are mocking | ||||||||||||||||||||||||||||
| 26 | $attributes->{mock_attribute_aliases} = | ||||||||||||||||||||||||||||
| 27 | 7 | DBD::Mock::_get_mock_attribute_aliases($driverParameters{database}); | |||||||||||||||||||||||||||
| 28 | 6 | $attributes->{mock_database_name} = $driverParameters{database}; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 89 | ▶ | if (($driverParameters{database} || '') eq 'PostgreSQL') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$driverParameters{'database'} || ''
MC/DC100%1/1
$driverParameters{'database'} || ''
$driverParameters{'database'}
Truth table100%2/2
A
A= $driverParameters{'database'}
| |||||||||||||||||||||||||||||
| 32 | 7 | $attributes->{mock_bound_array_support} = 1; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | # holds statement parsing coderefs/objects | ||||||||||||||||||||||||||||
| 35 | 89 | $attributes->{mock_parser} = []; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # holds all statements applied to handle until manually cleared | ||||||||||||||||||||||||||||
| 38 | 89 | $attributes->{mock_statement_history} = []; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # ability to fake a failed DB connection | ||||||||||||||||||||||||||||
| 41 | 89 | $attributes->{mock_can_connect} = 1; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # ability to make other things fail :) | ||||||||||||||||||||||||||||
| 44 | 89 | $attributes->{mock_can_prepare} = 1; | |||||||||||||||||||||||||||
| 45 | 89 | $attributes->{mock_can_execute} = 1; | |||||||||||||||||||||||||||
| 46 | 89 | $attributes->{mock_can_fetch} = 1; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 89 | ▶ | my $dbh = DBI::_new_dbh( $drh, { Name => $dbname } ) | ||||||||||||||||||||||||||
|
Condition50%1/2
DBI::_new_dbh($drh, {'Name', $dbname}) || (return)
MC/DC0%0/1
DBI::_new_dbh($drh, {'Name', $dbname}) || (return)
DBI::_new_dbh($drh, {'Name', $dbname})
Truth table50%1/2
A
A= DBI::_new_dbh($drh, {'Name', $dbname})
| |||||||||||||||||||||||||||||
| 49 | || return; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 89 | foreach my $callback (@connect_callbacks) { | |||||||||||||||||||||||||||
| 52 | 5 | $callback->( $dbh, $dbname, $user, $auth, $attributes ); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 89 | return $dbh; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 4 | ▶ | sub FETCH { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 59 | 4 | my ( $drh, $attr ) = @_; | |||||||||||||||||||||||||||
| 60 | 4 | ▶ | if ( $attr =~ /^mock_/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 3 | ▶ | if ( $attr eq 'mock_connect_fail' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 62 | 0 | return $drh->{'mock_connect_fail'}; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | elsif ( $attr eq 'mock_data_sources' ) { | ||||||||||||||||||||||||||||
| 65 | 2 | ▶ | unless ( defined $drh->{'mock_data_sources'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 2 | $drh->{'mock_data_sources'} = ['DBI:Mock:']; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 2 | return $drh->{'mock_data_sources'}; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | else { | ||||||||||||||||||||||||||||
| 71 | 1 | return $drh->SUPER::FETCH($attr); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | else { | ||||||||||||||||||||||||||||
| 75 | 1 | return $drh->SUPER::FETCH($attr); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 7 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||
| 80 | 7 | my ( $drh, $attr, $value ) = @_; | |||||||||||||||||||||||||||
| 81 | 7 | ▶ | if ( $attr =~ /^mock_/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 6 | ▶ | if ( $attr eq 'mock_connect_fail' ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 83 | 2 | ▶ | return $drh->{'mock_connect_fail'} = $value ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | elsif ( $attr eq 'mock_data_sources' ) { | ||||||||||||||||||||||||||||
| 86 | 1 | ▶ | if ( ref($value) ne 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | $drh->set_err( 1, | |||||||||||||||||||||||||||
| 88 | "You must pass an array ref of data sources" ); | ||||||||||||||||||||||||||||
| 89 | 0 | return; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 1 | return $drh->{'mock_data_sources'} = $value; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | elsif ( $attr eq 'mock_add_data_sources' ) { | ||||||||||||||||||||||||||||
| 94 | 2 | return push @{ $drh->{'mock_data_sources'} } => $value; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||
| 98 | 1 | return $drh->SUPER::STORE( $attr, $value ); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 5 | ▶ | sub data_sources { | ||||||||||||||||||||||||||
|
Subroutine
data_sources: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 5 | my $drh = shift; | |||||||||||||||||||||||||||
| 104 | return | ||||||||||||||||||||||||||||
| 105 | 9 | ▶ | map { (/^DBI\:Mock\:/i) ? $_ : "DBI:Mock:$_" } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 5 | @{ $drh->FETCH('mock_data_sources') }; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # Necessary to support DBI < 1.34 | ||||||||||||||||||||||||||||
| 110 | # from CPAN RT bug #7057 | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 38 | ▶ | sub disconnect_all { | ||||||||||||||||||||||||||
|
Subroutine
disconnect_all: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # no-op | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub DESTROY { undef } | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 3 | ▶ | sub set_connect_callbacks { | ||||||||||||||||||||||||||
|
Subroutine
set_connect_callbacks: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 120 | 3 | ▶ | @connect_callbacks = map { die "connect callbacks needs to be a reference to a function " unless ref $_ eq "CODE"; $_ } @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | ▶ | sub add_connect_callbacks { | ||||||||||||||||||||||||||
|
Subroutine
add_connect_callbacks: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 124 | 1 | ▶ | push @connect_callbacks, map { die "connect callbacks needs to be a reference to a function " unless ref $_ eq "CODE"; $_ } @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 90 | ▶ | sub _parse_driver_dsn { | ||||||||||||||||||||||||||
|
Subroutine
_parse_driver_dsn: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 128 | 90 | my ( $driverDsn ) = @_; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 90 | ▶ | $driverDsn = $driverDsn ? $driverDsn : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 90 | my %driverParameters; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 90 | foreach my $parameter ( split /;/, $driverDsn ) { | |||||||||||||||||||||||||||
| 135 | 21 | ▶ | if ( my ( $key, $value ) = $parameter =~ m/^(.*?)=(.*)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 8 | $driverParameters{ $key } = $value; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 90 | ▶ | $driverParameters{database} = $driverDsn unless %driverParameters; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 90 | return %driverParameters; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 41 | 1; | |||||||||||||||||||||||||||