Author: geoff Date: Thu Jan 27 12:11:40 2005 New Revision: 128421 URL: http://svn.apache.org/viewcvs?view=rev&rev=128421 Log: add ability to run t/foo/bar.php using php and the generated php.ini
Added: httpd/test/trunk/perl-framework/Apache-Test/lib/Apache/TestHarnessPHP.pm Modified: httpd/test/trunk/perl-framework/Apache-Test/lib/Apache/TestRunPHP.pm Added: httpd/test/trunk/perl-framework/Apache-Test/lib/Apache/TestHarnessPHP.pm Url: http://svn.apache.org/viewcvs/httpd/test/trunk/perl-framework/Apache-Test/lib/Apache/TestHarnessPHP.pm?view=auto&rev=128421 ============================================================================== --- (empty file) +++ httpd/test/trunk/perl-framework/Apache-Test/lib/Apache/TestHarnessPHP.pm Thu Jan 27 12:11:40 2005 @@ -0,0 +1,115 @@ +# 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::TestHarnessPHP; + +use strict; +use warnings FATAL => 'all'; + +use File::Spec::Functions qw(catfile catdir); +use File::Find qw(finddepth); +use Apache::TestHarness (); + +use vars qw(@ISA); [EMAIL PROTECTED] = qw(Apache::TestHarness); + +# Test::Harness didn't start using Test::Harness::Straps until 5.8.0 +# everything except t/foo.php with earlier perls, so let things +# go for the moment +eval { + require Test::Harness::Straps; + push @ISA, qw(Test::Harness::Straps); + $Test::Harness::Strap = __PACKAGE__->new; +}; + +sub get_tests { + + my $self = shift; + my $args = shift; + my @tests = (); + + my $base = -d 't' ? catdir('t', '.') : '.'; + + my $ts = $args->{tests} || []; + + if (@$ts) { + for (@$ts) { + if (-d $_) { + push(@tests, sort <$base/$_/*.t>); + push(@tests, sort <$base/$_/*.php>); + } + else { + $_ .= ".t" unless /(\.t|\.php)$/; + push(@tests, $_); + } + } + } + else { + if ($args->{tdirs}) { + push @tests, map { sort <$base/$_/*.t> } @{ $args->{tdirs} }; + push @tests, map { sort <$base/$_/*.php> } @{ $args->{tdirs} }; + } + else { + finddepth(sub { + return unless /(\.t|\.php)$/; + my $t = catfile $File::Find::dir, $_; + my $dotslash = catfile '.', ""; + $t =~ s:^\Q$dotslash::; + push @tests, $t + }, $base); + @tests = sort @tests; + } + } + + @tests = $self->prune(@tests); + + if (my $skip = $self->skip) { + # Allow / \ and \\ path delimiters in SKIP file + $skip =~ s![/\\\\]+![/\\\\]!g; + + @tests = grep { not /(?:$skip)/ } @tests; + } + + Apache::TestSort->run([EMAIL PROTECTED], $args); + + #when running 't/TEST t/dir' shell tab completion adds a / + #dir//foo output is annoying, fix that. + s:/+:/:g for @tests; + + return @tests; +} + +sub run { + my $self = shift; + my $args = shift || {}; + + $Test::Harness::verbose ||= $args->{verbose}; + + if (my(@subtests) = @{ $args->{subtests} || [] }) { + $ENV{HTTPD_TEST_SUBTESTS} = "@subtests"; + } + + Test::Harness::runtests($self->get_tests($args, @_)); +} + +sub _command { + return 'php'; +} + +sub _switches { + my $ini = catfile(Apache::Test::vars('server_root'), qw(conf php.ini)); + return "--php-ini $ini"; +} + +1; Modified: httpd/test/trunk/perl-framework/Apache-Test/lib/Apache/TestRunPHP.pm Url: http://svn.apache.org/viewcvs/httpd/test/trunk/perl-framework/Apache-Test/lib/Apache/TestRunPHP.pm?view=diff&rev=128421&p1=httpd/test/trunk/perl-framework/Apache-Test/lib/Apache/TestRunPHP.pm&r1=128420&p2=httpd/test/trunk/perl-framework/Apache-Test/lib/Apache/TestRunPHP.pm&r2=128421 ============================================================================== --- httpd/test/trunk/perl-framework/Apache-Test/lib/Apache/TestRunPHP.pm (original) +++ httpd/test/trunk/perl-framework/Apache-Test/lib/Apache/TestRunPHP.pm Thu Jan 27 12:11:40 2005 @@ -17,12 +17,13 @@ use strict; use warnings FATAL => 'all'; -use File::Spec::Functions qw(catfile); +use File::Spec::Functions qw(catfile canonpath); use Apache::TestRun (); use Apache::TestConfigParse (); use Apache::TestTrace; use Apache::TestConfigPHP (); +use Apache::TestHarnessPHP (); use vars qw($VERSION); $VERSION = '1.00'; # make CPAN.pm's r() version scanner happy @@ -76,6 +77,78 @@ $self->configure_php; } +my @request_opts = qw(get post head); + +sub run_tests { + my $self = shift; + + my $test_opts = { + verbose => $self->{opts}->{verbose}, + tests => $self->{tests}, + times => $self->{opts}->{times}, + order => $self->{opts}->{order}, + subtests => $self->{subtests} || [], + }; + + if (grep { exists $self->{opts}->{$_} } @request_opts) { + run_request($self->{test_config}, $self->{opts}); + } + else { + Apache::TestHarnessPHP->run($test_opts) + if $self->{opts}->{'run-tests'}; + } +} + +sub split_test_args { + my($self) = @_; + + my(@tests); + my $top_dir = $self->{test_config}->{vars}->{top_dir}; + my $t_dir = $self->{test_config}->{vars}->{t_dir}; + + my $argv = $self->{argv}; + my @leftovers = (); + for (@$argv) { + my $arg = $_; + # need the t/ (or t\) for stat-ing, but don't want to include + # it in test output + $arg =~ [EMAIL PROTECTED](?:\.[\\/])?t[\\/]@@; + my $file = catfile $t_dir, $arg; + if (-d $file and $_ ne '/') { + my @files = <$file/*.t>; + push @files, <$file/*.php>; + my $remove = catfile $top_dir, ""; + if (@files) { + push @tests, map { s,^\Q$remove,,; $_ } @files; + next; + } + } + else { + if (($file =~ /\.t$/ || $file =~ /\.php$/) and -e $file) { + push @tests, "t/$arg"; + next; + } + elsif (-e "$file.t") { + push @tests, "t/$arg.t"; + next; + } + elsif (/^[\d.]+$/) { + my @t = $_; + #support range of subtests: t/TEST t/foo/bar 60..65 + if (/^(\d+)\.\.(\d+)$/) { + @t = $1..$2; + } + + push @{ $self->{subtests} }, @t; + next; + } + } + push @leftovers, $_; + } + + $self->{tests} = [ map { canonpath($_) } @tests ]; + $self->{argv} = [EMAIL PROTECTED]; +} 1; __END__ @@ -209,13 +282,13 @@ something like this after running the tests in verbose mode (eg C<make test TEST_VERBOSE=1>): - t/php/foo....1..6 + 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/TestPHP/foo.php at line 13) + # 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/TestPHP/foo.php at line 17) + # Failed test (/src/devel/perl-php-test/t/response/TestFoo/bar.php at line 17) # got: 'baz' # expected: 'bar' ok 5 - bar is not beer @@ -226,19 +299,31 @@ Failed 3/6 tests, 50.00% okay Failed Test Stat Wstat Total Fail Failed List of Failed ------------------------------------------------------------------------------- - t/php/foo.t 6 3 50.00% 2 4 7 + t/php/bar.t 6 3 50.00% 2 4 7 Failed 1/1 test scripts, 0.00% okay. 1/6 subtests failed, 83.33% okay. +note that the actual test file that was run was C<t/php/bar.t>. this +file is autogenerated based on the C<t/response/TestFoo/bar.php> +pattern of your PHP script. C<t/php/bar.t> happens to be written in +Perl, but you really don't need to worry about it too much. + +as an interesting aside, if you are using perl-5.8.0 or later you can +actually create your own C<t/foo.php> client-side scripts and they +will be run via php (using our C<php.ini>). but more on that later... + =head1 SEE ALSO -The Apache-Test tutorial: +the best source of information about using Apache-Test with +PHP (at this time) is probably the talk given at ApacheCon 2004 +(L<http://xrl.us/phpperl>), as well as the code from the talk +(L<http://xrl.us/phpperlcode>). there is also the online tutorial L<http://perl.apache.org/docs/general/testing/testing.html> -as all of the mod_perl-specific syntax and features have been +which has 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 +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
