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

PostsControllerTest::testPostListResponseFields()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 26

Duplication

Lines 26
Ratio 100 %

Importance

Changes 0
Metric Value
dl 26
loc 26
rs 9.504
c 0
b 0
f 0
cc 3
nc 4
nop 0
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