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

testLanguageDefaultLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace Drupal\Tests\Driver\Kernel\Drupal8\Entity;
4
5
use Drupal\language\Entity\ConfigurableLanguage;
6
use Drupal\Driver\Wrapper\Entity\DriverEntityDrupal8;
7
8
/**
9
 * Tests the driver's handling of language entities.
10
 *
11
 * @group driver
12
 */
13
class ConfigurableLanguageTest extends DriverEntityKernelTestBase
14
{
15
16
  /**
17
   * {@inheritdoc}
18
   */
19
    public static $modules = ['language',];
20
21
  /**
22
   * Machine name of the entity type being tested.
23
   *
24
   * @string
25
   */
26
    protected $entityType = 'configurable_language';
27
28
  /**
29
   * Our entity is a config entity.
30
   *
31
   * @boolean
32
   */
33
    protected $config = true;
34
35
  /**
36
   * Test that a language can be created and deleted.
37
   */
38
    public function testLanguageCreateDelete()
39
    {
40
        $langcode = 'de';
41
        $language = (object) [
42
        'langcode' => $langcode,
43
        ];
44
        $languageReturn = $this->driver->languageCreate($language);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Drupal\Driver\DriverInterface as the method languageCreate() does only exist in the following implementations of said interface: Drupal\Driver\DrupalDriver.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
45
46
        // Test return object has langcode property.
47
        $this->assertEquals($langcode, $languageReturn->langcode);
48
49
        // Test language is created.
50
        $loadedLanguage = \Drupal::languageManager()->getLanguage($langcode);
51
        $this->assertNotNull($loadedLanguage);
52
53
        // Test false is returned if language already exists.
54
        $languageReturn = $this->driver->languageCreate($language);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Drupal\Driver\DriverInterface as the method languageCreate() does only exist in the following implementations of said interface: Drupal\Driver\DrupalDriver.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
55
        $this->assertFalse($languageReturn);
56
57
        // Check the language can be deleted.
58
        $this->driver->languageDelete($language);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Drupal\Driver\DriverInterface as the method languageDelete() does only exist in the following implementations of said interface: Drupal\Driver\DrupalDriver.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
59
        $loadedLanguage = ConfigurableLanguage::load($langcode);
60
        $this->assertNull($loadedLanguage);
61
    }
62
63
  /**
64
   * Test that a language can be created and deleted.
65
   */
66 View Code Duplication
    public function testLanguageCreateDeleteByWrapper()
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...
67
    {
68
        $langcode = $this->randomMachineName();
69
        $label = $this->randomMachineName();
70
        $entity = new DriverEntityDrupal8(
71
            $this->entityType
72
        );
73
        $entity->set('id', $langcode);
74
        $entity->set('label', $label);
75
        $entity->save();
76
77
        $language = ConfigurableLanguage::load($langcode);
78
        $this->assertNotNull($language);
79
        $this->assertEquals($langcode, $language->get('id'));
80
81
        // Check the language can be deleted.
82
        $entity->delete();
83
        $language = ConfigurableLanguage::load($langcode);
84
        $this->assertNull($language);
85
    }
86
87
    /**
88
     * Test that a default label is provided based on langcode.
89
     */
90
    public function testLanguageDefaultLabel()
91
    {
92
      $langcode = 'hu';
93
      $label = 'Hungarian';
94
      $entity = new DriverEntityDrupal8(
95
        $this->entityType
96
      );
97
      $entity->set('id', $langcode);
98
      $entity->save();
99
100
      $language = ConfigurableLanguage::load($langcode);
101
      $this->assertNotNull($language);
102
      $this->assertEquals($label, $language->get('label'));
103
104
    }
105
}
106