summaryrefslogtreecommitdiffstats
path: root/core/modules/responsive_image/tests/src/ResponsiveImageMappingEntityTest.php
blob: 78303f6e9167a1992097dcbb7d32345f1ff28583 (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
<?php

/**
 * @file
 * Contains \Drupal\responsive_image\Tests\ResponsiveImageMappingEntityTest.
 */

namespace Drupal\responsive_image\Tests;

use Drupal\responsive_image\Entity\ResponsiveImageMapping;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\DependencyInjection\ContainerBuilder;

/**
 * @coversDefaultClass \Drupal\responsive_image\Entity\ResponsiveImageMapping
 * @group responsive_image
 */
class ResponsiveImageMappingEntityTest extends UnitTestCase {

  /**
   * 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;

  /**
   * The ID of the breakpoint group used for testing.
   *
   * @var string
   */
  protected $breakpointGroupId;

  /**
   * The breakpoint group used for testing.
   *
   * @var \Drupal\breakpoint\Entity\BreakpointGroup|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $breakpointGroup;

  /**
   * The breakpoint group storage used for testing.
   *
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $breakpointGroupStorage;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->entityTypeId = $this->randomMachineName();
    $this->provider = $this->randomMachineName();
    $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
    $this->entityType->expects($this->any())
                     ->method('getProvider')
                     ->will($this->returnValue($this->provider));

    $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');

    $this->breakpointGroupId = $this->randomMachineName(9);
    $this->breakpointGroup = $this->getMock('Drupal\breakpoint\Entity\BreakpointGroup', array(), array(array('name' => 'test', 'id' => $this->breakpointGroupId)));

    $this->breakpointGroupStorage = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
    $this->breakpointGroupStorage
      ->expects($this->any())
      ->method('load')
      ->with($this->breakpointGroupId)
      ->will($this->returnValue($this->breakpointGroup));

    $this->entityManager->expects($this->any())
      ->method('getStorage')
      ->will($this->returnValue($this->breakpointGroupStorage));

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

  /**
   * @covers ::calculateDependencies
   */
  public function testCalculateDependencies() {
    $responsive_image_mapping = new ResponsiveImageMapping(array(), $this->entityTypeId);
    // Set the breakpoint group after creating the entity to avoid the calls
    // in the constructor.
    $responsive_image_mapping->setBreakpointGroup($this->breakpointGroupId);
    $this->breakpointGroup->expects($this->once())
      ->method('getConfigDependencyName')
      ->will($this->returnValue('breakpoint.breakpoint_group.' . $this->breakpointGroupId));

    $dependencies = $responsive_image_mapping->calculateDependencies();
    $this->assertContains('breakpoint.breakpoint_group.' . $this->breakpointGroupId, $dependencies['entity']);
  }

}