|
1
|
|
|
<?php |
|
2
|
|
|
namespace TildBJ\Seeder\Tests\Unit\Collection; |
|
3
|
|
|
|
|
4
|
|
|
/*************************************************************** |
|
5
|
|
|
* Copyright notice |
|
6
|
|
|
* |
|
7
|
|
|
* (c) 2018 Dennis Römmich <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* All rights reserved |
|
10
|
|
|
* |
|
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
|
12
|
|
|
* free software; you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU General Public License as published by |
|
14
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
15
|
|
|
* (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* The GNU General Public License can be found at |
|
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
|
19
|
|
|
* |
|
20
|
|
|
* This script is distributed in the hope that it will be useful, |
|
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23
|
|
|
* GNU General Public License for more details. |
|
24
|
|
|
* |
|
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
|
26
|
|
|
***************************************************************/ |
|
27
|
|
|
use TildBJ\Seeder\Collection\SeedCollection; |
|
28
|
|
|
use Nimut\TestingFramework\TestCase\UnitTestCase; |
|
29
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* SeedCollectionTest |
|
33
|
|
|
* |
|
34
|
|
|
* @author Dennis Römmich<[email protected]> |
|
35
|
|
|
* @copyright Copyright belongs to the respective authors |
|
36
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later |
|
37
|
|
|
*/ |
|
38
|
|
|
class SeedCollectionTest extends UnitTestCase |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* @var \TildBJ\Seeder\SeedCollection |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $subject; |
|
44
|
|
|
|
|
45
|
|
|
public function setUp() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->subject = new SeedCollection(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @method attach |
|
52
|
|
|
* @test |
|
53
|
|
|
*/ |
|
54
|
|
|
public function attachAttachesNewItemIfItNotExist() |
|
55
|
|
|
{ |
|
56
|
|
|
$seed = $this->createMock(\TildBJ\Seeder\Seed::class); |
|
57
|
|
|
$this->subject->attach($seed); |
|
|
|
|
|
|
58
|
|
|
$this->assertSame($seed, $this->subject->current()['NEW1']); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @method attach |
|
63
|
|
|
* @test |
|
64
|
|
|
*/ |
|
65
|
|
|
public function attachAttachesOnlyOneSeedIfSeedsAreIdentical() |
|
66
|
|
|
{ |
|
67
|
|
|
$seed = $this->createMock(\TildBJ\Seeder\Seed::class); |
|
68
|
|
|
$this->subject->attach($seed); |
|
|
|
|
|
|
69
|
|
|
$this->subject->attach($seed); |
|
|
|
|
|
|
70
|
|
|
$this->assertSame([ |
|
71
|
|
|
0 => 'NEW1', |
|
72
|
|
|
1 => 'NEW2', |
|
73
|
|
|
], array_keys($this->subject->current())); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param $name |
|
78
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function mockSeed($name) |
|
81
|
|
|
{ |
|
82
|
|
|
$seed = $this->createMock(\TildBJ\Seeder\Seed::class); |
|
83
|
|
|
$seed->method('getTitle')->willReturn($name); |
|
84
|
|
|
$seed->method('getTarget')->willReturn(strtolower(preg_replace('/\B([A-Z])/', '_$1', $name))); |
|
85
|
|
|
$seed->method('getProperties')->willReturn(['pid' => 123]); |
|
86
|
|
|
|
|
87
|
|
|
return $seed; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @test |
|
92
|
|
|
* @method get |
|
93
|
|
|
*/ |
|
94
|
|
View Code Duplication |
public function getReturnsArrayWithTwoKeysWhenClassNameFooBar() |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
$seeder = $this->createMock(\TildBJ\Seeder\Seeder::class); |
|
97
|
|
|
$seeder->method('getClass')->willReturn('FooBar'); |
|
98
|
|
|
$fooBarSeed = $this->mockSeed('FooBar'); |
|
99
|
|
|
$fooSeed = $this->mockSeed('Foo'); |
|
100
|
|
|
$barSeed = $this->mockSeed('Bar'); |
|
101
|
|
|
|
|
102
|
|
|
// NEW1 |
|
103
|
|
|
$this->subject->attach($fooBarSeed); |
|
104
|
|
|
// NEW2 |
|
105
|
|
|
$this->subject->attach($fooSeed); |
|
106
|
|
|
// NEW3 |
|
107
|
|
|
$this->subject->attach(clone $fooBarSeed); |
|
108
|
|
|
// NEW4 |
|
109
|
|
|
$this->subject->attach($barSeed); |
|
110
|
|
|
|
|
111
|
|
|
$this->subject->amount = 4; |
|
|
|
|
|
|
112
|
|
|
$this->assertSame([ |
|
113
|
|
|
0 => 'NEW1', |
|
114
|
|
|
1 => 'NEW3', |
|
115
|
|
|
], array_keys($this->subject->get($seeder))); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @test |
|
120
|
|
|
* @method get |
|
121
|
|
|
*/ |
|
122
|
|
View Code Duplication |
public function getReturnsArrayWithOneKeyWhenClassNameFoo() |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
$seeder = $this->createMock(\TildBJ\Seeder\Seeder::class); |
|
125
|
|
|
$seeder->method('getClass')->willReturn('Foo'); |
|
126
|
|
|
$fooBarSeed = $this->mockSeed('FooBar'); |
|
127
|
|
|
$fooSeed = $this->mockSeed('Foo'); |
|
128
|
|
|
$barSeed = $this->mockSeed('Bar'); |
|
129
|
|
|
|
|
130
|
|
|
// NEW1 |
|
131
|
|
|
$this->subject->attach($fooBarSeed); |
|
132
|
|
|
// NEW2 |
|
133
|
|
|
$this->subject->attach($fooSeed); |
|
134
|
|
|
// NEW3 |
|
135
|
|
|
$this->subject->attach($fooBarSeed); |
|
136
|
|
|
// NEW4 |
|
137
|
|
|
$this->subject->attach($barSeed); |
|
138
|
|
|
|
|
139
|
|
|
$this->subject->amount = 4; |
|
|
|
|
|
|
140
|
|
|
$this->assertSame([ |
|
141
|
|
|
0 => 'NEW2', |
|
142
|
|
|
], array_keys($this->subject->get($seeder))); |
|
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @test |
|
147
|
|
|
* @method get |
|
148
|
|
|
*/ |
|
149
|
|
View Code Duplication |
public function getReturnsArrayWithOneKeyWhenClassNameFooBar() |
|
|
|
|
|
|
150
|
|
|
{ |
|
151
|
|
|
$seeder = $this->createMock(\TildBJ\Seeder\Seeder::class); |
|
152
|
|
|
$seeder->method('getClass')->willReturn('FooBar'); |
|
153
|
|
|
$fooBarSeed = $this->mockSeed('FooBar'); |
|
154
|
|
|
$fooSeed = $this->mockSeed('Foo'); |
|
155
|
|
|
$barSeed = $this->mockSeed('Bar'); |
|
156
|
|
|
|
|
157
|
|
|
// NEW1 |
|
158
|
|
|
$this->subject->attach($fooBarSeed); |
|
159
|
|
|
// NEW2 |
|
160
|
|
|
$this->subject->attach($fooSeed); |
|
161
|
|
|
// NEW3 |
|
162
|
|
|
$this->subject->attach($fooBarSeed); |
|
163
|
|
|
// NEW4 |
|
164
|
|
|
$this->subject->attach($barSeed); |
|
165
|
|
|
|
|
166
|
|
|
$this->subject->amount = 4; |
|
|
|
|
|
|
167
|
|
|
$this->assertSame([ |
|
168
|
|
|
0 => 'NEW1', |
|
169
|
|
|
1 => 'NEW3', |
|
170
|
|
|
], array_keys($this->subject->get($seeder))); |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @method clear |
|
175
|
|
|
* @test |
|
176
|
|
|
*/ |
|
177
|
|
|
public function clearSeedCollectionRemovesAllSeeds() |
|
178
|
|
|
{ |
|
179
|
|
|
$seed = $this->createMock(\TildBJ\Seeder\Seed::class); |
|
180
|
|
|
$this->subject->attach($seed); |
|
|
|
|
|
|
181
|
|
|
$this->subject->clear(); |
|
182
|
|
|
|
|
183
|
|
|
$this->assertSame([], $this->subject->toArray()); |
|
184
|
|
|
$this->assertSame(0, $this->subject->count()); |
|
185
|
|
|
$this->assertSame(false, $this->subject->current()); |
|
186
|
|
|
$this->assertSame(false, $this->subject->valid()); |
|
187
|
|
|
$this->assertSame(false, $this->subject->next()); |
|
188
|
|
|
$this->assertSame(null, $this->subject->key()); |
|
189
|
|
|
$this->assertSame(false, $this->subject->rewind()); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @method clear |
|
194
|
|
|
* @test |
|
195
|
|
|
*/ |
|
196
|
|
|
public function clearSeedCollectionSetCounterToZero() |
|
197
|
|
|
{ |
|
198
|
|
|
$seed = $this->createMock(\TildBJ\Seeder\Seed::class); |
|
199
|
|
|
$this->subject->attach($seed); |
|
|
|
|
|
|
200
|
|
|
$this->subject->clear(); |
|
201
|
|
|
$this->subject->attach($seed); |
|
|
|
|
|
|
202
|
|
|
$this->subject->attach(clone $seed); |
|
|
|
|
|
|
203
|
|
|
$this->assertSame([ |
|
204
|
|
|
0 => 'NEW1', |
|
205
|
|
|
1 => 'NEW2', |
|
206
|
|
|
], array_keys($this->subject->current())); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @method toArray |
|
211
|
|
|
* @test |
|
212
|
|
|
*/ |
|
213
|
|
|
public function toArrayReturnsCorrectArray() |
|
214
|
|
|
{ |
|
215
|
|
|
$seed = $this->createMock(\TildBJ\Seeder\Seed::class); |
|
216
|
|
|
$seed->method('getTitle')->willReturn('FooBar'); |
|
217
|
|
|
$seed->method('getTarget')->willReturn('foo_bar'); |
|
218
|
|
|
$seed->method('getProperties')->willReturn(['pid' => 123]); |
|
219
|
|
|
$this->subject->attach($seed); |
|
|
|
|
|
|
220
|
|
|
$this->subject->attach(clone $seed); |
|
|
|
|
|
|
221
|
|
|
$this->assertSame([ |
|
222
|
|
|
'foo_bar' => [ |
|
223
|
|
|
'NEW1' => [ |
|
224
|
|
|
'pid' => 123 |
|
225
|
|
|
], |
|
226
|
|
|
'NEW2' => [ |
|
227
|
|
|
'pid' => 123 |
|
228
|
|
|
], |
|
229
|
|
|
], |
|
230
|
|
|
], $this->subject->toArray()); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
public function tearDown() |
|
234
|
|
|
{ |
|
235
|
|
|
$this->subject->clear(); |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: