You are here

class MultigraphUnitTest in Monitoring 8

@coversDefaultClass \Drupal\monitoring_multigraph\Entity\Multigraph

@group monitoring

Hierarchy

Expanded class hierarchy of MultigraphUnitTest

File

modules/multigraph/tests/src/Unit/Entity/MultigraphUnitTest.php, line 17

Namespace

Drupal\Tests\monitoring_multigraph\Unit\Entity
View source
class MultigraphUnitTest extends UnitTestCase {

  /**
   * A mock entity manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->entityTypeManager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $container = new ContainerBuilder();
    $container
      ->set('entity_type.manager', $this->entityTypeManager);
    \Drupal::setContainer($container);
  }

  /**
   * @covers ::calculateDependencies
   */
  public function testCalculateDependencies() {

    // Mock a couple of sensors.
    $sensor1_id = $this
      ->getRandomGenerator()
      ->word(16);
    $sensor1 = $this
      ->getMockSensor($sensor1_id);
    $sensor2_id = $this
      ->getRandomGenerator()
      ->word(16);
    $sensor2 = $this
      ->getMockSensor($sensor2_id);

    // Create a Multigraph containing the sensors.
    $multigraph = new Multigraph(array(
      'sensors' => array(
        $sensor1_id => array(
          'weight' => 0,
          'label' => '',
        ),
        $sensor2_id => array(
          'weight' => 1,
          'label' => '',
        ),
      ),
    ), 'monitoring_multigraph');

    // Mock whatever is used in calculateDependencies().
    $sensor_storage = $this
      ->createMock(ConfigEntityStorageInterface::class);
    $sensor_storage
      ->expects($this
      ->any())
      ->method('load')
      ->willReturnMap(array(
      array(
        $sensor1_id,
        $sensor1,
      ),
      array(
        $sensor2_id,
        $sensor2,
      ),
    ));
    $this->entityTypeManager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->with('monitoring_sensor_config')
      ->willReturn($sensor_storage);

    // Assert dependencies are calculated correctly for the Multigraph.
    $dependencies = $multigraph
      ->calculateDependencies();
    $this
      ->assertEquals(array(
      'entity' => array(
        "sensor.{$sensor1_id}",
        "sensor.{$sensor2_id}",
      ),
    ), $dependencies);
  }

  /**
   * Returns a mock SensorConfig entity.
   *
   * @param array $id
   *   An ID to set on the sensor.
   *
   * @return \Drupal\monitoring\Entity\SensorConfig|\PHPUnit_Framework_MockObject_MockObject
   *   The mock sensor object.
   */
  protected function getMockSensor($id) {
    $sensor1 = $this
      ->getMockBuilder(SensorConfig::class)
      ->setConstructorArgs([
      [],
      'monitoring_sensor_config',
    ])
      ->getMock();
    $sensor1
      ->expects($this
      ->any())
      ->method('getConfigDependencyName')
      ->willReturn("sensor.{$id}");
    return $sensor1;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MultigraphUnitTest::$entityTypeManager protected property A mock entity manager.
MultigraphUnitTest::getMockSensor protected function Returns a mock SensorConfig entity.
MultigraphUnitTest::setUp protected function Overrides UnitTestCase::setUp
MultigraphUnitTest::testCalculateDependencies public function @covers ::calculateDependencies
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.