Completed
Push — feature/linting ( 138974...3440f6 )
by Christopher
13:39
created

MockContextTrait::createEditBuffer()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 22
nc 2
nop 1
dl 0
loc 36
ccs 0
cts 24
cp 0
crap 12
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\Tests\paragraphs_editor\Traits;
4
5
use Drupal\Component\Uuid;
6
use Drupal\Core\Field\FieldConfigInterface;
7
use Drupal\paragraphs\ParagraphInterface;
8
use Drupal\paragraphs_editor\EditBuffer\EditBufferInterface;
9
use Drupal\paragraphs_editor\EditBuffer\EditBufferItemInterface;
10
use Drupal\paragraphs_editor\EditorCommand\CommandContextFactoryInterface;
11
use Drupal\paragraphs_editor\EditorCommand\CommandContextInterface;
12
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...
13
14
trait MockContextTrait {
15
16 17
  protected function mockParagraphDefaults() {
17
    return [
18 17
      'id' => rand(),
0 ignored issues
show
Bug introduced by
The call to rand() has too few arguments starting with min. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

18
      'id' => /** @scrutinizer ignore-call */ rand(),

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
19 17
      'uuid' => (new Uuid\Php())->generate(),
20 17
      'bundle' => 'default',
21
    ];
22
  }
23
24 5
  protected function contextDefaults() {
25 5
    $prophecy = $this->prophesize(FieldConfigInterface::CLASS);
0 ignored issues
show
Bug introduced by
It seems like prophesize() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
    /** @scrutinizer ignore-call */ 
26
    $prophecy = $this->prophesize(FieldConfigInterface::CLASS);
Loading history...
Bug introduced by
The constant Drupal\Core\Field\FieldConfigInterface::CLASS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
26 5
    $prophecy->id()->willReturn('default_field_id');
27
    return [
28 5
      'context_id' => md5(rand() . rand() . rand()),
0 ignored issues
show
Bug introduced by
The call to rand() has too few arguments starting with min. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
      'context_id' => md5(/** @scrutinizer ignore-call */ rand() . rand() . rand()),

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
29
      'bundle_filter' => [],
30
      'entity' => NULL,
31 5
      'field_definition' => $prophecy->reveal(),
32
      'edit_buffer' => [],
33
      'valid' => TRUE,
34
      'settings' => [],
35
    ];
36
  }
37
38
  protected function editBufferDefaults() {
39
    return [
40
      'uid' => 1,
41
    ];
42
  }
43
44 5
  protected function createContext(array $options = []) {
45 5
    $options += $this->contextDefaults();
46 5
    $options['edit_buffer']['context_id'] = $options['context_id'];
47
48 5
    $prophecy = $this->prophesize(CommandContextInterface::CLASS);
0 ignored issues
show
Bug introduced by
The constant Drupal\paragraphs_editor...ContextInterface::CLASS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
49 5
    $prophecy->getEntity()->willReturn($options['entity']);
50 5
    $prophecy->getFieldConfig()->willReturn($options['field_definition']);
51
52 5
    if (!empty($options['bundle_filter'])) {
53
      $prophecy->isValidBundle(Argument::any())->will(function ($args) use ($options) {
54
        return isset(array_flip($options)[$args[0]]);
55
      });
56
    }
57
    else {
58 5
      $prophecy->isValidBundle(Argument::any())->willReturn(TRUE);
59
    }
60
61
    $prophecy->getEditBuffer()->willReturn($this->createEditBuffer($options['edit_buffer']));
62
    $prophecy->getContextString()->willReturn($options['context_id']);
63
64
    $prophecy->getTemporary(Argument::any())->willReturn(NULL);
65
    $prophecy->setTemporary(Argument::cetera())->will(function ($args, $prophecy) {
66
      $prophecy->getTemporary($args[0])->willReturn($args[1]);
67
    });
68
69
    $prophecy->isValid()->willReturn($options['valid']);
70
71
    $prophecy->getPlugin(Argument::any())->willReturn(NULL);
72
    $prophecy->setPlugin(Argument::any())->will(function ($args, $prophecy) {
73
      $prophecy->getPlugin($args[0])->willReturn($args[1]);
74
    });
75
76
    $prophecy->getSettings()->willReturn($options['settings']);
77
    $prophecy->getSetting(Argument::any())->willReturn(NULL);
78
    foreach ($options['settings'] as $key => $val) {
79
      $prophecy->getSetting($key)->willReturn($val);
80
    }
81
82
    $prophecy->getAdditionalContext(Argument::any())->willReturn(NULL);
83
    $prophecy->addAdditionalContext(Argument::cetera())->will(function ($args, $prophecy) {
84
      $prophecy->getAdditionalContext($args[0])->willReturn($args[1]);
85
    });
86
87
    return $prophecy->reveal();
88
  }
89
90 15
  protected function createContextFactory(array $options = [], $return_prophecy = FALSE) {
91 15
    $prophecy_factory = $this;
92 15
    $prophecy = $this->prophesize(CommandContextFactoryInterface::CLASS);
0 ignored issues
show
Bug introduced by
The constant Drupal\paragraphs_editor...FactoryInterface::CLASS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
93
94
    $prophecy->get(Argument::any())->will(function ($args) use ($options, $prophecy_factory) {
95 4
      if (!empty($options['contexts'][$args[0]])) {
96
        return $prophecy_factory->createContext($options['contexts'][$args[0]]);
97
      }
98
      else {
99 4
        return $prophecy_factory->createContext();
100
      }
101 15
    });
102 15
    return $return_prophecy ? $prophecy : $prophecy->reveal();
103
  }
104
105
  protected function createEditBuffer(array $options = []) {
106
    $options += $this->editBufferDefaults();
107
    $prophecy = $this->prophesize(EditBufferInterface::CLASS);
0 ignored issues
show
Bug introduced by
The constant Drupal\paragraphs_editor...tBufferInterface::CLASS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
108
    $prophecy->getUser()->willReturn($options['uid']);
109
    $prophecy->getContextString()->willReturn($options['context_id']);
110
111
    $prophecy->getItem(Argument::any())->willReturn(NULL);
112
113
    $prophecy->setItem(Argument::any())->will(function ($args, $prophecy) {
114
      $prophecy->getItem($item->getEntity()->uuid())->willReturn($item->getEntity());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $item seems to be never defined.
Loading history...
115
    });
116
117
    $prophecy_factory = $this;
118
    $prophecy->createItem(Argument::any())->will(function ($args, $prophecy) use($prophecy_factory) {
119
      $item_prophecy = $prophecy_factory->prophesize(EditBufferItemInterface::CLASS);
0 ignored issues
show
Bug introduced by
The constant Drupal\paragraphs_editor...ferItemInterface::CLASS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
120
      $item_prophecy->getEntity()->willReturn($args[0]);
121
      $item_prophecy->overwrite(Argument::any())->will(function ($args, $item_prophecy) {
122
        $item_prophecy->getEntity()->willReturn($args[0]);
123
      });
124
      $item_prophecy->save()->willReturn(NULL);
125
      $item = $item_prophecy->reveal();
126
127
      $prophecy->getItem($args[0]->uuid())->willReturn($item);
128
129
      return $item;
130
    });
131
132
    $edit_buffer = $prophecy->reveal();
133
134
    if (!empty($options['default_items'])) {
135
      foreach ($options['default_items'] as $entity) {
136
        $edit_buffer->createItem($entity);
137
      }
138
    }
139
140
    return $edit_buffer;
141
  }
142
143 17
  protected function createMockParagraph(array $options = []) {
144 17
    $options += $this->mockParagraphDefaults();
145 17
    $prophecy = $this->prophesize(ParagraphInterface::CLASS);
0 ignored issues
show
Bug introduced by
The constant Drupal\paragraphs\ParagraphInterface::CLASS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
146 17
    $prophecy->id()->willReturn($options['id']);
147 17
    $prophecy->uuid()->willReturn($options['uuid']);
148 17
    $prophecy->bundle()->willReturn($options['bundle']);
149 17
    return $prophecy->reveal();
150
  }
151
}
152