e46797a4807cfd92d87138ee1c5dc2cc2ebfbaa7
[catagits/Catalyst-Runtime.git] / t / unit_core_plugin.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 22;
7
8 use lib 't/lib';
9
10 {
11
12     package Faux::Plugin;
13
14     sub new { bless {}, shift }
15     my $count = 1;
16     sub count { $count++ }
17 }
18
19 {
20
21     package PluginTestApp;
22     use Test::More;
23
24     use Catalyst qw(
25       Test::Plugin
26       +TestApp::Plugin::FullyQualified
27     );
28
29     sub compile_time_plugins : Local {
30         my ( $self, $c ) = @_;
31
32         isa_ok $c, 'Catalyst::Plugin::Test::Plugin';
33         isa_ok $c, 'TestApp::Plugin::FullyQualified';
34
35         can_ok $c, 'registered_plugins';
36         $c->_test_plugins;
37
38         $c->res->body("ok");
39     }
40
41     sub run_time_plugins : Local {
42         my ( $self, $c ) = @_;
43
44         $c->_test_plugins;
45         my $faux_plugin = 'Faux::Plugin';
46
47         # Trick perl into thinking the plugin is already loaded
48         $INC{'Faux/Plugin.pm'} = 1;
49
50         __PACKAGE__->plugin( faux => $faux_plugin );
51
52         isa_ok $c, 'Catalyst::Plugin::Test::Plugin';
53         isa_ok $c, 'TestApp::Plugin::FullyQualified';
54         ok !$c->isa($faux_plugin),
55           '... and it should not inherit from the instant plugin';
56         can_ok $c, 'faux';
57         is $c->faux->count, 1, '... and it should behave correctly';
58         is_deeply [ $c->registered_plugins ],
59             [
60                 qw/Catalyst::Plugin::Test::Plugin
61                    Faux::Plugin
62                    TestApp::Plugin::FullyQualified/
63             ],
64             'registered_plugins() should report all plugins';
65         ok $c->registered_plugins('Faux::Plugin'),
66             '... and even the specific instant plugin';
67
68         $c->res->body("ok");
69     }
70
71     sub _test_plugins {
72         my $c = shift;
73         is_deeply [ $c->registered_plugins ],
74             [
75                 qw/Catalyst::Plugin::Test::Plugin
76                    TestApp::Plugin::FullyQualified/
77             ],
78             '... and it should report the correct plugins';
79         ok $c->registered_plugins('Catalyst::Plugin::Test::Plugin'),
80             '... or if we have a particular plugin';
81         ok $c->registered_plugins('Test::Plugin'),
82             '... even if it is not fully qualified';
83         ok !$c->registered_plugins('No::Such::Plugin'),
84             '... and it should return false if the plugin does not exist';
85     }
86
87     __PACKAGE__->setup;
88 }
89
90 use Catalyst::Test qw/PluginTestApp/;
91
92 ok( get("/compile_time_plugins"), "get ok" );
93 ok( get("/run_time_plugins"),     "get ok" );
94
95 use_ok 'TestApp';
96 my @expected = qw(
97   Catalyst::Plugin::Test::Errors
98   Catalyst::Plugin::Test::Headers
99   Catalyst::Plugin::Test::Plugin
100   TestApp::Plugin::FullyQualified
101 );
102
103 # Faux::Plugin is no longer reported
104 is_deeply [ TestApp->registered_plugins ], \@expected,
105   'registered_plugins() should only report the plugins for the current class';