Passed
Push — master ( 46e57d...ec86a4 )
by Christian
01:49
created

EntityManagerMockFactory::create()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 28
nc 2
nop 3
1
<?php
2
3
/*
4
 * (c) Christian Gripp <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Core23\DoctrineExtensions\Test\ORM;
11
12
use Doctrine\DBAL\Connection;
13
use Doctrine\ORM\AbstractQuery;
14
use Doctrine\ORM\EntityManager;
15
use Doctrine\ORM\EntityManagerInterface;
16
use Doctrine\ORM\EntityRepository;
17
use Doctrine\ORM\Mapping\ClassMetadataInfo;
18
use Doctrine\ORM\QueryBuilder;
19
use Doctrine\ORM\Version;
20
21
final class EntityManagerMockFactory
22
{
23
    /**
24
     * @param \PHPUnit_Framework_TestCase $test
25
     * @param \Closure                    $qbCallback
26
     * @param mixed                       $fields
27
     *
28
     * @return \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface
29
     */
30
    public static function create(\PHPUnit_Framework_TestCase $test, \Closure $qbCallback, $fields): \PHPUnit_Framework_MockObject_MockObject
31
    {
32
        $query = $test->getMockBuilder(AbstractQuery::class)
33
            ->disableOriginalConstructor()->getMock();
34
        $query->expects($test->any())->method('execute')->will($test->returnValue(true));
35
36
        if (Version::compare('2.5.0') < 1) {
37
            $entityManager = $test->getMockBuilder(EntityManagerInterface::class)->getMock();
38
            $qb            = $test->getMockBuilder(QueryBuilder::class)->setConstructorArgs(array($entityManager))->getMock();
39
        } else {
40
            $qb = $test->getMockBuilder(QueryBuilder::class)->disableOriginalConstructor()->getMock();
41
        }
42
43
        $qb->expects($test->any())->method('select')->will($test->returnValue($qb));
44
        $qb->expects($test->any())->method('getQuery')->will($test->returnValue($query));
45
        $qb->expects($test->any())->method('where')->will($test->returnValue($qb));
46
        $qb->expects($test->any())->method('orderBy')->will($test->returnValue($qb));
47
        $qb->expects($test->any())->method('andWhere')->will($test->returnValue($qb));
48
        $qb->expects($test->any())->method('leftJoin')->will($test->returnValue($qb));
49
50
        $qbCallback($qb);
51
52
        $repository = $test->getMockBuilder(EntityRepository::class)->disableOriginalConstructor()->getMock();
53
        $repository->expects($test->any())->method('createQueryBuilder')->will($test->returnValue($qb));
54
55
        $metadata = $test->getMockBuilder(ClassMetadataInfo::class)->disableOriginalConstructor()->getMock();
56
        $metadata->expects($test->any())->method('getFieldNames')->will($test->returnValue($fields));
57
        $metadata->expects($test->any())->method('getName')->will($test->returnValue('className'));
58
        $metadata->expects($test->any())->method('getIdentifier')->will($test->returnValue('id'));
59
        $metadata->expects($test->any())->method('getTableName')->will($test->returnValue('dummy'));
60
61
        $connection = $test->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
62
63
        $em = $test->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock();
64
        $em->expects($test->any())->method('getRepository')->will($test->returnValue($repository));
65
        $em->expects($test->any())->method('getClassMetadata')->will($test->returnValue($metadata));
66
        $em->expects($test->any())->method('getConnection')->will($test->returnValue($connection));
67
68
        return $em;
69
    }
70
}
71