2 use UNIVERSAL::require;
3 use URI; use URI::QueryParam;
4 use Maypole::Constants;
13 die "Couldn't require $package - $@" if $@;
15 unshift @{$package."::ISA"}, "Maypole::CLI";
19 sub get_template_root { $ENV{MAYPOLE_TEMPLATES} || "." }
23 my $url = URI->new(shift @ARGV);
24 my $root = URI->new($self->config->{uri_base})->path;
25 $self->{path} = $url->path;
26 $self->{path} =~ s/^$root//i if $root;
33 $self->{params} = $url->query_form_hash;
34 $self->{query} = $url->query_form_hash;
37 sub send_output { $buffer = shift->{output} }
40 CHECK { if ((caller(0))[1] eq "-e") {
41 $package->handler() == OK and print $buffer;
48 Maypole::CLI - Command line interface to Maypole for testing and debugging
52 % setenv MAYPOLE_TEMPLATES /var/www/beerdb/
53 % perl -MMaypole::CLI=BeerDB -e1 http://localhost/beerdb/brewery/frontpage
57 This module is used to test Maypole sites without going through a web
58 server or modifying them to use a CGI frontend. To use it, you should
59 first either be in the template root for your Maypole site or set the
60 environment variable C<MAYPOLE_TEMPLATES> to the right value.
62 Next, you import the C<Maypole::CLI> module specifying your base Maypole
63 subclass. The usual way to do this is with the C<-M> flag:
64 C<perl -MMaypole::CLI=MyApp>. This is equivalent to:
66 use Maypole::CLI qw(MyApp);
68 Now Maypole will automatically call your application's handler with the
69 URL specified as the first command line parameter. This should be the
70 full URL, starting from whatever you have defined as the C<uri_base> in
71 your application's configuration, and may include query parameters.
73 The Maypole HTML output should then end up on standard output.
75 =head1 Support for testing
77 The module can also be used as part of a test script.
79 When used programmatically, rather than from the command line, its
80 behaviour is slightly different.
82 Although the URL is taken from C<@ARGV> as normal, your application's
83 C<handler> method is not called automatically, as it is when used on the
84 command line; you need to call it manually. Additionally, when
85 C<handler> is called, the output is not printed to standard output but
86 stored in C<$Maypole::CLI::buffer>, to allow you to check the contents
89 For instance, a test script could look like this:
91 use Test::More tests => 5;
92 use Maypole::CLI qw(BeerDB);
93 use Maypole::Constants;
94 $ENV{MAYPOLE_TEMPLATES} = "t/templates";
96 # Hack because isa_ok only supports object isa not class isa
97 isa_ok( (bless {},"BeerDB") , "Maypole");
99 @ARGV = ("http://localhost/beerdb/");
100 is(BeerDB->handler, OK, "OK");
101 like($Maypole::CLI::buffer, qr/frontpage/, "Got the front page");
103 @ARGV = ("http://localhost/beerdb/beer/list");
104 is(BeerDB->handler, OK, "OK");
105 like($Maypole::CLI::buffer, qr/Organic Best/, "Found a beer in the list");