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

GenericContentEntityTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testEntityTestCreateDelete() 0 21 1
A testEntityTestCreateDeleteByWrapper() 0 21 1
1
<?php
2
3
namespace Drupal\Tests\Driver\Kernel\Drupal8\Entity;
4
5
use Drupal\Driver\Wrapper\Entity\DriverEntityDrupal8;
6
7
/**
8
 * Tests the driver's handling of generic content entities using 'entity_test'.
9
 *
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
   * Machine name of the entity type being tested.
19
   *
20
   * @var string
21
   */
22
  protected $entityType = 'entity_test';
23
24
  /**
25
   * Test that an entity_test can be created and deleted.
26
   */
27
  public function testEntityTestCreateDelete() {
28
    $name = $this->randomString();
29
    $entity_test = (object) [
30
      'name' => $name,
31
    ];
32
    $entity_test = $this->driver->createEntity('entity_test', $entity_test);
33
34
    // The test driverfield plugin has been matched,  which mutates the text.
35
    $processedName = 'now' . $name . 'processed';
36
    $entities = $this->storage->loadByProperties(['name' => $processedName]);
37
    $this->assertEquals(1, count($entities));
38
39
    // Check the id of the new entity has been added to the returned object.
40
    $entity = reset($entities);
41
    $this->assertEquals($entity->id(), $entity_test->id);
42
43
    // Check the entity can be deleted.
44
    $this->driver->entityDelete('entity_test', $entity_test);
45
    $entities = $this->storage->loadByProperties(['name' => $processedName]);
46
    $this->assertEquals(0, count($entities));
47
  }
48
49
  /**
50
   * Test that an entity_test can be created and deleted.
51
   */
52
  public function testEntityTestCreateDeleteByWrapper() {
53
    $name = $this->randomString();
54
    $fields = [
55
      'name' => [$name],
56
    ];
57
    $entity_test = DriverEntityDrupal8::create($fields, $this->entityType)->save();
58
59
    // The test driverfield plugin has been matched,  which mutates the text.
60
    $processedName = 'now' . $name . 'processed';
61
    $entities = $this->storage->loadByProperties(['name' => $processedName]);
62
    $this->assertEquals(1, count($entities));
63
64
    // Check the id of the new entity has been added to the returned object.
65
    $entity = reset($entities);
66
    $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...
67
68
    // Check the entity can be deleted.
69
    $entity_test->delete();
70
    $entities = $this->storage->loadByProperties(['name' => $name]);
71
    $this->assertEquals(0, count($entities));
72
  }
73
74
}
75