| 1 | package Eve::TemplateStub; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 2 | ▶ | use Test::MockObject; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 2 | ▶ | use Test::MockObject::Extends; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 2 | ▶ | use Digest::MD5 (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 2 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 2 | my $stash_stub = {}; | |||||||||||||
| 13 | |||||||||||||||
| 14 | =head1 NAME | ||||||||||||||
| 15 | |||||||||||||||
| 16 | B<Eve::TemplateStub> - a stub class that replaces the template class | ||||||||||||||
| 17 | with a mock. | ||||||||||||||
| 18 | |||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||
| 20 | |||||||||||||||
| 21 | use Eve::TemplateStub; | ||||||||||||||
| 22 | use Eve::Template; | ||||||||||||||
| 23 | |||||||||||||||
| 24 | my $mocked_template = Eve::Template->new( | ||||||||||||||
| 25 | path => $path_string, | ||||||||||||||
| 26 | compile_path => $compile_path_string, | ||||||||||||||
| 27 | expiration_interval => $interval_value); | ||||||||||||||
| 28 | |||||||||||||||
| 29 | my $output = $template->process( | ||||||||||||||
| 30 | file => $file_name, var_hash => $var_hash); | ||||||||||||||
| 31 | |||||||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||||||
| 33 | |||||||||||||||
| 34 | B<Eve::TemplateStub> is a class that provides a mocked template object | ||||||||||||||
| 35 | with limited stubbed templating functionality.. | ||||||||||||||
| 36 | |||||||||||||||
| 37 | =head1 METHODS | ||||||||||||||
| 38 | |||||||||||||||
| 39 | =head2 B<mock_template()> | ||||||||||||||
| 40 | |||||||||||||||
| 41 | Returns a mocked B<Eve::Template> object. | ||||||||||||||
| 42 | |||||||||||||||
| 43 | =cut | ||||||||||||||
| 44 | |||||||||||||||
| 45 | 5 | ▶ | sub mock_template { | ||||||||||||
|
Subroutine
mock_template: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||
| 46 | 5 | my @args = @_; | |||||||||||||
| 47 | |||||||||||||||
| 48 | 5 | my $template_mock = Test::MockObject::Extends->new('Template'); | |||||||||||||
| 49 | |||||||||||||||
| 50 | $template_mock->mock( | ||||||||||||||
| 51 | 'new', | ||||||||||||||
| 52 | 5 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 53 | 5 | my ($self, undef, $arg_hash) = @_; | |||||||||||||
| 54 | |||||||||||||||
| 55 | 5 | my $result = $self; | |||||||||||||
| 56 | 5 | ▶ | if ($arg_hash->{'INCLUDE_PATH'} eq '/some/buggy/path') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | 1 | $result = undef | |||||||||||||
| 58 | } | ||||||||||||||
| 59 | |||||||||||||||
| 60 | 5 | return $result; | |||||||||||||
| 61 | 5 | }); | |||||||||||||
| 62 | |||||||||||||||
| 63 | $template_mock->mock( | ||||||||||||||
| 64 | 5 | ▶ | 'error', sub { return 'Oops'; }); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 65 | |||||||||||||||
| 66 | $template_mock->mock( | ||||||||||||||
| 67 | 'process', | ||||||||||||||
| 68 | 3 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=15.4
| |||||||||||||||
| 69 | 3 | my (undef, $file, $var_hash, $output_ref) = @_; | |||||||||||||
| 70 | |||||||||||||||
| 71 | 3 | my $result = 1; | |||||||||||||
| 72 | 3 | ▶ | if ($file eq 'buggy.html') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | 1 | $result = undef; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 3 | ▶ | if ($file eq 'empty.html') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 77 | 0 | ${$output_ref} = ''; | |||||||||||||
| 78 | } elsif ($file eq 'dump.html') { | ||||||||||||||
| 79 | 0 | local $Data::Dumper::Maxdepth = 2; | |||||||||||||
| 80 | 0 | ${$output_ref} = Dumper($var_hash); | |||||||||||||
| 81 | } else { | ||||||||||||||
| 82 | 3 | delete $var_hash->{'matches_hash'}; | |||||||||||||
| 83 | 3 | ${$output_ref} = Digest::MD5::md5_hex(Dumper($file, $var_hash)); | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | |||||||||||||||
| 86 | 3 | return $result; | |||||||||||||
| 87 | 5 | }); | |||||||||||||
| 88 | |||||||||||||||
| 89 | 5 | return $template_mock->new(@args); | |||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | =head2 B<main()> | ||||||||||||||
| 93 | |||||||||||||||
| 94 | =cut | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 2 | ▶ | sub main { | ||||||||||||
|
Subroutine
main: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 97 | Test::MockObject::Extends->new('Template')->fake_module( | ||||||||||||||
| 98 | 2 | ▶ | 'Template', 'new' => sub { return mock_template(@_); }); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 99 | Test::MockObject::Extends->new('Template')->fake_module( | ||||||||||||||
| 100 | 2 | ▶ | 'Template::Stash::XS', 'new' => sub { return $stash_stub; }); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | 2 | main(); | |||||||||||||
| 104 | |||||||||||||||
| 105 | =head1 SEE ALSO | ||||||||||||||
| 106 | |||||||||||||||
| 107 | =over 4 | ||||||||||||||
| 108 | |||||||||||||||
| 109 | =item L<Eve::Test> | ||||||||||||||
| 110 | |||||||||||||||
| 111 | =item L<Test::Class> | ||||||||||||||
| 112 | |||||||||||||||
| 113 | =back | ||||||||||||||
| 114 | |||||||||||||||
| 115 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||
| 116 | |||||||||||||||
| 117 | Copyright 2010-2013 Sergey Konoplev, Igor Zinovyev. | ||||||||||||||
| 118 | |||||||||||||||
| 119 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||
| 120 | under the terms of either: the GNU General Public License as published | ||||||||||||||
| 121 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||
| 122 | |||||||||||||||
| 123 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||
| 124 | |||||||||||||||
| 125 | =head1 AUTHOR | ||||||||||||||
| 126 | |||||||||||||||
| 127 | =over 4 | ||||||||||||||
| 128 | |||||||||||||||
| 129 | =item L<Sergey Konoplev|mailto:gray.ru@gmail.com> | ||||||||||||||
| 130 | |||||||||||||||
| 131 | =item L<Igor Zinovyev|mailto:zinigor@gmail.com> | ||||||||||||||
| 132 | |||||||||||||||
| 133 | =back | ||||||||||||||
| 134 | |||||||||||||||
| 135 | =cut | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 2 | 1; | |||||||||||||