Completed
Pull Request — master (#812)
by
unknown
05:20 queued 01:45
created

TypeGuesserTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 175
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testGuessTypeNoMetadata() 0 12 1
A testGuessTypeWithAssociation() 0 18 1
A associationData() 0 21 1
A testGuessTypeNoAssociation() 0 17 1
B noAssociationData() 0 85 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrineORMAdminBundle\Tests\Guesser;
15
16
use Doctrine\ORM\Mapping\ClassMetadata;
17
use Doctrine\ORM\Mapping\MappingException;
18
use PHPUnit\Framework\TestCase;
19
use Sonata\DoctrineORMAdminBundle\Guesser\TypeGuesser;
20
use Sonata\DoctrineORMAdminBundle\Model\ModelManager;
21
use Symfony\Component\Form\Guess\Guess;
22
23
/**
24
 * @author Marko Kunic <[email protected]>
25
 */
26
class TypeGuesserTest extends TestCase
27
{
28
    private $modelManager;
29
    private $guesser;
30
31
    public function setUp(): void
32
    {
33
        $this->modelManager = $this->prophesize(ModelManager::class);
34
        $this->guesser = new TypeGuesser();
35
    }
36
37
    public function testGuessTypeNoMetadata(): void
38
    {
39
        $this->modelManager->getParentMetadataForProperty(
40
            $class = 'FakeClass',
41
            $property = 'fakeProperty'
42
        )->willThrow(MappingException::class);
43
44
        $result = $this->guesser->guessType($class, $property, $this->modelManager->reveal());
45
46
        $this->assertSame('text', $result->getType());
47
        $this->assertSame(Guess::LOW_CONFIDENCE, $result->getConfidence());
48
    }
49
50
    /**
51
     * @dataProvider associationData
52
     */
53
    public function testGuessTypeWithAssociation($mappingType, $type): void
54
    {
55
        $classMetadata = $this->prophesize(ClassMetadata::class);
56
57
        $classMetadata->hasAssociation($property = 'fakeProperty')->willReturn(true);
58
        $classMetadata->getAssociationMapping($property)
59
            ->willReturn(['type' => $mappingType]);
60
61
        $this->modelManager->getParentMetadataForProperty(
62
            $class = 'FakeClass',
63
            $property
64
        )->willReturn([$classMetadata, $property, 'notUsed']);
65
66
        $result = $this->guesser->guessType($class, $property, $this->modelManager->reveal());
67
68
        $this->assertSame($type, $result->getType());
69
        $this->assertSame(Guess::HIGH_CONFIDENCE, $result->getConfidence());
70
    }
71
72
    public function associationData(): array
73
    {
74
        return [
75
            'many-to-one' => [
76
                ClassMetadata::MANY_TO_ONE,
77
                'orm_many_to_one',
78
            ],
79
            'one-to-many' => [
80
                ClassMetadata::ONE_TO_MANY,
81
                'orm_one_to_many',
82
            ],
83
            'one-to-one' => [
84
                ClassMetadata::ONE_TO_ONE,
85
                'orm_one_to_one',
86
            ],
87
            'many-to-many' => [
88
                ClassMetadata::MANY_TO_MANY,
89
                'orm_many_to_many',
90
            ],
91
        ];
92
    }
93
94
    /**
95
     * @dataProvider noAssociationData
96
     */
97
    public function testGuessTypeNoAssociation($type, $resultType, $confidence): void
98
    {
99
        $classMetadata = $this->prophesize(ClassMetadata::class);
100
101
        $classMetadata->hasAssociation($property = 'fakeProperty')->willReturn(false);
102
        $classMetadata->getTypeOfField($property)->willReturn($type);
103
104
        $this->modelManager->getParentMetadataForProperty(
105
            $class = 'FakeClass',
106
            $property
107
        )->willReturn([$classMetadata, $property, 'notUsed']);
108
109
        $result = $this->guesser->guessType($class, $property, $this->modelManager->reveal());
110
111
        $this->assertSame($resultType, $result->getType());
112
        $this->assertSame($confidence, $result->getConfidence());
113
    }
114
115
    public function noAssociationData(): array
116
    {
117
        return [
118
            'array' => [
119
                $array = 'array',
120
                $array,
121
                Guess::HIGH_CONFIDENCE,
122
            ],
123
            'json' => [
124
                'json',
125
                $array,
126
                Guess::HIGH_CONFIDENCE,
127
            ],
128
            'boolean' => [
129
                $boolean = 'boolean',
130
                $boolean,
131
                Guess::HIGH_CONFIDENCE,
132
            ],
133
            'datetime' => [
134
                $datetime = 'datetime',
135
                $datetime,
136
                Guess::HIGH_CONFIDENCE,
137
            ],
138
            'vardatetime' => [
139
                'vardatetime',
140
                $datetime,
141
                Guess::HIGH_CONFIDENCE,
142
            ],
143
            'datetimetz' => [
144
                'datetimetz',
145
                $datetime,
146
                Guess::HIGH_CONFIDENCE,
147
            ],
148
            'date' => [
149
                $date = 'date',
150
                $date,
151
                Guess::HIGH_CONFIDENCE,
152
            ],
153
            'decimal' => [
154
                'decimal',
155
                $number = 'number',
156
                Guess::MEDIUM_CONFIDENCE,
157
            ],
158
            'float' => [
159
                'float',
160
                $number,
161
                Guess::MEDIUM_CONFIDENCE,
162
            ],
163
            'integer' => [
164
                $integer = 'integer',
165
                $integer,
166
                Guess::MEDIUM_CONFIDENCE,
167
            ],
168
            'bigint' => [
169
                'bigint',
170
                $integer,
171
                Guess::MEDIUM_CONFIDENCE,
172
            ],
173
            'smallint' => [
174
                'smallint',
175
                $integer,
176
                Guess::MEDIUM_CONFIDENCE,
177
            ],
178
            'string' => [
179
                'string',
180
                $text = 'text',
181
                Guess::MEDIUM_CONFIDENCE,
182
            ],
183
            'text' => [
184
                'text',
185
                'textarea',
186
                Guess::MEDIUM_CONFIDENCE,
187
            ],
188
            'time' => [
189
                $time = 'time',
190
                $time,
191
                Guess::HIGH_CONFIDENCE,
192
            ],
193
            'somefake' => [
194
                'somefake',
195
                $text,
196
                Guess::LOW_CONFIDENCE,
197
            ],
198
        ];
199
    }
200
}
201