MultigraphUnitTest.php in Monitoring 8
File
modules/multigraph/tests/src/Unit/Entity/MultigraphUnitTest.php
View source
<?php
namespace Drupal\Tests\monitoring_multigraph\Unit\Entity;
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\monitoring\Entity\SensorConfig;
use Drupal\monitoring_multigraph\Entity\Multigraph;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class MultigraphUnitTest extends UnitTestCase {
protected $entityTypeManager;
protected function setUp() {
$this->entityTypeManager = $this
->createMock(EntityTypeManagerInterface::class);
$container = new ContainerBuilder();
$container
->set('entity_type.manager', $this->entityTypeManager);
\Drupal::setContainer($container);
}
public function testCalculateDependencies() {
$sensor1_id = $this
->getRandomGenerator()
->word(16);
$sensor1 = $this
->getMockSensor($sensor1_id);
$sensor2_id = $this
->getRandomGenerator()
->word(16);
$sensor2 = $this
->getMockSensor($sensor2_id);
$multigraph = new Multigraph(array(
'sensors' => array(
$sensor1_id => array(
'weight' => 0,
'label' => '',
),
$sensor2_id => array(
'weight' => 1,
'label' => '',
),
),
), 'monitoring_multigraph');
$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);
$dependencies = $multigraph
->calculateDependencies();
$this
->assertEquals(array(
'entity' => array(
"sensor.{$sensor1_id}",
"sensor.{$sensor2_id}",
),
), $dependencies);
}
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;
}
}