ControllerAnnotatorTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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