Passed
Pull Request — 2.7 (#7560)
by Asmir
06:56
created

testHydrateSingleScalarResultCreatesCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 17
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Hydration;
4
5
use Doctrine\Common\Cache\ArrayCache;
6
use Doctrine\DBAL\Cache\ResultCacheStatement;
7
use Doctrine\ORM\Internal\Hydration\SingleScalarHydrator;
8
use Doctrine\ORM\NonUniqueResultException;
9
use Doctrine\Tests\Mocks\HydratorMockStatement;
10
use Doctrine\ORM\Query\ResultSetMapping;
11
use Doctrine\Tests\Models\CMS\CmsUser;
12
13
class SingleScalarHydratorTest extends HydrationTestCase
14
{
15
    /** Result set provider for the HYDRATE_SINGLE_SCALAR tests */
16
    public static function singleScalarResultSetProvider(): array
17
    {
18
        return [
19
            // valid
20
            'valid' => [
21
                'name'      => 'result1',
22
                'resultSet' => [
23
                    [
24
                        'u__name' => 'romanb',
25
                    ],
26
                ],
27
            ],
28
            // valid
29
            [
30
                'name'      => 'result2',
31
                'resultSet' => [
32
                    [
33
                        'u__id' => '1',
34
                    ],
35
                ],
36
            ],
37
            // invalid
38
            [
39
                'name'      => 'result3',
40
                'resultSet' => [
41
                    [
42
                        'u__id'   => '1',
43
                        'u__name' => 'romanb',
44
                    ],
45
                ],
46
            ],
47
            // invalid
48
            [
49
                'name'      => 'result4',
50
                'resultSet' => [
51
                    [
52
                        'u__id' => '1',
53
                    ],
54
                    [
55
                        'u__id' => '2',
56
                    ],
57
                ],
58
            ],
59
        ];
60
    }
61
62
    /**
63
     * select u.name from CmsUser u where u.id = 1
64
     *
65
     * @dataProvider singleScalarResultSetProvider
66
     */
67
    public function testHydrateSingleScalar($name, $resultSet)
68
    {
69
        $rsm = new ResultSetMapping;
70
        $rsm->addEntityResult(CmsUser::class, 'u');
71
        $rsm->addFieldResult('u', 'u__id', 'id');
72
        $rsm->addFieldResult('u', 'u__name', 'name');
73
74
        $stmt = new HydratorMockStatement($resultSet);
75
        $hydrator = new SingleScalarHydrator($this->_em);
76
77
        if ($name === 'result1') {
78
            $result = $hydrator->hydrateAll($stmt, $rsm);
79
            $this->assertEquals('romanb', $result);
80
            return;
81
        }
82
83
        if ($name === 'result2') {
84
            $result = $hydrator->hydrateAll($stmt, $rsm);
85
            $this->assertEquals(1, $result);
86
87
            return;
88
        }
89
90
        if (in_array($name, ['result3', 'result4'], true)) {
91
            $this->expectException(NonUniqueResultException::class);
92
            $hydrator->hydrateAll($stmt, $rsm);
93
        }
94
    }
95
96
    public function testHydrateSingleScalarResultCreatesCache()
97
    {
98
        $rsm = new ResultSetMapping;
99
        $rsm->addEntityResult(CmsUser::class, 'u');
100
        $rsm->addFieldResult('u', 'u__id', 'id');
101
        $rsm->addFieldResult('u', 'u__name', 'name');
102
103
        $cache    = new ArrayCache();
104
        $rows     = [['u__name' => 'romanb']];
105
        $stmt     = new HydratorMockStatement($rows);
106
107
        $stmt     = new ResultCacheStatement($stmt, $cache, 'foo', 'bar', 30);
108
        $hydrator = new SingleScalarHydrator($this->_em);
109
110
        $result = $hydrator->hydrateAll($stmt, $rsm);
111
        $this->assertEquals('romanb', $result);
112
        $this->assertEquals(['bar' => $rows], $cache->fetch('foo'));
113
    }
114
}
115