Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / blazy / tests / src / Traits / BlazyManagerUnitTestTrait.php
1 <?php
2
3 namespace Drupal\Tests\blazy\Traits;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\blazy\BlazyManager;
7
8 /**
9  * A Trait common for Blazy related service managers.
10  */
11 trait BlazyManagerUnitTestTrait {
12
13   /**
14    * Setup the unit manager.
15    */
16   protected function setUpUnitServices() {
17     $this->entityManager      = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
18     $this->entityStorage      = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
19     $this->entityViewBuilder  = $this->getMock('Drupal\Core\Entity\EntityViewBuilderInterface');
20     $this->entityTypeMock     = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
21     $this->entityFieldManager = $this->getMock('\Drupal\Core\Entity\EntityFieldManagerInterface');
22     $this->entityTypeManager  = $this->getMock('\Drupal\Core\Entity\EntityTypeManagerInterface');
23     $this->moduleHandler      = $this->getMock('\Drupal\Core\Extension\ModuleHandlerInterface');
24     $this->renderer           = $this->getMock('\Drupal\Core\Render\RendererInterface');
25     $this->cache              = $this->getMock('\Drupal\Core\Cache\CacheBackendInterface');
26
27     $this->token = $this->getMockBuilder('\Drupal\Core\Utility\Token')
28       ->disableOriginalConstructor()
29       ->getMock();
30     $this->token->expects($this->any())
31       ->method('replace')
32       ->willReturnArgument(0);
33
34     $this->configFactory = $this->getConfigFactoryStub([
35       'blazy.settings' => [
36         'admin_css' => TRUE,
37         'responsive_image' => TRUE,
38         'one_pixel' => TRUE,
39         'blazy' => ['loadInvisible' => FALSE, 'offset' => 100],
40       ],
41     ]);
42   }
43
44   /**
45    * Setup the unit manager.
46    */
47   protected function setUpUnitContainer() {
48     $container = new ContainerBuilder();
49     $container->set('entity.manager', $this->entityManager);
50     $container->set('entity_field.manager', $this->entityFieldManager);
51     $container->set('entity_type.manager', $this->entityTypeManager);
52     $container->set('module_handler', $this->moduleHandler);
53     $container->set('renderer', $this->renderer);
54     $container->set('config.factory', $this->configFactory);
55     $container->set('cache.default', $this->cache);
56     $container->set('token', $this->token);
57
58     \Drupal::setContainer($container);
59
60     $this->blazyManager = new BlazyManager(
61       $this->entityTypeManager,
62       $this->moduleHandler,
63       $this->renderer,
64       $this->configFactory,
65       $this->cache
66     );
67   }
68
69   /**
70    * Prepare image styles.
71    */
72   protected function setUpImageStyle() {
73     $styles = [];
74
75     $dummies = ['blazy_crop', 'large', 'medium', 'small'];
76     foreach ($dummies as $key => $style) {
77       $mock = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityInterface');
78       $mock->expects($this->any())
79         ->method('getCacheTags')
80         ->willReturn([]);
81
82       $styles[$style] = $mock;
83     }
84
85     $ids = array_keys($styles);
86     $storage = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
87     $storage->expects($this->any())
88       ->method('loadMultiple')
89       ->with($ids)
90       ->willReturn($styles);
91
92     $style = 'large';
93     $storage->expects($this->any())
94       ->method('load')
95       ->with($style)
96       ->will($this->returnValue($styles[$style]));
97
98     $this->entityTypeManager->expects($this->any())
99       ->method('getStorage')
100       ->with('image_style')
101       ->willReturn($storage);
102
103     return $styles;
104   }
105
106   /**
107    * Prepare Responsive image styles.
108    */
109   protected function setUpResponsiveImageStyle() {
110     $styles = $image_styles = [];
111     foreach (['fallback', 'small', 'medium', 'large'] as $style) {
112       $mock = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityInterface');
113       $mock->expects($this->any())
114         ->method('getConfigDependencyName')
115         ->willReturn('image.style.' . $style);
116       $mock->expects($this->any())
117         ->method('getCacheTags')
118         ->willReturn([]);
119
120       $image_styles[$style] = $mock;
121     }
122
123     foreach (['blazy_picture_test', 'blazy_responsive_test'] as $style) {
124       $mock = $this->getMock('Drupal\responsive_image\ResponsiveImageStyleInterface');
125       $mock->expects($this->any())
126         ->method('getImageStyleIds')
127         ->willReturn(array_keys($image_styles));
128       $mock->expects($this->any())
129         ->method('getCacheTags')
130         ->willReturn([]);
131
132       $styles[$style] = $mock;
133     }
134
135     $ids = array_keys($styles);
136     $storage = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
137     $storage->expects($this->any())
138       ->method('loadMultiple')
139       ->with($ids)
140       ->willReturn($styles);
141
142     $style = 'blazy_picture_test';
143     $storage->expects($this->any())
144       ->method('load')
145       ->with($style)
146       ->willReturn($styles[$style]);
147
148     $this->entityTypeManager->expects($this->any())
149       ->method('getStorage')
150       ->with('responsive_image_style')
151       ->willReturn($storage);
152     $this->entityTypeManager->expects($this->any())
153       ->method('getEntityTypeFromClass')
154       ->with('Drupal\image\Entity\ImageStyle')
155       ->willReturn('image_style');
156
157     return $styles;
158   }
159
160 }