« lib/Mojolicious/Plugin/Fondation/Action/Templates.pm
↑ Summary
lib/Mojolicious/Plugin/Fondation.pm »
| 1 | package Mojolicious::Plugin::Fondation::TestHelper; | ||||||||||||
| 2 | 14 | $Mojolicious::Plugin::Fondation::TestHelper::VERSION = '0.08'; | |||||||||||
| 3 | # ABSTRACT: Test helpers for Fondation and its plugins | ||||||||||||
| 4 | |||||||||||||
| 5 | 14 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 14 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 14 | ▶ | use Mojolicious; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 14 | ▶ | use Mojo::Home; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 14 | ▶ | use File::Temp 'tempdir'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 14 | ▶ | use File::Spec; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 14 | ▶ | use File::Path 'make_path'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 14 | our @EXPORT_OK = qw( | |||||||||||
| 14 | create_test_app | ||||||||||||
| 15 | create_fondation_app | ||||||||||||
| 16 | capture_command | ||||||||||||
| 17 | ); | ||||||||||||
| 18 | |||||||||||||
| 19 | 14 | ▶ | use base 'Exporter'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | |||||||||||||
| 21 | # Create a bare Mojolicious app with a temporary home directory. | ||||||||||||
| 22 | # The app's share/ directory is created to avoid permission errors. | ||||||||||||
| 23 | # | ||||||||||||
| 24 | # Options (optional hashref, second argument): | ||||||||||||
| 25 | # log_level => 'debug' # default: 'fatal' (silent during tests) | ||||||||||||
| 26 | 69 | ▶ | sub create_test_app { | ||||||||||
|
Subroutine
create_test_app: called CC=6 SCAR=19.1
Pod
undocumented
| |||||||||||||
| 27 | 69 | my ($temp_dir, $opts) = @_; | |||||||||||
| 28 | 69 | ▶ | $opts //= {}; | ||||||||||
|
Condition50%1/2
$opts //= {}
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
| |||||||||||||
| 29 | |||||||||||||
| 30 | 69 | ▶ | if (!$temp_dir) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 31 | 0 | $temp_dir = tempdir(CLEANUP => 1); | |||||||||||
| 32 | } | ||||||||||||
| 33 | |||||||||||||
| 34 | 69 | my $app = Mojolicious->new; | |||||||||||
| 35 | |||||||||||||
| 36 | 69 | my $app_home = File::Spec->catdir($temp_dir, 'app_home'); | |||||||||||
| 37 | 69 | ▶ | mkdir $app_home or die "Cannot create app_home: $!"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | 69 | $app->home(Mojo::Home->new($app_home)); | |||||||||||
| 39 | |||||||||||||
| 40 | 69 | my $share_dir = $app->home->child('share'); | |||||||||||
| 41 | 69 | ▶ | make_path($share_dir) unless -d $share_dir; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | |||||||||||||
| 43 | 69 | ▶ | $app->log->level($opts->{log_level} // 'fatal'); | ||||||||||
|
Condition50%1/2
$opts->{"log_level"} // "fatal"
MC/DC0%0/1
$opts->{"log_level"} // "fatal"
$opts->{"log_level"}
Truth table50%1/2
A
A= $opts->{"log_level"}
| |||||||||||||
| 44 | |||||||||||||
| 45 | 69 | return $app; | |||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | # Create a Fondation app with the given config loaded. | ||||||||||||
| 49 | # Equivalent to: create_test_app + $app->plugin('Fondation' => $config) | ||||||||||||
| 50 | 7 | ▶ | sub create_fondation_app { | ||||||||||
|
Subroutine
create_fondation_app: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 51 | 7 | my ($temp_dir, $config, $opts) = @_; | |||||||||||
| 52 | 7 | ▶ | $config //= {}; | ||||||||||
|
Condition50%1/2
$config //= {}
MC/DC0%0/1
$config //= {}
$config
Truth table50%1/2
A
A= $config
| |||||||||||||
| 53 | |||||||||||||
| 54 | 7 | my $app = create_test_app($temp_dir, $opts); | |||||||||||
| 55 | 7 | $app->plugin('Fondation' => $config); | |||||||||||
| 56 | 7 | return $app; | |||||||||||
| 57 | } | ||||||||||||
| 58 | |||||||||||||
| 59 | # Run a command on the app and capture STDOUT + STDERR. | ||||||||||||
| 60 | # Returns the combined output as a string. | ||||||||||||
| 61 | 0 | ▶ | sub capture_command { | ||||||||||
|
Subroutine
capture_command: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 62 | 0 | my ($app, @args) = @_; | |||||||||||
| 63 | |||||||||||||
| 64 | 0 | my $buf; | |||||||||||
| 65 | 0 | open my $fh, '>', \$buf; | |||||||||||
| 66 | 0 | local *STDOUT = $fh; | |||||||||||
| 67 | 0 | local *STDERR = $fh; | |||||||||||
| 68 | 0 | $app->commands->run(@args); | |||||||||||
| 69 | 0 | close $fh; | |||||||||||
| 70 | |||||||||||||
| 71 | 0 | return $buf; | |||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | 14 | 1; | |||||||||||
| 75 | |||||||||||||
| 76 | __END__ | ||||||||||||