Debian Codegraph @

lib/DBD/Mock/Session.pm

raw
1 package DBD::Mock::Session;
2
3 use strict;
4 use warnings;
5
6 my $INSTANCE_COUNT = 1;
7
8 # - Class - #
9
10 sub new {
11 my $class = shift;
12 my $name = ref( $_[0] ) ? "Session $INSTANCE_COUNT" : shift;
13 $INSTANCE_COUNT++;
14
15 $class->_verify_states( $name, @_ );
16
17 bless {
18 name => $name,
19 states => \@_,
20 state_index => 0
21 }, $class;
22 }
23
24 sub _verify_state {
25 my ( $class, $state, $index, $name ) = @_;
26
27 die "You must specify session states as HASH refs"
28 if ref($state) ne 'HASH';
29
30 die "Bad state '$index' in DBD::Mock::Session ($name)"
31 if not exists $state->{statement}
32 or not exists $state->{results};
33
34 my $stmt = $state->{statement};
35 my $ref = ref $stmt;
36
37 die "Bad 'statement' value '$stmt' in DBD::Mock::Session ($name)",
38 if ref($stmt) ne ''
39 and $ref ne 'CODE'
40 and $ref ne 'Regexp';
41 }
42
43 sub _verify_states {
44 my ( $class, $name, @states ) = @_;
45
46 die "You must specify at least one session state"
47 if scalar @states == 0;
48
49 for ( 0 .. scalar @states - 1 ) {
50 $class->_verify_state( $states[$_], $_, $name );
51 }
52 }
53
54 # - Instance - #
55
56 sub name {
57 my $self = shift;
58 $self->{name};
59 }
60
61 sub reset {
62 my $self = shift;
63 $self->{state_index} = 0;
64 }
65
66 sub current_state {
67 my $self = shift;
68 my $idx = $self->{state_index};
69 return $self->{states}[$idx];
70 }
71
72 sub has_states_left {
73 my $self = shift;
74 return $self->{state_index} < $self->_num_states;
75 }
76
77 sub verify_statement {
78 my ( $self, $got ) = @_;
79
80 unless ( $self->has_states_left ) {
81 die "Session states exhausted, only '"
82 . $self->_num_states
83 . "' in DBD::Mock::Session ($self->name})";
84 }
85
86 my $state = $self->current_state;
87 my $expected = $state->{statement};
88 my $ref = ref($expected);
89
90 if ( $ref eq 'Regexp' and $got !~ /$expected/ ) {
91 die "Statement does not match current state (with Regexp) in "
92 . "DBD::Mock::Session ($self->{name})\n"
93 . " got: $got\n"
94 . " expected: $expected",
95
96 }
97
98 if ( $ref eq 'CODE' and not $expected->( $got, $state ) ) {
99 die "Statement does not match current state (with CODE ref) in "
100 . "DBD::Mock::Session ($self->{name})";
101 }
102
103 if ( not $ref and $got ne $expected ) {
104 die "Statement does not match current state in "
105 . "DBD::Mock::Session ($self->{name})\n"
106 . " got: $got\n"
107 . " expected: $expected";
108 }
109 }
110
111 sub results_for {
112 my ( $self, $statment ) = @_;
113 $self->_find_state_for($statment)->{results};
114 }
115
116 sub verify_bound_params {
117 my ( $self, $params, $options ) = @_;
118
119 my $current_state = $self->current_state;
120 if ( exists ${$current_state}{bound_params} ) {
121 my $expected = $current_state->{bound_params};
122
123 if ( scalar @$expected != scalar @$params ) {
124 die "Not the same number of bound params in current state in "
125 . "DBD::Mock::Session ($self->{name})\n"
126 . " got: @{$params}"
127 . " expected: @{$expected}";
128 }
129
130 for ( 0 .. scalar @{$params} - 1 ) {
131 $self->_verify_bound_param( $params->[$_], $expected->[$_], $_, $options );
132 }
133
134 }
135
136 # and make sure we go to
137 # the next statement
138 $self->{state_index}++;
139 }
140
141 sub _find_state_for {
142 my ( $self, $statement ) = @_;
143
144 foreach ( $self->_remaining_states ) {
145 my $stmt = $_->{statement};
146 my $ref = ref($stmt);
147
148 return $_ if ( $ref eq 'Regexp' and $statement =~ /$stmt/ );
149 return $_ if ( $ref eq 'CODE' and $stmt->( $statement, $_ ) );
150 return $_ if ( not $ref and $stmt eq $statement );
151 }
152
153 die "Statement '$statement' not found in session ($self->{name})";
154 }
155
156 sub _num_states {
157 my $self = shift;
158 scalar @{ $self->{states} };
159 }
160
161 sub _remaining_states {
162 my $self = shift;
163 my $start_index = $self->{state_index};
164 my $end_index = $self->_num_states - 1;
165 @{ $self->{states} }[ $start_index .. $end_index ];
166 }
167
168 sub _array_compare {
169 my ( $self, $got, $expected, $index ) = @_;
170 no warnings;
171
172 my $ref_got = ref $got;
173
174 if (not $ref_got or $ref_got ne 'ARRAY') {
175 die "Bound param $index does not match (using ARRAY) "
176 . "in current state in DBD::Mock::Session ($self->{name})\n"
177 . " got: $got\n"
178 . " expected: ARRAY\n";
179 }
180
181 if ( scalar( @{ $got } ) != scalar( @{ $expected } ) ) {
182 my $got_len = scalar( @{ $got } );
183 my $expected_len = scalar( @{ $expected } );
184 die "Bound param $index does not match (using ARRAY) "
185 . "in current state in DBD::Mock::Session ($self->{name})\n"
186 . " got: array of length $got_len\n"
187 . " expected: array of length $expected_len\n";
188 }
189
190 for my $i ( 0 .. $#{ $expected } ) {
191 my $got_value = $got->[$i];
192 my $expected_value = $expected->[$i];
193
194 if ( not defined $expected_value
195 and not defined $got_value ) {
196 next;
197 }
198
199 if ( not defined $got_value ) {
200 die "Bound array param $index differs at offset $i "
201 . "in current state in DBD::Mock::Session ($self->{name})\n"
202 . " got: <undef>\n"
203 . " expected: $expected_value\n";
204 }
205
206 if ( not defined $expected_value ) {
207 die "Bound array param $index differs at offset $i "
208 . "in current state in DBD::Mock::Session ($self->{name})\n"
209 . " got: $got_value"
210 . " expected: <undef>\n";
211 }
212
213 my $ref = ref $expected_value;
214 if ( $ref and $ref eq 'Regexp' ) {
215
216 if ( $got_value !~ /$expected_value/ ) {
217 die "Bound array param $index differs at offset $i "
218 . "in current state in DBD::Mock::Session ($self->{name})\n"
219 . " got: $got_value\n"
220 . " expected: $expected_value\n";
221 }
222
223 } elsif ( $got_value ne $expected_value ) {
224 die "Bound array param $index differs at offset $i "
225 . "in current state in DBD::Mock::Session ($self->{name})\n"
226 . " got: $got_value"
227 . " expected: $expected_value\n";
228 }
229 }
230 }
231
232 sub _verify_bound_param {
233 my ( $self, $got, $expected, $index, $options ) = @_;
234 no warnings;
235
236 my $ref = ref $expected;
237
238 if ( $ref eq 'Regexp' ) {
239
240 if ( $got !~ /$expected/ ) {
241 die "Bound param $index do not match (using regexp) "
242 . "in current state in DBD::Mock::Session ($self->{name})"
243 . " got: $got\n"
244 . " expected: $expected";
245 }
246 } elsif ( $ref eq 'ARRAY' and $options->{bound_arrays} ) {
247 $self->_array_compare( $got, $expected, $index, $options );
248 } elsif ( $got ne $expected ) {
249 die "Bound param $index do not match "
250 . "in current state in DBD::Mock::Session ($self->{name})\n"
251 . " got: $got\n"
252 . " expected: $expected";
253 }
254 }
255
256 1;