WidgetBinderDataCompilerKernelTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 98
ccs 59
cts 59
cp 1
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A topDownParagraphs() 0 5 3
A createCompiler() 0 5 1
A testCompile() 0 53 1
1
<?php
2
3
namespace Drupal\Tests\paragraphs_editor\Kernel\WidgetBinder;
4
5
use Drupal\KernelTests\KernelTestBase;
6
use Drupal\Tests\paragraphs_editor\Traits\MockContextTrait;
7
use Drupal\Tests\paragraphs_editor\Traits\MockFieldValueManagerTrait;
8
use Drupal\Tests\paragraphs_editor\Traits\TestContentGenerationTrait;
9
use Drupal\paragraphs_editor\WidgetBinder\GeneratorInterface;
10
use Drupal\paragraphs_editor\WidgetBinder\WidgetBinderDataCompiler;
11
use Prophecy\Argument;
0 ignored issues
show
Bug introduced by
The type Prophecy\Argument was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
/**
14
 * @coversDefaultClass \Drupal\paragraphs_editor\WidgetBinder\WidgetBinderDataCompiler
15
 * @group paragraphs_editor
16
 */
17
class WidgetBinderDataCompilerKernelTest extends KernelTestBase {
18
  use MockContextTrait;
19
  use MockFieldValueManagerTrait;
20
  use TestContentGenerationTrait;
21
22
  static public $modules = [
23
    'system',
24
    'user',
25
    'entity_reference_revisions',
26
    'paragraphs',
27
    'field',
28
    'file',
29
    'text',
30
    'node',
31
    'paragraphs_editor_test',
32
  ];
33
34
  protected $strictConfigSchema = FALSE;
35
36 2
  public function setUp() {
37 2
    parent::setUp();
38 2
    $this->installEntitySchema('user');
39 2
    $this->installEntitySchema('node');
40 2
    $this->installEntitySchema('paragraph');
41 2
    $this->installConfig('field');
42 2
    $this->installConfig('text');
43 2
    $this->installConfig('node');
44 2
    $this->installConfig('paragraphs_editor_test');
45 2
  }
46
47 1
  public function testCompile() {
48 1
    $storage = $this->container->get('entity_type.manager')->getStorage('paragraph');
49 1
    $paragraph = $this->generateTabs($storage);
50 1
    $context = $this->createContext();
51 1
    $buffer_item = $context->getEditBuffer()->createItem($paragraph);
52
53 1
    $compiler = $this->createCompiler();
54 1
    $paragraph = $buffer_item->getEntity();
55
56 1
    $prophecy = $this->prophesize(GeneratorInterface::CLASS);
0 ignored issues
show
Bug introduced by
The constant Drupal\paragraphs_editor...neratorInterface::CLASS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
57 1
    $prophecy->id()->willReturn('mock');
58
59 1
    $initialized_on = NULL;
60
    $prophecy->initialize(Argument::cetera())->will(function($args) use (&$initialized_on) {
61 1
      $initialized_on = $args[2]->uuid();
62 1
    })->shouldBeCalledTimes(1);
63
64 1
    $actual_paragraph_map = $expected_paragraph_map = [];
65 1
    $this->topDownParagraphs($paragraph, $expected_paragraph_map);
66
    $prophecy->processParagraph(Argument::cetera())->will(function($args) use (&$actual_paragraph_map) {
67 1
      $actual_paragraph_map[] = $args[2]->uuid();
68 1
    })->shouldBeCalledTimes(count($expected_paragraph_map));
69 1
    $prophecy->postprocessParagraph(Argument::cetera())->shouldBeCalledTimes(count($expected_paragraph_map));
70
71
    $expected_field_map = [
72 1
      [$expected_paragraph_map[0], 'field_tabs', FALSE],
73 1
      [$expected_paragraph_map[1], 'field_content', TRUE],
74
    ];
75 1
    $actual_field_map = [];
76
    $prophecy->processField(Argument::cetera())->will(function($args) use (&$actual_field_map) {
77 1
      $actual_field_map[] = [
78 1
        $args[2]->getEntity()->uuid(),
79 1
        $args[2]->getName(),
80 1
        $args[3],
81
      ];
82 1
    })->shouldBeCalledTimes(count($expected_field_map));
83 1
    $prophecy->postprocessField(Argument::cetera())->shouldBeCalledTimes(count($expected_field_map));
84
85 1
    $actual_markup = NULL;
86 1
    $prophecy->complete(Argument::cetera())->will(function ($args) use(&$actual_markup) {
87 1
      $actual_markup = $args[3];
88 1
    })->shouldBeCalledTimes(1);
89
90 1
    $compiler->addGenerator($prophecy->reveal($paragraph));
91 1
    $compiler->compile($context, $buffer_item);
92
93 1
    $this->assertEquals($paragraph->uuid(), $initialized_on);
94 1
    $this->assertEquals($expected_paragraph_map, $actual_paragraph_map);
95 1
    $this->assertEquals($expected_field_map, $actual_field_map);
96
97 1
    $view = $this->container->get('entity_type.manager')->getViewBuilder('paragraph')->view($paragraph);
98 1
    $expected_markup = $this->container->get('renderer')->renderRoot($view);
99 1
    $this->assertEquals($expected_markup, $actual_markup);
100 1
  }
101
102 1
  protected function createCompiler() {
103 1
    return new WidgetBinderDataCompiler(
104 1
      $this->container->get('entity_type.manager'),
105 1
      $this->container->get('renderer'),
106 1
      $this->createFieldValueManager()
107
    );
108
  }
109
110 1
  protected function topDownParagraphs($paragraph, array &$call_map) {
111 1
    $call_map[] = $paragraph->uuid();
112 1
    if ($paragraph->field_tabs) {
113 1
      foreach ($paragraph->field_tabs->referencedEntities() as $paragraph) {
114 1
        $call_map[] = $paragraph->uuid();
115
      }
116
    }
117 1
  }
118
}
119