1
|
|
|
<?php |
2
|
|
|
/* Copyright (C) 2017 Michael Giesler |
3
|
|
|
* |
4
|
|
|
* This file is part of Dembelo. |
5
|
|
|
* |
6
|
|
|
* Dembelo is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* Dembelo is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU Affero General Public License 3 for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU Affero General Public License 3 |
17
|
|
|
* along with Dembelo. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
declare(strict_types = 1); |
20
|
|
|
|
21
|
|
|
namespace DembeloMain\Tests\Service; |
22
|
|
|
|
23
|
|
|
use DembeloMain\Service\ReadpathUndoService; |
24
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
25
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
26
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class ReadpathUndoServiceTest |
30
|
|
|
* @package DembeloMain\Tests\Service |
31
|
|
|
*/ |
32
|
|
|
class ReadpathUndoServiceTest extends WebTestCase |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var ReadpathUndoService |
36
|
|
|
*/ |
37
|
|
|
private $undoStack; |
38
|
|
|
|
39
|
|
|
private $session; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
public function setUp(): void |
45
|
|
|
{ |
46
|
|
|
parent::setUp(); |
47
|
|
|
$this->session = new Session(new MockArraySessionStorage()); |
48
|
|
|
$this->undoStack = new ReadpathUndoService($this->session); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
|
|
public function tearDown(): void |
55
|
|
|
{ |
56
|
|
|
parent::tearDown(); |
57
|
|
|
$this->undoStack = null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* tests add() to return same value with getCurrentItem() |
62
|
|
|
*/ |
63
|
|
|
public function testAddAnItemWillReturnThisAsCurrentItem(): void |
64
|
|
|
{ |
65
|
|
|
$item = 'someItem'; |
66
|
|
|
$this->undoStack->add($item); |
67
|
|
|
self::assertEquals($item, $this->undoStack->getCurrentItem()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* tests add() throwing a typeError |
72
|
|
|
* @expectedException \TypeError |
73
|
|
|
*/ |
74
|
|
|
public function testAddAnIntegerWillThrowTypeError(): void |
75
|
|
|
{ |
76
|
|
|
$this->undoStack->add(123); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* tests getCurrentItem with no current item |
81
|
|
|
*/ |
82
|
|
|
public function testGetCurrentWithNoCurrentItem(): void |
83
|
|
|
{ |
84
|
|
|
self::assertNull($this->undoStack->getCurrentItem()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* tests get current |
89
|
|
|
*/ |
90
|
|
|
public function testGetCurrent(): void |
91
|
|
|
{ |
92
|
|
|
$this->undoStack->add('first'); |
93
|
|
|
self::assertEquals('first', $this->undoStack->getCurrentItem()); |
94
|
|
|
$this->undoStack->add('second'); |
95
|
|
|
self::assertEquals('second', $this->undoStack->getCurrentItem()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* tests undo |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public function testUndo(): void |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$this->undoStack->add('first'); |
104
|
|
|
$this->undoStack->add('second'); |
105
|
|
|
self::assertTrue($this->undoStack->undo()); |
106
|
|
|
self::assertEquals('first', $this->undoStack->getCurrentItem()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* tests undo() to return false when nothing can be undone |
111
|
|
|
*/ |
112
|
|
|
public function testUndoReturnsFalseWhenNothingCanBeUndone(): void |
113
|
|
|
{ |
114
|
|
|
self::assertFalse($this->undoStack->undo()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* tests undo() to return false when past stack is still empty |
119
|
|
|
*/ |
120
|
|
|
public function testUndoReturnsFalseWhenPastStackIsStillEmpty(): void |
121
|
|
|
{ |
122
|
|
|
$this->undoStack->add('first'); |
123
|
|
|
self::assertFalse($this->undoStack->undo()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* tests redo() to return false when nothing can be undone |
128
|
|
|
*/ |
129
|
|
|
public function testRedoReturnFalseWhenNothingCanBeRedone(): void |
130
|
|
|
{ |
131
|
|
|
self::assertFalse($this->undoStack->redo()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* tests redo() |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public function testRedo(): void |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$this->undoStack->add('first'); |
140
|
|
|
$this->undoStack->add('second'); |
141
|
|
|
self::assertTrue($this->undoStack->undo()); |
142
|
|
|
self::assertTrue($this->undoStack->redo()); |
143
|
|
|
self::assertEquals('second', $this->undoStack->getCurrentItem()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* tests add(), removing future values |
148
|
|
|
*/ |
149
|
|
|
public function testAddRemovingFutureValues(): void |
150
|
|
|
{ |
151
|
|
|
$this->undoStack->add('first'); |
152
|
|
|
self::assertEquals('first', $this->undoStack->getCurrentItem()); |
153
|
|
|
$this->undoStack->add('second'); |
154
|
|
|
self::assertEquals('second', $this->undoStack->getCurrentItem()); |
155
|
|
|
self::assertTrue($this->undoStack->undo()); |
156
|
|
|
self::assertEquals('first', $this->undoStack->getCurrentItem()); |
157
|
|
|
$this->undoStack->add('third'); |
158
|
|
|
self::assertEquals('third', $this->undoStack->getCurrentItem()); |
159
|
|
|
self::assertFalse($this->undoStack->redo()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* tests the ignoring of add() after an undo |
164
|
|
|
*/ |
165
|
|
|
public function testIgnoreAddAfterUndo(): void |
166
|
|
|
{ |
167
|
|
|
$this->undoStack->add('first'); |
168
|
|
|
$this->undoStack->add('second'); |
169
|
|
|
$this->undoStack->add('third'); |
170
|
|
|
self::assertTrue($this->undoStack->undo()); |
171
|
|
|
self::assertEquals('second', $this->undoStack->getCurrentItem()); |
172
|
|
|
$this->undoStack->add('second'); |
173
|
|
|
self::assertTrue($this->undoStack->undo()); |
174
|
|
|
self::assertEquals('first', $this->undoStack->getCurrentItem()); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* tests duplicate entries avoiding |
179
|
|
|
*/ |
180
|
|
View Code Duplication |
public function testAvoidDuplicateEntries(): void |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
$this->undoStack->add('first'); |
183
|
|
|
$this->undoStack->add('second'); |
184
|
|
|
$this->undoStack->add('second'); |
185
|
|
|
self::assertTrue($this->undoStack->undo()); |
186
|
|
|
self::assertSame('first', $this->undoStack->getCurrentItem()); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* tests a complex sequence |
191
|
|
|
*/ |
192
|
|
|
public function testAComplexSequence(): void |
193
|
|
|
{ |
194
|
|
|
$this->undoStack->add('first'); |
195
|
|
|
self::assertEquals('first', $this->undoStack->getCurrentItem()); |
196
|
|
|
$this->undoStack->add('second'); |
197
|
|
|
self::assertEquals('second', $this->undoStack->getCurrentItem()); |
198
|
|
|
$this->undoStack->add('third'); |
199
|
|
|
self::assertEquals('third', $this->undoStack->getCurrentItem()); |
200
|
|
|
self::assertTrue($this->undoStack->undo()); |
201
|
|
|
self::assertEquals('second', $this->undoStack->getCurrentItem()); |
202
|
|
|
self::assertTrue($this->undoStack->undo()); |
203
|
|
|
self::assertEquals('first', $this->undoStack->getCurrentItem()); |
204
|
|
|
self::assertTrue($this->undoStack->redo()); |
205
|
|
|
self::assertEquals('second', $this->undoStack->getCurrentItem()); |
206
|
|
|
self::assertTrue($this->undoStack->redo()); |
207
|
|
|
self::assertEquals('third', $this->undoStack->getCurrentItem()); |
208
|
|
|
self::assertTrue($this->undoStack->undo()); |
209
|
|
|
self::assertEquals('second', $this->undoStack->getCurrentItem()); |
210
|
|
|
$this->undoStack->add('fourth'); |
211
|
|
|
self::assertEquals('fourth', $this->undoStack->getCurrentItem()); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* tests add() from session |
216
|
|
|
*/ |
217
|
|
|
public function testAddFromSession(): void |
218
|
|
|
{ |
219
|
|
|
$this->undoStack->add('first'); |
220
|
|
|
$undoStack = new ReadpathUndoService($this->session); |
221
|
|
|
self::assertEquals('first', $undoStack->getCurrentItem()); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
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.