e933c00a370a97a7b28fc875b9d22c1bd17a5a45
[dbsrgits/DBIx-Class.git] / xt / extra / internals / ensure_class_loaded.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7 use DBIx::Class::_Util 'sigwarn_silencer';
8 use Class::Inspector;
9
10 BEGIN {
11   package TestPackage::A;
12   sub some_method {}
13 }
14
15 my $schema = DBICTest->init_schema();
16
17 plan tests => 28;
18
19 # Test ensure_class_found
20 ok( $schema->ensure_class_found('DBIx::Class::Schema'),
21     'loaded package DBIx::Class::Schema was found' );
22 ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
23     'DBICTest::FakeComponent not loaded yet' );
24 ok( $schema->ensure_class_found('DBICTest::FakeComponent'),
25     'package DBICTest::FakeComponent was found' );
26 ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
27     'DBICTest::FakeComponent not loaded by ensure_class_found()' );
28 ok( $schema->ensure_class_found('TestPackage::A'),
29     'anonymous package TestPackage::A found' );
30 ok( !$schema->ensure_class_found('FAKE::WONT::BE::FOUND'),
31         'fake package not found' );
32
33 # Test load_optional_class
34 my $retval = eval { $schema->load_optional_class('ANOTHER::FAKE::PACKAGE') };
35 ok( !$@, 'load_optional_class on a nonexistent class did not throw' );
36 ok( !$retval, 'nonexistent package not loaded' );
37 $retval = eval { $schema->load_optional_class('DBICTest::OptionalComponent') };
38 ok( !$@, 'load_optional_class on an existing class did not throw' );
39 ok( $retval, 'DBICTest::OptionalComponent loaded' );
40 eval { $schema->load_optional_class('DBICTest::ErrorComponent') };
41 like( $@, qr/did not return a true value/,
42       'DBICTest::ErrorComponent threw ok' );
43
44 # Simulate a PAR environment
45 {
46   my @code;
47   local @INC = @INC;
48   unshift @INC, sub {
49     if ($_[1] eq 'VIRTUAL/PAR/PACKAGE.pm') {
50       return (sub { return 0 unless @code; $_ = shift @code; 1; } );
51     }
52     else {
53       return ();
54     }
55   };
56
57   $retval = eval { $schema->load_optional_class('FAKE::PAR::PACKAGE') };
58   ok( !$@, 'load_optional_class on a nonexistent PAR class did not throw' );
59   ok( !$retval, 'nonexistent PAR package not loaded' );
60
61
62   # simulate a class which does load but does not return true
63   @code = (
64     q/package VIRTUAL::PAR::PACKAGE;/,
65     q/0;/,
66   );
67
68   $retval = eval { $schema->load_optional_class('VIRTUAL::PAR::PACKAGE') };
69   ok( $@, 'load_optional_class of a no-true-returning PAR module did throw' );
70   ok( !$retval, 'no-true-returning PAR package not loaded' );
71
72   # simulate a normal class (no one adjusted %INC so it will be tried again
73   @code = (
74     q/package VIRTUAL::PAR::PACKAGE;/,
75     q/1;/,
76   );
77
78   $retval = eval { $schema->load_optional_class('VIRTUAL::PAR::PACKAGE') };
79   ok( !$@, 'load_optional_class of a PAR module did not throw' );
80   ok( $retval, 'PAR package "loaded"' );
81
82   # see if we can still load stuff with the coderef present
83   $retval = eval { $schema->load_optional_class('DBIx::Class::ResultClass::HashRefInflator') };
84   ok( !$@, 'load_optional_class did not throw' ) || diag $@;
85   ok( $retval, 'DBIx::Class::ResultClass::HashRefInflator loaded' );
86 }
87
88 # Test ensure_class_loaded
89 ok( Class::Inspector->loaded('TestPackage::A'), 'anonymous package exists' );
90 eval { $schema->ensure_class_loaded('TestPackage::A'); };
91 ok( !$@, 'ensure_class_loaded detected an anon. class' );
92 eval { $schema->ensure_class_loaded('FakePackage::B'); };
93 like( $@, qr/Can't locate/,
94      'ensure_class_loaded threw exception for nonexistent class' );
95 ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
96    'DBICTest::FakeComponent not loaded yet' );
97 eval { $schema->ensure_class_loaded('DBICTest::FakeComponent'); };
98 ok( !$@, 'ensure_class_loaded detected an existing but non-loaded class' );
99 ok( Class::Inspector->loaded('DBICTest::FakeComponent'),
100    'DBICTest::FakeComponent now loaded' );
101
102 {
103   # Squash warnings about syntax errors in SytaxErrorComponent.pm
104   local $SIG{__WARN__} = sigwarn_silencer(
105     qr/String found where operator expected|Missing operator before/
106   );
107
108   eval { $schema->ensure_class_loaded('DBICTest::SyntaxErrorComponent1') };
109   like( $@, qr/syntax error/,
110         'ensure_class_loaded(DBICTest::SyntaxErrorComponent1) threw ok' );
111   eval { $schema->load_optional_class('DBICTest::SyntaxErrorComponent2') };
112   like( $@, qr/syntax error/,
113         'load_optional_class(DBICTest::SyntaxErrorComponent2) threw ok' );
114 }
115
116
117 eval {
118   package Fake::ResultSet;
119
120   use base 'DBIx::Class::ResultSet';
121
122   __PACKAGE__->load_components('+DBICTest::SyntaxErrorComponent3');
123 };
124
125 # Make sure the errors in components of resultset classes are reported right.
126 like($@, qr!\Qsyntax error at t/lib/DBICTest/SyntaxErrorComponent3.pm!, "Errors from RS components reported right");
127
128 1;