1use strict; 2use warnings; 3use Test::More 'tests' => 3; 4use Plack::Test; 5use HTTP::Request::Common; 6 7{ 8 package Dancer2::Plugin::Foo; 9 use Dancer2::Plugin; 10 11 BEGIN { 12 has 'foo_message' => ( 13 'is' => 'ro', 14 'default' => sub {'foo'}, 15 ); 16 17 plugin_keywords('foo_message'); 18 } 19} 20 21{ 22 package Dancer2::Plugin::Bar; 23 use Dancer2::Plugin; 24 25 BEGIN { 26 has 'bar_message' => ( 27 'is' => 'ro', 28 'lazy' => 1, 29 'default' => sub { 30 my $self = shift; 31 ::isa_ok( $self, 'Dancer2::Plugin::Bar' ); 32 33 my $foo = $self->find_plugin('Dancer2::Plugin::Foo') 34 or Carp::croak('Cannot find Dancer2::Plugin::Foo'); 35 36 ::isa_ok( $foo, 'Dancer2::Plugin::Foo' ); 37 ::can_ok( $foo, 'foo_message' ); 38 return $foo->foo_message . ':bar'; 39 } 40 ); 41 42 plugin_keywords('bar_message'); 43 } 44} 45 46{ 47 package AppWithFoo; 48 use Dancer2; 49 use Dancer2::Plugin::Foo; 50 get '/' => sub { return foo_message() }; 51} 52 53{ 54 package AppWithBar; 55 use Dancer2; 56 use Dancer2::Plugin::Bar; 57 set 'logger' => 'Capture'; 58 get '/' => sub { return bar_message() }; 59} 60 61{ 62 package AppWithFooAndBar; 63 use Dancer2; 64 use Dancer2::Plugin::Foo; 65 use Dancer2::Plugin::Bar; 66 get '/' => sub { return bar_message() }; 67} 68 69subtest 'Baseline' => sub { 70 my $test = Plack::Test->create( AppWithFoo->to_app ); 71 my $res = $test->request( GET '/' ); 72 ok( $res->is_success, 'Successful response' ); 73 is( $res->content, 'foo', 'Foo plugin works correctly' ); 74}; 75 76subtest 'When parent plugin not available' => sub { 77 my $test = Plack::Test->create( AppWithBar->to_app ); 78 my $res = $test->request( GET '/' ); 79 80 ok( !$res->is_success, 'Response failed' ); 81 82 my $trap = AppWithBar::app->config()->{'logger'}; 83 isa_ok( $trap, 'Dancer2::Logger::Capture' ); 84 85 my $trapper = $trap->trapper; 86 my $logs = $trapper->read; 87 isa_ok( $logs, 'ARRAY', 'Found logs' ); 88 is( scalar @{$logs}, 1, 'One log message' ); 89 90 my $message = $logs->[0]; 91 is( $message->{'level'}, 'error' ); 92 like( 93 $message->{'message'}, 94 qr{\QRoute exception: Cannot find Dancer2::Plugin::Foo\E}, 95 'Correct error', 96 ); 97}; 98 99subtest 'When both parent and child plugins available' => sub { 100 my $test = Plack::Test->create( AppWithFooAndBar->to_app ); 101 my $res = $test->request( GET '/' ); 102 ok( $res->is_success, 'Successful response' ); 103 is( $res->content, 'foo:bar', 'Bar plugin found Foo and worked' ); 104}; 105