File Coverage

blib/lib/Test/Mojo/Role/SubmitForm.pm
Criterion Covered Total %
statement 46 50 92.0
branch 28 30 93.3
condition 12 14 85.7
subroutine 6 6 100.0
pod 1 1 100.0
total 93 101 92.0


line stmt bran cond sub pod time code
1             package Test::Mojo::Role::SubmitForm;
2              
3 1     1   98106 use Mojo::Base -base;
  1         4  
  1         7  
4 1     1   165 use Role::Tiny;
  1         2  
  1         8  
5 1     1   230 use Carp qw/croak/;
  1         6  
  1         1070  
6              
7             our $VERSION = '1.004002'; # VERSION
8              
9             sub click_ok {
10 16     16 1 309986 my ( $self, $selector_or_dom, $extra_params ) = @_;
11 16   100     107 $extra_params ||= {};
12              
13 16 100 66     32 my $el = eval { $selector_or_dom->isa('Mojo::DOM') }
  16         194  
14             ? $selector_or_dom
15             : $self->tx->res->dom->at($selector_or_dom)
16             || croak 'Test::Mojo::Role::SubmitForm::click_ok()'
17             . " Did not find element matching selector $selector_or_dom";
18 15 100       102837 unless ( $el->tag eq 'form' ) {
19 7 50       130 if (length $el->{name}) {
20 7 100 50     146 if ( ($el->{type}//'') eq 'image' ) {
21 2         41 $extra_params->{ $el->{name} . '.x' } = 1;
22 2         38 $extra_params->{ $el->{name} . '.y' } = 1;
23             }
24             else {
25 5         93 $extra_params->{ $el->{name} } = $el->val;
26             }
27             }
28              
29 7         354 $el = $el->ancestors('form')->first;
30             }
31              
32 15         2892 for ( sort keys %$extra_params ) {
33 19 100       98 next unless ref $extra_params->{$_} eq 'CODE';
34 5         17 ( my $name = $_ ) =~ s/"/\\"/g;
35 5         23 $extra_params->{$_} = $extra_params->{$_}->(
36             $self->_gather_vals( $el->find(qq{[name="$name"]})->each )
37             );
38             }
39              
40 15         59 my %form = (
41             $self->_get_controls($el),
42             %$extra_params,
43             );
44 15         62 for (keys %form) {
45 82 100       180 delete $form{$_} unless defined $form{$_}
46             }
47              
48 15 50       76 if ( $ENV{MOJO_SUBMITFORM_DEBUG} ) {
49 0         0 warn "\n########## SUBMITTING FORM ##########\n";
50 0         0 require Mojo::Util;
51 0         0 warn Mojo::Util::dumper(\%form);
52 0         0 warn "########## END FORM ##########\n\n";
53             }
54              
55             my $tx = $self->ua->build_tx(
56 15 100 100     79 $el->{method}||'GET' => defined $el->{action} && $el->{action} ne '' ? $el->{action} : $self->tx->req->url
      100        
57             => form => \%form,
58             );
59              
60 15         15439 $self->request_ok( $tx );
61             }
62              
63             sub _gather_vals {
64 123     123   10261 my ( $self, @els ) = @_;
65 123         204 my @vals;
66 123         221 for ( @els ) {
67 125 100       288 next if $_
68             ->matches('[type=radio]:not(:checked), [type=checkbox]:not(:checked)');
69              
70 109 100       42715 defined( my $val = $_->val ) or next;
71 104 100       38952 push @vals, ref $val ? @$val : $val;
72             }
73 123 100       8625 return @vals == 1 ? $vals[0] : @vals ? \@vals : undef;
    100          
74             }
75              
76             sub _get_controls {
77 15     15   47 my ( $self, $form ) = @_;
78              
79 15         62 my @els = $form->find(
80             'input:not([type=button]):not([type=submit]):not([type=image]),' . 'select, textarea'
81             )->each;
82              
83 15         39510 my %controls;
84 15         44 for ( @els ) {
85 118 100       1628 defined(my $vals = $self->_gather_vals( $_ )) or next;
86 98 100       163 push @{ $controls{$_->{name}} }, ref $vals ? @$vals : $vals;
  98         229  
87             }
88 15   100     249 $#$_ or $_= $_->[0] for values %controls; # chage 1-el arrayrefs to strings
89              
90 15         165 return %controls;
91             }
92              
93             q|
94             The fantastic element that explains the appeal of games to
95             many developers is neither the fire-breathing monsters nor the
96             milky-skinned, semi-clad sirens; it is the experience of
97             carrying out a task from start to finish without any change
98             in the user requirements.
99             |;
100              
101             __END__