1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7use DBIx::Class::_Util 'sigwarn_silencer';
8use Class::Inspector;
9
10BEGIN {
11  package TestPackage::A;
12  sub some_method {}
13}
14
15my $schema = DBICTest->init_schema();
16
17plan tests => 28;
18
19# Test ensure_class_found
20ok( $schema->ensure_class_found('DBIx::Class::Schema'),
21    'loaded package DBIx::Class::Schema was found' );
22ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
23    'DBICTest::FakeComponent not loaded yet' );
24ok( $schema->ensure_class_found('DBICTest::FakeComponent'),
25    'package DBICTest::FakeComponent was found' );
26ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
27    'DBICTest::FakeComponent not loaded by ensure_class_found()' );
28ok( $schema->ensure_class_found('TestPackage::A'),
29    'anonymous package TestPackage::A found' );
30ok( !$schema->ensure_class_found('FAKE::WONT::BE::FOUND'),
31        'fake package not found' );
32
33# Test load_optional_class
34my $retval = eval { $schema->load_optional_class('ANOTHER::FAKE::PACKAGE') };
35ok( !$@, 'load_optional_class on a nonexistent class did not throw' );
36ok( !$retval, 'nonexistent package not loaded' );
37$retval = eval { $schema->load_optional_class('DBICTest::OptionalComponent') };
38ok( !$@, 'load_optional_class on an existing class did not throw' );
39ok( $retval, 'DBICTest::OptionalComponent loaded' );
40eval { $schema->load_optional_class('DBICTest::ErrorComponent') };
41like( $@, 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
89ok( Class::Inspector->loaded('TestPackage::A'), 'anonymous package exists' );
90eval { $schema->ensure_class_loaded('TestPackage::A'); };
91ok( !$@, 'ensure_class_loaded detected an anon. class' );
92eval { $schema->ensure_class_loaded('FakePackage::B'); };
93like( $@, qr/Can't locate/,
94     'ensure_class_loaded threw exception for nonexistent class' );
95ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
96   'DBICTest::FakeComponent not loaded yet' );
97eval { $schema->ensure_class_loaded('DBICTest::FakeComponent'); };
98ok( !$@, 'ensure_class_loaded detected an existing but non-loaded class' );
99ok( 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
117eval {
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.
126like($@, qr!\Qsyntax error at t/lib/DBICTest/SyntaxErrorComponent3.pm!, "Errors from RS components reported right");
127
1281;
129