class MultigraphUnitTest in Monitoring 8
@coversDefaultClass \Drupal\monitoring_multigraph\Entity\Multigraph
@group monitoring
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\monitoring_multigraph\Unit\Entity\MultigraphUnitTest
Expanded class hierarchy of MultigraphUnitTest
File
- modules/
multigraph/ tests/ src/ Unit/ Entity/ MultigraphUnitTest.php, line 17
Namespace
Drupal\Tests\monitoring_multigraph\Unit\EntityView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MultigraphUnitTest:: |
protected | property | A mock entity manager. | |
MultigraphUnitTest:: |
protected | function | Returns a mock SensorConfig entity. | |
MultigraphUnitTest:: |
protected | function |
Overrides UnitTestCase:: |
|
MultigraphUnitTest:: |
public | function | @covers ::calculateDependencies | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |