ContentLoaderTest.php in YAML Content 8
File
tests/src/Unit/ContentLoader/ContentLoaderTest.php
View source
<?php
namespace Drupal\Tests\yaml_content\Unit\ContentLoader;
use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldItemList;
use Drupal\yaml_content\ContentLoader\ContentLoader;
class ContentLoaderTest extends ContentLoaderTestBase {
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));
}
public function testParseContent() {
$this
->markTestIncomplete();
}
public function testMissingContentFile() {
$test_file = 'missing.content.yml';
$this
->assertFalse($this->root
->hasChild('content/missing.content.yml'));
$this->contentLoader
->setContentPath($this->root
->url());
$this
->expectException(\PHPUnit\Framework\Error\Warning::class);
$this->contentLoader
->parseContent($test_file);
}
public function testEmptyContentFile() {
$test_file = 'emptyFile.content.yml';
$this
->createContentTestFile($test_file, '');
$this->contentLoader = $this
->getContentLoaderMock([
'getEventDispatcher',
]);
$event_dispatcher_mock = $this
->createMock(ContainerAwareEventDispatcher::class);
$this->contentLoader
->method('getEventDispatcher')
->willReturn($event_dispatcher_mock);
$this->contentLoader
->setContentPath($this->root
->url());
$parsed_content = $this->contentLoader
->parseContent($test_file);
$this
->assertArrayEquals([], $parsed_content, 'Empty content files return an empty array.');
}
public function testLoadContent() {
$this
->markTestIncomplete();
}
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);
}
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);
}
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);
}
}