Completed
Pull Request — 2.x (#176)
by
unknown
13:20
created

TranslatableAdminExtensionTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 19.44 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 4
dl 14
loc 72
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 16 1
A shouldCreateClient() 0 4 1
A testSetLocaleForTranslatableObject() 0 6 1
A testAlertObjectForTranslatableObject() 0 6 1
A testPreUpdate() 7 7 1
A testPrePersist() 7 7 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 Sonata\TranslationBundle\Tests\AdminExtension\Knplabs;
4
5
use Sonata\AdminBundle\Admin\AdminInterface;
6
use Sonata\TranslationBundle\Admin\Extension\Knplabs\TranslatableAdminExtension;
7
use Sonata\TranslationBundle\Checker\TranslatableChecker;
8
use Sonata\TranslationBundle\Tests\Fixtures\Model\Knplabs\TranslatableEntity;
9
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
10
use Symfony\Component\HttpFoundation\Request;
11
12
/**
13
 * @group  translatable-knplabs
14
 */
15
class TranslatableAdminExtensionTest extends WebTestCase
16
{
17
    /**
18
     * @var AdminInterface
19
     */
20
    protected $admin;
21
22
    /**
23
     * @var TranslatableEntity
24
     */
25
    protected $object;
26
27
    /**
28
     * @var TranslatableAdminExtension
29
     */
30
    protected $extension;
31
32
    protected function setUp()
33
    {
34
        $translatableChecker = new TranslatableChecker();
35
        $translatableChecker->setSupportedInterfaces(array(
36
            'Sonata\TranslationBundle\Model\TranslatableInterface',
37
        ));
38
        $this->extension = new TranslatableAdminExtension($translatableChecker);
39
40
        $request = $this->prophesize(Request::class);
41
        $request->get('tl')->willReturn('es');
42
43
        $this->admin = $this->prophesize(AdminInterface::class);
44
        $this->admin->getRequest()->willReturn($request->reveal());
45
46
        $this->object = new TranslatableEntity();
47
    }
48
49
    /**
50
     *
51
     */
52
    public function shouldCreateClient()
53
    {
54
        $client = static::createClient();
0 ignored issues
show
Unused Code introduced by
$client is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
    }
56
57
    public function testSetLocaleForTranslatableObject()
58
    {
59
        $this->extension->alterNewInstance($this->admin->reveal(), $this->object);
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
61
        $this->assertEquals('es', $this->object->getLocale());
62
    }
63
64
    public function testAlertObjectForTranslatableObject()
65
    {
66
        $this->extension->alterObject($this->admin->reveal(), $this->object);
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
68
        $this->assertEquals('es', $this->object->getLocale());
69
    }
70
71 View Code Duplication
    public function testPreUpdate()
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...
72
    {
73
        $object = $this->prophesize(TranslatableEntity::class);
74
        $object->mergeNewTranslations()->shouldBeCalled();
75
76
        $this->extension->preUpdate($this->admin->reveal(), $object->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
    }
78
79 View Code Duplication
    public function testPrePersist()
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...
80
    {
81
        $object = $this->prophesize(TranslatableEntity::class);
82
        $object->mergeNewTranslations()->shouldBeCalled();
83
84
        $this->extension->prePersist($this->admin->reveal(), $object->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
    }
86
}
87