EntityManagerMockFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 28
c 1
b 0
f 0
dl 0
loc 56
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 21 1
A prepareQueryBuilder() 0 12 1
A prepareMetadata() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\Doctrine\Test\ORM;
13
14
use Closure;
15
use Doctrine\DBAL\Connection;
16
use Doctrine\ORM\AbstractQuery;
17
use Doctrine\ORM\EntityManager;
18
use Doctrine\ORM\EntityRepository;
19
use Doctrine\ORM\Mapping\ClassMetadataInfo;
20
use Doctrine\ORM\QueryBuilder;
21
use PHPUnit\Framework\MockObject\MockObject;
22
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by core23
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
24
final class EntityManagerMockFactory
25
{
26
    /**
27
     * @param string[] $fields
28
     *
29
     * @return EntityManager|MockObject
30
     */
31
    public static function create(TestCase $test, Closure $qbCallback, array $fields): MockObject
32
    {
33
        $qb = $test->getMockBuilder(QueryBuilder::class)->disableOriginalConstructor()->getMock();
34
35
        self::prepareQueryBuilder($test, $qb);
36
37
        $qbCallback($qb);
38
39
        $repository = $test->getMockBuilder(EntityRepository::class)->disableOriginalConstructor()->getMock();
40
        $repository->method('createQueryBuilder')->willReturn($qb);
41
42
        $metadata = self::prepareMetadata($test, $fields);
43
44
        $connection = $test->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
45
46
        $em = $test->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock();
47
        $em->method('getRepository')->willReturn($repository);
48
        $em->method('getClassMetadata')->willReturn($metadata);
49
        $em->method('getConnection')->willReturn($connection);
50
51
        return $em;
52
    }
53
54
    private static function prepareQueryBuilder(TestCase $test, MockObject $qb): void
55
    {
56
        $query = $test->getMockBuilder(AbstractQuery::class)
57
            ->disableOriginalConstructor()->getMock();
58
        $query->method('execute')->willReturn(true);
59
60
        $qb->method('select')->willReturn($qb);
61
        $qb->method('getQuery')->willReturn($query);
62
        $qb->method('where')->willReturn($qb);
63
        $qb->method('orderBy')->willReturn($qb);
64
        $qb->method('andWhere')->willReturn($qb);
65
        $qb->method('leftJoin')->willReturn($qb);
66
    }
67
68
    /**
69
     * @param string[] $fields
70
     */
71
    private static function prepareMetadata(TestCase $test, array $fields): MockObject
72
    {
73
        $metadata = $test->getMockBuilder(ClassMetadataInfo::class)->disableOriginalConstructor()->getMock();
74
        $metadata->method('getFieldNames')->willReturn($fields);
75
        $metadata->method('getName')->willReturn('className');
76
        $metadata->method('getIdentifier')->willReturn(['id']);
77
        $metadata->method('getTableName')->willReturn('dummy');
78
79
        return $metadata;
80
    }
81
}
82