Passed
Push — master ( d70c89...2d756e )
by Christian
01:55
created

EntityManagerMockFactory::prepareMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 TestCase $test
28
     * @param Closure  $qbCallback
29
     * @param mixed    $fields
30
     *
31
     * @return EntityManager|MockObject
32
     */
33
    public static function create(TestCase $test, Closure $qbCallback, $fields): MockObject
34
    {
35
        $qb = $test->getMockBuilder(QueryBuilder::class)->disableOriginalConstructor()->getMock();
36
37
        self::prepareQueryBuilder($test, $qb);
38
39
        $qbCallback($qb);
40
41
        $repository = $test->getMockBuilder(EntityRepository::class)->disableOriginalConstructor()->getMock();
42
        $repository->method('createQueryBuilder')->willReturn($qb);
43
44
        $metadata = self::prepareMetadata($test, $fields);
45
46
        $connection = $test->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
47
48
        $em = $test->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock();
49
        $em->method('getRepository')->willReturn($repository);
50
        $em->method('getClassMetadata')->willReturn($metadata);
51
        $em->method('getConnection')->willReturn($connection);
52
53
        return $em;
54
    }
55
56
    /**
57
     * @param TestCase   $test
58
     * @param MockObject $qb
59
     */
60
    private static function prepareQueryBuilder(TestCase $test, MockObject $qb): void
61
    {
62
        $query = $test->getMockBuilder(AbstractQuery::class)
63
            ->disableOriginalConstructor()->getMock();
64
        $query->method('execute')->willReturn(true);
65
66
        $qb->method('select')->willReturn($qb);
67
        $qb->method('getQuery')->willReturn($query);
68
        $qb->method('where')->willReturn($qb);
69
        $qb->method('orderBy')->willReturn($qb);
70
        $qb->method('andWhere')->willReturn($qb);
71
        $qb->method('leftJoin')->willReturn($qb);
72
    }
73
74
    /**
75
     * @param TestCase $test
76
     * @param mixed    $fields
77
     *
78
     * @return MockObject
79
     */
80
    private static function prepareMetadata(TestCase $test, $fields): MockObject
81
    {
82
        $metadata = $test->getMockBuilder(ClassMetadataInfo::class)->disableOriginalConstructor()->getMock();
83
        $metadata->method('getFieldNames')->willReturn($fields);
84
        $metadata->method('getName')->willReturn('className');
85
        $metadata->method('getIdentifier')->willReturn(['id']);
86
        $metadata->method('getTableName')->willReturn('dummy');
87
88
        return $metadata;
89
    }
90
}
91