Completed
Pull Request — master (#107)
by Simon
03:56 queued 44s
created

testShortPageControllerGetsAnnotator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverLeague\IDEAnnotator\Tests;
4
5
use PHPUnit_Framework_TestCase;
6
use SilverLeague\IDEAnnotator\AnnotateClassInfo;
7
use SilverLeague\IDEAnnotator\AnnotatePermissionChecker;
8
use SilverLeague\IDEAnnotator\DataObjectAnnotator;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Dev\SapphireTest;
12
13
/**
14
 * Class DataObjectAnnotatorTest
15
 *
16
 * Several tests to make sure the Annotator does it's job correctly
17
 *
18
 * @mixin PHPUnit_Framework_TestCase
19
 */
20
class ControllerAnnotatorTest extends SapphireTest
21
{
22
    /**
23
     * @var MockDataObjectAnnotator
24
     */
25
    private $annotator;
26
27
    /**
28
     * @var AnnotatePermissionChecker $permissionChecker
29
     */
30
    private $permissionChecker;
31
32
    /**
33
     * Setup Defaults
34
     */
35
    protected function setUp()
36
    {
37
        parent::setUp();
38
        Config::modify()->set(DataObjectAnnotator::class, 'use_short_name', false);
39
40
        Config::modify()->set(DataObjectAnnotator::class, 'enabled', true);
41
        Config::modify()->set(DataObjectAnnotator::class, 'enabled_modules', ['ideannotator']);
42
43
        $this->annotator = Injector::inst()->get(MockDataObjectAnnotator::class);
44
        $this->permissionChecker = Injector::inst()->get(AnnotatePermissionChecker::class);
45
    }
46
47
    /**
48
     * Check if Page is annotated correctly
49
     */
50
    public function testPageGetsAnnotated()
51
    {
52
        $classInfo = new AnnotateClassInfo(AnnotatorPageTest::class);
53
        $filePath = $classInfo->getClassFilePath();
54
55
        $content = $this->annotator->getGeneratedFileContent(file_get_contents($filePath), AnnotatorPageTest::class);
56
57
        $this->assertContains(' * Class \SilverLeague\IDEAnnotator\Tests\AnnotatorPageTest', $content);
58
        $this->assertContains('@property string $SubTitle', $content);
59
    }
60
61
    public function testPageControllerGetsAnnotator()
62
    {
63
        $classInfo = new AnnotateClassInfo(AnnotatorPageTestController::class);
64
        $filePath = $classInfo->getClassFilePath();
65
66
        $content = $this->annotator->getGeneratedFileContent(
67
            file_get_contents($filePath),
68
            AnnotatorPageTestController::class
69
        );
70
71
        $this->assertContains(' * Class \SilverLeague\IDEAnnotator\Tests\AnnotatorPageTestController', $content);
72
        $this->assertContains('@property \SilverLeague\IDEAnnotator\Tests\AnnotatorPageTest dataRecord', $content);
73
        $this->assertContains('@method \SilverLeague\IDEAnnotator\Tests\AnnotatorPageTest data()', $content);
74
        $this->assertContains('@mixin \SilverLeague\IDEAnnotator\Tests\AnnotatorPageTest', $content);
75
        $this->assertContains('@mixin \SilverLeague\IDEAnnotator\Tests\AnnotatorPageTest_Extension', $content);
76
    }
77
78
    /**
79
     * Test the generation of annotations for an Extension
80
     */
81
    public function testAnnotateControllerExtension()
82
    {
83
        $classInfo = new AnnotateClassInfo(AnnotatorPageTest_Extension::class);
84
        $filePath = $classInfo->getClassFilePath();
85
        $original = file_get_contents($filePath);
86
        $annotated = $this->annotator->getGeneratedFileContent($original, AnnotatorPageTest_Extension::class);
87
88
        $this->assertContains(' * Class \SilverLeague\IDEAnnotator\Tests\AnnotatorPageTest_Extension', $annotated);
89
        $this->assertContains(
90
            '@property \SilverLeague\IDEAnnotator\Tests\AnnotatorPageTestController|\SilverLeague\IDEAnnotator\Tests\AnnotatorPageTest_Extension $owner',
91
            $annotated
92
        );
93
    }
94
95
    public function testShortPageGetsAnnotated()
96
    {
97
        Config::modify()->set(DataObjectAnnotator::class, 'use_short_name', true);
98
        $classInfo = new AnnotateClassInfo(AnnotatorPageTest::class);
99
        $filePath = $classInfo->getClassFilePath();
100
101
        $content = $this->annotator->getGeneratedFileContent(file_get_contents($filePath), AnnotatorPageTest::class);
102
103
        $this->assertContains('@property string $SubTitle', $content);
104
    }
105
106
    public function testShortPageControllerGetsAnnotator()
107
    {
108
        Config::modify()->set(DataObjectAnnotator::class, 'use_short_name', true);
109
        $classInfo = new AnnotateClassInfo(AnnotatorPageTestController::class);
110
        $filePath = $classInfo->getClassFilePath();
111
112
        $content = $this->annotator->getGeneratedFileContent(
113
            file_get_contents($filePath),
114
            AnnotatorPageTestController::class
115
        );
116
117
        $this->assertContains('@property AnnotatorPageTest dataRecord', $content);
118
        $this->assertContains('@method AnnotatorPageTest data()', $content);
119
        $this->assertContains('@mixin AnnotatorPageTest', $content);
120
        $this->assertContains('@mixin AnnotatorPageTest_Extension', $content);
121
    }
122
123
    /**
124
     * Test the generation of annotations for an Extension
125
     */
126
    public function testShortAnnotateControllerExtension()
127
    {
128
        Config::modify()->set(DataObjectAnnotator::class, 'use_short_name', true);
129
        $classInfo = new AnnotateClassInfo(AnnotatorPageTest_Extension::class);
130
        $filePath = $classInfo->getClassFilePath();
131
        $original = file_get_contents($filePath);
132
        $annotated = $this->annotator->getGeneratedFileContent($original, AnnotatorPageTest_Extension::class);
133
134
        $this->assertContains(
135
            '@property AnnotatorPageTestController|AnnotatorPageTest_Extension $owner',
136
            $annotated
137
        );
138
    }
139
140
    public function tearDown()
141
    {
142
        parent::tearDown();
143
    }
144
}
145