Passed
Pull Request — master (#116)
by David
09:20 queued 05:53
created

AnnotationParserTest::testParseParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 testException()
51
    {
52
        $parser = new AnnotationParser([
53
            'UUID' => UUID::class,
54
            'Autoincrement' => Autoincrement::class
55
        ]);
56
        $table = new Table('bar', [], [], [], 0, ['comment'=>"@UUID\n@UUID"]);
57
        $annotations = $parser->getTableAnnotations($table);
58
59
        $this->expectException(TDBMException::class);
60
        $annotations->findAnnotation(UUID::class);
61
    }
62
63
    public function testParseParameters()
64
    {
65
        $parser = new AnnotationParser([
66
            'UUID' => UUID::class,
67
            'Autoincrement' => Autoincrement::class
68
        ]);
69
        $table = new Table('bar', [], [], [], 0, ['comment'=>'@UUID("v4")']);
70
        $annotations = $parser->getTableAnnotations($table);
71
72
        $annotation = $annotations->findAnnotation(UUID::class);
73
        $this->assertSame('v4', $annotation->value);
74
    }
75
76
    public function testParseOldUUID()
77
    {
78
        $parser = new AnnotationParser([
79
            'UUID' => UUID::class,
80
        ]);
81
        // First generation UUID did not use the Doctrine syntax.
82
        $table = new Table('bar', [], [], [], 0, ['comment'=>'@UUID v4']);
83
        $annotations = $parser->getTableAnnotations($table);
84
85
        $annotation = $annotations->findAnnotation(UUID::class);
86
        $this->assertSame('v4', $annotation->value);
87
    }
88
}
89