3 namespace Drupal\Tests\blazy\Traits;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\blazy\BlazyManager;
9 * A Trait common for Blazy related service managers.
11 trait BlazyManagerUnitTestTrait {
14 * Setup the unit manager.
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');
27 $this->token = $this->getMockBuilder('\Drupal\Core\Utility\Token')
28 ->disableOriginalConstructor()
30 $this->token->expects($this->any())
32 ->willReturnArgument(0);
34 $this->configFactory = $this->getConfigFactoryStub([
37 'responsive_image' => TRUE,
39 'blazy' => ['loadInvisible' => FALSE, 'offset' => 100],
45 * Setup the unit manager.
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);
58 \Drupal::setContainer($container);
60 $this->blazyManager = new BlazyManager(
61 $this->entityTypeManager,
70 * Prepare image styles.
72 protected function setUpImageStyle() {
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')
82 $styles[$style] = $mock;
85 $ids = array_keys($styles);
86 $storage = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
87 $storage->expects($this->any())
88 ->method('loadMultiple')
90 ->willReturn($styles);
93 $storage->expects($this->any())
96 ->will($this->returnValue($styles[$style]));
98 $this->entityTypeManager->expects($this->any())
99 ->method('getStorage')
100 ->with('image_style')
101 ->willReturn($storage);
107 * Prepare Responsive image styles.
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')
120 $image_styles[$style] = $mock;
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')
132 $styles[$style] = $mock;
135 $ids = array_keys($styles);
136 $storage = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
137 $storage->expects($this->any())
138 ->method('loadMultiple')
140 ->willReturn($styles);
142 $style = 'blazy_picture_test';
143 $storage->expects($this->any())
146 ->willReturn($styles[$style]);
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');