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