Completed
Push — master ( ac4032...1b5ffa )
by David
11s
created

AnnotationParserTest::testParseComments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
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 TheCodingMachine\TDBM\TDBMException;
11
12
class AnnotationParserTest extends \PHPUnit_Framework_TestCase
13
{
14
    public function testParse()
15
    {
16
        $parser = new AnnotationParser([
17
            'UUID' => UUID::class,
18
            'Autoincrement' => Autoincrement::class
19
        ]);
20
        $column = new Column('foo', Type::getType(Type::STRING), ['comment'=>'@UUID']);
21
        $table = new Table('bar');
22
        $annotations = $parser->getColumnAnnotations($column, $table);
23
24
        $annotation = $annotations->findAnnotation(UUID::class);
25
        $this->assertInstanceOf(UUID::class, $annotation);
26
27
        $annotationsArray = $annotations->getAnnotations();
28
        $this->assertCount(1, $annotationsArray);
29
        $this->assertSame($annotation, $annotationsArray[0]);
30
31
        $annotation = $annotations->findAnnotation('not_exist');
32
        $this->assertNull($annotation);
33
    }
34
35
    public function testException()
36
    {
37
        $parser = new AnnotationParser([
38
            'UUID' => UUID::class,
39
            'Autoincrement' => Autoincrement::class
40
        ]);
41
        $table = new Table('bar', [], [], [], 0, ['comment'=>"@UUID\n@UUID"]);
42
        $annotations = $parser->getTableAnnotations($table);
43
44
        $this->expectException(TDBMException::class);
45
        $annotations->findAnnotation(UUID::class);
46
    }
47
48
    public function testParseParameters()
49
    {
50
        $parser = new AnnotationParser([
51
            'UUID' => UUID::class,
52
            'Autoincrement' => Autoincrement::class
53
        ]);
54
        $table = new Table('bar', [], [], [], 0, ['comment'=>'@UUID("v4")']);
55
        $annotations = $parser->getTableAnnotations($table);
56
57
        $annotation = $annotations->findAnnotation(UUID::class);
58
        $this->assertSame('v4', $annotation->value);
59
    }
60
61
    public function testParseOldUUID()
62
    {
63
        $parser = new AnnotationParser([
64
            'UUID' => UUID::class,
65
        ]);
66
        // First generation UUID did not use the Doctrine syntax.
67
        $table = new Table('bar', [], [], [], 0, ['comment'=>'@UUID v4']);
68
        $annotations = $parser->getTableAnnotations($table);
69
70
        $annotation = $annotations->findAnnotation(UUID::class);
71
        $this->assertSame('v4', $annotation->value);
72
    }
73
}
74