From: Tim Otten Date: Tue, 14 Sep 2021 23:45:14 +0000 (-0700) Subject: (REF) ExampleDataLoader - Extract private method `createObj()`. Improve errors. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=102824961421d3865d586609221a15b1d34c19f1;p=civicrm-core.git (REF) ExampleDataLoader - Extract private method `createObj()`. Improve errors. __Before__: Two different bits of code responsible for loading+instantiating test-data class. Different (and somewhat opaque) error-handling behaviors. __After__: One private helper called twice. Consistent (and clearer) error-reporting. --- diff --git a/Civi/Test/ExampleDataLoader.php b/Civi/Test/ExampleDataLoader.php index 115f0a8de9..68a0443bc7 100644 --- a/Civi/Test/ExampleDataLoader.php +++ b/Civi/Test/ExampleDataLoader.php @@ -64,10 +64,7 @@ class ExampleDataLoader { return NULL; } - if ($example['file']) { - include_once $example['file']; - } - $obj = new $example['class'](); + $obj = $this->createObj($example['file'], $example['class']); $obj->build($example); return $example; } @@ -91,8 +88,7 @@ class ExampleDataLoader { $all = []; foreach ($classes as $file => $class) { - require_once $file; - $obj = new $class(); + $obj = $this->createObj($file, $class); $offset = 0; foreach ($obj->getExamples() as $example) { $example['file'] = $file; @@ -132,4 +128,15 @@ class ExampleDataLoader { return $r; } + private function createObj(?string $file, ?string $class): ExampleDataInterface { + if ($file) { + include_once $file; + } + if (!class_exists($class)) { + throw new \CRM_Core_Exception("Failed to read example (class '{$class}' in file '{$file}')"); + } + + return new $class(); + } + }