Completed
Push — 3.x ( b582dd...8b3829 )
by Jordi Sala
01:51
created

TypeGuesserTest::testGuessTypeNoMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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