|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maketok\DataMigration\Action\Type; |
|
4
|
|
|
|
|
5
|
|
|
use Faker\Generator; |
|
6
|
|
|
use Faker\Provider\Base; |
|
7
|
|
|
use Faker\Provider\Lorem; |
|
8
|
|
|
use Maketok\DataMigration\ArrayMap; |
|
9
|
|
|
use Maketok\DataMigration\Expression\LanguageAdapter; |
|
10
|
|
|
use Maketok\DataMigration\Storage\Db\ResourceHelperInterface; |
|
11
|
|
|
use Maketok\DataMigration\Storage\Filesystem\ResourceInterface; |
|
12
|
|
|
use Maketok\DataMigration\Unit\Type\Unit; |
|
13
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
|
14
|
|
|
|
|
15
|
|
|
class GenerateTest extends \PHPUnit_Framework_TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
use ServiceGetterTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Generate |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $action; |
|
23
|
|
|
|
|
24
|
|
|
public function setUp() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->action = new Generate( |
|
27
|
|
|
$this->getUnitBag(), |
|
28
|
|
|
$this->getConfig(), |
|
29
|
|
|
new LanguageAdapter(), |
|
30
|
|
|
new Generator(), |
|
31
|
|
|
2, |
|
32
|
|
|
new ArrayMap(), |
|
33
|
|
|
$this->getResourceHelper() |
|
34
|
|
|
); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $code |
|
39
|
|
|
* @return Unit |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getUnit($code) |
|
42
|
|
|
{ |
|
43
|
|
|
$unit = new Unit($code); |
|
44
|
|
|
$arr = \SplFixedArray::fromArray([1,1]); |
|
45
|
|
|
$unit->setGenerationSeed($arr); |
|
46
|
|
|
return $unit; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return ResourceHelperInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function getResourceHelper() |
|
53
|
|
|
{ |
|
54
|
|
|
$helper = $this->getMockBuilder('\Maketok\DataMigration\Storage\Db\ResourceHelperInterface') |
|
55
|
|
|
->getMock(); |
|
56
|
|
|
return $helper; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testGetCode() |
|
60
|
|
|
{ |
|
61
|
|
|
$this->assertEquals('generate', $this->action->getCode()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param int $expects |
|
66
|
|
|
* @return ResourceInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function getFS($expects = 0) |
|
69
|
|
|
{ |
|
70
|
|
|
$filesystem = $this->getMockBuilder('\Maketok\DataMigration\Storage\Filesystem\ResourceInterface') |
|
71
|
|
|
->getMock(); |
|
72
|
|
|
$filesystem->expects($this->exactly($expects)) |
|
73
|
|
|
->method('writeRow'); |
|
74
|
|
|
return $filesystem; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testProcess() |
|
78
|
|
|
{ |
|
79
|
|
|
$unit = $this->getUnit('test_table1'); |
|
80
|
|
|
$unit->setFilesystem($this->getFS(2)); |
|
81
|
|
|
$unit->setGeneratorMapping([ |
|
82
|
|
|
'id' => 'generator.randomDigit', |
|
83
|
|
|
'code' => function (Generator $generator) { |
|
84
|
|
|
return $generator->word; |
|
85
|
|
|
} |
|
86
|
|
|
]); |
|
87
|
|
|
$generator = new Generator(); |
|
88
|
|
|
$generator->addProvider(new Base($generator)); |
|
89
|
|
|
$generator->addProvider(new Lorem($generator)); |
|
90
|
|
|
$action = new Generate( |
|
91
|
|
|
$this->getUnitBag([$unit]), |
|
92
|
|
|
$this->getConfig(), |
|
93
|
|
|
new LanguageAdapter(new ExpressionLanguage()), |
|
94
|
|
|
$generator, |
|
95
|
|
|
2, |
|
96
|
|
|
new ArrayMap(), |
|
97
|
|
|
$this->getResourceHelper() |
|
98
|
|
|
); |
|
99
|
|
|
$action->process($this->getResultMock()); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertEquals( |
|
102
|
|
|
'/tmp/test_table1.csv', |
|
103
|
|
|
$unit->getTmpFileName() |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @throws \Exception |
|
109
|
|
|
*/ |
|
110
|
|
|
public function testComplexProcess() |
|
111
|
|
|
{ |
|
112
|
|
|
$unit = $this->getUnit('test_table1'); |
|
113
|
|
|
$unit->setFilesystem($this->getFS(10)); |
|
114
|
|
|
$unit->setGeneratorMapping([ |
|
115
|
|
|
'id' => 'generator.randomDigit', |
|
116
|
|
|
'code' => function (Generator $generator) { |
|
117
|
|
|
return $generator->word; |
|
118
|
|
|
} |
|
119
|
|
|
]); |
|
120
|
|
|
$filesystem = $this->getMockBuilder('\Maketok\DataMigration\Storage\Filesystem\ResourceInterface') |
|
121
|
|
|
->getMock(); |
|
122
|
|
|
$counter1 = 0; |
|
123
|
|
|
$filesystem->expects($this->any()) |
|
124
|
|
|
->method('writeRow')->willReturnCallback(function () use (&$counter1) { |
|
125
|
|
|
$counter1++; |
|
126
|
|
|
}); |
|
127
|
|
|
/** @var ResourceInterface $filesystem */ |
|
128
|
|
|
$childUnit = $this->getUnit('test_table2'); |
|
129
|
|
|
$childUnit->setFilesystem($filesystem); |
|
130
|
|
|
$childUnit->setGeneratorMapping([ |
|
131
|
|
|
'id' => 'generator.randomDigit', |
|
132
|
|
|
'parent_id' => 'map.test_table1_id', |
|
133
|
|
|
'field1' => '0' |
|
134
|
|
|
]); |
|
135
|
|
|
$childUnit->setParent($unit); |
|
136
|
|
|
$seed = new \SplFixedArray(2); |
|
137
|
|
|
$seed[0] = 4; |
|
138
|
|
|
$seed[1] = 1; |
|
139
|
|
|
$childUnit->setGenerationSeed($seed); |
|
140
|
|
|
$childDataUnit = $this->getUnit('test_table3'); |
|
141
|
|
|
$counter2 = 0; |
|
142
|
|
|
$filesystem2 = $this->getMockBuilder('\Maketok\DataMigration\Storage\Filesystem\ResourceInterface') |
|
143
|
|
|
->getMock(); |
|
144
|
|
|
$filesystem2->expects($this->any()) |
|
145
|
|
|
->method('writeRow')->willReturnCallback(function () use (&$counter2) { |
|
146
|
|
|
$counter2++; |
|
147
|
|
|
}); |
|
148
|
|
|
/** @var ResourceInterface $filesystem2 */ |
|
149
|
|
|
$childDataUnit->setFilesystem($filesystem2); |
|
150
|
|
|
$childDataUnit->setGeneratorMapping([ |
|
151
|
|
|
'id' => 'generator.randomDigit', |
|
152
|
|
|
'parent_id' => 'map.test_table2_id', |
|
153
|
|
|
'field2' => '2' |
|
154
|
|
|
]); |
|
155
|
|
|
$childDataUnit->setGenerationSeed($seed); |
|
156
|
|
|
$childUnit->addSibling($childDataUnit); |
|
157
|
|
|
$generator = new Generator(); |
|
158
|
|
|
$generator->addProvider(new Base($generator)); |
|
159
|
|
|
$generator->addProvider(new Lorem($generator)); |
|
160
|
|
|
$action = new Generate( |
|
161
|
|
|
$this->getUnitBag([$unit, $childUnit, $childDataUnit]), |
|
162
|
|
|
$this->getConfig(), |
|
163
|
|
|
new LanguageAdapter(new ExpressionLanguage()), |
|
164
|
|
|
$generator, |
|
165
|
|
|
10, |
|
166
|
|
|
new ArrayMap(), |
|
167
|
|
|
$this->getResourceHelper() |
|
168
|
|
|
); |
|
169
|
|
|
$action->process($this->getResultMock()); |
|
170
|
|
|
|
|
171
|
|
|
$this->assertSame($counter1, $counter2); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @expectedException \Maketok\DataMigration\Action\Exception\WrongContextException |
|
176
|
|
|
* @expectedExceptionMessage Can not use generation with unit test_table1 |
|
177
|
|
|
*/ |
|
178
|
|
|
public function testWrongProcess() |
|
179
|
|
|
{ |
|
180
|
|
|
$unit = $this->getUnit('test_table1'); |
|
181
|
|
|
$action = new Generate( |
|
182
|
|
|
$this->getUnitBag([$unit]), |
|
183
|
|
|
$this->getConfig(), |
|
184
|
|
|
new LanguageAdapter(), |
|
185
|
|
|
new Generator(), |
|
186
|
|
|
2, |
|
187
|
|
|
new ArrayMap(), |
|
188
|
|
|
$this->getResourceHelper() |
|
189
|
|
|
); |
|
190
|
|
|
$action->process($this->getResultMock()); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public function testGetRandom() |
|
194
|
|
|
{ |
|
195
|
|
|
// distribution 1...40 with peak at 10; |
|
196
|
|
|
// o |
|
197
|
|
|
// o o |
|
198
|
|
|
// o o |
|
199
|
|
|
// o | | o |
|
200
|
|
|
// o | 50% | o |
|
201
|
|
|
// | | o |
|
202
|
|
|
//0 10 20 30 40 |
|
203
|
|
|
$numbers = []; |
|
204
|
|
|
$count = 100000; |
|
205
|
|
|
for ($i = 0; $i < $count; $i++) { |
|
206
|
|
|
$rnd = $this->action->getRandom(40, 10); |
|
207
|
|
|
if (isset($numbers[$rnd])) { |
|
208
|
|
|
$numbers[$rnd]++; |
|
209
|
|
|
} else { |
|
210
|
|
|
$numbers[$rnd] = 1; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
$percentage = []; |
|
214
|
|
|
foreach ($numbers as $numb => $cnt) { |
|
215
|
|
|
$percentage[$numb] = $cnt/$count*100; |
|
216
|
|
|
} |
|
217
|
|
|
// statistics |
|
218
|
|
|
$centerZone = 0; |
|
219
|
|
|
foreach (range(5, 15) as $indx) { |
|
220
|
|
|
$centerZone += $percentage[$indx]; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
$this->assertGreaterThan(50, $centerZone); // actual value is around 56% (+1 range) |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|