CommentsControllerTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 10
Bugs 6 Features 1
Metric Value
wmc 9
c 10
b 6
f 1
lcom 1
cbo 4
dl 0
loc 185
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 20 1
A testCountCommentHtmlList() 0 6 1
A testRemoveAssociateComment() 0 9 1
A testAddComment() 0 21 1
A testAddCommentWithEmptyName() 0 19 1
A testAddCommentWithWrongEmailFormat() 0 19 1
B testAddCommmentWithAChild() 0 39 1
B testPreDeleteComment() 0 24 1
A testWithCommentDepthToZero() 0 5 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Rafidion Michael
5
 * Date: 23/04/2015
6
 * Time: 02:28
7
 */
8
9
namespace Mykees\CommentBundle\Tests\Controller;
10
11
12
use Liip\FunctionalTestBundle\Test\WebTestCase;
13
14
class CommentsControllerTest extends WebTestCase{
15
16
    protected $client;
17
    protected $container;
18
	protected $manager;
19
	protected $query_manager;
20
    protected $em;
21
    protected $commentClass;
22
    protected $request;
23
24
   public function setUp()
25
   {
26
27
28
       $this->client = static::createClient();
29
       $this->container = $this->client->getContainer();
30
	   $this->manager = $this->container->get('mykees.comment.manager');
31
	   $this->query_manager = $this->container->get('mykees.comment.query.manager');
32
       $this->em = static::$kernel->getContainer()
33
           ->get('doctrine')
34
           ->getManager()
35
       ;
36
37
       $fixtures = [
38
           'Mykees\CommentBundle\DataFixtures\ORM\LoadCommentData',
39
           'Mvc\BlogBundle\DataFixtures\ORM\LoadPostsData',
40
       ];
41
       $this->loadFixtures($fixtures);
42
       parent::setUp();
43
   }
44
45
    public function testCountCommentHtmlList()
46
    {
47
        $crawler = $this->client->request('GET', '/blog/title-1');
48
        $this->assertEquals(200,$this->client->getResponse()->getStatusCode());
49
        $this->assertEquals(3,$crawler->filter('.comment-list')->count());
50
    }
51
52
	public function testRemoveAssociateComment()
53
	{
54
		$this->client->request('GET', '/admin/delete/5');
55
		$this->assertEquals(302,$this->client->getResponse()->getStatusCode());
56
57
		$count = count($this->query_manager->findAllComments());
58
59
		$this->assertEquals(4, $count);
60
	}
61
62
	public function testAddComment()
63
	{
64
		$crawler = $this->client->request('GET','/blog/title-1');
65
		$this->assertEquals('Mvc\BlogBundle\Controller\BlogController::showAction',  $this->client->getRequest()->attributes->get('_controller'));
0 ignored issues
show
Bug introduced by
The property attributes does not seem to exist in Symfony\Component\BrowserKit\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
66
		$form = $crawler->selectButton('Poster')->form([
67
			'mykees_comment[username]'=>'Mykees',
68
			'mykees_comment[email]'=>'[email protected]',
69
			'mykees_comment[content]'=>'Salut les guedins',
70
			'mykees_comment[parentId]'=>0,
71
			'mykees_comment[model]'=>'Post',
72
			'mykees_comment[modelId]'=>1,
73
		]);
74
75
		$this->client->submit($form);
76
77
		$this->assertEquals(302,$this->client->getResponse()->getStatusCode());
78
79
		$count = count($this->query_manager->findAllComments());
80
81
		$this->assertEquals(6, $count);
82
	}
83
84
	public function testAddCommentWithEmptyName()
85
	{
86
		$crawler = $this->client->request('GET','/blog/title-1');
87
		$this->assertEquals('Mvc\BlogBundle\Controller\BlogController::showAction',  $this->client->getRequest()->attributes->get('_controller'));
0 ignored issues
show
Bug introduced by
The property attributes does not seem to exist in Symfony\Component\BrowserKit\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
88
		$form = $crawler->selectButton('Poster')->form([
89
			'mykees_comment[username]'=>'',
90
			'mykees_comment[email]'=>'[email protected]',
91
			'mykees_comment[content]'=>'Salut les guedins',
92
			'mykees_comment[parentId]'=>0,
93
			'mykees_comment[model]'=>'Post',
94
			'mykees_comment[modelId]'=>1,
95
		]);
96
97
		$this->client->submit($form);
98
99
		$count = count($this->query_manager->findAllComments());
100
101
		$this->assertEquals(5, $count);
102
	}
103
104
	public function testAddCommentWithWrongEmailFormat()
105
	{
106
107
		$crawler = $this->client->request('GET','/blog/title-1');
108
		$form = $crawler->selectButton('Poster')->form([
109
			'mykees_comment[username]'=>'Mykees',
110
			'mykees_comment[email]'=>'contact.fr',
111
			'mykees_comment[content]'=>'Salut les guedins',
112
			'mykees_comment[parentId]'=>0,
113
			'mykees_comment[model]'=>'Post',
114
			'mykees_comment[modelId]'=>1,
115
		]);
116
117
		$this->client->submit($form);
118
119
		$count = count($this->query_manager->findAllComments());
120
121
		$this->assertEquals(5, $count);
122
	}
123
124
125
	public function testAddCommmentWithAChild()
126
	{
127
		$crawler = $this->client->request('GET','/blog/title-2');
128
129
		$form = $crawler->selectButton('Poster')->form([
130
			'mykees_comment[username]'=>'Mykees',
131
			'mykees_comment[email]'=>'[email protected]',
132
			'mykees_comment[content]'=>'Salut les guedins',
133
			'mykees_comment[parentId]'=>0,
134
			'mykees_comment[model]'=>'Post',
135
			'mykees_comment[modelId]'=>1,
136
		]);
137
		$this->client->submit($form);
138
139
		$this->assertEquals(302,$this->client->getResponse()->getStatusCode());
140
141
		$count = count($this->query_manager->findAllComments());
142
143
		$this->assertEquals(6, $count);
144
145
		$form = $crawler->selectButton('Poster')->form([
146
			'mykees_comment[username]'=>'Marion',
147
			'mykees_comment[email]'=>'[email protected]',
148
			'mykees_comment[content]'=>'Salut les guedins',
149
			'mykees_comment[parentId]'=>27,
150
			'mykees_comment[model]'=>'Post',
151
			'mykees_comment[modelId]'=>1,
152
		]);
153
		$this->client->submit($form);
154
155
		$this->assertEquals(302,$this->client->getResponse()->getStatusCode());
156
157
		$count = count($this->query_manager->findAllComments());
158
		$this->assertEquals(7, $count);
159
160
161
		$count = count($this->query_manager->findAllComments(['parentId'=>27]));
162
		$this->assertEquals(1, $count);
163
	}
164
165
166
	public function testPreDeleteComment()
167
	{
168
		$crawler = $this->client->request('GET','/blog/title-1');
169
		$form = $crawler->selectButton('Poster')->form([
170
			'mykees_comment[username]'=>'Mykees',
171
			'mykees_comment[email]'=>'[email protected]',
172
			'mykees_comment[content]'=>'Salut les guedins',
173
			'mykees_comment[parentId]'=>0,
174
			'mykees_comment[model]'=>'Post',
175
			'mykees_comment[modelId]'=>25,
176
		]);
177
		$this->client->submit($form);
178
		$this->assertEquals(302,$this->client->getResponse()->getStatusCode());
179
		$count = count($this->query_manager->findAllComments());
180
		$this->assertEquals(6, $count);
181
182
183
		$post = $this->em->getRepository('MvcBlogBundle:Post')->find(25);
184
		$this->em->remove($post);
185
		$this->em->flush();
186
187
		$count = count($this->query_manager->findAllComments());
188
		$this->assertEquals(5, $count);
189
	}
190
191
192
	public function testWithCommentDepthToZero(){
193
		$crawler = $this->client->request('GET', '/blog/title-1');
194
		$this->assertEquals(200,$this->client->getResponse()->getStatusCode());
195
		$this->assertEquals(0,$crawler->filter('.reply')->count());
196
	}
197
198
}