You are here

public static function Inspector::assertAllObjects in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Component/Assertion/Inspector.php \Drupal\Component\Assertion\Inspector::assertAllObjects()

Asserts that all members are objects.

When testing if a collection is composed of objects those objects should be given a common interface to implement and the test should be written to search for just that interface. While this method will allow tests for just object status or for multiple classes and interfaces this was done to allow tests to be written for existing code without altering it. Only use this method in that manner when testing code from third party vendors.

Here are some examples:


  // Just test all are objects, like a cache.
  assert('Drupal\\Component\\Assertion\\Inspector::assertAllObjects(
    $collection');

  // Test if traversable objects (arrays won't pass this)
  assert('Drupal\\Component\\Assertion\\Inspector::assertAllObjects(
    $collection', \'\\Traversable\');

  // Test for the Foo class or Bar\None interface
  assert('Drupal\\Component\\Assertion\\Inspector::assertAllObjects(
    $collection', \'\\Foo\', \'\\Bar\\None\'');

Parameters

mixed $traversable: Variable to be examined.

string ...: Classes and interfaces to test objects against.

Return value

bool TRUE if $traversable can be traversed and all members are objects with at least one of the listed classes or interfaces.

1 call to Inspector::assertAllObjects()
InspectorTest::testAssertAllObjects in core/tests/Drupal/Tests/Component/Assertion/InspectorTest.php
Tests asserting all members are objects.

File

core/lib/Drupal/Component/Assertion/Inspector.php, line 403
Contains \Drupal\Component\Assertion\Inspector.

Class

Inspector
Generic inspections for the assert() statement.

Namespace

Drupal\Component\Assertion

Code

public static function assertAllObjects() {
  $args = func_get_args();
  $traversable = array_shift($args);
  if (static::assertTraversable($traversable)) {
    foreach ($traversable as $member) {
      if (count($args) > 0) {
        foreach ($args as $instance) {
          if ($member instanceof $instance) {

            // We're continuing to the next member on the outer loop.
            // @see http://php.net/continue
            continue 2;
          }
        }
        return FALSE;
      }
      elseif (!is_object($member)) {
        return FALSE;
      }
    }
    return TRUE;
  }
  return FALSE;
}