File Coverage

lib/Mojolicious/Plugin/Fondation/TestHelper.pm
Criterion Covered Total %
statement 40 49 81.6
branch 3 6 50.0
condition 3 6 50.0
subroutine 10 11 90.9
pod 0 3 0.0
total 56 75 74.6


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Fondation::TestHelper;
2             $Mojolicious::Plugin::Fondation::TestHelper::VERSION = '0.03';
3             # ABSTRACT: Test helpers for Fondation and its plugins
4              
5 12     12   5536693 use strict;
  12         21  
  12         350  
6 12     12   46 use warnings;
  12         30  
  12         487  
7 12     12   5029 use Mojolicious;
  12         1269044  
  12         114  
8 12     12   462 use Mojo::Home;
  12         32  
  12         489  
9 12     12   56 use File::Temp 'tempdir';
  12         23  
  12         475  
10 12     12   90 use File::Spec;
  12         21  
  12         247  
11 12     12   43 use File::Path 'make_path';
  12         20  
  12         838  
12              
13             our @EXPORT_OK = qw(
14             create_test_app
15             create_fondation_app
16             capture_command
17             );
18              
19 12     12   73 use base 'Exporter';
  12         24  
  12         4713  
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             sub create_test_app {
27 65     65 0 586263 my ($temp_dir, $opts) = @_;
28 65   50     409 $opts //= {};
29              
30 65 50       187 if (!$temp_dir) {
31 0         0 $temp_dir = tempdir(CLEANUP => 1);
32             }
33              
34 65         496 my $app = Mojolicious->new;
35              
36 65         613690 my $app_home = File::Spec->catdir($temp_dir, 'app_home');
37 65 50       8514 mkdir $app_home or die "Cannot create app_home: $!";
38 65         617 $app->home(Mojo::Home->new($app_home));
39              
40 65         1175 my $share_dir = $app->home->child('share');
41 65 50       1533 make_path($share_dir) unless -d $share_dir;
42              
43 65   50     15642 $app->log->level($opts->{log_level} // 'fatal');
44              
45 65         3376 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             sub create_fondation_app {
51 7     7 0 25419 my ($temp_dir, $config, $opts) = @_;
52 7   50     18 $config //= {};
53              
54 7         23 my $app = create_test_app($temp_dir, $opts);
55 7         32 $app->plugin('Fondation' => $config);
56 7         24 return $app;
57             }
58              
59             # Run a command on the app and capture STDOUT + STDERR.
60             # Returns the combined output as a string.
61             sub capture_command {
62 0     0 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             1;
75              
76             __END__