| 1 | # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| 2 | # contributor license agreements. See the NOTICE file distributed with | ||
| 3 | # this work for additional information regarding copyright ownership. | ||
| 4 | # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| 5 | # (the "License"); you may not use this file except in compliance with | ||
| 6 | # the License. You may obtain a copy of the License at | ||
| 7 | # | ||
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | # | ||
| 10 | # Unless required by applicable law or agreed to in writing, software | ||
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | # See the License for the specific language governing permissions and | ||
| 14 | # limitations under the License. | ||
| 15 | # | ||
| 16 | package Apache::TestRunPHP; | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use warnings FATAL => 'all'; | ||
| 20 | |||
| 21 | use File::Spec::Functions qw(catfile canonpath); | ||
| 22 | |||
| 23 | use Apache::TestRun (); | ||
| 24 | use Apache::TestConfigParse (); | ||
| 25 | use Apache::TestTrace; | ||
| 26 | use Apache::TestConfigPHP (); | ||
| 27 | use Apache::TestHarnessPHP (); | ||
| 28 | |||
| 29 | use vars qw($VERSION); | ||
| 30 | $VERSION = '1.00'; # make CPAN.pm's r() version scanner happy | ||
| 31 | |||
| 32 | use File::Spec::Functions qw(catfile); | ||
| 33 | |||
| 34 | # subclass of Apache::TestRun that configures php things | ||
| 35 | use vars qw(@ISA); | ||
| 36 | @ISA = qw(Apache::TestRun); | ||
| 37 | |||
| 38 | sub start { | ||
| 39 | my $self = shift; | ||
| 40 | |||
| 41 | # point php to our own php.ini file | ||
| 42 | $ENV{PHPRC} = catfile $self->{test_config}->{vars}->{serverroot}, | ||
| 43 | 'conf'; | ||
| 44 | |||
| 45 | $self->SUPER::start(@_); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub new_test_config { | ||
| 49 | my $self = shift; | ||
| 50 | |||
| 51 | Apache::TestConfigPHP->new($self->{conf_opts}); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub configure_php { | ||
| 55 | my $self = shift; | ||
| 56 | |||
| 57 | my $test_config = $self->{test_config}; | ||
| 58 | |||
| 59 | $test_config->postamble_register(qw(configure_php_inc | ||
| 60 | configure_php_ini | ||
| 61 | configure_php_functions | ||
| 62 | configure_php_tests)); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub configure { | ||
| 66 | my $self = shift; | ||
| 67 | |||
| 68 | $self->configure_php; | ||
| 69 | |||
| 70 | $self->SUPER::configure; | ||
| 71 | } | ||
| 72 | |||
| 73 | #if Apache::TestRun refreshes config in the middle of configure | ||
| 74 | #we need to re-add php configure hooks | ||
| 75 | sub refresh { | ||
| 76 | my $self = shift; | ||
| 77 | $self->SUPER::refresh; | ||
| 78 | $self->configure_php; | ||
| 79 | } | ||
| 80 | |||
| 81 | my @request_opts = qw(get post head); | ||
| 82 | |||
| 83 | sub run_tests { | ||
| 84 | my $self = shift; | ||
| 85 | |||
| 86 | my $test_opts = { | ||
| 87 | verbose => $self->{opts}->{verbose}, | ||
| 88 | tests => $self->{tests}, | ||
| 89 | order => $self->{opts}->{order}, | ||
| 90 | subtests => $self->{subtests} || [], | ||
| 91 | }; | ||
| 92 | |||
| 93 | if (grep { exists $self->{opts}->{$_} } @request_opts) { | ||
| 94 | run_request($self->{test_config}, $self->{opts}); | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | Apache::TestHarnessPHP->run($test_opts) | ||
| 98 | if $self->{opts}->{'run-tests'}; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | sub split_test_args { | ||
| 103 | my($self) = @_; | ||
| 104 | |||
| 105 | my(@tests); | ||
| 106 | my $top_dir = $self->{test_config}->{vars}->{top_dir}; | ||
| 107 | my $t_dir = $self->{test_config}->{vars}->{t_dir}; | ||
| 108 | |||
| 109 | my $argv = $self->{argv}; | ||
| 110 | my @leftovers = (); | ||
| 111 | for (@$argv) { | ||
| 112 | my $arg = $_; | ||
| 113 | # need the t/ (or t\) for stat-ing, but don't want to include | ||
| 114 | # it in test output | ||
| 115 | $arg =~ s@^(?:\.[\\/])?t[\\/]@@; | ||
| 116 | my $file = catfile $t_dir, $arg; | ||
| 117 | if (-d $file and $_ ne '/') { | ||
| 118 | my @files = <$file/*.t>; | ||
| 119 | push @files, <$file/*.php>; | ||
| 120 | my $remove = catfile $top_dir, ""; | ||
| 121 | if (@files) { | ||
| 122 | push @tests, map { s,^\Q$remove,,; $_ } @files; | ||
| 123 | next; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | else { | ||
| 127 | if (($file =~ /\.t$/ || $file =~ /\.php$/) and -e $file) { | ||
| 128 | push @tests, "t/$arg"; | ||
| 129 | next; | ||
| 130 | } | ||
| 131 | elsif (-e "$file.t") { | ||
| 132 | push @tests, "t/$arg.t"; | ||
| 133 | next; | ||
| 134 | } | ||
| 135 | elsif (/^[\d.]+$/) { | ||
| 136 | my @t = $_; | ||
| 137 | #support range of subtests: t/TEST t/foo/bar 60..65 | ||
| 138 | if (/^(\d+)\.\.(\d+)$/) { | ||
| 139 | @t = $1..$2; | ||
| 140 | } | ||
| 141 | |||
| 142 | push @{ $self->{subtests} }, @t; | ||
| 143 | next; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | push @leftovers, $_; | ||
| 147 | } | ||
| 148 | |||
| 149 | $self->{tests} = [ map { canonpath($_) } @tests ]; | ||
| 150 | $self->{argv} = \@leftovers; | ||
| 151 | } | ||
| 152 | 1; | ||
| 153 | __END__ |