Completed
Pull Request — master (#157)
by
unknown
01:41
created

CommentTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 100
Duplicated Lines 51 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 51
loc 100
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 18 1
B testCommentCreateDelete() 25 25 1
B testCommentCreateDeleteByWrapper() 26 26 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Drupal\Tests\Driver\Kernel\Drupal8\Entity;
4
5
use Drupal\Driver\Wrapper\Entity\DriverEntityDrupal8;
6
use Drupal\Tests\Driver\Kernel\Drupal8\Entity\DriverEntityKernelTestBase;
7
use Drupal\comment\Entity\CommentType;
8
use Drupal\comment\Tests\CommentTestTrait;
9
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
10
11
/**
12
 * Tests the driver's handling of comment entities.
13
 * They have the peculiarity of having 'entity_type' as a field name.
14
 *
15
 * @group Driver
16
 */
17
class CommentTest extends DriverEntityKernelTestBase
18
{
19
20
    use CommentTestTrait;
21
22
  /**
23
   * Modules to install.
24
   *
25
   * @var array
26
   */
27
    public static $modules = ['comment', 'user'];
28
29
  /**
30
   * Machine name of the entity type being tested.
31
   *
32
   * @string
33
   */
34
    protected $entityType = 'comment';
35
36
  /**
37
   * {@inheritdoc}
38
   */
39
    protected function setUp()
40
    {
41
        parent::setUp();
42
        $this->installConfig(['user', 'comment']);
43
        $this->installSchema('comment', ['comment_entity_statistics']);
44
45
        // Create a comment type.
46
        $comment_type = CommentType::create([
47
        'id' => 'testcomment',
48
        'label' => 'Default comments',
49
        'description' => 'Default comment field',
50
        'target_entity_type_id' => 'user',
51
        ]);
52
        $comment_type->save();
53
54
        // Add a comment field to the user entity.
55
        $this->addDefaultCommentField('user', 'user', 'comment', $default_value = CommentItemInterface::OPEN, $comment_type_id = 'testcomment');
56
    }
57
58
  /**
59
   * Test that a comment can be created and deleted.
60
   */
61 View Code Duplication
    public function testCommentCreateDelete()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        // Create a comment on a test user.
64
        $user = $this->createUser();
65
        $subject = $this->randomString();
66
        $comment = (object) [
67
        'subject' => $subject,
68
        'entity_type' => 'user',
69
        'entity_id' => $user->getUsername(),
70
        'step_bundle' => 'testcomment'
71
        ];
72
        $comment = $this->driver->createEntity('comment', $comment);
73
74
        $entities = $this->storage->loadByProperties(['subject' => $subject]);
75
        $this->assertEquals(1, count($entities));
76
77
        // Check the id of the new comment has been added to the returned object.
78
        $entity = reset($entities);
79
        $this->assertEquals($entity->id(), $comment->id);
80
81
        // Check the comment can be deleted.
82
        $this->driver->entityDelete('comment', $comment);
83
        $entities = $this->storage->loadByProperties(['subject' => $subject]);
84
        $this->assertEquals(0, count($entities));
85
    }
86
87
  /**
88
   * Test that a comment can be created and deleted.
89
   */
90 View Code Duplication
    public function testCommentCreateDeleteByWrapper()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        // Create a comment on a test user.
93
        $user = $this->createUser();
94
        $subject = $this->randomString();
95
96
        $fields = [
97
        'subject' => $subject,
98
        'entity_type' => 'user',
99
        'entity_id' => $user->getUsername(),
100
        'comment_type' => 'testcomment'
101
        ];
102
        $comment = DriverEntityDrupal8::create($fields, $this->entityType)->save();
103
104
        $entities = $this->storage->loadByProperties(['subject' => $subject]);
105
        $this->assertEquals(1, count($entities));
106
107
        // Check the id of the new comment has been added to the returned object.
108
        $entity = reset($entities);
109
        $this->assertEquals($entity->id(), $comment->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Drupal\Driver\Wra...ntity\DriverEntityBase>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
110
111
        // Check the comment can be deleted.
112
        $comment->delete();
113
        $entities = $this->storage->loadByProperties(['subject' => $subject]);
114
        $this->assertEquals(0, count($entities));
115
    }
116
}
117