Cancelled
Pull Request — master (#19)
by Wachter
05:00 queued 21s
created

ThreadControllerTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 179
Duplicated Lines 32.96 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
lcom 1
cbo 7
dl 59
loc 179
rs 10
c 1
b 0
f 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
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Functional\Controller;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\ORM\EntityManagerInterface;
16
use Sulu\Bundle\CommentBundle\Entity\Thread;
17
use Sulu\Bundle\CommentBundle\Entity\ThreadInterface;
18
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
19
20
class ThreadControllerTest extends SuluTestCase
21
{
22
    /**
23
     * @var EntityManagerInterface
24
     */
25
    private $entityManager;
26
27
    protected function setUp()
28
    {
29
        $this->entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
30
31
        $this->purgeDatabase();
32
    }
33
34
    public function testGet()
35
    {
36
        $thread = $this->createThread();
37
        $this->entityManager->flush();
38
        $this->entityManager->clear();
39
40
        $client = $this->createAuthenticatedClient();
41
        $client->request('GET', '/api/threads/' . $thread->getId());
42
43
        $this->assertHttpStatusCode(200, $client->getResponse());
44
        $data = json_decode($client->getResponse()->getContent(), true);
45
46
        $this->assertEquals($thread->getId(), $data['id']);
47
        $this->assertEquals($thread->getTitle(), $data['title']);
48
    }
49
50
    public function testCGetFilter()
51
    {
52
        $this->createThread('Test 1', 'page', 'Test 1');
53
        $this->createThread('Test 2', 'page', 'Test 2');
54
        $this->createThread('Test 3', 'article', 'Test 3');
55
56
        $this->entityManager->flush();
57
        $this->entityManager->clear();
58
59
        $client = $this->createAuthenticatedClient();
60
        $client->request('GET', '/api/threads?type=page');
61
62
        $this->assertHttpStatusCode(200, $client->getResponse());
63
        $data = json_decode($client->getResponse()->getContent(), true);
64
65
        $this->assertCount(2, $data['_embedded']['threads']);
66
    }
67
68
    public function testPut()
69
    {
70
        $thread = $this->createThread();
71
        $this->entityManager->flush();
72
        $this->entityManager->clear();
73
74
        $client = $this->createAuthenticatedClient();
75
        $client->request('PUT', '/api/threads/' . $thread->getId(), ['title' => 'My new Title']);
76
77
        $this->assertHttpStatusCode(200, $client->getResponse());
78
        $data = json_decode($client->getResponse()->getContent(), true);
79
80
        $this->assertEquals($thread->getId(), $data['id']);
81
        $this->assertEquals('My new Title', $data['title']);
82
    }
83
84
    public function testDelete()
85
    {
86
        $thread = $this->createThread();
87
        $this->entityManager->flush();
88
        $this->entityManager->clear();
89
90
        $client = $this->createAuthenticatedClient();
91
        $client->request('DELETE', '/api/threads/' . $thread->getId());
92
93
        $this->assertHttpStatusCode(204, $client->getResponse());
94
95
        $this->assertNull($this->entityManager->find(ThreadInterface::class, $thread->getId()));
96
    }
97
98
    public function testCDelete()
99
    {
100
        $threads = [
101
            $this->createThread('Test 1', 'Test', 1),
102
            $this->createThread('Test 1', 'Test', 2),
103
            $this->createThread('Test 1', 'Test', 3),
104
        ];
105
        $this->entityManager->flush();
106
        $this->entityManager->clear();
107
108
        $client = $this->createAuthenticatedClient();
109
        $client->request(
110
            'DELETE',
111
            '/api/threads?ids=' . implode(
112
                ',',
113
                array_map(
114
                    function (ThreadInterface $thread) {
115
                        return $thread->getId();
116
                    },
117
                    [$threads[0], $threads[1]]
118
                )
119
            )
120
        );
121
122
        $this->assertHttpStatusCode(204, $client->getResponse());
123
124
        foreach ([$threads[0], $threads[1]] as $comment) {
125
            $this->assertNull($this->entityManager->find(ThreadInterface::class, $comment->getId()));
126
        }
127
128
        $this->assertNotNull($this->entityManager->find(ThreadInterface::class, $threads[2]->getId()));
129
    }
130
131
    public function testCGet()
132
    {
133
        /** @var Thread[] $threads */
134
        $threads = [
135
            $this->createThread('Test 1', 'Test1', 1),
136
            $this->createThread('Test 2', 'Test2', 2),
137
            $this->createThread('Test 3', 'Test3', 3),
138
        ];
139
        $this->entityManager->flush();
140
        $this->entityManager->clear();
141
142
        $client = $this->createAuthenticatedClient();
143
        $client->request('GET', '/api/threads?fields=id,title');
144
145
        $this->assertHttpStatusCode(200, $client->getResponse());
146
147
        $result = json_decode($client->getResponse()->getContent(), true);
148
        $result = $result['_embedded']['threads'];
149
        for ($i = 0, $length = count($threads); $i < $length; ++$i) {
150
            $this->assertEquals($threads[$i]->getId(), $result[$i]['id']);
151
            $this->assertEquals($threads[$i]->getTitle(), $result[$i]['title']);
152
        }
153
    }
154
155
    public function testCGetTypes()
156
    {
157
        /** @var Thread[] $threads */
158
        $threads = [
159
            $this->createThread('Test 1', 'Test1', 1),
160
            $this->createThread('Test 2', 'Test2', 2),
161
            $this->createThread('Test 3', 'Test3', 3),
162
        ];
163
        $this->entityManager->flush();
164
        $this->entityManager->clear();
165
166
        $client = $this->createAuthenticatedClient();
167
        $client->request('GET', '/api/threads?fields=id,title&types=Test1,Test3');
168
169
        $this->assertHttpStatusCode(200, $client->getResponse());
170
171
        $result = json_decode($client->getResponse()->getContent(), true);
172
        $result = $result['_embedded']['threads'];
173
174
        $expected = [$threads[0], $threads[2]];
175
        for ($i = 0, $length = count($expected); $i < $length; ++$i) {
176
            $this->assertEquals($expected[$i]->getId(), $result[$i]['id']);
177
            $this->assertEquals($expected[$i]->getTitle(), $result[$i]['title']);
178
        }
179
    }
180
181
    /**
182
     * Create and persists new thread.
183
     *
184
     * @param string $title
185
     * @param string $type
186
     * @param string $entityId
187
     *
188
     * @return Thread
189
     */
190
    private function createThread($title = 'Sulu is awesome', $type = 'Test', $entityId = '123-123-123')
191
    {
192
        $thread = new Thread($type, $entityId, new ArrayCollection());
193
        $thread->setTitle($title);
194
        $this->entityManager->persist($thread);
195
196
        return $thread;
197
    }
198
}
199