1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Badger\Bundle\GameBundle\Tests\Controller; |
4
|
|
|
|
5
|
|
|
use Badger\BadgerTestCase; |
6
|
|
|
use Badger\Component\Game\Repository\AdventureRepositoryInterface; |
7
|
|
|
use Badger\Component\Game\Repository\AdventureStepCompletionRepositoryInterface; |
8
|
|
|
use Badger\Component\Game\Repository\BadgeRepositoryInterface; |
9
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Marie Bochu <[email protected]> |
13
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
14
|
|
|
*/ |
15
|
|
|
class AdventureControllerTest extends BadgerTestCase |
16
|
|
|
{ |
17
|
|
|
/** @var AdventureRepositoryInterface */ |
18
|
|
|
private $adventureRepository; |
19
|
|
|
|
20
|
|
|
/** @var ObjectRepository */ |
21
|
|
|
private $adventureStepRepository; |
22
|
|
|
|
23
|
|
|
/** @var AdventureStepCompletionRepositoryInterface */ |
24
|
|
|
private $adventureComplRepo; |
25
|
|
|
|
26
|
|
|
/** @var BadgeRepositoryInterface */ |
27
|
|
|
private $badgeRepository; |
28
|
|
|
|
29
|
|
|
public function setUp() |
30
|
|
|
{ |
31
|
|
|
parent::setUp(); |
32
|
|
|
|
33
|
|
|
$this->adventureRepository = $this->get('badger.game.repository.adventure'); |
34
|
|
|
$this->adventureStepRepository = $this->get('badger.game.repository.adventure_step'); |
35
|
|
|
$this->adventureComplRepo = $this->get('badger.game.repository.adventure_step_completion'); |
36
|
|
|
$this->badgeRepository = $this->get('badger.game.repository.badge'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testCreateAdventureAction() |
40
|
|
|
{ |
41
|
|
|
$client = $this->createUser(); |
42
|
|
|
|
43
|
|
|
$adventureCount = count($this->adventureRepository->findAll()); |
44
|
|
|
$adventureStepsCount = count($this->adventureStepRepository->findAll()); |
45
|
|
|
|
46
|
|
|
$client->request('POST', 'admin/adventure/form', [ |
47
|
|
|
'adventure' => [ |
48
|
|
|
'title' => 'title', |
49
|
|
|
'description' => 'Summer Session', |
50
|
|
|
'rewardPoint' => 0, |
51
|
|
|
'isStepLinked' => false, |
52
|
|
|
'badge' => null, |
53
|
|
|
'steps' => [ |
54
|
|
|
[ |
55
|
|
|
'title' => 2013, |
56
|
|
|
'position' => 1, |
57
|
|
|
'description' => 'Summer Session 2013', |
58
|
|
|
'rewardPoint' => 0 |
59
|
|
|
], |
60
|
|
|
[ |
61
|
|
|
'title' => 2014, |
62
|
|
|
'position' => 2, |
63
|
|
|
'description' => 'Summer Session 2014', |
64
|
|
|
'rewardPoint' => 0 |
65
|
|
|
] |
66
|
|
|
] |
67
|
|
|
] |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
$this->assertCount($adventureCount += 1, $this->adventureRepository->findAll()); |
71
|
|
|
$this->assertCount($adventureStepsCount += 2, $this->adventureStepRepository->findAll()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testContraintsFormAction() |
75
|
|
|
{ |
76
|
|
|
$client = $this->createUser(); |
77
|
|
|
|
78
|
|
|
$client->enableProfiler(); |
79
|
|
|
$adventureCount = count($this->adventureRepository->findAll()); |
80
|
|
|
|
81
|
|
|
$client->request('POST', 'admin/adventure/form', [ |
82
|
|
|
'adventure' => [ |
83
|
|
|
'title' => 'ASS', |
84
|
|
|
] |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
$profile = $client->getProfile(); |
88
|
|
|
|
89
|
|
|
$form = $profile->getCollector('form')->getData(); |
90
|
|
|
$this->assertEquals(2, $form['nb_errors']); |
91
|
|
|
$this->assertEquals('This value is already used.', $form['forms']['adventure']['children']['title']['errors'][0]['message']); |
92
|
|
|
$this->assertCount($adventureCount, $this->adventureRepository->findAll()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function testDeleteAction() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$client = $this->createUser(); |
98
|
|
|
|
99
|
|
|
$adventureCount = count($this->adventureRepository->findAll()); |
100
|
|
|
$adventureStepCount = count($this->adventureStepRepository->findAll()); |
101
|
|
|
$adventureComplRepoCount = count($this->adventureComplRepo->findAll()); |
102
|
|
|
$badgeCount = count($this->badgeRepository->findAll()); |
103
|
|
|
|
104
|
|
|
$adventure = $this->adventureRepository->findOneBy(['title' => 'Community']); |
105
|
|
|
$client->request('POST', 'admin/adventure/delete/' . $adventure->getId(), [ |
106
|
|
|
'_method' => 'DELETE', |
107
|
|
|
'form' => [] |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
$this->assertCount($adventureCount -= 1, $this->adventureRepository->findAll(), 'Adventure is removed'); |
111
|
|
|
$this->assertCount($adventureStepCount -= 2, $this->adventureStepRepository->findAll(), 'Steps of adventure are removed'); |
112
|
|
|
$this->assertCount($adventureComplRepoCount -= 1, $this->adventureComplRepo->findAll(), 'Completions steps of adventure are removed'); |
113
|
|
|
$this->assertCount($badgeCount, $this->badgeRepository->findAll(), 'Badge are not removed when an adventure is removed'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testUpdateAnAdventureToDeleteAStep() |
117
|
|
|
{ |
118
|
|
|
$client = $this->createUser(); |
119
|
|
|
|
120
|
|
|
$adventureCount = count($this->adventureRepository->findAll()); |
121
|
|
|
$adventureStepsCount = count($this->adventureStepRepository->findAll()); |
122
|
|
|
$adventureCompletionCount = count($this->adventureComplRepo->findAll()); |
123
|
|
|
|
124
|
|
|
$adventure = $this->adventureRepository->findOneBy(['title' => 'Community']); |
125
|
|
|
$badge = $this->badgeRepository->findOneBy(['title' => 'Master Contributor']); |
126
|
|
|
|
127
|
|
|
$client->request('POST', sprintf('admin/adventure/form/%s', $adventure->getId()), [ |
128
|
|
|
'adventure' => [ |
129
|
|
|
'title' => 'Community', |
130
|
|
|
'description' => '', |
131
|
|
|
'rewardPoint' => 10, |
132
|
|
|
'isStepLinked' => true, |
133
|
|
|
'badge' => $badge->getId(), |
134
|
|
|
'steps' => [ |
135
|
|
|
[ |
136
|
|
|
'title' => 'Core Contributor', |
137
|
|
|
'position' => 1, |
138
|
|
|
'description' => 'You merged your first Pull Request on our PIM Community Edition!', |
139
|
|
|
'rewardPoint' => 0 |
140
|
|
|
] |
141
|
|
|
] |
142
|
|
|
] |
143
|
|
|
]); |
144
|
|
|
|
145
|
|
|
$this->assertCount($adventureCount, $this->adventureRepository->findAll(), 'No adventure added or removed'); |
146
|
|
|
$this->assertCount($adventureStepsCount -= 1, $this->adventureStepRepository->findAll(), 'Step deleted'); |
147
|
|
|
$this->assertCount($adventureCompletionCount -= 1, $this->adventureComplRepo->findAll(), 'Adventure Completion linked to deleted step is removed'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
View Code Duplication |
public function testDeleteWhenStepsAreLinkedToBadges() |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$client = $this->createUser(); |
153
|
|
|
|
154
|
|
|
$adventureCount = count($this->adventureRepository->findAll()); |
155
|
|
|
$adventureStepCount = count($this->adventureStepRepository->findAll()); |
156
|
|
|
$adventureComplRepoCount = count($this->adventureComplRepo->findAll()); |
157
|
|
|
$badgeCount = count($this->badgeRepository->findAll()); |
158
|
|
|
|
159
|
|
|
$adventure = $this->adventureRepository->findOneBy(['title' => 'ASS']); |
160
|
|
|
$client->request('POST', 'admin/adventure/delete/' . $adventure->getId(), [ |
161
|
|
|
'_method' => 'DELETE', |
162
|
|
|
'form' => [] |
163
|
|
|
]); |
164
|
|
|
|
165
|
|
|
$this->assertCount($adventureCount -= 1, $this->adventureRepository->findAll(), 'Adventure is removed'); |
166
|
|
|
$this->assertCount($adventureStepCount -= 4, $this->adventureStepRepository->findAll(), 'Steps of adventure are removed'); |
167
|
|
|
$this->assertCount($adventureComplRepoCount -= 2, $this->adventureComplRepo->findAll(), 'Completions steps of adventure are removed'); |
168
|
|
|
$this->assertCount($badgeCount, $this->badgeRepository->findAll(), 'Badge are not removed when an adventure is removed'); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
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.