Make the extended tests pass. This is now fairly frightning code ;)
[catagits/CatalystX-DynamicComponent.git] / t / 03_dynamiccomponent.t
CommitLineData
4bb0a920 1use strict;
2use warnings;
3
b20d38ca 4use Test::More tests => 26;
4bb0a920 5use Test::Exception;
6
7use Moose ();
8BEGIN { use_ok('CatalystX::DynamicComponent') }
9
10my $testapp_counter = 0;
11sub generate_testapp {
12 my $role_options = shift || {};
13 my $meta = Moose->init_meta( for_class => "TestApp" . $testapp_counter++ );
14 $meta->superclasses('Catalyst');
15 Moose::Util::apply_all_roles($meta, 'CatalystX::DynamicComponent', $role_options);
7191ab41 16 $meta->name->setup;
4bb0a920 17 return $meta;
18}
19
20throws_ok { generate_testapp(); }
21 qr/name\) is required/, 'name is required';
22
23{
24 my $app_meta = generate_testapp({ name => 'dynamic_component_method' });
25 my $app = $app_meta->name;
26 ok $app->can('dynamic_component_method'), 'dynamic component method added';
7191ab41 27 $app->dynamic_component_method( $app . "::Model::Foo", {} );
28 my $foo = $app->model('Foo');
29 ok $foo, 'Have added Foo component';
30 isa_ok($foo, 'Catalyst::Component');
31}
4bb0a920 32
7191ab41 33{
34 my $COMPONENT = sub { return bless {}, 'TestClass' };
35 my $app_meta = generate_testapp({
36 name => 'dynamic_component_method',
b20d38ca 37 methods => {
38 COMPONENT => $COMPONENT,
39 },
7191ab41 40 });
41 my $app = $app_meta->name;
42 ok $app->can('dynamic_component_method'), 'dynamic component method added';
43 $app->dynamic_component_method( $app . "::Model::Foo", {} );
44 my $foo = $app->model('Foo');
45 ok $foo, 'Have added Foo component';
46 isa_ok($foo, 'TestClass', 'COMPONENT method returned totally different class');
47 my $name = $app . "::Model::Foo";
48 isa_ok(bless({}, $name), 'Catalyst::Component', 'Underlying $app::Model::Foo is a C::C');
49 is($name->can('COMPONENT'), $COMPONENT, 'Supplied COMPONENT method is on $app::Model::Foo');
4bb0a920 50}
b20d38ca 51{
52 package My::Other::Superclass;
53 use Moose;
54 __PACKAGE__->meta->make_immutable;
55}
c52d8688 56{
57 package My::Model;
58 use Moose;
59 extends qw/Catalyst::Model/;
60 __PACKAGE__->meta->make_immutable;
61}
62{
63 package My::Role;
64 use Moose::Role;
65 sub _some_method_from_role {}
66}
67{
b20d38ca 68 package My::Other::Role;
69 use Moose::Role;
70 sub _some_method_from_other_role {}
71}
72my %generator_config = (
73 name => 'dynamic_component_method',
74 roles => [qw/My::Other::Role/],
75 superclasses => ['My::Other::Superclass'],
76 methods => {
77 my_other_injected_method => sub {},
78 },
79);
80my $extra_config = {
81 superclasses => ['My::Model'],
82 roles => ['My::Role'],
83 methods => {
84 my_injected_method => sub { 'quuux' },
85 }
86};
87{
88 # Do not specify any resolve strategies, so get defaults:
89 # methods - merge
90 # superclasses - replace
91 # roles - merge
c52d8688 92 my $app_meta = generate_testapp({
b20d38ca 93 %generator_config,
94 });
95 my $app = $app_meta->name;
96 $app->dynamic_component_method( $app . "::Model::Foo", $extra_config );
97 my $model = $app->model('Foo');
98 isa_ok($model, 'My::Model', 'Correct superclass');
99 ok(!$model->isa('My::Other::Superclass'),
100 'superclass in extra config replaces by default');
101
102 ok $model->can('_some_method_from_role'), 'Has had role applied';
103 ok !My::Model->can('_some_method_from_role'), 'Role applied at right place';
104
ac8aab7d 105 ok $model->can('_some_method_from_other_role'),
b20d38ca 106 'Role application merges by default';
107
108 ok $model->can('my_injected_method'), 'Injected method there as expected';
109 is $model->my_injected_method, 'quuux', 'Injected method returns correct val';
110
111 ok $model->can('my_other_injected_method'),
112 'Injected methods merged by default';
113}
114{
115 # Specify resolve strategies, totally opposite the defaults:
116 my $app_meta = generate_testapp({
117 %generator_config,
118 roles_resolve_strategy => 'replace',
119 superclasses_resolve_strategy => 'merge',
120 methods_resolve_strategy => 'replace',
c52d8688 121 });
122 my $app = $app_meta->name;
c52d8688 123 $app->dynamic_component_method( $app . "::Model::Foo", $extra_config );
124 my $model = $app->model('Foo');
b20d38ca 125 isa_ok($model, 'My::Model', 'Correct superclass');
126 isa_ok($model, 'My::Other::Superclass',
127 'superclasses merged');
128
c52d8688 129 ok $model->can('_some_method_from_role'), 'Has had role applied';
130 ok !My::Model->can('_some_method_from_role'), 'Role applied at right place';
b20d38ca 131
132 ok !$model->can('_some_other_method_from_role'),
133 'Role application merges replaces when configured';
134
c52d8688 135 ok $model->can('my_injected_method'), 'Injected method there as expected';
136 is $model->my_injected_method, 'quuux', 'Injected method returns correct val';
b20d38ca 137
138 ok !$model->can('my_other_injected_method'),
139 'Injected methods replaced';
c52d8688 140}
7191ab41 141