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

GenericContentEntityTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 3
dl 0
loc 62
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testEntityTestCreateDelete() 0 22 1
A testEntityTestCreateDeleteByWrapper() 0 22 1
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
8
/**
9
 * Tests the driver's handling of generic content entities using 'entity_test'.
10
 * We provide no specific handling for this entity type, so this tests the
11
 * fallback handling for generic content entities.
12
 *
13
 * @group Driver
14
 */
15
class GenericContentEntityTest extends DriverEntityKernelTestBase
16
{
17
18
  /**
19
   * Machine name of the entity type being tested.
20
   *
21
   * @string
22
   */
23
    protected $entityType = 'entity_test';
24
25
  /**
26
   * Test that an entity_test can be created and deleted.
27
   */
28
    public function testEntityTestCreateDelete()
29
    {
30
        $name = $this->randomString();
31
        $entity_test = (object) [
32
        'name' => $name,
33
        ];
34
        $entity_test = $this->driver->createEntity('entity_test', $entity_test);
35
36
        // The test driverfield plugin has been matched,  which mutates the text.
37
        $processedName = 'now' . $name . 'processed';
38
        $entities = $this->storage->loadByProperties(['name' => $processedName]);
39
        $this->assertEquals(1, count($entities));
40
41
        // Check the id of the new entity has been added to the returned object.
42
        $entity = reset($entities);
43
        $this->assertEquals($entity->id(), $entity_test->id);
44
45
        // Check the entity can be deleted.
46
        $this->driver->entityDelete('entity_test', $entity_test);
47
        $entities = $this->storage->loadByProperties(['name' => $processedName]);
48
        $this->assertEquals(0, count($entities));
49
    }
50
51
  /**
52
   * Test that an entity_test can be created and deleted.
53
   */
54
    public function testEntityTestCreateDeleteByWrapper()
55
    {
56
        $name = $this->randomString();
57
        $fields = [
58
        'name' => [$name],
59
        ];
60
        $entity_test = DriverEntityDrupal8::create($fields, $this->entityType)->save();
61
62
        // The test driverfield plugin has been matched,  which mutates the text.
63
        $processedName = 'now' . $name . 'processed';
64
        $entities = $this->storage->loadByProperties(['name' => $processedName]);
65
        $this->assertEquals(1, count($entities));
66
67
        // Check the id of the new entity has been added to the returned object.
68
        $entity = reset($entities);
69
        $this->assertEquals($entity->id(), $entity_test->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...
70
71
        // Check the entity can be deleted.
72
        $entity_test->delete();
73
        $entities = $this->storage->loadByProperties(['name' => $name]);
74
        $this->assertEquals(0, count($entities));
75
    }
76
}
77