GeneratedRestControllerEmbeddedAssociationTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 99
Duplicated Lines 21.21 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 6
c 2
b 1
f 1
lcom 1
cbo 4
dl 21
loc 99
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetWithAssociation() 0 10 1
A testPostWithNewAssociation() 0 22 1
A testPutWithDifferentAssociation() 0 21 1
A testPutWithNoAssocation() 11 11 1
A testPatchWithDifferentAssociation() 0 17 1
A testPatchWithNoAssocation() 10 10 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\Command\ODM;
3
4
use Test\TestBundle\Document\Order;
5
6
class GeneratedRestControllerEmbeddedAssociationTest extends BaseTestGeneratedRestController {
7
8
    public function testGetWithAssociation() {
9
        $filter = json_encode(array(
10
            array('property'=>'name','value'=>'Invoice 1')
11
        ));
12
        $this->client->request('GET', '/orders.json?filter='.$filter);
13
        $content = json_decode($this->client->getResponse()->getContent(), true);
14
        $this->assertArrayHasKey('lineItems', $content[0]);
15
        $this->assertInternalType('array', $content[0]['lineItems']);
16
        $this->assertEquals(1, count($content[0]['lineItems']));
17
    }
18
19
    public function testPostWithNewAssociation() {
20
        $this->client->request('POST', '/orders.json', array(), array(), array(), json_encode(array(
21
            'name'=>'Invoice New',
22
            'totalPrice'=>1.99,
23
            'lineItems' => array(
24
                array(
25
                    'productId' => 12,
26
                    'quantity' => 1,
27
                    'price' => 1.99,
28
                    'totalPrice' => 1.99
29
                )
30
            ),
31
        )));
32
        $this->assertEquals("201", $this->client->getResponse()->getStatusCode(), $this->client->getResponse()->getContent());
33
        $repo = $this->client->getContainer()->get('doctrine_mongodb.odm.document_manager')->getRepository('TestTestBundle:Order');
34
        $record = json_decode($this->client->getResponse()->getContent(), true);
35
        /** @var Order $order */
36
        $order = $repo->find($record['id']);
37
        $this->assertEquals('Invoice New', $order->getName());
38
        $this->assertEquals(1, $order->getLineItems()->count());
39
        $this->assertEquals(12, $order->getLineItems()->first()->getProductId());
40
    }
41
42
    public function testPutWithDifferentAssociation() {
43
        $record = $this->records[0];
44
        $this->client->request('PUT', '/orders/'.$record->getId().'.json', array(), array(), array(), json_encode(array(
45
            'name'=>'Invoice 1',
46
            'lineItems' => array(
47
                array(
48
                    'productId' => 12,
49
                    'quantity' => 1,
50
                    'price' => 1.99,
51
                    'totalPrice' => 1.99
52
                )
53
            )
54
        )));
55
        $this->assertEquals("200", $this->client->getResponse()->getStatusCode(), $this->client->getResponse()->getContent());
56
        $repo = $this->client->getContainer()->get('doctrine_mongodb.odm.document_manager')->getRepository('TestTestBundle:Order');
57
        /** @var Order $order */
58
        $order = $repo->find($record->getId());
59
        $this->assertNull($order->getTotalPrice());
60
        $this->assertEquals(1, $order->getLineItems()->count());
61
        $this->assertEquals(12, $order->getLineItems()->first()->getProductId());
62
    }
63
64 View Code Duplication
    public function testPutWithNoAssocation() {
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...
65
        $record = $this->records[0];
66
        $this->client->request('PUT', '/orders/'.$record->getId().'.json', array(), array(), array(), json_encode(array(
67
            'name'=>'Invoice 1'
68
        )));
69
        $repo = $this->client->getContainer()->get('doctrine_mongodb.odm.document_manager')->getRepository('TestTestBundle:Order');
70
        $record = json_decode($this->client->getResponse()->getContent(), true);
71
        /** @var Order $order */
72
        $order = $repo->find($record['id']);
73
        $this->assertEquals(0, $order->getLineItems()->count());
74
    }
75
76
    public function testPatchWithDifferentAssociation() {
77
        $this->client->request('PATCH', '/orders/'.$this->records[0]->getId().'.json', array(), array(), array(), json_encode(array(
78
            'lineItems' => array(
79
                array(
80
                    'productId' => 12,
81
                    'quantity' => 1,
82
                    'price' => 1.99,
83
                    'totalPrice' => 1.99
84
                )
85
            ),
86
        )));
87
        $this->assertEquals("200", $this->client->getResponse()->getStatusCode());
88
        $repo = $this->client->getContainer()->get('doctrine_mongodb.odm.document_manager')->getRepository('TestTestBundle:Order');
89
        /** @var Order $order */
90
        $order = $repo->find($this->records[0]->getId());
91
        $this->assertEquals(2, $order->getLineItems()->count());
92
    }
93
94 View Code Duplication
    public function testPatchWithNoAssocation() {
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...
95
        $this->client->request('PATCH', '/orders/'.$this->records[0]->getId().'.json', array(), array(), array(), json_encode(array(
96
            'lineItems'=>array()
97
        )));
98
        $this->assertEquals("200", $this->client->getResponse()->getStatusCode());
99
        $repo = $this->client->getContainer()->get('doctrine_mongodb.odm.document_manager')->getRepository('TestTestBundle:Order');
100
        /** @var Order $order */
101
        $order = $repo->find($this->records[0]->getId());
102
        $this->assertEquals(1, $order->getLineItems()->count());
103
    }
104
}