Completed
Push — master ( a80802...7141a9 )
by Serhii
13s queued 10s
created

PostsControllerTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 26 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 26
loc 100
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetPosts() 0 4 1
A testGetPostsSlug() 0 6 1
A testPostListResponseFields() 26 26 3
A testPinnedPost() 0 28 1
A getListFields() 0 10 1
A getEntityFields() 0 18 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 App\Tests\Functional\Controller;
4
5
class PostsControllerTest extends AbstractController
6
{
7
    public function testGetPosts()
8
    {
9
        $this->restRequest('/api/posts');
10
    }
11
12
    public function testGetPostsSlug()
13
    {
14
        $slug = $this->getEm()->getRepository('App:Post')->findOneBy([])->getSlug();
15
        $this->restRequest('/api/posts/'.$slug);
16
        $this->restRequest('/api/posts/nonexistent-slug', 'GET', 404);
17
    }
18
19 View Code Duplication
    public function testPostListResponseFields()
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...
20
    {
21
        $this->restRequest('/api/posts');
22
23
        $response = json_decode($this->getSessionClient()->getResponse()->getContent(), true);
24
25
        $this->assertEquals(
26
            count($this->getListFields()),
27
            count(array_keys($response))
28
        );
29
30
        foreach ($this->getListFields() as $field) {
31
            $this->assertArrayHasKey($field, $response);
32
        }
33
34
        $firstEntity = array_shift($response['posts']);
35
36
        $this->assertEquals(
37
            count($this->getEntityFields()),
38
            count(array_keys($firstEntity))
39
        );
40
41
        foreach ($this->getEntityFields() as $field) {
42
            $this->assertArrayHasKey($field, $firstEntity);
43
        }
44
    }
45
46
    public function testPinnedPost()
47
    {
48
        $client =  $this->getSessionClient();
49
50
        $this->restRequest('/api/posts');
51
        $result = $client->getResponse()->getContent();
52
        $result = json_decode($result);
53
54
        $firstPostSlug = $result->posts[0]->slug;
55
        $secondPostSlug = $result->posts[1]->slug;
56
57
        $em = $this->getEm();
58
        $secondPost = $em->getRepository('App:Post')->findOneBy(['slug' => $secondPostSlug]);
59
        $secondPost->setPinned(true);
60
        $em->flush($secondPost);
61
62
        $this->restRequest('/api/posts');
63
        $result = $client->getResponse()->getContent();
64
        $result = json_decode($result);
65
66
        $this->assertEquals($secondPostSlug, $result->posts[0]->slug);
67
        $this->assertEquals($firstPostSlug, $result->posts[1]->slug);
68
69
        //cleenup
70
        $secondPost = $em->getRepository('App:Post')->findOneBy(['slug' => $secondPostSlug]);
71
        $secondPost->setPinned(false);
72
        $em->flush($secondPost);
73
    }
74
75
    private function getListFields()
76
    {
77
        return array (
78
            '_links',
79
            'page',
80
            'total_count',
81
            'posts',
82
            'count',
83
        );
84
    }
85
86
    private function getEntityFields()
87
    {
88
        return array (
89
            'locale',
90
            'title',
91
            'short_description',
92
            'text',
93
            'main_picture',
94
            'mainPicture',
95
            'slug',
96
            'created_at',
97
            'updated_at',
98
            'created_by',
99
            'updated_by',
100
            'tags',
101
            'pinned',
102
        );
103
    }
104
}
105