ORMBatchProcessorTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 116
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A can_batch_persist_array_of_arrays() 0 20 3
A results_do_not_have_to_be_countable() 0 9 1
A can_batch_persist_new_entities() 0 20 3
A can_batch_persist_results() 0 20 3
A can_batch_persist_results_from_array() 0 20 3
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);
0 ignored issues
show
Bug introduced by
It seems like $this->em can also be of type null; however, parameter $em of Zenstruck\Porpaginas\Doc...rocessor::__construct() does only seem to accept Doctrine\ORM\EntityManagerInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
        $batchProcessor = new ORMBatchProcessor(new ArrayResult($array), /** @scrutinizer ignore-type */ $this->em);
Loading history...
30
31
        foreach ($batchProcessor as $item) {
32
            $this->em->persist(new ORMEntity($item));
0 ignored issues
show
Bug introduced by
The method persist() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
            $this->em->/** @scrutinizer ignore-call */ 
33
                       persist(new ORMEntity($item));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $this->em can also be of type null; however, parameter $em of Zenstruck\Porpaginas\Doc...rocessor::__construct() does only seem to accept Doctrine\ORM\EntityManagerInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        $batchProcessor = new ORMBatchProcessor(new ArrayResult($array), /** @scrutinizer ignore-type */ $this->em);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $this->em can also be of type null; however, parameter $em of Zenstruck\Porpaginas\Doc...rocessor::__construct() does only seem to accept Doctrine\ORM\EntityManagerInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        $batchProcessor = new ORMBatchProcessor($array, /** @scrutinizer ignore-type */ $this->em);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $this->em can also be of type null; however, parameter $em of Zenstruck\Porpaginas\Doc...rocessor::__construct() does only seem to accept Doctrine\ORM\EntityManagerInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
        $batchProcessor = new ORMBatchProcessor($array, /** @scrutinizer ignore-type */ $this->em);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $this->em can also be of type null; however, parameter $em of Zenstruck\Porpaginas\Doc...rocessor::__construct() does only seem to accept Doctrine\ORM\EntityManagerInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

126
        $batchProcessor = new ORMBatchProcessor($iterator(), /** @scrutinizer ignore-type */ $this->em);
Loading history...
127
128
        $this->assertFalse(\is_countable($batchProcessor));
129
        $this->assertSame(['foo'], \iterator_to_array($batchProcessor));
130
    }
131
}
132