You are here

class ContentLoaderTest in YAML Content 8

Test generic functionality of the ContentLoader class.

@coversDefaultClass \Drupal\yaml_content\ContentLoader\ContentLoader @group yaml_content

Hierarchy

Expanded class hierarchy of ContentLoaderTest

File

tests/src/Unit/ContentLoader/ContentLoaderTest.php, line 16

Namespace

Drupal\Tests\yaml_content\Unit\ContentLoader
View 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

Namesort descending Modifiers Type Description Overrides
ContentLoaderTest::testEmptyContentFile public function Tests the correct return value when parsing an empty file.
ContentLoaderTest::testLoadContent public function Test the entry point content loading behavior.
ContentLoaderTest::testMissingContentFile public function Tests behavior when a content file is unavailable.
ContentLoaderTest::testParseContent public function Test general behavior of the parseContent() method.
ContentLoaderTest::testPopulateFieldCardinalityTooMuchData public function @covers ::populateField
ContentLoaderTest::testPopulateFieldCardinalityZero public function @covers ::populateField
ContentLoaderTest::testPopulateFieldProcess public function @covers ::populateField
ContentLoaderTest::testSetPath public function Test the setContentPath() method.
ContentLoaderTestBase::$contentLoader protected property A prepared ContentLoader object for testing.
ContentLoaderTestBase::createContentTestFile protected function Create a test file with specified contents for testing.
ContentLoaderTestBase::getContentLoaderMock protected function Mock the ContentLoader class to support test inspections.
ContentLoaderTestBase::setUp public function Overrides UnitTestCase::setUp
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.