summaryrefslogtreecommitdiffstats
path: root/core/modules/breakpoint/tests/src/BreakpointGroupConfigEntityUnitTest.php
blob: 61ad1aca6df26a0b2a8ef57ac3f9986c69fb41b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
52
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php

/**
 * @file
 * Contains \Drupal\breakpoint\Tests\BreakpointGroupConfigEntityUnitTest.
 */

namespace Drupal\breakpoint\Tests;

use Drupal\breakpoint\Entity\Breakpoint;
use Drupal\breakpoint\Entity\BreakpointGroup;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;

/**
 * @coversDefaultClass \Drupal\breakpoint\Entity\BreakpointGroup
 * @group breakpoint
 */
class BreakpointGroupConfigEntityUnitTest extends UnitTestCase {

  /**
   * The entity under test.
   *
   * @var \Drupal\breakpoint\Entity\BreakpointGroup|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entity;

  /**
   * The entity type used for testing.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityType;

  /**
   * The entity manager used for testing.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityManager;

  /**
   * The ID of the type of the entity under test.
   *
   * @var string
   */
  protected $entityTypeId;

  /**
   * The UUID generator used for testing.
   *
   * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $uuid;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->entityTypeId = $this->randomMachineName();

    $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
    $this->entityType->expects($this->any())
      ->method('getProvider')
      ->will($this->returnValue('breakpoint'));

    $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
    $this->entityManager->expects($this->any())
      ->method('getDefinition')
      ->with($this->entityTypeId)
      ->will($this->returnValue($this->entityType));

    $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');

    $container = new ContainerBuilder();
    $container->set('entity.manager', $this->entityManager);
    $container->set('uuid', $this->uuid);
    \Drupal::setContainer($container);
  }

  /**
   * Sets up the entity to test.
   */
  public function setUpEntity($values) {
    // Mocking the entity under test because the class contains calls to
    // procedural code.
    $this->entity = $this->getMockBuilder('\Drupal\breakpoint\Entity\BreakpointGroup')
      ->setConstructorArgs(array($values, $this->entityTypeId))
      ->setMethods(array('getBreakpoints'))
      ->getMock();
  }

  /**
   * @covers ::calculateDependencies
   */
  public function testCalculateDependenciesModule() {
    $this->setUpEntity(
      array(
        'name' => 'test',
        'source' => 'test_module',
        'sourceType' => Breakpoint::SOURCE_TYPE_MODULE,
      )
    );
    $breakpoint = $this->getMock('\Drupal\breakpoint\BreakpointInterface');
    $breakpoint->expects($this->once())
      ->method('getConfigDependencyName')
      ->will($this->returnValue('breakpoint.breakpoint.test'));

    $this->entity->expects($this->once())
      ->method('getBreakpoints')
      ->will($this->returnValue(array($breakpoint)));

    $dependencies = $this->entity->calculateDependencies();
    $this->assertArrayNotHasKey('theme', $dependencies);
    $this->assertContains('test_module', $dependencies['module']);
    $this->assertContains('breakpoint.breakpoint.test', $dependencies['entity']);
  }

  /**
   * @covers ::calculateDependencies
   */
  public function testCalculateDependenciesTheme() {
    $this->setUpEntity(
      array(
        'name' => 'test',
        'source' => 'test_theme',
        'sourceType' => Breakpoint::SOURCE_TYPE_THEME,
      )
    );

    $breakpoint = $this->getMockBuilder('\Drupal\breakpoint\Entity\Breakpoint')
                       ->disableOriginalConstructor()->getMock();
    $breakpoint->expects($this->once())
               ->method('getConfigDependencyName')
               ->will($this->returnValue('breakpoint.breakpoint.test'));

    $this->entity->expects($this->once())
                 ->method('getBreakpoints')
                 ->will($this->returnValue(array($breakpoint)));

    $dependencies = $this->entity->calculateDependencies();
    $this->assertArrayNotHasKey('module', $dependencies);
    $this->assertContains('test_theme', $dependencies['theme']);
    $this->assertContains('breakpoint.breakpoint.test', $dependencies['entity']);
  }

  /**
   * @expectedException \Drupal\breakpoint\InvalidBreakpointNameException
   */
  public function testNameException () {
    new BreakpointGroup(array(
      'label' => $this->randomMachineName(),
      'source' => 'custom_module',
      'sourceType' => 'oops',
    ));
  }

}