added code and tests for Componentized::ensure_class_found and load_optional_class
Justin Guenther [Fri, 9 Jun 2006 21:04:50 +0000 (21:04 +0000)]
lib/DBIx/Class/Componentised.pm
lib/DBIx/Class/Storage/DBI.pm
t/90ensure_class_loaded.t
t/92storage.t [new file with mode: 0644]
t/lib/DBICTest/ErrorComponent.pm [new file with mode: 0644]
t/lib/DBICTest/FakeComponent.pm
t/lib/DBICTest/OptionalComponent.pm [new file with mode: 0644]

index 412958f..109ad36 100644 (file)
@@ -58,6 +58,10 @@ sub _load_components {
   $class->inject_base($class => @comp);
 }
 
+# Given a class name, tests to see if it is already loaded or otherwise
+# defined. If it is not yet loaded, the package is require'd, and an exception
+# is thrown if the class is still not loaded.
+#
 # TODO: handle ->has_many('rel', 'Class'...) instead of
 #              ->has_many('rel', 'Some::Schema::Class'...)
 sub ensure_class_loaded {
@@ -65,8 +69,29 @@ sub ensure_class_loaded {
   eval "require $f_class";
   my $err = $@;
   Class::Inspector->loaded($f_class)
-    or die $err || "require $f_class was successful but the package".
-                   "is not defined";
+    or $class->throw_exception($err || "`require $f_class' was successful".
+                                       "but the package is not defined");
+}
+
+# Returns true if the specified class is installed or already loaded, false
+# otherwise
+sub ensure_class_found {
+  my ($class, $f_class) = @_;
+  return Class::Inspector->loaded($f_class) ||
+         Class::Inspector->installed($f_class);
+}
+
+# Returns a true value if the specified class is installed and loaded
+# successfully, throws an exception if the class is found but not loaded
+# successfully, and false if the class is not installed
+sub load_optional_class {
+  my ($class, $f_class) = @_;
+  if ($class->ensure_class_found($f_class)) {
+    $class->ensure_class_loaded($f_class);
+    return 1;
+  } else {
+    return 0;
+  }
 }
 
 1;
index 2b4b68b..ad5e3fc 100644 (file)
@@ -501,8 +501,7 @@ sub _populate_dbh {
 
   if(ref $self eq 'DBIx::Class::Storage::DBI') {
     my $driver = $self->_dbh->{Driver}->{Name};
-    eval "require DBIx::Class::Storage::DBI::${driver}";
-    unless ($@) {
+    if ($self->load_optional_class("DBIx::Class::Storage::DBI::${driver}")) {
       bless $self, "DBIx::Class::Storage::DBI::${driver}";
       $self->_rebless() if $self->can('_rebless');
     }
index 8f66c2e..672450b 100644 (file)
@@ -13,32 +13,44 @@ BEGIN {
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 6;
-
-ok(Class::Inspector->loaded('TestPackage::A'),
-   'anon. package exists');
-eval {
-  $schema->ensure_class_loaded('TestPackage::A');
-};
-
-ok(!$@, 'ensure_class_loaded detected an anon. class');
-
-eval {
-  $schema->ensure_class_loaded('FakePackage::B');
-};
-
-like($@, qr/Can't locate/,
-     'ensure_class_loaded threw exception for nonexistent class');
-
-ok(!Class::Inspector->loaded('DBICTest::FakeComponent'),
-   'DBICTest::FakeComponent not loaded yet');
-
-eval {
-  $schema->ensure_class_loaded('DBICTest::FakeComponent');
-};
-
-ok(!$@, 'ensure_class_loaded detected an existing but non-loaded class');
-ok(Class::Inspector->loaded('DBICTest::FakeComponent'),
-   'DBICTest::FakeComponent now loaded');
+plan tests => 17;
+
+# Test ensure_class_found
+ok( $schema->ensure_class_found('DBIx::Class::Schema'),
+    'loaded package DBIx::Class::Schema was found' );
+ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
+    'DBICTest::FakeComponent not loaded yet' );
+ok( $schema->ensure_class_found('DBICTest::FakeComponent'),
+    'package DBICTest::FakeComponent was found' );
+ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
+    'DBICTest::FakeComponent not loaded by ensure_class_found()' );
+ok( $schema->ensure_class_found('TestPackage::A'),
+    'anonymous package TestPackage::A found' );
+ok( !$schema->ensure_class_found('FAKE::WONT::BE::FOUND'),
+        'fake package not found' );
+
+# Test load_optional_class
+my $retval = eval { $schema->load_optional_class('ANOTHER::FAKE::PACKAGE') };
+ok( !$@, 'load_optional_class on a nonexistent class did not throw' );
+ok( !$retval, 'nonexistent package not loaded' );
+$retval = eval { $schema->load_optional_class('DBICTest::OptionalComponent') };
+ok( !$@, 'load_optional_class on an existing class did not throw' );
+ok( $retval, 'DBICTest::OptionalComponent loaded' );
+eval { $schema->load_optional_class('DBICTest::ErrorComponent') };
+like( $@, qr/did not return a true value/, 'DBICTest::ErrorComponent threw ok' );
+
+# Test ensure_class_loaded
+ok( Class::Inspector->loaded('TestPackage::A'), 'anonymous package exists' );
+eval { $schema->ensure_class_loaded('TestPackage::A'); };
+ok( !$@, 'ensure_class_loaded detected an anon. class' );
+eval { $schema->ensure_class_loaded('FakePackage::B'); };
+like( $@, qr/Can't locate/,
+     'ensure_class_loaded threw exception for nonexistent class' );
+ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
+   'DBICTest::FakeComponent not loaded yet' );
+eval { $schema->ensure_class_loaded('DBICTest::FakeComponent'); };
+ok( !$@, 'ensure_class_loaded detected an existing but non-loaded class' );
+ok( Class::Inspector->loaded('DBICTest::FakeComponent'),
+   'DBICTest::FakeComponent now loaded' );
 
 1;
diff --git a/t/92storage.t b/t/92storage.t
new file mode 100644 (file)
index 0000000..67a594f
--- /dev/null
@@ -0,0 +1,15 @@
+use strict;
+use warnings;  
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+
+plan tests => 1;
+
+my $schema = DBICTest->init_schema();
+
+is( ref($schema->storage), 'DBIx::Class::Storage::DBI::SQLite',
+    'Storage reblessed correctly into DBIx::Class::Storage::DBI::SQLite' );
+
+1;
diff --git a/t/lib/DBICTest/ErrorComponent.pm b/t/lib/DBICTest/ErrorComponent.pm
new file mode 100644 (file)
index 0000000..67f54e8
--- /dev/null
@@ -0,0 +1,8 @@
+#   belongs to t/run/90ensure_class_loaded.tl
+package # hide from PAUSE 
+    DBICTest::ErrorComponent;
+use warnings;
+use strict;
+
+# this is missing on purpose
+# 1;
index 5fe3b66..fbe21f0 100644 (file)
@@ -1,4 +1,4 @@
-#   belongs to t/run/30ensure_class_loaded.tl
+#   belongs to t/run/90ensure_class_loaded.tl
 package # hide from PAUSE 
     DBICTest::FakeComponent;
 use warnings;
diff --git a/t/lib/DBICTest/OptionalComponent.pm b/t/lib/DBICTest/OptionalComponent.pm
new file mode 100644 (file)
index 0000000..5f0d36a
--- /dev/null
@@ -0,0 +1,7 @@
+#   belongs to t/run/90ensure_class_loaded.tl
+package # hide from PAUSE 
+    DBICTest::OptionalComponent;
+use warnings;
+use strict;
+
+1;