Passed
Pull Request — master (#96)
by David
02:58
created

AnnotationParserTest::testParse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 20
rs 9.8333
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->parse('@UUID', '');
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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 testException()
37
    {
38
        $parser = new AnnotationParser([
39
            'UUID' => UUID::class,
40
            'Autoincrement' => Autoincrement::class
41
        ]);
42
        $table = new Table('bar', [], [], [], 0, ['comment'=>"@UUID\n@UUID"]);
43
        $annotations = $parser->getTableAnnotations($table);
44
45
        $this->expectException(TDBMException::class);
46
        $annotations->findAnnotation(UUID::class);
47
    }
48
49
    public function testParseParameters()
50
    {
51
        $parser = new AnnotationParser([
52
            'UUID' => UUID::class,
53
            'Autoincrement' => Autoincrement::class
54
        ]);
55
        $table = new Table('bar', [], [], [], 0, ['comment'=>'@UUID("v4")']);
56
        $annotations = $parser->getTableAnnotations($table);
57
58
        $annotation = $annotations->findAnnotation(UUID::class);
59
        $this->assertSame('v4', $annotation->value);
60
    }
61
62
    public function testParseOldUUID()
63
    {
64
        $parser = new AnnotationParser([
65
            'UUID' => UUID::class,
66
        ]);
67
        // First generation UUID did not use the Doctrine syntax.
68
        $table = new Table('bar', [], [], [], 0, ['comment'=>'@UUID v4']);
69
        $annotations = $parser->getTableAnnotations($table);
70
71
        $annotation = $annotations->findAnnotation(UUID::class);
72
        $this->assertSame('v4', $annotation->value);
73
    }
74
}
75