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\ORMCountableBatchProcessor; |
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 ORMCountableBatchProcessorTest 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 ORMCountableBatchProcessor(new ArrayResult($array), $this->em); |
|
|
|
|
30
|
|
|
|
31
|
|
|
$this->assertTrue(\is_countable($batchProcessor)); |
32
|
|
|
$this->assertCount(211, $batchProcessor); |
33
|
|
|
|
34
|
|
|
foreach ($batchProcessor as $item) { |
35
|
|
|
$this->em->persist(new ORMEntity($item)); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$entities = $this->em->getRepository(ORMEntity::class)->findAll(); |
39
|
|
|
|
40
|
|
|
$this->assertCount(211, $entities); |
41
|
|
|
$this->assertSame('value 32', $entities[31]->value); |
42
|
|
|
$this->assertSame('value 200', $entities[199]->value); |
43
|
|
|
$this->assertSame('value 211', $entities[210]->value); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @test |
48
|
|
|
*/ |
49
|
|
|
public function can_batch_persist_new_entities(): void |
50
|
|
|
{ |
51
|
|
|
$array = []; |
52
|
|
|
|
53
|
|
|
for ($i = 1; $i <= 211; ++$i) { |
54
|
|
|
$array[] = new ORMEntity('value '.$i); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$batchProcessor = new ORMCountableBatchProcessor(new ArrayResult($array), $this->em); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$this->assertTrue(\is_countable($batchProcessor)); |
60
|
|
|
$this->assertCount(211, $batchProcessor); |
61
|
|
|
|
62
|
|
|
foreach ($batchProcessor as $item) { |
63
|
|
|
$this->em->persist($item); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$entities = $this->em->getRepository(ORMEntity::class)->findAll(); |
67
|
|
|
|
68
|
|
|
$this->assertCount(211, $entities); |
69
|
|
|
$this->assertSame('value 32', $entities[31]->value); |
70
|
|
|
$this->assertSame('value 200', $entities[199]->value); |
71
|
|
|
$this->assertSame('value 211', $entities[210]->value); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @test |
76
|
|
|
*/ |
77
|
|
|
public function can_batch_persist_results_from_array(): void |
78
|
|
|
{ |
79
|
|
|
$array = []; |
80
|
|
|
|
81
|
|
|
for ($i = 1; $i <= 211; ++$i) { |
82
|
|
|
$array[] = 'value '.$i; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$batchProcessor = new ORMCountableBatchProcessor($array, $this->em); |
|
|
|
|
86
|
|
|
|
87
|
|
|
$this->assertTrue(\is_countable($batchProcessor)); |
88
|
|
|
$this->assertCount(211, $batchProcessor); |
89
|
|
|
|
90
|
|
|
foreach ($batchProcessor as $item) { |
91
|
|
|
$this->em->persist(new ORMEntity($item)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$entities = $this->em->getRepository(ORMEntity::class)->findAll(); |
95
|
|
|
|
96
|
|
|
$this->assertCount(211, $entities); |
97
|
|
|
$this->assertSame('value 32', $entities[31]->value); |
98
|
|
|
$this->assertSame('value 200', $entities[199]->value); |
99
|
|
|
$this->assertSame('value 211', $entities[210]->value); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @test |
104
|
|
|
*/ |
105
|
|
|
public function results_must_be_countable(): void |
106
|
|
|
{ |
107
|
|
|
$iterator = static function() { |
108
|
|
|
yield 'foo'; |
109
|
|
|
}; |
110
|
|
|
|
111
|
|
|
$this->expectException(\InvalidArgumentException::class); |
112
|
|
|
|
113
|
|
|
new ORMCountableBatchProcessor($iterator(), $this->em); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|