You are here

class InfoParserUnitTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Extension/InfoParserUnitTest.php \Drupal\Tests\Core\Extension\InfoParserUnitTest

Tests InfoParser class and exception.

Files for this test are stored in core/modules/system/tests/fixtures and end with .info.txt instead of info.yml in order not not be considered as real extensions.

@coversDefaultClass \Drupal\Core\Extension\InfoParser

@group Extension

Hierarchy

Expanded class hierarchy of InfoParserUnitTest

File

core/tests/Drupal/Tests/Core/Extension/InfoParserUnitTest.php, line 25
Contains \Drupal\Tests\Core\Extension\InfoParserUnitTest.

Namespace

Drupal\Tests\Core\Extension
View source
class InfoParserUnitTest extends UnitTestCase {

  /**
   * The InfoParser object.
   *
   * @var \Drupal\Core\Extension\InfoParser
   */
  protected $infoParser;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->infoParser = new InfoParser();
  }

  /**
   * Tests the functionality of the infoParser object.
   *
   * @covers ::parse
   */
  public function testInfoParserNonExisting() {
    vfsStream::setup('modules');
    $info = $this->infoParser
      ->parse(vfsStream::url('modules') . '/does_not_exist.info.txt');
    $this
      ->assertTrue(empty($info), 'Non existing info.yml returns empty array.');
  }

  /**
   * Test if correct exception is thrown for a broken info file.
   *
   * @covers ::parse
   *
   * @expectedException \Drupal\Core\Extension\InfoParserException
   * @expectedExceptionMessageRegExp #broken\.info\.txt#
   */
  public function testInfoParserBroken() {
    $broken_info = <<<BROKEN_INFO
# info.yml for testing broken YAML parsing exception handling.
name: File
type: module
description: 'Defines a file field type.'
package: Core
version: VERSION
core: 8.x
dependencies::;;
  - field
BROKEN_INFO;
    vfsStream::setup('modules');
    vfsStream::create([
      'fixtures' => [
        'broken.info.txt' => $broken_info,
      ],
    ]);
    $filename = vfsStream::url('modules/fixtures/broken.info.txt');
    $this->infoParser
      ->parse($filename);
  }

  /**
   * Tests that missing required keys are detected.
   *
   * @covers ::parse
   *
   * @expectedException \Drupal\Core\Extension\InfoParserException
   * @expectedExceptionMessageRegExp #Missing required keys \(type, core, name\) in .+?missing_keys\.info\.txt#
   */
  public function testInfoParserMissingKeys() {
    $missing_keys = <<<MISSINGKEYS
# info.yml for testing missing name, description, and type keys.
package: Core
version: VERSION
dependencies:
  - field
MISSINGKEYS;
    vfsStream::setup('modules');
    vfsStream::create([
      'fixtures' => [
        'missing_keys.info.txt' => $missing_keys,
      ],
    ]);
    $filename = vfsStream::url('modules/fixtures/missing_keys.info.txt');
    $this->infoParser
      ->parse($filename);
  }

  /**
   * Tests that missing required key is detected.
   *
   * @covers ::parse
   *
   * @expectedException \Drupal\Core\Extension\InfoParserException
   * @expectedExceptionMessageRegExp #Missing required keys \(type\) in .+?missing_key\.info\.txt#
   */
  public function testInfoParserMissingKey() {
    $missing_key = <<<MISSINGKEY
# info.yml for testing missing type key.
name: File
description: 'Defines a file field type.'
package: Core
version: VERSION
core: 8.x
dependencies:
  - field
MISSINGKEY;
    vfsStream::setup('modules');
    vfsStream::create([
      'fixtures' => [
        'missing_key.info.txt' => $missing_key,
      ],
    ]);
    $filename = vfsStream::url('modules/fixtures/missing_key.info.txt');
    $this->infoParser
      ->parse($filename);
  }

  /**
   * Tests common info file.
   *
   * @covers ::parse
   */
  public function testInfoParserCommonInfo() {
    $common = <<<COMMONTEST
core: 8.x
name: common_test
type: module
description: 'testing info file parsing'
simple_string: 'A simple string'
version: "VERSION"
double_colon: dummyClassName::
COMMONTEST;
    vfsStream::setup('modules');
    vfsStream::create([
      'fixtures' => [
        'common_test.info.txt' => $common,
      ],
    ]);
    $info_values = $this->infoParser
      ->parse(vfsStream::url('modules/fixtures/common_test.info.txt'));
    $this
      ->assertEquals($info_values['simple_string'], 'A simple string', 'Simple string value was parsed correctly.');
    $this
      ->assertEquals($info_values['version'], \Drupal::VERSION, 'Constant value was parsed correctly.');
    $this
      ->assertEquals($info_values['double_colon'], 'dummyClassName::', 'Value containing double-colon was parsed correctly.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
InfoParserUnitTest::$infoParser protected property The InfoParser object.
InfoParserUnitTest::setUp protected function Overrides UnitTestCase::setUp
InfoParserUnitTest::testInfoParserBroken public function Test if correct exception is thrown for a broken info file.
InfoParserUnitTest::testInfoParserCommonInfo public function Tests common info file.
InfoParserUnitTest::testInfoParserMissingKey public function Tests that missing required key is detected.
InfoParserUnitTest::testInfoParserMissingKeys public function Tests that missing required keys are detected.
InfoParserUnitTest::testInfoParserNonExisting public function Tests the functionality of the infoParser object.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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.