GeneratorServiceTest   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 118
Duplicated Lines 17.8 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 13
Bugs 2 Features 7
Metric Value
wmc 16
c 13
b 2
f 7
lcom 1
cbo 4
dl 21
loc 118
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A testCustomFieldParameters() 0 10 3
A testEntityProperty() 12 12 2
A testEntityPropertyType() 9 9 2
A testEntityPropertyValidation() 0 12 2
B testEntityAssociation() 0 22 4
A testEntityProxy() 0 5 1
A testGenerateRemotingApi() 0 22 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
namespace Tpg\ExtjsBundle\Tests\Service;
3
4
use Doctrine\Common\Annotations\AnnotationReader;
5
use Test\TestBundle\Mockup\TwigEngineMokcup;
6
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
7
use Tpg\ExtjsBundle\Service\GeneratorService;
8
9
class GeneratorServiceTest extends TestCase {
10
11
    /** @var GeneratorService */
12
    protected $service;
13
14
    /** @var TwigEngineMokcup */
15
    protected $twigEngine;
16
17
    protected function setUp() {
18
        parent::setUp();
19
        $this->service = new GeneratorService();
20
        $this->service->setAnnotationReader(new AnnotationReader());
21
        $this->twigEngine = new TwigEngineMokcup();
22
        $this->service->setTwigEngine($this->twigEngine);
23
        $this->service->setModelFieldsParameters(array(
24
          "date" => array( "format" => "d-m-y")
25
        ));
26
    }
27
28
    public function testCustomFieldParameters() {
29
      $this->service->generateMarkupForEntity('Test\TestBundle\Model\Person');
30
      $fieldsType = array();
31
      foreach ($this->twigEngine->renderParameters['fields'] as $field) {
32
        if (isset($field['format'])) {
33
          $fieldsType[$field['name']] = $field['format'];
34
        }
35
      }
36
      $this->assertEquals("d-m-y", $fieldsType['createdAt']);
37
    }
38
39 View Code Duplication
    public function testEntityProperty() {
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...
40
        $this->service->generateMarkupForEntity('Test\TestBundle\Model\Person');
41
        $this->assertContains("Test.model.Person", $this->twigEngine->renderParameters['name']);
42
        $fieldsName = array();
43
        foreach ($this->twigEngine->renderParameters['fields'] as $field) {
44
            $fieldsName[] = $field['name'];
45
        }
46
        $this->assertContains("id", $fieldsName);
47
        $this->assertContains("firstName", $fieldsName);
48
        $this->assertContains("lastName", $fieldsName);
49
        $this->assertNotContains("dob", $fieldsName);
50
    }
51
52 View Code Duplication
    public function testEntityPropertyType() {
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...
53
        $this->service->generateMarkupForEntity('Test\TestBundle\Model\Person');
54
        $fieldsType = array();
55
        foreach ($this->twigEngine->renderParameters['fields'] as $field) {
56
            $fieldsType[$field['name']] = $field['type'];
57
        }
58
        $this->assertEquals("int", $fieldsType['id']);
59
        $this->assertEquals("string", $fieldsType['firstName']);
60
    }
61
62
    public function testEntityPropertyValidation() {
63
        $this->service->generateMarkupForEntity('Test\TestBundle\Model\Person');
64
        $fields = array();
65
        foreach ($this->twigEngine->renderParameters['validators'] as $validator) {
66
            $fields[$validator['field']][] = $validator['type'];
67
        }
68
        $this->assertContains("presence", $fields['firstName']);
69
        $this->assertContains("presence", $fields['lastName']);
70
        $this->assertContains("email", $fields['email']);
71
        $this->assertContains("length", $fields['email']);
72
        $this->assertContains("length", $fields['email']);
73
    }
74
75
    public function testEntityAssociation() {
76
        $this->service->generateMarkupForEntity('Test\TestBundle\Model\Person');
77
        $associations = array();
78
        foreach ($this->twigEngine->renderParameters['associations'] as $assoc) {
79
            $associations[$assoc['name']] = $assoc;
80
        }
81
        $this->assertEquals('Test.model.Book', $associations['books']['model']);
82
        $this->assertEquals('books', $associations['books']['name']);
83
        $this->assertEquals('OneToMany', $associations['books']['type']);
84
        $this->service->generateMarkupForEntity('Test\TestBundle\Model\Book');
85
        $associations = array();
86
        foreach ($this->twigEngine->renderParameters['associations'] as $assoc) {
87
            $associations[$assoc['name']] = $assoc;
88
        }
89
        $this->assertEquals('Test.model.Person', $associations['person']['model']);
90
        $this->assertEquals('person', $associations['person']['name']);
91
        $this->assertEquals('ManyToOne', $associations['person']['type']);
92
        $fieldsName = array();
93
        foreach ($this->twigEngine->renderParameters['fields'] as $field) {
94
            $fieldsName[] = $field['name'];
95
        }
96
    }
97
98
    public function testEntityProxy() {
99
        $this->service->generateMarkupForEntity('Test\TestBundle\Model\Person');
100
        $parameters = $this->twigEngine->renderParameters;
101
        $this->assertNotNull($parameters['proxy']);
102
    }
103
104
    public function testGenerateRemotingApi() {
105
        $this->service->setRemotingBundles(array('TestBundle'=>'Test\TestBundle\TestTestBundle'));
106
        $api = $this->service->generateRemotingApi();
107
        $this->assertSame(array(
108
            'Test.TestBundle'=>array(
109
                'Test'=>array(
110
                    array(
111
                        'name'=>'test',
112
                        'len'=>0,
113
                    ),
114
                    array(
115
                        'name'=>'test2',
116
                        'len'=>1,
117
                    ),
118
                    array(
119
                        'name'=>'testRequestParam',
120
                        'len'=>1,
121
                    )
122
                ),
123
            ),
124
        ), $api);
125
    }
126
}