class ContentLoaderTest in YAML Content 8
Test generic functionality of the ContentLoader class.
@coversDefaultClass \Drupal\yaml_content\ContentLoader\ContentLoader @group yaml_content
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\yaml_content\Unit\ContentLoader\ContentLoaderTestBase
- class \Drupal\Tests\yaml_content\Unit\ContentLoader\ContentLoaderTest
- class \Drupal\Tests\yaml_content\Unit\ContentLoader\ContentLoaderTestBase
Expanded class hierarchy of ContentLoaderTest
File
- tests/
src/ Unit/ ContentLoader/ ContentLoaderTest.php, line 16
Namespace
Drupal\Tests\yaml_content\Unit\ContentLoaderView source
class ContentLoaderTest extends ContentLoaderTestBase {
/**
* Test the setContentPath() method.
*
* @covers ::setContentPath
*/
public function testSetPath() {
$this->contentLoader
->setContentPath($this->root
->url());
$reflected_path = (new \ReflectionObject($this->contentLoader))
->getProperty('path');
$reflected_path
->setAccessible(TRUE);
$this
->assertEquals($this->root
->url(), $reflected_path
->getValue($this->contentLoader));
}
/**
* Test general behavior of the parseContent() method.
*
* @covers ::parseContent
*
* @todo Test if $contentPath is not set
* @todo Handle parse failure
* @todo Test no array at top level of content
* @todo Confirm array structure loaded
*/
public function testParseContent() {
$this
->markTestIncomplete();
}
/**
* Tests behavior when a content file is unavailable.
*/
public function testMissingContentFile() {
$test_file = 'missing.content.yml';
// Confirm the file is not actually present.
$this
->assertFalse($this->root
->hasChild('content/missing.content.yml'));
// Prepare the path for the missing content file.
$this->contentLoader
->setContentPath($this->root
->url());
// Parse the test file expecting an error for the missing file.
$this
->expectException(\PHPUnit\Framework\Error\Warning::class);
$this->contentLoader
->parseContent($test_file);
}
/**
* Tests the correct return value when parsing an empty file.
*
* When parsing an empty file an empty array should be returned.
*/
public function testEmptyContentFile() {
// Prepare an empty content file for parsing.
$test_file = 'emptyFile.content.yml';
$this
->createContentTestFile($test_file, '');
// Get the mock content loader.
$this->contentLoader = $this
->getContentLoaderMock([
'getEventDispatcher',
]);
// Stub event dispatching.
$event_dispatcher_mock = $this
->createMock(ContainerAwareEventDispatcher::class);
$this->contentLoader
->method('getEventDispatcher')
->willReturn($event_dispatcher_mock);
// Prepare and parse the empty content file.
$this->contentLoader
->setContentPath($this->root
->url());
$parsed_content = $this->contentLoader
->parseContent($test_file);
// Confirm an empty array was returned.
$this
->assertArrayEquals([], $parsed_content, 'Empty content files return an empty array.');
}
/**
* Test the entry point content loading behavior.
*
* @covers ::loadContent
*/
public function testLoadContent() {
$this
->markTestIncomplete();
}
/**
* @covers ::populateField
*/
public function testPopulateFieldCardinalityZero() {
$field_definition = new BaseFieldDefinition();
$field_definition
->setCardinality(0);
$field = new FieldItemList($field_definition, 'foobar');
$field_data = [];
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage("'foobar' cannot hold any values.");
$this->contentLoader
->populateField($field, $field_data);
}
/**
* @covers ::populateField
*/
public function testPopulateFieldCardinalityTooMuchData() {
$field_definition = new BaseFieldDefinition();
$field_definition
->setCardinality(1);
$field = new FieldItemList($field_definition, 'foobar');
$field_data = [
[],
[],
[],
];
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage("'foobar' cannot hold more than 1 values. 3 values were parsed from the YAML file.");
$this->contentLoader
->populateField($field, $field_data);
}
/**
* @covers ::populateField
*/
public function testPopulateFieldProcess() {
$field_definition = new BaseFieldDefinition();
$field_definition
->setCardinality(1);
$field = new FieldItemList($field_definition, 'foobar');
$field_data = [
[],
];
$this
->markTestIncomplete('We cannot easily test processing is triggered because we cannot inject a Plugin Manager yet.');
$this->contentLoader
->populateField($field, $field_data);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentLoaderTest:: |
public | function | Tests the correct return value when parsing an empty file. | |
ContentLoaderTest:: |
public | function | Test the entry point content loading behavior. | |
ContentLoaderTest:: |
public | function | Tests behavior when a content file is unavailable. | |
ContentLoaderTest:: |
public | function | Test general behavior of the parseContent() method. | |
ContentLoaderTest:: |
public | function | @covers ::populateField | |
ContentLoaderTest:: |
public | function | @covers ::populateField | |
ContentLoaderTest:: |
public | function | @covers ::populateField | |
ContentLoaderTest:: |
public | function | Test the setContentPath() method. | |
ContentLoaderTestBase:: |
protected | property | A prepared ContentLoader object for testing. | |
ContentLoaderTestBase:: |
protected | function | Create a test file with specified contents for testing. | |
ContentLoaderTestBase:: |
protected | function | Mock the ContentLoader class to support test inspections. | |
ContentLoaderTestBase:: |
public | function |
Overrides UnitTestCase:: |
|
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. |