WWW/Mechanize/PhantomJS/Catalyst.pm

filter: Statementstmt 25.5% 25 / 98 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 40.0% 6 / 15 Podpod 50.0% 2 / 4 totaltotal 23.4% 33 / 141 CC 13 SCAR 24.0
CC
13
Cov
20%
start_catalyst_server
26.7
new
20.0
DESTROY
12.0
CRAP 11.0
« Test/WWW/Mechanize/PhantomJS/Catalyst.pm ↑ Summary
1 package WWW::Mechanize::PhantomJS::Catalyst;
2  
3 2 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Catalyst::EngineLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use HTTP::Server::PSGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 extends 'WWW::Mechanize::PhantomJS' => { -version => 0.03 };
10  
11 2 our $VERSION = '0.01';
12  
13 2 has app => (
14 is => 'ro',
15 isa => 'ClassName',
16 required => 1,
17 );
18  
19 2 has debug => (
20 is => 'rw',
21 isa => 'Bool',
22 );
23  
24 2 has server_pid => (
25 is => 'rw',
26 isa => 'Int',
27 );
28  
29 2 has server => (
30 is => 'rw',
31 );
32  
33 2 has port => (
34 is => 'rw',
35 isa => 'Int',
36 );
37  
38 sub new
39 {
40 0 ▶ my ($class, @opt) = @_;
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
41 0 my $self = $class->SUPER::new(@opt);
42 0 my ( $port, $status) = $self->start_catalyst_server;
43 0 ▶ if ( $status ne 'ready') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status ne "ready") 0 0 0
44 0 ▶ print STDERR "[$$] Catalyst server failed to start\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
45 0 return undef;
46 }
47 0 $self->port($port);
48 0 ▶ print STDERR "[$$] Catalyst server started ok\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
49 0 return $self;
50 }
51  
52 sub DESTROY
53 {
54 0 ▶ my ($self) = @_;
Subroutine DESTROY: not called CC=3 SCAR=24.8
55 0 ▶ if ($self->server_pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->server_pid) 0 0 0
56 0 local $?;
57 0 ▶ print STDERR "[$$] Waiting for Catalyst server [", $self->server_pid, "] to finish..\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
58 0 kill 2, $self->server_pid;
59 0 local $SIG{PIPE} = 'IGNORE';
60 0 close $self->server;
61 }
62 0 $self->SUPER::DESTROY;
63 };
64  
65 sub test_port {
66 0 ▶ my ($port) = @_;
Subroutine test_port: not called CC=1 SCAR=6.9
Pod undocumented
67 0 IO::Socket::INET->new(
68 Listen => 5,
69 Proto => 'tcp',
70 Reuse => 1,
71 LocalPort => $port
72 );
73 }
74  
75 sub start_catalyst_server {
76 0 ▶ my ($self) = @_;
Subroutine start_catalyst_server: not called CC=5 SCAR=32.9
Pod undocumented
77  
78 0 my $pid;
79 0 ▶ if (my $pid = open my $server, '-|') {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = open(my $server, '-|')) { } 0 0 0
80 0 $self->server_pid($pid);
81 0 $self->server($server);
82 0 my $port = <$server>;
83 0 chomp $port;
84 0 my $status = <$server>;
85 0 chomp $status;
86 0 return $port, $status;
87 }
88 else {
89 0 require Catalyst::ScriptRunner;
90 0 require Catalyst::Script::Server;
91
92 0 my $css_pla = \&Catalyst::Script::Server::_plack_loader_args;
93 my $new_css_pla = sub {
94 0 ▶ my %args = $css_pla->(@_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
95 0 my $sr = delete $args{server_ready};
96 $args{server_ready} = sub {
97 0 ▶ print "ready\n";
Subroutine __ANON__: not called CC=2 SCAR=17.9
98 0 ▶ $sr ? $sr->(@_) : ();
Branch: 1 branch
BranchTrueFalseTotal
$sr ? : 0 0 0
99 0 };
100 0 return %args;
101 0 };
102
103 0 my $css_run = \&Catalyst::Script::Server::_run_application;
104 my $new_css_run = sub {
105 0 ▶ my $ret;
Subroutine __ANON__: not called CC=2 SCAR=17.9
106 0 eval { $ret = $css_run->(@_); };
107 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
108 0 my $msg = $@;
109 0 print STDERR "$@\n";
110 0 print "fail\n";
111 0 die $@;
112 } else {
113 0 return $ret;
114 }
115 0 };
116  
117 # avoid race condition between testing and using port
118 0 my $socket;
119 0 my $hsp_sl = \&HTTP::Server::PSGI::setup_listener;
120 my $new_hsp_sl = sub {
121 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 0 $self->{listen_sock} = $socket;
123 0 return $hsp_sl->($self,@_);
124 0 };
125  
126 {
127 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.2
128 0 *Catalyst::Script::Server::_plack_loader_args = $new_css_pla;
129 0 *Catalyst::Script::Server::_run_application = $new_css_run;
130 0 *HTTP::Server::PSGI::setup_listener = $new_hsp_sl;
131 }
132  
133 0 my ($port, $catalyst) = (4000);
134 0 while (1) {
135 0 ▶ $port++, next unless $socket = test_port($port);
Branch: 1 branch
BranchTrueFalseTotal
unless $socket = test_port($port) 0 0 0
136 0 ▶ print STDERR "[$$] Starting Catalyst server on port $port..\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
137 0 print "$port\n";
138 0 @ARGV = ('-p', $port);
139 0 Catalyst::ScriptRunner->run($self->app, 'Server');
140 0 ▶ print STDERR "[$$] Catalyst server exited early, aborting\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
141 0 print "fail\n";
142 0 exit 0;
143 }
144 }
145 }
146  
147 sub get {
148 0 ▶ my ($self, $url, %options ) = @_;
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
149 0 ▶ $url = "http://localhost:".$self->port."/$url" unless $url =~ /^http:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ /^http:/ 0 0 0
150 0 return $self->SUPER::get($url, %options);
151 }
152  
153 2 1;
154  
155 __END__
156  
157 =pod
158  
159 =head1 NAME
160  
161 WWW::Mechanize::PhantomJS::Catalyst - mechanize javascript in your catalyst apps
162  
163 =head1 DESCRIPTION
164  
165 A mix of L<WWW::Mechanize::Catalyst> and L<WWW::Mechanize::PhantomJS>, exposes a
166 WWW::Mechanize API for driving a catalyst server running in the background by PhantomJS/GhostDriver.
167  
168 =head1 SYNOPSIS
169  
170 use WWW::Mechanize::PhantomJS::Catalyst;
171 my $mech = WWW::Mechanize::PhantomJS::Catalyst->new(app => 'MyApp');
172 $mech->get("/hello.html");
173  
174 =head1 PREREQUISITES
175  
176 You'll need PhantomJS v1.9+, and WWW::Mechanize::PhantomJS from here:
177 L<https://github.com/dk/www-mechanize-phantomjs> ( or v0.04 from the official
178 distro when pull requests from github will be incorporated and published). The
179 same is also for valid the latest Selenium::Remote::Driver
180 (https://github.com/dk/Selenium-Remote-Driver or 0.2002 when out)
181  
182 =head1 AUTHOR
183  
184 Dmitry Karasik E<lt>dmitry@karasik.eu.orgE<gt>.
185  
186 =head1 COPYRIGHT
187  
188 This program is distributed under the standard Perl licence.
189  
190 Half of the code is borrowed from Stefan Seifert's L<Test::WWW::WebKit::Catalyst>, reused under the same licence.
191  
192 =cut
« Test/WWW/Mechanize/PhantomJS/Catalyst.pm ↑ Summary