Apache-Test

 view release on metacpan or  search on metacpan

lib/Apache/TestRunPHP.pm  view on Meta::CPAN


=head1 EXAMPLE

C<TestRunPHP> works almost identially to C<TestRunPerl>, but in
case you are new to C<Apache-Test> here is a quick getting started
guide.  be sure to see the links at the end of this document for
places to find additional details.

because C<Apache-Test> is a Perl-based testing framework we start
from a C<Makefile.PL>, which should have the following lines (in
addition to the standard C<Makefile.PL> parts):

  use Apache::TestMM qw(test clean);
  use Apache::TestRunPHP ();

  Apache::TestMM::filter_args();

  Apache::TestRunPHP->generate_script();

C<generate_script()> will create a script named C<t/TEST>, the gateway
to the Perl testing harness and what is invoked when you call
C<make test>.  C<filter_args()> accepts some C<Apache::Test>-specific
arguments and passes them along.  for example, to point to a specific
C<httpd> installation you would invoke C<Makefile.PL> as follows

  $ perl Makefile.PL -httpd /my/local/apache/bin/httpd

and C</my/local/apache/bin/httpd> will be propagated throughout the
rest of the process.  note that PHP needs to be active within Apache
prior to configuring the test framework as shown above, either by
virtue of PHP being compiled into the C<httpd> binary statically or
through an active C<LoadModule> statement within the configuration
located in C</my/local/apache/conf/httpd.conf>.  Other required modules
are the (very common) mod_alias and mod_env.

now, like with C<Apache::TestRun> and C<Apache::TestRunPerl>, you can
place client-side Perl test scripts under C<t/>, such as C<t/01basic.t>,
and C<Apache-Test> will run these scripts when you call C<make test>.
however, what makes C<Apache::TestRunPHP> unique is some added magic
specifically tailored to a PHP environment.  here are the mechanics.

C<Apache::TestRunPHP> will look for PHP test scripts in that match
the following pattern

  t/response/TestFoo/bar.php

where C<Foo> and C<bar> can be anything you like, and C<t/response/Test*>
is case sensitive.  when this format is adhered to, C<Apache::TestRunPHP>
will create an associated Perl test script called C<t/foo/bar.t>, which
will be executed when you call C<make test>.  all C<bar.t> does is issue
a simple GET to C<bar.php>, leaving the actual testing to C<bar.php>.  in
essence, you can forget that C<bar.t> even exists.

what does C<bar.php> look like?  here is an example:

  <?php
    print "1..1\n";
    print "ok 1\n"
  ?>

if it looks odd, that's ok because it is.  I could explain to you exactly
what this means, but it isn't important to understand the gory details.
instead, it is sufficient to understand that when C<Apache::Test> calls
C<bar.php> it feeds the results directly to C<Test::Harness>, a module
that comes with every Perl installation, and C<Test::Harness> expects
what it receives to be formated in a very specific way.  by itself, all
of this is pretty useless, so C<Apache::Test> provides PHP testers with
something much better.  here is a much better example:

  <?php
    # import the Test::More emulation layer
    # see
    #   http://search.cpan.org/dist/Test-Simple/lib/Test/More.pm
    # for Perl's documentation - these functions should behave
    # in the same way
    require 'test-more.php';

    # plan() the number of tests
    plan(6);

    # call ok() for each test you plan
    ok ('foo' == 'foo', 'foo is equal to foo');
    ok ('foo' != 'foo', 'foo is not equal to foo');

    # ok() can be other things as well
    is ('bar', 'bar', 'bar is bar');
    is ('baz', 'bar', 'baz is baz');
    isnt ('bar', 'beer', 'bar is not beer');
    like ('bar', '/ar$/', 'bar matches ar$');

    diag("printing some debugging information");

    # whoops! one too many tests.  I wonder what will happen...
    is ('biff', 'biff', 'baz is a baz');
  ?>

the include library C<test-more.php> is automatically generated by
C<Apache::TestConfigPHP> and configurations tweaked in such a
a way that your PHP scripts can find it without issue.  the
functions provided by C<test-more.php> are equivalent in name and
function to those in C<Test::More>, a standard Perl testing
library, so you can see that manpage for details on the syntax
and functionality of each.

at this point, we have enough in place to run some tests from
PHP-land - a C<Makefile.PL> to configure Apache for us, and
a PHP script in C<t/response/TestFoo/bar.php> to send some
results out to the testing engine.  issuing C<make test>
would start Apache, issue the request to C<bar.php>, generate
a report, and shut down Apache.  the report would look like
something like this after running the tests in verbose mode
(eg C<make test TEST_VERBOSE=1>):

  t/php/bar....1..6
  ok 1 - foo is equal to foo
  not ok 2 - foo is not equal to foo
  #     Failed test (/src/devel/perl-php-test/t/response/TestFoo/bar.php at line 13)
  ok 3 - bar is bar
  not ok 4 - baz is baz
  #     Failed test (/src/devel/perl-php-test/t/response/TestFoo/bar.php at line 17)
  #           got: 'baz'



( run in 1.501 second using v1.01-cache-2.11-cpan-39bf76dae61 )