| 1 | package WebDAO::Test; | ||
| 2 | |||
| 3 | #$Id$ | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | WebDAO::Test - Class for tests | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use WebDAO::Test; | ||
| 12 | |||
| 13 | my $eng = WebDAO::Test::make_engine( | ||
| 14 | class => 'MyClass', | ||
| 15 | config => 't/tests.ini', | ||
| 16 | index_file => 'contrib/www/index.xhtml' | ||
| 17 | ) | ||
| 18 | |||
| 19 | my $tlib = new WebDAO::Test::(eng =>$eng); | ||
| 20 | |||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | Class for tests | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | our $VERSION = '0.01'; | ||
| 29 | |||
| 30 | require Exporter; | ||
| 31 | @WebDAO::Test::ISA = qw(Exporter ); | ||
| 32 | |||
| 33 | #@WebDAO::Test::EXPORT = qw/ t_get_engine t_get_tlib/; | ||
| 34 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 35 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 36 | 3 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 37 | 3 | ▶ | use Test::More; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 38 | 3 | ▶ | use Digest::MD5 qw(md5 md5_hex md5_base64); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 39 | 3 | ▶ | use WebDAO::Lex; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 40 | use WebDAO::SessionSH; | ||
| 41 | use WebDAO::Engine; | ||
| 42 | our $default_engine_class = 'WebDAO::Engine'; | ||
| 43 | |||
| 44 | =head1 FUNCTIONS | ||
| 45 | |||
| 46 | =head2 t_get_engine | ||
| 47 | |||
| 48 | Return Engine | ||
| 49 | |||
| 50 | my $eng = t_get_engine ('www/index.html', config=>'tests.ini'); | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | sub main::t_get_engine { | ||
| 55 | my $index_file = shift; | ||
| 56 | my %eng_pars = @_; | ||
| 57 | if ( $index_file && -e $index_file ) { | ||
| 58 | my $content = qq!<wD><include file="$index_file"/></wD>!; | ||
| 59 | my $lex = new WebDAO::Lex:: content => $content; | ||
| 60 | $eng_pars{lexer} = $lex; | ||
| 61 | } | ||
| 62 | else { | ||
| 63 | $eng_pars{source} = ''; | ||
| 64 | } | ||
| 65 | my $session = new WebDAO::SessionSH::; | ||
| 66 | my $eng = $__PACKAGE__::default_engine_class->new( | ||
| 67 | session => $session, | ||
| 68 | %eng_pars | ||
| 69 | ); | ||
| 70 | return $eng; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub t_get_engine { | ||
| 74 | my $index_file = shift; | ||
| 75 | warn "$index_file" unless -e $index_file; | ||
| 76 | my %eng_pars = @_; | ||
| 77 | if ( $index_file && -e $index_file ) { | ||
| 78 | my $content = qq!<wD><include file="$index_file"/></wD>!; | ||
| 79 | my $lex = new WebDAO::Lex:: content => $content; | ||
| 80 | $eng_pars{lexer} = $lex; | ||
| 81 | } | ||
| 82 | else { | ||
| 83 | $eng_pars{source} = ''; | ||
| 84 | } | ||
| 85 | my $session = new WebDAO::SessionSH::; | ||
| 86 | my $eng = $__PACKAGE__::default_engine_class->new( | ||
| 87 | session => $session, | ||
| 88 | %eng_pars | ||
| 89 | ); | ||
| 90 | return $eng; | ||
| 91 | } | ||
| 92 | |||
| 93 | =head2 make_eng [class=><CLASS>, index_file=><file_path>, %other_params_to_engine] | ||
| 94 | |||
| 95 | Return engine object. Used params: | ||
| 96 | |||
| 97 | class - Class of engine (default -"WebDAO::Kernel"), | ||
| 98 | index_file - path to index file | ||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub make_engine { | ||
| 105 | my %eng_pars = @_; | ||
| 106 | my $class = delete $eng_pars{class} || 'WebDAO::Kernel'; | ||
| 107 | if ( my $index_file = delete $eng_pars{index_file} ) { | ||
| 108 | warn "$index_file not found!!!" unless -e $index_file; | ||
| 109 | if ( $index_file && -e $index_file ) { | ||
| 110 | open FH, "<$index_file" or die $!; | ||
| 111 | my $content =''; | ||
| 112 | { local $/=undef; | ||
| 113 | $content = <FH>; | ||
| 114 | } | ||
| 115 | close FH; | ||
| 116 | my $lex = new WebDAO::Lex:: tmpl => $content; | ||
| 117 | $eng_pars{lex} = $lex; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | my $session = new WebDAO::SessionSH::; | ||
| 121 | my $eng = $class->new( | ||
| 122 | session => $session, | ||
| 123 | %eng_pars | ||
| 124 | ); | ||
| 125 | return $eng; | ||
| 126 | |||
| 127 | } | ||
| 128 | |||
| 129 | =head2 make_test_lib <engine_object> [, <class_of_testlib>] | ||
| 130 | |||
| 131 | Return object of test class B<class_of_testlib> (default "WebDAO::Test") | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | sub make_test_lib { | ||
| 136 | my ( $eng, $tclass ) = @_; | ||
| 137 | $tclass ||= "WebDAO::Test"; | ||
| 138 | return $tclass->new( eng => $eng ); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub import { | ||
| 142 | my $self = shift; | ||
| 143 | my $engine_class = shift || $default_engine_class; | ||
| 144 | $__PACKAGE__::default_engine_class = $engine_class; | ||
| 145 | |||
| 146 | # $self->export_to_level( 1, 't_get_engine' ); | ||
| 147 | $self->export_to_level( 1, 't_get_tlib' ); | ||
| 148 | } | ||
| 149 | |||
| 150 | sub t_get_tlib { | ||
| 151 | my $eng = shift || die "need \$eng"; | ||
| 152 | my $tlib = __PACKAGE__->new( eng => $eng ); | ||
| 153 | return $tlib; | ||
| 154 | } | ||
| 155 | |||
| 156 | =head1 METHODS | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | sub new { | ||
| 161 | my $class = shift; | ||
| 162 | $class = ref($class) || $class; | ||
| 163 | my %args = @_; | ||
| 164 | my $self = bless( \%args, $class ); | ||
| 165 | return $self; | ||
| 166 | |||
| 167 | } | ||
| 168 | |||
| 169 | =head2 tree [ $contaner ] | ||
| 170 | |||
| 171 | return tree of node $contaner . default $engine | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | sub tree { | ||
| 176 | my $self = shift; | ||
| 177 | my $obj = shift || $self->{eng}; | ||
| 178 | my @res = (); | ||
| 179 | foreach my $o ( @{ $obj->_get_childs_ } ) { | ||
| 180 | push @res, $self->tree($o),; | ||
| 181 | } | ||
| 182 | return { $obj->__my_name . ":" . ref($obj) => \@res }; | ||
| 183 | } | ||
| 184 | |||
| 185 | =head2 xget | ||
| 186 | |||
| 187 | get object by path (query) | ||
| 188 | |||
| 189 | $tlib->xget('/page') | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | sub xget { | ||
| 194 | my $self = shift; | ||
| 195 | my $path = shift; | ||
| 196 | # $path =~ s/^\///; | ||
| 197 | my $eng = $self->{eng}; | ||
| 198 | |||
| 199 | #check if exists args | ||
| 200 | my $pars; | ||
| 201 | ( $path, $pars ) = split /\?/, $path; | ||
| 202 | my %args = (); | ||
| 203 | %args = map { split /\=/, $_ } split /\&/, $pars if $pars; | ||
| 204 | $eng->_session->Params( \%args ); | ||
| 205 | return $self->resolve_path( $path, @_ ); | ||
| 206 | # return $eng->resolve_path( $eng->_session, $path ); | ||
| 207 | } | ||
| 208 | |||
| 209 | =head2 resolve_path '/path/Path' | ||
| 210 | |||
| 211 | #[, arg1=>1, arg2=>2] | ||
| 212 | |||
| 213 | Query path | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | sub resolve_path { | ||
| 218 | my $self = shift; | ||
| 219 | my $path = shift; | ||
| 220 | my $sess = $self->eng->_session(); | ||
| 221 | |||
| 222 | # return $self if $path eq '/'; | ||
| 223 | |||
| 224 | #skip root objects | ||
| 225 | my ( undef, @path ) = split( /\//, $path ); | ||
| 226 | my ( $src, $res ) = ( shift || $self->eng)->_traverse_( $sess, @path ); | ||
| 227 | $res; | ||
| 228 | } | ||
| 229 | |||
| 230 | =head2 get_by_path | ||
| 231 | |||
| 232 | get object by resolve_path query | ||
| 233 | |||
| 234 | $tlib->get_by_path('/page') | ||
| 235 | $tlib->get_by_path('/page', $root_object) | ||
| 236 | =cut | ||
| 237 | |||
| 238 | sub get_by_path { | ||
| 239 | my $self = shift; | ||
| 240 | my $path = shift; | ||
| 241 | my $eng = $self->{eng}; | ||
| 242 | my $root_obj = shift || $eng; | ||
| 243 | #skip root objects | ||
| 244 | my ( undef, @path ) = split( /\//, $path ); | ||
| 245 | my ( $src, $res ) = $root_obj->_traverse_($self->eng->_session() | ||
| 246 | , @path ); | ||
| 247 | $res; | ||
| 248 | } | ||
| 249 | |||
| 250 | =head2 eng | ||
| 251 | |||
| 252 | get root object | ||
| 253 | |||
| 254 | =cut | ||
| 255 | |||
| 256 | sub eng { | ||
| 257 | return $_[0]->{eng}; | ||
| 258 | } | ||
| 259 | |||
| 260 | =head2 get_session | ||
| 261 | |||
| 262 | Get curren Session object | ||
| 263 | |||
| 264 | =cut | ||
| 265 | |||
| 266 | sub get_session { | ||
| 267 | return $_[0]->eng->_session; | ||
| 268 | } | ||
| 269 | |||
| 270 | 1; | ||
| 271 | |||
| 272 | sub make_cv { | ||
| 273 | my %args = @_; | ||
| 274 | my $out; | ||
| 275 | my $cv = WebDAO::CV->new( | ||
| 276 | env => $args{env}, | ||
| 277 | writer => sub { | ||
| 278 | new Test::Writer:: | ||
| 279 | out => \$out, | ||
| 280 | status => $_[0]->[0], | ||
| 281 | headers => $_[0]->[1]; | ||
| 282 | } | ||
| 283 | ); | ||
| 284 | |||
| 285 | } | ||
| 286 | |||
| 287 | package Test::Writer; | ||
| 288 | our $VERSION = '0.01'; | ||
| 289 | use warnings; | ||
| 290 | use strict; | ||
| 291 | sub new { | ||
| 292 | my $class = shift; | ||
| 293 | my $self = bless( ( $#_ == 0 ) ? shift : {@_}, ref($class) || $class ); | ||
| 294 | } | ||
| 295 | sub write { | ||
| 296 | ${ $_[0]->{out} } .= $_[1] ; | ||
| 297 | } | ||
| 298 | sub close { } | ||
| 299 | sub headers { return $_[0]->{headers} } | ||
| 300 | sub output { ${ $_[0]->{out} } } | ||
| 301 | |||
| 302 | 1; | ||
| 303 | |||
| 304 | package TestCV; | ||
| 305 | use strict; | ||
| 306 | use warnings; | ||
| 307 | use WebDAO::CV; | ||
| 308 | our $VERSION = '0.01'; | ||
| 309 | use base 'WebDAO::CV'; | ||
| 310 | |||
| 311 | sub new { | ||
| 312 | my $self = shift; | ||
| 313 | my $buf_ref = shift; | ||
| 314 | my $writer = sub { | ||
| 315 | new Test::Writer:: | ||
| 316 | out => $buf_ref, | ||
| 317 | status => $_[0]->[0], | ||
| 318 | headers => $_[0]->[1]; | ||
| 319 | }; | ||
| 320 | $self->SUPER::new(writer=>$writer, @_ ) | ||
| 321 | } | ||
| 322 | # for skip headers | ||
| 323 | # $cv{SKIP_HEADERS} = 1 | ||
| 324 | sub _init__ { | ||
| 325 | my $self = shift; | ||
| 326 | my $buf_ref = shift; | ||
| 327 | return $self->SUPER::_init(@_); | ||
| 328 | |||
| 329 | } | ||
| 330 | |||
| 331 | sub response__ { | ||
| 332 | my $self = shift; | ||
| 333 | my $res = shift || return; | ||
| 334 | unless ( exists( $self->{SKIP_HEADERS} ) ) { | ||
| 335 | $self->SUPER::response($res) | ||
| 336 | } else { | ||
| 337 | $self->print( $res->{data} ); | ||
| 338 | } | ||
| 339 | } | ||
| 340 | |||
| 341 | sub print__ { | ||
| 342 | my $self = shift; | ||
| 343 | my $out_ref = $self->{ctr}; | ||
| 344 | if ( grep { ! defined $_} @_ ) { | ||
| 345 | warn "NOT DEFINED IN PUT"; | ||
| 346 | |||
| 347 | } | ||
| 348 | $$out_ref .=join "",grep {defined $_ } @_ if scalar(@_); | ||
| 349 | } | ||
| 350 | |||
| 351 | |||
| 352 | 1; | ||
| 353 | __DATA__ | ||
| 354 | |||
| 355 | =head1 SEE ALSO | ||
| 356 | |||
| 357 | http://webdao.sourceforge.net | ||
| 358 | |||
| 359 | =head1 AUTHOR | ||
| 360 | |||
| 361 | Zahatski Aliaksandr, E<lt>zag@cpan.orgE<gt> | ||
| 362 | |||
| 363 | =head1 COPYRIGHT AND LICENSE | ||
| 364 | |||
| 365 | Copyright 2002-2009 by Zahatski Aliaksandr | ||
| 366 | |||
| 367 | This library is free software; you can redistribute it and/or modify | ||
| 368 | it under the same terms as Perl itself. | ||
| 369 | |||
| 370 | =cut | ||
| 371 | |||