httpd-test-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject cvs commit: httpd-test/perl-framework/Apache-Test/lib/Apache TestConfigPHP.pm TestRunPHP.pm
Date Sat, 23 Oct 2004 15:41:11 GMT
geoff       2004/10/23 08:41:10

  Modified:    perl-framework/Apache-Test Changes
  Added:       perl-framework/Apache-Test/lib/Apache TestConfigPHP.pm
                        TestRunPHP.pm
  Log:
  added Apache::TestRunPHP and Apache::TestConfigPHP classes,
  which provide a framework for server-side testing via PHP scripts
  
  Revision  Changes    Path
  1.187     +4 -0      httpd-test/perl-framework/Apache-Test/Changes
  
  Index: Changes
  ===================================================================
  RCS file: /home/cvs/httpd-test/perl-framework/Apache-Test/Changes,v
  retrieving revision 1.186
  retrieving revision 1.187
  diff -u -r1.186 -r1.187
  --- Changes	23 Oct 2004 02:13:57 -0000	1.186
  +++ Changes	23 Oct 2004 15:41:10 -0000	1.187
  @@ -8,6 +8,10 @@
   
   =item 1.16-dev
   
  +added Apache::TestRunPHP and Apache::TestConfigPHP classes,
  +which provide a framework for server-side testing via PHP scripts
  +[Geoffrey Young]
  +
   fix problem with multiple all.t files where only the final
   file was being run through the test harness.  [Geoffrey Young]
   
  
  
  
  1.1                  httpd-test/perl-framework/Apache-Test/lib/Apache/TestConfigPHP.pm
  
  Index: TestConfigPHP.pm
  ===================================================================
  # Copyright 2001-2004 The Apache Software Foundation
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  #
  package Apache::TestConfigPHP;
  
  #things specific to php
  
  use strict;
  use warnings FATAL => 'all';
  use File::Spec::Functions qw(catfile splitdir abs2rel);
  use File::Find qw(finddepth);
  use Apache::TestTrace;
  use Apache::TestRequest;
  use Apache::TestConfig;
  use Apache::TestConfigPerl;
  use Config;
  
  @Apache::TestConfigPHP::ISA = qw(Apache::TestConfig);
  
  sub new {
      return shift->SUPER::new(@_);
  }
  
  my %outside_container = map { $_, 1 } qw{
  Alias AliasMatch
  };
  
  my %strip_tags = map { $_ => 1} qw(base noautoconfig);
  
  #test .php's can have configuration after the __DATA__ token
  sub add_module_config {
      # this is just a stub at the moment until somebody gives me
      # an end-of-file PHP token that is similar to __DATA__ or __END__
  }
  
  my @extra_subdirs = qw(Response);
  
  sub configure_php_tests_pick {
      my($self, $entries) = @_;
  
      for my $subdir (@extra_subdirs) {
          my $dir = catfile $self->{vars}->{t_dir}, lc $subdir;
          next unless -d $dir;
  
          finddepth(sub {
              return unless /\.php$/;
  
              my $file = catfile $File::Find::dir, $_;
              my $module = abs2rel $file, $dir;
              my $status = $self->run_apache_test_config_scan($file);
              push @$entries, [$file, $module, $subdir, $status];
          }, $dir);
      }
  }
  
  sub write_php_test {
      my($self, $location, $test) = @_;
  
      (my $path = $location) =~ s/test//i;
      (my $file = $test) =~ s/php$/t/i;
  
      my $dir = catfile $self->{vars}->{t_dir}, lc $path;
      my $t = catfile $dir, $file;
      return if -e $t;
  
      unless (-e $t) {
          $self->gendir($dir);
          my $fh = $self->genfile($t);
  
          print $fh <<EOF;
  use Apache::TestRequest 'GET_BODY_ASSERT';
  print GET_BODY_ASSERT "/$location/$test";
  EOF
  
          close $fh or die "close $t: $!";
      }
  
      # write out an all.t file for the directory
      # that will skip running all PHP test unless have_php
  
      my $all = catfile $dir, 'all.t';
  
      unless (-e $all) {
          my $fh = $self->genfile($all);
  
          print $fh <<EOF;
  use strict;
  use warnings FATAL => 'all';
  
  use Apache::Test;
  
  # skip all tests in this directory unless a php module is enabled
  plan tests => 1, need_php;
  
  ok 1;
  EOF
      }
  }
  
  sub configure_php_inc {
      my $self = shift;
  
      my $serverroot = $self->{vars}->{serverroot};
  
      my $path = catfile $serverroot, 'conf';
  
      my $cfg = "php_value include_path $path\n";
  
      my $php = $self->{vars}->{php_module};
  
      $self->postamble(IfModule => $php, $cfg);
  }
  
  sub configure_php_functions {
      my $self = shift;
  
      my $dir  = catfile $self->{vars}->{serverroot}, 'conf';
      my $file = catfile $dir, 'more.php';
  
      $self->gendir($dir);
      my $fh = $self->genfile($file, undef, 1);
  
      while (my $line = <DATA>) {
        print $fh $line;
      }
  
      close $fh or die "close $file: $!";
  
      $self->clean_add_file($file);
  }
  
  sub configure_php_tests {
      my $self = shift;
  
      my @entries = ();
      $self->configure_php_tests_pick(\@entries);
      $self->configure_pm_tests_sort(\@entries);
  
      my %seen = ();
  
      for my $entry (@entries) {
          my ($file, $module, $subdir, $status) = @$entry;
  
          my @args = ();
  
          my $directives = $self->add_module_config($file, \@args);
  
          my @parts    = splitdir $file;
          my $test     = pop @parts;
          my $location = $parts[-1];
  
          debug "configuring PHP test file $file";
  
          if ($directives->{noautoconfig}) {
              $self->postamble(""); # which adds "\n"
          }
          else {
              unless ($seen{$location}++) {
                  $self->postamble(Alias => [ catfile('', $parts[-1]), catfile(@parts)
]);
  
                  my @args = (AddType => 'application/x-httpd-php .php');
  
                  $self->postamble(Location => "/$location", \@args);
              }
          }
  
          $self->write_php_test($location, $test);
      }
  }
  
  1;
  
  __DATA__
  <?php
  
  // more.php based on work from Andy Lester.  see
  // http://use.perl.org/~petdance/journal/14227
  
  $_test_ntests = 0;
  $_nfailures = 0;
  $_no_plan = 0;
  
  register_shutdown_function( 'test_end' );
  
  function ok( $condition, $name = '' ) {
      global $_test_ntests;
      global $_nfailures;
      $current_failures = 0;
      ++$_test_ntests;
  
      $caller = debug_backtrace();
      if (preg_match('/more.php$/', $caller[0]['file'])) {
          $file  = $caller[1]['file'];
          $line  = $caller[1]['line'];
      }
      else {
          $file  = $caller[0]['file'];
          $line  = $caller[0]['line'];
      }
  
      if ( !$condition ) {
          print "not ";
          ++$_nfailures;
          ++$current_failures;
      }
      print "ok $_test_ntests";
  
      if ( $name != "" ) {
          print " - $name";
      }
      print "\n";
  
      if ($current_failures) {
          print "#     Failed test ($file at line $line)\n";
      }
  
      return $condition;
  }
  
  function pass( $name = '' ) {
      return ok( TRUE, $name );
  }
  
  function fail( $name = '' ) {
      return ok( FALSE, $name );
  }
  
  function skip( $msg, $num ) {
      for ( $i=0; $i<$num; $i++ ) {
          pass( "# SKIP $msg" );
      }
  }
  
  function is( $actual, $expected, $name = '' ) {
      $ok = ($expected == $actual);
      ok( $ok, $name );
      if ( !$ok ) {
          diag( "          got: '$actual'" );
          diag( "     expected: '$expected'" );
      }
      return $ok;
  }
  
  function isnt( $actual, $dontwant, $name = '' ) {
      $ok = ($actual != $dontwant);
      ok( $ok, $name );
      if ( !$ok ) {
          diag( "Didn't want \"$actual\"" );
      }
      return $ok;
  }
  
  function isa_ok( $object, $class, $name = null ) {
      if ( isset( $object ) ) {
          $actual = get_class( $object );
          if ( !isset( $name ) ) {
              $name = "Object is of type $class";
          }
          return is( get_class( $object ), strtolower( $class ), $name );
      } else {
          return fail( "object is undefined" );
      }
  }
  
  function like( $string, $regex, $name='' ) {
      return ok( preg_match( $regex, $string ), $name );
  }
  
  // We diag() to stderr instead of stdout like Perl does.  It shouldn't
  // make any difference to the harness.
  function diag( $lines ) {
      if ( is_string( $lines ) ) {
          $lines = split( "\n", $lines );
      }
      foreach ( $lines as $str ) {
          print "# $str\n";
      }
  }
  
  function plan( $ntests ) {
      print "1..$ntests\n";
  }
  
  function no_plan() {
      global $_no_plan;
  
      $_no_plan = 1;
  }
  
  function test_end() {
      global $_no_plan;
      global $_test_ntests;
      if ( $_no_plan ) {
          print "1..$_test_ntests\n";
      }
  
      // reset for next run
      global $_test_ntests;
      global $_no_plan;
      global $_nfailures;
      $_test_ntests = 0;
      $_no_plan = 0;
  
      $ver = phpversion();
      if ( version_compare( $ver, '4.2.2' ) > 0 ) { # >4.2.2?
          $_report_failures = $_nfailures;
          $_nfailures = 0;
          exit( $_report_failures > 254 ? 254 : $_report_failures );
      } else {
          $_nfailures = 0;
          # Don't return anything
      }
  }
  
  function html_ok( $str, $name = "" ) {
      $fname = tempnam( getenv("TMP"), 'lint-' );
      $fh = fopen( $fname, "w" );
      fwrite( $fh, $str );
      fclose( $fh );
  
      $results = Array();
      $results = shell_exec( "weblint $fname" );
      unlink( $fname );
  
      if ( $results ) {
          $ok = fail( $name );
          diag( $results );
      } else {
          $ok = pass( $name );
      }
      return $ok;
  }
  
  ?>
  
  
  
  1.1                  httpd-test/perl-framework/Apache-Test/lib/Apache/TestRunPHP.pm
  
  Index: TestRunPHP.pm
  ===================================================================
  # Copyright 2001-2004 The Apache Software Foundation
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  #
  package Apache::TestRunPHP;
  
  use strict;
  use warnings FATAL => 'all';
  
  use Apache::TestRun ();
  use Apache::TestConfigParse ();
  use Apache::TestTrace;
  use Apache::TestConfigPHP ();
  
  use vars qw($VERSION);
  $VERSION = '1.00'; # make CPAN.pm's r() version scanner happy
  
  use File::Spec::Functions qw(catfile);
  
  #subclass of Apache::TestRun that configures mod_perlish things
  use vars qw(@ISA);
  @ISA = qw(Apache::TestRun);
  
  sub new_test_config {
      my $self = shift;
  
      Apache::TestConfigPHP->new($self->{conf_opts});
  }
  
  sub configure_php {
      my $self = shift;
  
      my $test_config = $self->{test_config};
  
      $test_config->postamble_register(qw(configure_php_inc
                                          configure_php_functions
                                          configure_php_tests));
  }
  
  sub configure {
      my $self = shift;
  
      $self->configure_php;
  
      $self->SUPER::configure;
  }
  
  #if Apache::TestRun refreshes config in the middle of configure
  #we need to re-add php configure hooks
  sub refresh {
      my $self = shift;
      $self->SUPER::refresh;
      $self->configure_php;
  }
  
  1;
  __END__
  
  =head1 NAME
  
  Apache::TestRunPHP - configure and run a PHP-based test suite
  
  =head1 SYNOPSIS
  
    use Apache::TestRunPHP;
    Apache::TestRunPHP->new->run(@ARGV);
  
  =head1 DESCRIPTION
  
  The C<Apache::TestRunPHP> package controls the configuration and
  running of the test suite for PHP-based tests.  It's a subclass
  of C<Apache::TestRun> and similar in function to C<Apache::TestRunPerl>.
  
  Refer to the C<Apache::TestRun> manpage for information on the
  available API.
  
  =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.
  
  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://www.perldoc.com/perl5.8.4/lib/Test/More.html
      // for more information
      require '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<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<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
  
    t/php/foo....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/TestPHP/foo.php at line 13)
    ok 3 - bar is bar
    not ok 4 - baz is baz
    #     Failed test (/src/devel/perl-php-test/t/response/TestPHP/foo.php at line 17)
    #           got: 'baz'
    #      expected: 'bar'
    ok 5 - bar is not beer
    ok 6 - bar matches ar$
    # printing some debugging information
    ok 7 - baz is a baz
    FAILED tests 2, 4, 7
            Failed 3/6 tests, 50.00% okay
    Failed Test Stat Wstat Total Fail  Failed  List of Failed
    Failed Test Stat Wstat Total Fail  Failed  List of Failed
    -------------------------------------------------------------------------------
    t/php/foo.t                6    3  50.00%  2 4 7
    Failed 1/1 test scripts, 0.00% okay. 1/6 subtests failed, 83.33% okay.
  
  =head1 SEE ALSO
  
  The Apache-Test tutorial:
  L<http://perl.apache.org/docs/general/testing/testing.html>
  as all of the mod_perl-specific syntax and features have been
  ported to PHP with this class.
  
  =head1 AUTHOR
  
  C<Apache::Test> is a community effort, maintained by a group of
  dedicated volunteers.
  
  Questions can be asked at the test-dev <at> httpd.apache.org list
  For more information see: http://httpd.apache.org/test/.
  
  =cut
  
  
  

Mime
View raw message