Completed
Push — master ( f1bf04...e7856f )
by Marco
10:04
created

EntityManagerTest::testCreateQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM;
4
5
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
6
use Doctrine\ORM\Configuration;
7
use Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\ORMException;
9
use Doctrine\ORM\ORMInvalidArgumentException;
10
use Doctrine\ORM\Query\ResultSetMapping;
11
use Doctrine\Tests\OrmTestCase;
12
13
class EntityManagerTest extends OrmTestCase
14
{
15
    private $_em;
16
17
    function setUp()
18
    {
19
        parent::setUp();
20
        $this->_em = $this->_getTestEntityManager();
21
    }
22
23
    /**
24
     * @group DDC-899
25
     */
26
    public function testIsOpen()
27
    {
28
        $this->assertTrue($this->_em->isOpen());
29
        $this->_em->close();
30
        $this->assertFalse($this->_em->isOpen());
31
    }
32
33
    public function testGetConnection()
34
    {
35
        $this->assertInstanceOf('Doctrine\DBAL\Connection', $this->_em->getConnection());
36
    }
37
38
    public function testGetMetadataFactory()
39
    {
40
        $this->assertInstanceOf('Doctrine\ORM\Mapping\ClassMetadataFactory', $this->_em->getMetadataFactory());
41
    }
42
43
    public function testGetConfiguration()
44
    {
45
        $this->assertInstanceOf('Doctrine\ORM\Configuration', $this->_em->getConfiguration());
46
    }
47
48
    public function testGetUnitOfWork()
49
    {
50
        $this->assertInstanceOf('Doctrine\ORM\UnitOfWork', $this->_em->getUnitOfWork());
51
    }
52
53
    public function testGetProxyFactory()
54
    {
55
        $this->assertInstanceOf('Doctrine\ORM\Proxy\ProxyFactory', $this->_em->getProxyFactory());
56
    }
57
58
    public function testGetEventManager()
59
    {
60
        $this->assertInstanceOf('Doctrine\Common\EventManager', $this->_em->getEventManager());
61
    }
62
63
    public function testCreateNativeQuery()
64
    {
65
        $rsm = new ResultSetMapping();
66
        $query = $this->_em->createNativeQuery('SELECT foo', $rsm);
67
68
        $this->assertSame('SELECT foo', $query->getSql());
69
    }
70
71
    /**
72
     * @covers Doctrine\ORM\EntityManager::createNamedNativeQuery
73
     */
74
    public function testCreateNamedNativeQuery()
75
    {
76
        $rsm = new ResultSetMapping();
77
        $this->_em->getConfiguration()->addNamedNativeQuery('foo', 'SELECT foo', $rsm);
78
79
        $query = $this->_em->createNamedNativeQuery('foo');
80
81
        $this->assertInstanceOf('Doctrine\ORM\NativeQuery', $query);
82
    }
83
84
    public function testCreateQueryBuilder()
85
    {
86
        $this->assertInstanceOf('Doctrine\ORM\QueryBuilder', $this->_em->createQueryBuilder());
87
    }
88
89
    public function testCreateQueryBuilderAliasValid()
90
    {
91
        $q = $this->_em->createQueryBuilder()
92
             ->select('u')->from('Doctrine\Tests\Models\CMS\CmsUser', 'u');
93
        $q2 = clone $q;
94
95
        $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q->getQuery()->getDql());
96
        $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q2->getQuery()->getDql());
97
98
        $q3 = clone $q;
99
100
        $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q3->getQuery()->getDql());
101
    }
102
103
    public function testCreateQuery_DqlIsOptional()
104
    {
105
        $this->assertInstanceOf('Doctrine\ORM\Query', $this->_em->createQuery());
106
    }
107
108
    public function testGetPartialReference()
109
    {
110
        $user = $this->_em->getPartialReference('Doctrine\Tests\Models\CMS\CmsUser', 42);
111
        $this->assertTrue($this->_em->contains($user));
112
        $this->assertEquals(42, $user->id);
113
        $this->assertNull($user->getName());
114
    }
115
116
    public function testCreateQuery()
117
    {
118
        $q = $this->_em->createQuery('SELECT 1');
119
        $this->assertInstanceOf('Doctrine\ORM\Query', $q);
120
        $this->assertEquals('SELECT 1', $q->getDql());
121
    }
122
123
    /**
124
     * @covers Doctrine\ORM\EntityManager::createNamedQuery
125
     */
126
    public function testCreateNamedQuery()
127
    {
128
        $this->_em->getConfiguration()->addNamedQuery('foo', 'SELECT 1');
129
130
        $query = $this->_em->createNamedQuery('foo');
131
        $this->assertInstanceOf('Doctrine\ORM\Query', $query);
132
        $this->assertEquals('SELECT 1', $query->getDql());
133
    }
134
135
    static public function dataMethodsAffectedByNoObjectArguments()
136
    {
137
        return array(
138
            array('persist'),
139
            array('remove'),
140
            array('merge'),
141
            array('refresh'),
142
            array('detach')
143
        );
144
    }
145
146
    /**
147
     * @dataProvider dataMethodsAffectedByNoObjectArguments
148
     */
149
    public function testThrowsExceptionOnNonObjectValues($methodName) {
150
        $this->expectException(ORMInvalidArgumentException::class);
151
        $this->expectExceptionMessage('EntityManager#' . $methodName . '() expects parameter 1 to be an entity object, NULL given.');
152
153
        $this->_em->$methodName(null);
154
    }
155
156
    static public function dataAffectedByErrorIfClosedException()
157
    {
158
        return array(
159
            array('flush'),
160
            array('persist'),
161
            array('remove'),
162
            array('merge'),
163
            array('refresh'),
164
        );
165
    }
166
167
    /**
168
     * @dataProvider dataAffectedByErrorIfClosedException
169
     * @param string $methodName
170
     */
171
    public function testAffectedByErrorIfClosedException($methodName)
172
    {
173
        $this->expectException(ORMException::class);
174
        $this->expectExceptionMessage('closed');
175
176
        $this->_em->close();
177
        $this->_em->$methodName(new \stdClass());
178
    }
179
180
    /**
181
     * @group DDC-1125
182
     */
183
    public function testTransactionalAcceptsReturn()
184
    {
185
        $return = $this->_em->transactional(function ($em) {
186
            return 'foo';
187
        });
188
189
        $this->assertEquals('foo', $return);
190
    }
191
192
    public function testTransactionalAcceptsVariousCallables()
193
    {
194
        $this->assertSame('callback', $this->_em->transactional(array($this, 'transactionalCallback')));
195
    }
196
197
    public function testTransactionalThrowsInvalidArgumentExceptionIfNonCallablePassed()
198
    {
199
        $this->expectException(\InvalidArgumentException::class);
200
        $this->expectExceptionMessage('Expected argument of type "callable", got "object"');
201
202
        $this->_em->transactional($this);
203
    }
204
205
    public function transactionalCallback($em)
206
    {
207
        $this->assertSame($this->_em, $em);
208
        return 'callback';
209
    }
210
211
    public function testCreateInvalidConnection()
212
    {
213
        $this->expectException(\InvalidArgumentException::class);
214
        $this->expectExceptionMessage('Invalid $connection argument of type integer given: "1".');
215
216
        $config = new Configuration();
217
        $config->setMetadataDriverImpl($this->createMock(MappingDriver::class));
218
        EntityManager::create(1, $config);
219
    }
220
}
221