Merge branch 'master' into gsoc_breadboard
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_component_setup_component.t
1 use strict;
2 use warnings;
3
4 # FIXME - backcompat?
5 use Test::More skip_all => "Removed setup_component from Catalyst.pm";
6 use Moose::Meta::Class;
7
8 my %config = (
9     foo => 42,
10     bar => 'myconf',
11 );
12
13 Moose::Meta::Class->create( TestAppComponent => (
14     superclasses => ['Catalyst'],
15 ));
16
17 TestAppComponent->config(
18     'Model::With::Config' => { %config },
19 );
20
21 TestAppComponent->setup_config;
22
23 my @comps;
24 push @comps, "TestAppComponent::$_" for qw/
25     Without::Component::Sub
26     Model::With::Config
27     Dieing
28     NotBlessed
29     Regular
30 /;
31 my ($no_sub, $config, $dieing, $not_blessed, $regular) = @comps;
32
33 Moose::Meta::Class->create( $no_sub => (
34     superclasses => ['Catalyst::Component'],
35 ));
36
37 Moose::Meta::Class->create( $config => (
38     superclasses => ['Catalyst::Component'],
39     methods      => {
40         COMPONENT => sub { bless $_[2] }
41     },
42 ));
43
44 Moose::Meta::Class->create( $dieing => (
45     superclasses => ['Catalyst::Component'],
46     methods      => {
47         COMPONENT => sub { die "Could not create component" }
48     },
49 ));
50
51 Moose::Meta::Class->create( $not_blessed => (
52     superclasses => ['Catalyst::Component'],
53     methods      => {
54         COMPONENT => sub { {} }
55     },
56 ));
57
58 Moose::Meta::Class->create( $regular => (
59     superclasses => ['Catalyst::Component'],
60     methods      => {
61         COMPONENT => sub { shift->new }
62     },
63 ));
64
65 {
66     no warnings 'redefine', 'once';
67     my $message;
68     my $component;
69
70     local *Catalyst::Exception::throw = sub { shift; my %h = @_; $message = $h{message} };
71
72     $component = eval { TestAppComponent->setup_component($no_sub) };
73     ok( !$@, "setup_component doesnt die with $no_sub" );
74     is( $message, undef, "no exception thrown" );
75     isa_ok( $component, $no_sub, "the returned value isa the component" );
76
77     undef $message;
78     $component = eval { TestAppComponent->setup_component($config) };
79     ok( !$@, "setup_component doesnt die with $config" );
80     is( $message, undef, "no exception thrown" );
81     is_deeply( $component, \%config, "the returned config is correct" );
82
83     undef $message;
84     $component = eval { TestAppComponent->setup_component($dieing) };
85     ok( !$@, "setup_component doesnt die with $dieing" );
86     like( $message, qr/Could not create component/, "the exception is thrown correctly" );
87
88     undef $message;
89     $component = eval { TestAppComponent->setup_component($not_blessed) };
90     ok( !$@, "setup_component doesnt die with $not_blessed" );
91     isnt( $message, undef, "it throws an exception" );
92
93     undef $message;
94     $component = eval { TestAppComponent->setup_component($regular) };
95     ok( !$@, "setup_component doesnt die with $regular" );
96     is( $message, undef, "no exception thrown" );
97     isa_ok( $component, $regular, "the returned value is correct" );
98 }
99
100 done_testing;