1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\TDBM\Utils\Annotation; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Annotations\AnnotationException; |
7
|
|
|
use Doctrine\DBAL\Schema\Column; |
8
|
|
|
use Doctrine\DBAL\Schema\Table; |
9
|
|
|
use Doctrine\DBAL\Types\Type; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use TheCodingMachine\TDBM\TDBMException; |
12
|
|
|
|
13
|
|
|
class AnnotationParserTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
public function testParse() |
16
|
|
|
{ |
17
|
|
|
$parser = new AnnotationParser([ |
18
|
|
|
'UUID' => UUID::class, |
19
|
|
|
'Autoincrement' => Autoincrement::class |
20
|
|
|
]); |
21
|
|
|
$column = new Column('foo', Type::getType(Type::STRING), ['comment'=>'@UUID']); |
22
|
|
|
$table = new Table('bar'); |
23
|
|
|
$annotations = $parser->getColumnAnnotations($column, $table); |
24
|
|
|
|
25
|
|
|
$annotation = $annotations->findAnnotation(UUID::class); |
26
|
|
|
$this->assertInstanceOf(UUID::class, $annotation); |
27
|
|
|
|
28
|
|
|
$annotationsArray = $annotations->getAnnotations(); |
29
|
|
|
$this->assertCount(1, $annotationsArray); |
30
|
|
|
$this->assertSame($annotation, $annotationsArray[0]); |
31
|
|
|
|
32
|
|
|
$annotation = $annotations->findAnnotation('not_exist'); |
33
|
|
|
$this->assertNull($annotation); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testParseMultiLine() |
37
|
|
|
{ |
38
|
|
|
$parser = new AnnotationParser([ |
39
|
|
|
'UUID' => UUID::class, |
40
|
|
|
'Autoincrement' => Autoincrement::class |
41
|
|
|
]); |
42
|
|
|
$column = new Column('foo', Type::getType(Type::STRING), ['comment'=>"\n@UUID"]); |
43
|
|
|
$table = new Table('bar'); |
44
|
|
|
$annotations = $parser->getColumnAnnotations($column, $table); |
45
|
|
|
|
46
|
|
|
$annotation = $annotations->findAnnotation(UUID::class); |
47
|
|
|
$this->assertInstanceOf(UUID::class, $annotation); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testParseMultiAnnotations() |
51
|
|
|
{ |
52
|
|
|
$parser = new AnnotationParser([ |
53
|
|
|
'UUID' => UUID::class, |
54
|
|
|
'Autoincrement' => Autoincrement::class |
55
|
|
|
]); |
56
|
|
|
$column = new Column('foo', Type::getType(Type::STRING), ['comment'=>"\n@UUID\n@Autoincrement"]); |
57
|
|
|
$table = new Table('bar'); |
58
|
|
|
$annotations = $parser->getColumnAnnotations($column, $table); |
59
|
|
|
|
60
|
|
|
$annotation = $annotations->findAnnotation(Autoincrement::class); |
61
|
|
|
$this->assertInstanceOf(Autoincrement::class, $annotation); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testException() |
65
|
|
|
{ |
66
|
|
|
$parser = new AnnotationParser([ |
67
|
|
|
'UUID' => UUID::class, |
68
|
|
|
'Autoincrement' => Autoincrement::class |
69
|
|
|
]); |
70
|
|
|
$table = new Table('bar', [], [], [], 0, ['comment'=>"@UUID\n@UUID"]); |
71
|
|
|
$annotations = $parser->getTableAnnotations($table); |
72
|
|
|
|
73
|
|
|
$this->expectException(TDBMException::class); |
74
|
|
|
$annotations->findAnnotation(UUID::class); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testParseParameters() |
78
|
|
|
{ |
79
|
|
|
$parser = new AnnotationParser([ |
80
|
|
|
'UUID' => UUID::class, |
81
|
|
|
'Autoincrement' => Autoincrement::class |
82
|
|
|
]); |
83
|
|
|
$table = new Table('bar', [], [], [], 0, ['comment'=>'@UUID("v4")']); |
84
|
|
|
$annotations = $parser->getTableAnnotations($table); |
85
|
|
|
|
86
|
|
|
$annotation = $annotations->findAnnotation(UUID::class); |
87
|
|
|
$this->assertSame('v4', $annotation->value); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testParseOldUUID() |
91
|
|
|
{ |
92
|
|
|
$parser = new AnnotationParser([ |
93
|
|
|
'UUID' => UUID::class, |
94
|
|
|
]); |
95
|
|
|
// First generation UUID did not use the Doctrine syntax. |
96
|
|
|
$table = new Table('bar', [], [], [], 0, ['comment'=>'@UUID v4']); |
97
|
|
|
$annotations = $parser->getTableAnnotations($table); |
98
|
|
|
|
99
|
|
|
$annotation = $annotations->findAnnotation(UUID::class); |
100
|
|
|
$this->assertSame('v4', $annotation->value); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|