From d982ac19853bc1c6ed068cdff412a6cd1bb09127 Mon Sep 17 00:00:00 2001 From: qadan Date: Thu, 2 Jul 2015 17:54:36 -0300 Subject: [PATCH] better asserting objects --- tests/includes/utilities.inc | 77 ++++++++++++++---------------------- 1 file changed, 29 insertions(+), 48 deletions(-) diff --git a/tests/includes/utilities.inc b/tests/includes/utilities.inc index 53a5107e..38a60462 100644 --- a/tests/includes/utilities.inc +++ b/tests/includes/utilities.inc @@ -112,10 +112,7 @@ class IslandoraTestUtilities extends IslandoraTestUtilityClass { * TRUE on success, FALSE on fail. */ public function assertDatastreams($object, array $datastreams) { - if (!self::assertFedoraObject($object)) { - $this->addResult(FALSE, "Failed. Object passed in is invalid.", 'Islandora'); - } - else { + if (self::assertFedoraObject($object)) { $missing_datastreams = array_diff_key(array_flip($datastreams), $object->repository->api->a->listDatastreams($object->id)); if (!empty($missing_datastreams)) { @@ -137,8 +134,8 @@ class IslandoraTestUtilities extends IslandoraTestUtilityClass { * An array of datastreams to confirm not present. */ public function assertNoDatastreams($object, array $datastreams) { - if (!self::assertFedoraObject($object)) { - $this->addResult(FALSE, "Failed. Object passed in is invalid.", 'Islandora'); + if (self::assertFedoraObject($object, FALSE)) { + $this->addResult("Failed to check the datastreams on the object; the object could not be loaded."); return; } $found_datastreams = array_intersect_key(array_flip($datastreams), $object->repository->api->a->listDatastreams($object->id)); @@ -169,8 +166,7 @@ class IslandoraTestUtilities extends IslandoraTestUtilityClass { * for examples. */ public function validateDatastreams($object, array $datastreams) { - - if (!self::assertFedoraObject($object)) { + if (self::assertFedoraObject($object, FALSE)) { $this->addResult(FALSE, "Datastream validation failed; Object passed in is invalid.", 'Islandora'); return; } @@ -347,48 +343,33 @@ QUERY; } /** - * Asserts that an object is a FedoraObject or an IslandoraFedoraObject. - * - * @param object $object - * The object to assess. - * - * @return bool - * TRUE if it is either of those object types, or FALSE otherwise. - */ - public static function assertFedoraObject($object) { - return ($object instanceof FedoraObject); - } - - /** - * Asserts that the given PID represents a Fedora object. + * Asserts a Fedora object from an object or PID, and loads it if possible. * - * @param string $pid - * The PID of the object. + * @param FedoraObject|string $object + * The object to assess, or a string to use as a PID. + * @param bool $exists + * Whether or not the object should exist in the repository, according to + * our assertion. Defaults to TRUE. * - * @return bool - * TRUE if the object exists in the repository; FALSE otherwise. + * @return FedoraObject|bool + * The object in question if it was a FedoraObject or loaded into one, or + * FALSE otherwise. */ - public function assertObjectExists($pid) { - $object = islandora_object_load($pid); - $exists = $this->assertFedoraObject($object); - $this->addResult($exists, "Object $pid exists in the repository."); - return $exists; - } - - /** - * Asserts that the given PID does not represent a Fedora object. - * - * @param string $pid - * The PID of the object. - * - * @return bool - * TRUE if the object does not exist in the repository, or FALSE otherwise. - */ - public function assertNoObjectExists($pid) { - $object = islandora_object_load($pid); - $exists = $this->assertFedoraObject($object); - $this->addResult(!$exists, "Object $pid does not exist in the repository."); - return !$exists; + public static function assertFedoraObject($object, $exists = TRUE) { + if ($object instanceof FedoraObject) { + $this->addResult($exists, "Object loaded from $object->id is a Fedora repository object."); + return $object; + } + elseif (is_string($object)) { + $loaded_object = islandora_object_load($object); + if ($loaded_object instanceof FedoraObject) { + $this->addResult($exists, "Successfully loaded object $object from the Fedora repository."); + return $loaded_object; + } + $this->addResult($exists, "Object identified by $object was not found in the repository."); + return FALSE; + } + $this->addResult($exists, "Object passed in was not found in the repository."); + return FALSE; } - }