FacetSourcePluginManagerTest.php 3.69 KB
Newer Older
1
2
<?php

3
namespace Drupal\Tests\facets\Unit\FacetSource;
4
5

use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
6
use Drupal\Component\Plugin\Exception\PluginException;
7
8
9
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
10
use Drupal\facets\FacetSource\FacetSourcePluginManager;
11
12
13
14
use Drupal\Tests\UnitTestCase;
use Zend\Stdlib\ArrayObject;

/**
borisson_'s avatar
borisson_ committed
15
16
 * Unit test for plugin manager.
 *
17
 * @group facets
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 */
class FacetSourcePluginManagerTest extends UnitTestCase {

  /**
   * The cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $cache;

  /**
   * The plugin discovery.
   *
   * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $discovery;

  /**
   * The plugin factory.
   *
   * @var \Drupal\Component\Plugin\Factory\DefaultFactory|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $factory;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $moduleHandler;

  /**
   * The plugin manager under test.
   *
52
   * @var \Drupal\facets\FacetSource\FacetSourcePluginManager
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
   */
  public $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $this->discovery = $this->getMock(DiscoveryInterface::class);

    $this->factory = $this->getMockBuilder(DefaultFactory::class)
      ->disableOriginalConstructor()
      ->getMock();

    $this->moduleHandler = $this->getMock(ModuleHandlerInterface::class);

    $this->cache = $this->getMock(CacheBackendInterface::class);

    $namespaces = new ArrayObject();

    $this->sut = new FacetSourcePluginManager($namespaces, $this->cache, $this->moduleHandler);
    $discovery_property = new \ReflectionProperty($this->sut, 'discovery');
    $discovery_property->setAccessible(TRUE);
    $discovery_property->setValue($this->sut, $this->discovery);
    $factory_property = new \ReflectionProperty($this->sut, 'factory');
    $factory_property->setAccessible(TRUE);
    $factory_property->setValue($this->sut, $this->factory);
  }

  /**
   * Tests plugin manager constructor.
   */
  public function testConstruct() {
    $namespaces = new ArrayObject();
86
    $sut = new FacetSourcePluginManager($namespaces, $this->cache, $this->moduleHandler);
87
    $this->assertInstanceOf(FacetSourcePluginManager::class, $sut);
88
89
90
91
92
93
  }

  /**
   * Tests plugin manager's getDefinitions method.
   */
  public function testGetDefinitions() {
94
95
96
97
98
99
100
101
    $definitions = [
      'foo' => [
        'id' => 'foo_bar',
        'label' => 'Foo bar',
        'description' => 'test',
        'display_id' => 'foo',
      ],
    ];
102
103
104
105
106
107
    $this->discovery->expects($this->once())
      ->method('getDefinitions')
      ->willReturn($definitions);
    $this->assertSame($definitions, $this->sut->getDefinitions());
  }

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  /**
   * Tests plugin manager definitions.
   *
   * @dataProvider invalidDefinitions
   */
  public function testInvalidDefinitions($invalid_definition) {
    $definitions = ['foo' => [$invalid_definition]];

    $this->discovery->expects($this->once())
      ->method('getDefinitions')
      ->willReturn($definitions);

    $this->setExpectedException(PluginException::class);
    $this->sut->getDefinitions();
  }

  /**
   * Provides invalid definitions.
   *
   * @return array
   *   An invalid data provider.
   */
  public function invalidDefinitions() {
    return [
      'only id' => ['id' => 'owl'],
      'only display_id' => ['display_id' => 'search_api:owl'],
      'only label' => ['label' => 'Owl'],
borisson_'s avatar
borisson_ committed
135
      'no label' => ['id' => 'owl', 'display_id' => 'Owl'],
136
137
138
    ];
  }

139
}