YamlDiscoveryTest.php 1.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
<?php

/**
 * @file
 * Contains \Drupal\Tests\Component\Discovery\YamlDiscoveryTest.
 */

namespace Drupal\Tests\Component\Discovery;

use Drupal\Tests\UnitTestCase;
use Drupal\Component\Discovery\YamlDiscovery;
12
13
14
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamWrapper;
use org\bovigo\vfs\vfsStreamDirectory;
15
16

/**
17
18
19
 * YamlDiscovery component unit tests.
 *
 * @group Discovery
20
21
22
23
24
25
26
 */
class YamlDiscoveryTest extends UnitTestCase {

  /**
   * Tests the YAML file discovery.
   */
  public function testDiscovery() {
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    vfsStreamWrapper::register();
    $root = new vfsStreamDirectory('modules');
    vfsStreamWrapper::setRoot($root);
    $url = vfsStream::url('modules');

    mkdir($url . '/test_1');
    file_put_contents($url . '/test_1/test_1.test.yml', 'name: test');
    file_put_contents($url . '/test_1/test_2.test.yml', 'name: test');

    mkdir($url . '/test_2');
    file_put_contents($url . '/test_2/test_3.test.yml', 'name: test');
    // Write an empty YAML file.
    file_put_contents($url . '/test_2/test_4.test.yml', '');

41
42
    // Set up the directories to search.
    $directories = array(
43
44
45
46
      'test_1' => $url . '/test_1',
      'test_2' => $url . '/test_1',
      'test_3' => $url . '/test_2',
      'test_4' => $url . '/test_2',
47
48
49
50
51
    );

    $discovery = new YamlDiscovery('test', $directories);
    $data = $discovery->findAll();

52
    $this->assertEquals(count($data), count($directories));
53
54
    $this->assertArrayHasKey('test_1', $data);
    $this->assertArrayHasKey('test_2', $data);
55
    $this->assertArrayHasKey('test_3', $data);
56
    $this->assertArrayHasKey('test_4', $data);
57

58
59
60
    foreach (array('test_1', 'test_2', 'test_3') as $key) {
      $this->assertArrayHasKey('name', $data[$key]);
      $this->assertEquals($data[$key]['name'], 'test');
61
    }
62
63

    $this->assertSame(array(), $data['test_4']);
64
65
66
  }

}