blib/lib/Test/WWW/Mechanize/MultiMech.pm

filter: Statementstmt 16.2% 19 / 117 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 35.2% 6 / 17 Podpod 100% 7 / 7 totaltotal 17.4% 32 / 183 CC 25 SCAR 35.7
CC
25
Cov
12%
login
110.0
AUTOLOAD
30.0
new
20.0
CRAP 35.6
↑ Summary
1 package Test::WWW::Mechanize::MultiMech;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '1.006002'; # VERSION
8  
9 2 ▶use Test::WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Test::Builder qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶sub _diag {
Subroutine _diag: not called CC=1 SCAR=6.9
14 0 Test::Builder->new->diag(@_);
15 }
16  
17 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
18 0 my ( $class, %args ) = @_;
19  
20 0 ▶ ref $args{users} eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $args{"users"} eq "ARRAY" or croak("You must give ``users'' to new->new(); and it needs to be an arrayref") 0 0 0
21 or croak 'You must give ``users\'\' to new->new(); '
22 . 'and it needs to be an arrayref';
23  
24 0 my @args_users = @{ delete $args{users} };
25 0 my ( %users, @users_order );
26 0 for ( grep !($_%2), 0 .. $#args_users ) {
27 0 my $user_args = $args_users[ $_+1 ];
28  
29 0 push @users_order, $args_users[ $_ ];
30  
31 0 my $mech = Test::WWW::Mechanize->new( %args );
32 $users{ $args_users[$_] } = {
33 login => (
34 defined $user_args->{login}
35 ? $user_args->{login} : $args_users[ $_ ]
36 ),
37 pass => $user_args->{pass},
38 0 ▶ mech => $mech,
Branch: 1 branch
BranchTrueFalseTotal
defined $user_args->{'login'} ? : 0 0 0
39 };
40 }
41  
42 0 my $self = bless {}, $class;
43 0 $self->{USERS} = \%users;
44 0 $self->{USERS_ORDER} = \@users_order;
45 0 $self->{MECH_ARGS} = \%args;
46 0 return $self;
47 }
48  
49 0 ▶sub _mech {
Subroutine _mech: not called CC=2 SCAR=17.9
50 0 my $self = shift;
51 my ( $any_user ) = grep !$self->{IGNORED_USERS}{$_},
52 0 @{$self->{USERS_ORDER}};
53  
54 0 ▶ $any_user
Branch: 1 branch
BranchTrueFalseTotal
$any_user or croak("Didn't find any available users when getting any user's mech object.") 0 0 0
55 or croak q{Didn't find any available users when getting any}
56 . q{ user's mech object.};
57  
58 0 return $self->{USERS}{ $any_user }{mech};
59 }
60  
61 0 ▶sub login {
Subroutine login: not called CC=10 SCAR=47.0
Pod documented
62 0 my ( $self, %args ) = @_;
63  
64 0 my $page = delete $args{login_page};
65 0 eval {
66 0 ▶ $page = $self->_mech->uri
Branch: 1 branch
BranchTrueFalseTotal
unless defined $page 0 0 0
67 unless defined $page;
68 };
69 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
70 0 croak 'You did not give ->login() a page and mech did not yet'
71 . ' access any pages. Cannot proceed further';
72 }
73  
74 0 my $users = $self->{USERS};
75 0 for my $alias (
76 0 grep !$self->{IGNORED_USERS}{$_}, @{$self->{USERS_ORDER}}
77 ) {
78 0 my $mech = $users->{ $alias }{mech};
79  
80 0 $mech->get_ok(
81 $page,
82 "[$alias] get_ok($page)",
83 );
84  
85 0 my $user_args = { %args };
86 0 ▶ if ( $user_args->{fields} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_args->{"fields"}) 0 0 0
87 0 $user_args->{fields} = {%{ $user_args->{fields} }};
88 }
89  
90 0 ▶ for ( values %{ $user_args->{fields} || {} } ) {
Condition0%0/2
$user_args->{'fields'} || {}
l!l
00
MC/DC0%0/1
$user_args->{'fields'} || {}
$user_args->{'fields'}
Truth table0%0/2
A
A= $user_args->{'fields'}
Aresult
11
00
91 0 ▶ next unless ref eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "SCALAR" 0 0 0
92 0 ▶ if ( $$_ eq 'LOGIN' ) { $_ = $users->{ $alias }{login}; }
Branch: 2 branches
BranchTrueFalseTotal
if ($$_ eq 'LOGIN') { } 0 0 0
elsif ($$_ eq 'PASS') { } 0 0 0
93 0 elsif ( $$_ eq 'PASS' ) { $_ = $users->{ $alias }{pass}; }
94 }
95  
96 $mech->submit_form_ok(
97 0 $user_args,
98 "[$alias] Submitting login form",
99 );
100 }
101 }
102  
103 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=5 SCAR=34.0
104 0 my ( $self, @args ) = @_;
105  
106 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
107  
108 0 our $AUTOLOAD;
109 0 my $method = (split /::/, $AUTOLOAD)[-1];
110 0 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 0 0 0
111  
112 0 ▶ if ( $self->_mech->can($method) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->_mech->can($method)) { } 0 0 0
elsif (grep(($_ eq $method), @{$$self{'USERS_ORDER'};})) { } 0 0 0
elsif ($method eq 'any') { } 0 0 0
113 0 return $self->_call_mech_method_on_each_user( $method, \@args );
114 }
115 0 elsif ( grep $_ eq $method, @{ $self->{USERS_ORDER} } ) {
116 0 my $alias = $method;
117 0 _diag "[$alias]-only call";
118 0 return $self->{USERS}{ $alias }{mech};
119 }
120 elsif ( $method eq 'any' ) {
121 0 _diag "[any] call";
122 0 return $self->_mech;
123 }
124  
125 0 croak qq|Can't locate object method "$method" via package |
126 . __PACKAGE__;
127 }
128  
129 0 ▶sub _call_mech_method_on_each_user {
Subroutine _call_mech_method_on_each_user: not called CC=2 SCAR=17.9
130 0 my ( $self, $method, $args ) = @_;
131  
132 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
133  
134 0 my %returns;
135 0 for my $alias (
136 0 grep !$self->{IGNORED_USERS}{$_}, @{$self->{USERS_ORDER}}
137 ) {
138 0 _diag("\n[$alias] Calling ->$method()\n");
139 $returns{ $alias }
140 0 = $self->{USERS}{ $alias }{mech}->$method( @$args );
141 }
142  
143 0 $returns{any} = (values %returns)[0];
144 0 return \%returns;
145 }
146  
147 0 ▶sub remove_user {
Subroutine remove_user: not called CC=3 SCAR=24.8
Pod documented
148 0 my ( $self, $alias ) = @_;
149  
150 0 ▶ return unless exists $self->{USERS}{ $alias };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"USERS"}{$alias} 0 0 0
151  
152 0 @{ $self->{USERS_ORDER} }
153 0 = grep $_ ne $alias, @{ $self->{USERS_ORDER} };
154  
155 0 my $args = delete $self->{USERS}{ $alias };
156  
157 croak 'You must have at least one user and you '
158 . 'just removed the last one'
159 0 ▶ unless @{ $self->{USERS_ORDER} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"USERS_ORDER"};} 0 0 0
160  
161 0 return ( $alias, $args );
162 }
163  
164 0 ▶sub add_user {
Subroutine add_user: not called CC=2 SCAR=17.9
Pod documented
165 0 my ( $self, $alias, $args ) = @_;
166  
167 0 my $mech = Test::WWW::Mechanize->new( %{ $self->{MECH_ARGS} } );
168  
169 $self->{USERS}{ $alias } = {
170 0 ▶ %{ $args || {} },
Condition0%0/2
$args || {}
l!l
00
MC/DC0%0/1
$args || {}
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
171 mech => $mech,
172 };
173  
174 0 @{ $self->{USERS_ORDER} } = (
175 0 ( grep $_ ne $alias, @{ $self->{USERS_ORDER} } ),
176 $alias,
177 );
178  
179 0 return;
180 }
181  
182 0 ▶sub all_users {
Subroutine all_users: not called CC=2 SCAR=17.9
Pod documented
183 0 my $self = shift;
184 0 my $is_include_ignored = shift;
185 return $is_include_ignored
186 0 ? @{ $self->{USERS_ORDER} }
187 0 ▶ : grep !$self->{IGNORED_USERS}{ $_ }, @{ $self->{USERS_ORDER} };
Branch: 1 branch
BranchTrueFalseTotal
$is_include_ignored ? : 0 0 0
188 }
189  
190 0 ▶sub ignore_user {
Subroutine ignore_user: not called CC=3 SCAR=24.8
Pod documented
191 0 my ( $self, $alias ) = @_;
192  
193 0 ▶ return unless exists $self->{USERS}{ $alias };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"USERS"}{$alias} 0 0 0
194  
195 0 $self->{IGNORED_USERS}{ $alias } = 1;
196 0 ▶ if ( keys %{$self->{IGNORED_USERS}} eq @{ $self->{USERS_ORDER} } ){
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$self{"IGNORED_USERS"};} eq @{$self->{"USERS_ORDER"};}) 0 0 0
197 0 croak q{You ignored all your users. Can't function without at least
198 one active user};
199 }
200 }
201  
202 0 ▶sub unignore_user {
Subroutine unignore_user: not called CC=1 SCAR=6.9
Pod documented
203 0 my ( $self, $alias ) = @_;
204  
205 0 delete $self->{IGNORED_USERS}{ $alias };
206 }
207  
208 2 q|
209 Why programmers like UNIX: unzip, strip, touch, finger, grep, mount, fsck,
210 more, yes, fsck, fsck, fsck, umount, sleep
211 |;
212 __END__
↑ Summary