testLaxModeWithBadAnnotation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace TheCodingMachine\GraphQL\Controllers;
4
5
use Doctrine\Common\Annotations\AnnotationException;
6
use InvalidArgumentException;
7
use PHPUnit\Framework\TestCase;
8
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
9
use ReflectionClass;
10
use ReflectionMethod;
11
use TheCodingMachine\GraphQL\Controllers\Annotations\Exceptions\ClassNotFoundException;
12
use TheCodingMachine\GraphQL\Controllers\Annotations\Field;
13
use TheCodingMachine\GraphQL\Controllers\Annotations\Type;
14
use TheCodingMachine\GraphQL\Controllers\Fixtures\Annotations\ClassWithInvalidClassAnnotation;
15
use TheCodingMachine\GraphQL\Controllers\Fixtures\Annotations\ClassWithInvalidExtendTypeAnnotation;
16
use TheCodingMachine\GraphQL\Controllers\Fixtures\Annotations\ClassWithInvalidTypeAnnotation;
17
18
class AnnotationReaderTest extends TestCase
19
{
20
    public function testBadConstructor()
21
    {
22
        $this->expectException(InvalidArgumentException::class);
23
        new AnnotationReader(new DoctrineAnnotationReader(), 'foo');
24
    }
25
26
    public function testStrictMode()
27
    {
28
        $annotationReader = new AnnotationReader(new DoctrineAnnotationReader(), AnnotationReader::STRICT_MODE, []);
29
30
        $this->expectException(AnnotationException::class);
31
        $annotationReader->getTypeAnnotation(new ReflectionClass(ClassWithInvalidClassAnnotation::class));
32
    }
33
34
    public function testLaxModeWithBadAnnotation()
35
    {
36
        $annotationReader = new AnnotationReader(new DoctrineAnnotationReader(), AnnotationReader::LAX_MODE, []);
37
38
        $type = $annotationReader->getTypeAnnotation(new ReflectionClass(ClassWithInvalidClassAnnotation::class));
39
        $this->assertNull($type);
40
    }
41
42
    public function testLaxModeWithSmellyAnnotation()
43
    {
44
        $annotationReader = new AnnotationReader(new DoctrineAnnotationReader(), AnnotationReader::LAX_MODE, []);
45
46
        $this->expectException(AnnotationException::class);
47
        $annotationReader->getTypeAnnotation(new ReflectionClass(ClassWithInvalidTypeAnnotation::class));
48
    }
49
50
    public function testLaxModeWithBadAnnotationAndStrictNamespace()
51
    {
52
        $annotationReader = new AnnotationReader(new DoctrineAnnotationReader(), AnnotationReader::LAX_MODE, ['TheCodingMachine\\GraphQL\\Controllers\\Fixtures']);
53
54
        $this->expectException(AnnotationException::class);
55
        $annotationReader->getTypeAnnotation(new ReflectionClass(ClassWithInvalidClassAnnotation::class));
56
    }
57
58
    public function testGetAnnotationsStrictMode()
59
    {
60
        $annotationReader = new AnnotationReader(new DoctrineAnnotationReader(), AnnotationReader::STRICT_MODE, []);
61
62
        $this->expectException(AnnotationException::class);
63
        $annotationReader->getClassAnnotations(new ReflectionClass(ClassWithInvalidClassAnnotation::class), Type::class);
64
    }
65
66
    public function testGetAnnotationsLaxModeWithBadAnnotation()
67
    {
68
        $annotationReader = new AnnotationReader(new DoctrineAnnotationReader(), AnnotationReader::LAX_MODE, []);
69
70
        $types = $annotationReader->getClassAnnotations(new ReflectionClass(ClassWithInvalidClassAnnotation::class), Type::class);
71
        $this->assertSame([], $types);
72
    }
73
74
    public function testGetAnnotationsLaxModeWithSmellyAnnotation()
75
    {
76
        $annotationReader = new AnnotationReader(new DoctrineAnnotationReader(), AnnotationReader::LAX_MODE, []);
77
78
        $this->expectException(AnnotationException::class);
79
        $annotationReader->getClassAnnotations(new ReflectionClass(ClassWithInvalidTypeAnnotation::class), Type::class);
80
    }
81
82
    public function testGetAnnotationsLaxModeWithBadAnnotationAndStrictNamespace()
83
    {
84
        $annotationReader = new AnnotationReader(new DoctrineAnnotationReader(), AnnotationReader::LAX_MODE, ['TheCodingMachine\\GraphQL\\Controllers\\Fixtures']);
85
86
        $this->expectException(AnnotationException::class);
87
        $annotationReader->getClassAnnotations(new ReflectionClass(ClassWithInvalidClassAnnotation::class), Type::class);
88
    }
89
90
    public function testMethodStrictMode()
91
    {
92
        $annotationReader = new AnnotationReader(new DoctrineAnnotationReader(), AnnotationReader::STRICT_MODE, []);
93
94
        $this->expectException(AnnotationException::class);
95
        $annotationReader->getRequestAnnotation(new ReflectionMethod(ClassWithInvalidClassAnnotation::class, 'testMethod'), Field::class);
96
    }
97
98
    public function testMethodLaxModeWithBadAnnotation()
99
    {
100
        $annotationReader = new AnnotationReader(new DoctrineAnnotationReader(), AnnotationReader::LAX_MODE, []);
101
102
        $type = $annotationReader->getRequestAnnotation(new ReflectionMethod(ClassWithInvalidClassAnnotation::class, 'testMethod'), Field::class);
103
        $this->assertNull($type);
104
    }
105
106
    public function testMethodLaxModeWithSmellyAnnotation()
107
    {
108
        $annotationReader = new AnnotationReader(new DoctrineAnnotationReader(), AnnotationReader::LAX_MODE, []);
109
110
        $this->expectException(AnnotationException::class);
111
        $annotationReader->getRequestAnnotation(new ReflectionMethod(ClassWithInvalidTypeAnnotation::class, 'testMethod'), Field::class);
112
    }
113
114
    public function testExtendAnnotationException()
115
    {
116
        $annotationReader = new AnnotationReader(new DoctrineAnnotationReader(), AnnotationReader::STRICT_MODE, []);
117
118
        $this->expectException(ClassNotFoundException::class);
119
        $this->expectExceptionMessage("Could not autoload class 'foo' defined in @ExtendType annotation of class 'TheCodingMachine\GraphQL\Controllers\Fixtures\Annotations\ClassWithInvalidExtendTypeAnnotation'");
120
        $annotationReader->getExtendTypeAnnotation(new ReflectionClass(ClassWithInvalidExtendTypeAnnotation::class));
121
    }
122
}
123