Issues (40)

tests/Doctrine/ORMQueryResultExtraFieldsTest.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Zenstruck\Porpaginas\Tests\Doctrine;
4
5
use Zenstruck\Porpaginas\Doctrine\ORMQueryResult;
6
use Zenstruck\Porpaginas\Result;
7
use Zenstruck\Porpaginas\Tests\Doctrine\Fixtures\ORMEntity;
8
use Zenstruck\Porpaginas\Tests\ResultTest;
9
10
class ORMQueryResultExtraFieldsTest extends ResultTest
11
{
12
    use HasEntityManager;
13
14
    /**
15
     * @test
16
     */
17
    public function detaches_entity_from_em_on_iterate(): void
18
    {
19
        $result = \iterator_to_array($this->createResultWithItems(2))[0][0];
20
21
        $this->assertFalse($this->em->contains($result));
0 ignored issues
show
The method contains() 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

21
        $this->assertFalse($this->em->/** @scrutinizer ignore-call */ contains($result));

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...
22
    }
23
24
    /**
25
     * @test
26
     */
27
    public function can_batch_update_results(): void
28
    {
29
        $result = $this->createResultWithItems(2);
30
        $values = \array_map(static function(array $row) { return $row[0]->value; }, \iterator_to_array($result));
31
32
        $this->assertSame(['value 1', 'value 2'], $values);
33
34
        $batchIterator = $result->batchProcessor();
35
36
        $this->assertCount(2, $batchIterator);
37
38
        foreach ($batchIterator as $row) {
39
            $row[0]->value = 'new '.$row[0]->value;
40
        }
41
42
        $values = \array_map(
43
            static function(ORMEntity $entity) { return $entity->value; },
44
            $this->em->getRepository(ORMEntity::class)->findAll()
45
        );
46
47
        $this->assertSame(['new value 1', 'new value 2'], $values);
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function can_batch_delete_results(): void
54
    {
55
        $result = $this->createResultWithItems(2);
56
57
        $this->assertCount(2, $result);
58
59
        $batchIterator = $result->batchProcessor();
60
61
        $this->assertCount(2, $batchIterator);
62
63
        foreach ($batchIterator as $item) {
64
            $this->em->remove($item[0]);
65
        }
66
67
        $this->assertCount(0, $this->em->getRepository(ORMEntity::class)->findAll());
68
    }
69
70
    protected function createResultWithItems(int $count): Result
71
    {
72
        $this->persistEntities($count);
73
74
        $query = $this->em->createQuery(\sprintf('SELECT e, UPPER(e.value) AS extra FROM %s e', ORMEntity::class));
75
76
        return new ORMQueryResult($query);
77
    }
78
79
    protected function getExpectedValueAtPosition(int $position)
80
    {
81
        $value = 'value '.$position;
82
83
        return [
84
            0 => new ORMEntity($value, $position),
85
            'extra' => \mb_strtoupper($value),
86
        ];
87
    }
88
}
89