1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Porpaginas\Tests\Doctrine\Batch; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Zenstruck\Porpaginas\Arrays\ArrayResult; |
7
|
|
|
use Zenstruck\Porpaginas\Doctrine\Batch\ORMBatchProcessor; |
8
|
|
|
use Zenstruck\Porpaginas\Tests\Doctrine\Fixtures\ORMEntity; |
9
|
|
|
use Zenstruck\Porpaginas\Tests\Doctrine\HasEntityManager; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Kevin Bond <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class ORMBatchProcessorTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
use HasEntityManager; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @test |
20
|
|
|
*/ |
21
|
|
|
public function can_batch_persist_results(): void |
22
|
|
|
{ |
23
|
|
|
$array = []; |
24
|
|
|
|
25
|
|
|
for ($i = 1; $i <= 211; ++$i) { |
26
|
|
|
$array[] = 'value '.$i; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$batchProcessor = new ORMBatchProcessor(new ArrayResult($array), $this->em); |
|
|
|
|
30
|
|
|
|
31
|
|
|
foreach ($batchProcessor as $item) { |
32
|
|
|
$this->em->persist(new ORMEntity($item)); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$entities = $this->em->getRepository(ORMEntity::class)->findAll(); |
36
|
|
|
|
37
|
|
|
$this->assertCount(211, $entities); |
38
|
|
|
$this->assertSame('value 32', $entities[31]->value); |
39
|
|
|
$this->assertSame('value 200', $entities[199]->value); |
40
|
|
|
$this->assertSame('value 211', $entities[210]->value); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @test |
45
|
|
|
*/ |
46
|
|
|
public function can_batch_persist_new_entities(): void |
47
|
|
|
{ |
48
|
|
|
$array = []; |
49
|
|
|
|
50
|
|
|
for ($i = 1; $i <= 211; ++$i) { |
51
|
|
|
$array[] = new ORMEntity('value '.$i); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$batchProcessor = new ORMBatchProcessor(new ArrayResult($array), $this->em); |
|
|
|
|
55
|
|
|
|
56
|
|
|
foreach ($batchProcessor as $item) { |
57
|
|
|
$this->em->persist($item); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$entities = $this->em->getRepository(ORMEntity::class)->findAll(); |
61
|
|
|
|
62
|
|
|
$this->assertCount(211, $entities); |
63
|
|
|
$this->assertSame('value 32', $entities[31]->value); |
64
|
|
|
$this->assertSame('value 200', $entities[199]->value); |
65
|
|
|
$this->assertSame('value 211', $entities[210]->value); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @test |
70
|
|
|
*/ |
71
|
|
|
public function can_batch_persist_results_from_array(): void |
72
|
|
|
{ |
73
|
|
|
$array = []; |
74
|
|
|
|
75
|
|
|
for ($i = 1; $i <= 211; ++$i) { |
76
|
|
|
$array[] = 'value '.$i; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$batchProcessor = new ORMBatchProcessor($array, $this->em); |
|
|
|
|
80
|
|
|
|
81
|
|
|
foreach ($batchProcessor as $item) { |
82
|
|
|
$this->em->persist(new ORMEntity($item)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$entities = $this->em->getRepository(ORMEntity::class)->findAll(); |
86
|
|
|
|
87
|
|
|
$this->assertCount(211, $entities); |
88
|
|
|
$this->assertSame('value 32', $entities[31]->value); |
89
|
|
|
$this->assertSame('value 200', $entities[199]->value); |
90
|
|
|
$this->assertSame('value 211', $entities[210]->value); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @test |
95
|
|
|
*/ |
96
|
|
|
public function can_batch_persist_array_of_arrays(): void |
97
|
|
|
{ |
98
|
|
|
$array = []; |
99
|
|
|
|
100
|
|
|
for ($i = 1; $i <= 211; ++$i) { |
101
|
|
|
$array[] = ['value', $i]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$batchProcessor = new ORMBatchProcessor($array, $this->em); |
|
|
|
|
105
|
|
|
|
106
|
|
|
foreach ($batchProcessor as [$value, $id]) { |
107
|
|
|
$this->em->persist(new ORMEntity($value.' '.$id)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$entities = $this->em->getRepository(ORMEntity::class)->findAll(); |
111
|
|
|
|
112
|
|
|
$this->assertCount(211, $entities); |
113
|
|
|
$this->assertSame('value 32', $entities[31]->value); |
114
|
|
|
$this->assertSame('value 200', $entities[199]->value); |
115
|
|
|
$this->assertSame('value 211', $entities[210]->value); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @test |
120
|
|
|
*/ |
121
|
|
|
public function results_do_not_have_to_be_countable(): void |
122
|
|
|
{ |
123
|
|
|
$iterator = static function() { |
124
|
|
|
yield 'foo'; |
125
|
|
|
}; |
126
|
|
|
$batchProcessor = new ORMBatchProcessor($iterator(), $this->em); |
|
|
|
|
127
|
|
|
|
128
|
|
|
$this->assertFalse(\is_countable($batchProcessor)); |
129
|
|
|
$this->assertSame(['foo'], \iterator_to_array($batchProcessor)); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|