YamlDirectoryDiscoveryTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace Drupal\Tests\Component\Discovery;
  3. use Drupal\Component\Discovery\DiscoveryException;
  4. use Drupal\Component\Discovery\YamlDirectoryDiscovery;
  5. use Drupal\Component\FileCache\FileCacheFactory;
  6. use org\bovigo\vfs\vfsStream;
  7. use PHPUnit\Framework\TestCase;
  8. /**
  9. * YamlDirectoryDiscoveryTest component unit tests.
  10. *
  11. * @coversDefaultClass \Drupal\Component\Discovery\YamlDirectoryDiscovery
  12. *
  13. * @group Discovery
  14. */
  15. class YamlDirectoryDiscoveryTest extends TestCase {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. protected function setUp() {
  20. // Ensure that FileCacheFactory has a prefix.
  21. FileCacheFactory::setPrefix('prefix');
  22. }
  23. /**
  24. * Tests YAML directory discovery.
  25. *
  26. * @covers ::findAll
  27. */
  28. public function testDiscovery() {
  29. vfsStream::setup('modules', NULL, [
  30. 'test_1' => [
  31. 'subdir1' => [
  32. 'item_1.test.yml' => "id: item1\nname: 'test1 item 1'",
  33. ],
  34. 'subdir2' => [
  35. 'item_2.test.yml' => "id: item2\nname: 'test1 item 2'",
  36. ],
  37. ],
  38. 'test_2' => [
  39. 'subdir1' => [
  40. 'item_3.test.yml' => "id: item3\nname: 'test2 item 3'",
  41. ],
  42. 'subdir2' => [],
  43. ],
  44. 'test_3' => [],
  45. 'test_4' => [
  46. 'subdir1' => [
  47. 'item_4.test.yml' => "id: item4\nname: 'test4 item 4'",
  48. 'item_5.test.yml' => "id: item5\nname: 'test4 item 5'",
  49. 'item_6.test.yml' => "id: item6\nname: 'test4 item 6'",
  50. ],
  51. ],
  52. ]);
  53. // Set up the directories to search.
  54. $directories = [
  55. // Multiple directories both with valid items.
  56. 'test_1' => [
  57. vfsStream::url('modules/test_1/subdir1'),
  58. vfsStream::url('modules/test_1/subdir2'),
  59. ],
  60. // The subdir2 directory is empty.
  61. 'test_2' => [
  62. vfsStream::url('modules/test_2/subdir1'),
  63. vfsStream::url('modules/test_2/subdir2'),
  64. ],
  65. // Directories that do not exist.
  66. 'test_3' => [
  67. vfsStream::url('modules/test_3/subdir1'),
  68. vfsStream::url('modules/test_3/subdir2'),
  69. ],
  70. // A single directory.
  71. 'test_4' => vfsStream::url('modules/test_4/subdir1'),
  72. ];
  73. $discovery = new YamlDirectoryDiscovery($directories, 'test');
  74. $data = $discovery->findAll();
  75. $this->assertSame(['id' => 'item1', 'name' => 'test1 item 1', YamlDirectoryDiscovery::FILE_KEY => 'vfs://modules/test_1/subdir1/item_1.test.yml'], $data['test_1']['item1']);
  76. $this->assertSame(['id' => 'item2', 'name' => 'test1 item 2', YamlDirectoryDiscovery::FILE_KEY => 'vfs://modules/test_1/subdir2/item_2.test.yml'], $data['test_1']['item2']);
  77. $this->assertCount(2, $data['test_1']);
  78. $this->assertSame(['id' => 'item3', 'name' => 'test2 item 3', YamlDirectoryDiscovery::FILE_KEY => 'vfs://modules/test_2/subdir1/item_3.test.yml'], $data['test_2']['item3']);
  79. $this->assertCount(1, $data['test_2']);
  80. $this->assertTrue(empty($data['test_3']), 'test_3 provides 0 items');
  81. $this->assertSame(['id' => 'item4', 'name' => 'test4 item 4', YamlDirectoryDiscovery::FILE_KEY => 'vfs://modules/test_4/subdir1/item_4.test.yml'], $data['test_4']['item4']);
  82. $this->assertSame(['id' => 'item5', 'name' => 'test4 item 5', YamlDirectoryDiscovery::FILE_KEY => 'vfs://modules/test_4/subdir1/item_5.test.yml'], $data['test_4']['item5']);
  83. $this->assertSame(['id' => 'item6', 'name' => 'test4 item 6', YamlDirectoryDiscovery::FILE_KEY => 'vfs://modules/test_4/subdir1/item_6.test.yml'], $data['test_4']['item6']);
  84. $this->assertCount(3, $data['test_4']);
  85. }
  86. /**
  87. * Tests YAML directory discovery with an alternate ID key.
  88. *
  89. * @covers ::findAll
  90. */
  91. public function testDiscoveryAlternateId() {
  92. vfsStream::setup('modules', NULL, [
  93. 'test_1' => [
  94. 'item_1.test.yml' => "alt_id: item1\nid: ignored",
  95. ],
  96. ]);
  97. // Set up the directories to search.
  98. $directories = ['test_1' => vfsStream::url('modules/test_1')];
  99. $discovery = new YamlDirectoryDiscovery($directories, 'test', 'alt_id');
  100. $data = $discovery->findAll();
  101. $this->assertSame(['alt_id' => 'item1', 'id' => 'ignored', YamlDirectoryDiscovery::FILE_KEY => 'vfs://modules/test_1/item_1.test.yml'], $data['test_1']['item1']);
  102. $this->assertCount(1, $data['test_1']);
  103. }
  104. /**
  105. * Tests YAML directory discovery with a missing ID key.
  106. *
  107. * @covers ::findAll
  108. * @covers ::getIdentifier
  109. */
  110. public function testDiscoveryNoIdException() {
  111. if (method_exists($this, 'expectException')) {
  112. $this->expectException(DiscoveryException::class);
  113. $this->expectExceptionMessage('The vfs://modules/test_1/item_1.test.yml contains no data in the identifier key \'id\'');
  114. }
  115. else {
  116. $this->setExpectedException(DiscoveryException::class, 'The vfs://modules/test_1/item_1.test.yml contains no data in the identifier key \'id\'');
  117. }
  118. vfsStream::setup('modules', NULL, [
  119. 'test_1' => [
  120. 'item_1.test.yml' => "",
  121. ],
  122. ]);
  123. // Set up the directories to search.
  124. $directories = ['test_1' => vfsStream::url('modules/test_1')];
  125. $discovery = new YamlDirectoryDiscovery($directories, 'test');
  126. $discovery->findAll();
  127. }
  128. /**
  129. * Tests YAML directory discovery with invalid YAML.
  130. *
  131. * @covers ::findAll
  132. */
  133. public function testDiscoveryInvalidYamlException() {
  134. if (method_exists($this, 'expectException')) {
  135. $this->expectException(DiscoveryException::class);
  136. $this->expectExceptionMessage('The vfs://modules/test_1/item_1.test.yml contains invalid YAML');
  137. }
  138. else {
  139. $this->setExpectedException(DiscoveryException::class, 'The vfs://modules/test_1/item_1.test.yml contains invalid YAML');
  140. }
  141. vfsStream::setup('modules', NULL, [
  142. 'test_1' => [
  143. 'item_1.test.yml' => "id: invalid\nfoo : [bar}",
  144. ],
  145. ]);
  146. // Set up the directories to search.
  147. $directories = ['test_1' => vfsStream::url('modules/test_1')];
  148. $discovery = new YamlDirectoryDiscovery($directories, 'test');
  149. $discovery->findAll();
  150. }
  151. }