ORMCountableBatchProcessorTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A can_batch_persist_results() 0 23 3
A can_batch_persist_new_entities() 0 23 3
A can_batch_persist_results_from_array() 0 23 3
A results_must_be_countable() 0 9 1
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);
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 ORMCountableBatchProcessor(new ArrayResult($array), /** @scrutinizer ignore-type */ $this->em);
Loading history...
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));
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

35
            $this->em->/** @scrutinizer ignore-call */ 
36
                       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...
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);
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

57
        $batchProcessor = new ORMCountableBatchProcessor(new ArrayResult($array), /** @scrutinizer ignore-type */ $this->em);
Loading history...
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);
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

85
        $batchProcessor = new ORMCountableBatchProcessor($array, /** @scrutinizer ignore-type */ $this->em);
Loading history...
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);
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

113
        new ORMCountableBatchProcessor($iterator(), /** @scrutinizer ignore-type */ $this->em);
Loading history...
114
    }
115
}
116