]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/CLI.pm
Oops, that was important. :(
[maypole.git] / lib / Maypole / CLI.pm
1 package Maypole::CLI;
2 use UNIVERSAL::require;
3 use URI; use URI::QueryParam;
4 use Maypole::Constants;
5
6 use strict;
7 use warnings;
8 my $package;
9 our $buffer;
10 sub import { 
11     $package = $_[1];
12     $package->require;
13     die "Couldn't require $package - $@" if $@;
14     no strict 'refs';
15     unshift @{$package."::ISA"}, "Maypole::CLI";
16 }
17
18 sub get_request {}
19 sub get_template_root { $ENV{MAYPOLE_TEMPLATES} || "." }
20
21 sub parse_location {
22     my $self = shift;
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;
27     $self->parse_path;
28     $self->{query} = $url->query_form_hash;
29 }
30
31 sub send_output { $buffer = shift->{output} }
32
33 # Do it!
34 CHECK { if ((caller(0))[1] eq "-e") { 
35             $package->handler() == OK and print $buffer; 
36        } }
37
38 1;
39
40 =head1 NAME
41
42 Maypole::CLI - Command line interface to Maypole for testing and debugging
43
44 =head1 SYNOPSIS
45
46   % setenv MAYPOLE_TEMPLATES /var/www/beerdb/
47   % perl -MMaypole::CLI=BeerDB -e1 http://localhost/beerdb/brewery/frontpage
48
49 =head1 DESCRIPTION
50
51 This module is used to test Maypole sites without going through a web
52 server or modifying them to use a CGI frontend. To use it, you should
53 first either be in the template root for your Maypole site or set the
54 environment variable C<MAYPOLE_TEMPLATES> to the right value.
55
56 Next, you import the C<Maypole::CLI> module specifying your base Maypole
57 subclass. The usual way to do this is with the C<-M> flag: 
58 C<perl -MMaypole::CLI=MyApp>. This is equivalent to:
59
60     use Maypole::CLI qw(MyApp);
61
62 Now Maypole will automatically call your application's handler with the
63 URL specified as the first command line parameter. This should be the
64 full URL, starting from whatever you have defined as the C<uri_base> in
65 your application's configuration, and may include query parameters.
66
67 The Maypole HTML output should then end up on standard output.
68
69 =head1 Support for testing
70
71 The module can also be used as part of a test script. 
72
73 When used programmatically, rather than from the command line, its
74 behaviour is slightly different. 
75
76 Although the URL is taken from C<@ARGV> as normal, your application's
77 C<handler> method is not called automatically, as it is when used on the
78 command line; you need to call it manually. Additionally, when
79 C<handler> is called, the output is not printed to standard output but
80 stored in C<$Maypole::CLI::buffer>, to allow you to check the contents
81 more easily.
82
83 For instance, a test script could look like this:
84
85     use Test::More tests => 5;
86     use Maypole::CLI qw(BeerDB);
87     use Maypole::Constants;
88     $ENV{MAYPOLE_TEMPLATES} = "t/templates";
89
90     # Hack because isa_ok only supports object isa not class isa
91     isa_ok( (bless {},"BeerDB") , "Maypole");
92
93     @ARGV = ("http://localhost/beerdb/");
94     is(BeerDB->handler, OK, "OK");
95     like($Maypole::CLI::buffer, qr/frontpage/, "Got the front page");
96
97     @ARGV = ("http://localhost/beerdb/beer/list");
98     is(BeerDB->handler, OK, "OK");
99     like($Maypole::CLI::buffer, qr/Organic Best/, "Found a beer in the list");
100