Completed
Pull Request — master (#99)
by Simon
01:41
created

DataObjectAnnotatorTest::testIsEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace SilverLeague\IDEAnnotator\Tests;
4
5
use PageController;
6
use PHPUnit_Framework_TestCase;
7
use RootTeam;
8
use SilverLeague\IDEAnnotator\Annotatable;
9
use SilverLeague\IDEAnnotator\AnnotateClassInfo;
10
use SilverLeague\IDEAnnotator\AnnotatePermissionChecker;
11
use SilverLeague\IDEAnnotator\DataObjectAnnotator;
12
use SilverStripe\Control\Director;
13
use SilverStripe\Core\Config\Config;
14
use SilverStripe\Core\Injector\Injector;
15
use SilverStripe\Dev\SapphireTest;
16
use SilverStripe\ORM\DataObject;
17
18
/**
19
 * Class DataObjectAnnotatorTest
20
 *
21
 * Several tests to make sure the Annotator does it's job correctly
22
 *
23
 * @mixin PHPUnit_Framework_TestCase
24
 */
25
class DataObjectAnnotatorTest extends SapphireTest
26
{
27
28
    /**
29
     * @var MockDataObjectAnnotator
30
     */
31
    private $annotator;
32
33
    /**
34
     * @var AnnotatePermissionChecker $permissionChecker
35
     */
36
    private $permissionChecker;
37
38
    /**
39
     * Setup Defaults
40
     */
41 View Code Duplication
    protected function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        parent::setUp();
44
        Config::modify()->set(DataObjectAnnotator::class, 'enabled', true);
45
        Config::modify()->set(DataObjectAnnotator::class, 'enabled_modules', ['ideannotator']);
46
47
        $this->annotator = Injector::inst()->get(MockDataObjectAnnotator::class);
48
        $this->permissionChecker = Injector::inst()->get(AnnotatePermissionChecker::class);
49
    }
50
51
    public function testIsEnabled()
52
    {
53
        $this->assertTrue(DataObjectAnnotator::isEnabled());
54
    }
55
56
    /**
57
     * Test the expected classes show up in the Classes for Module
58
     */
59
    public function testGetClassesForModule()
60
    {
61
        $expectedClasses = [
62
            Team::class                             => Director::baseFolder() . DIRECTORY_SEPARATOR . 'ideannotator' . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'mock' . DIRECTORY_SEPARATOR . 'DataObjectAnnotatorTest_Team.php',
63
            TeamChanged::class                      => Director::baseFolder() . DIRECTORY_SEPARATOR . 'ideannotator' . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'mock' . DIRECTORY_SEPARATOR . 'DataObjectAnnotatorTest_TeamChanged.php',
64
            TeamComment::class                      => Director::baseFolder() . DIRECTORY_SEPARATOR . 'ideannotator' . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'mock' . DIRECTORY_SEPARATOR . 'DataObjectAnnotatorTest_TeamComment.php',
65
            DocBlockMockWithDocBlock::class         => Director::baseFolder() . DIRECTORY_SEPARATOR . 'ideannotator' . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'mock' . DIRECTORY_SEPARATOR . 'DocBlockMockWithDocBlock.php',
66
            OtherDocBlockMockWithDocBlock::class    => Director::baseFolder() . DIRECTORY_SEPARATOR . 'ideannotator' . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'mock' . DIRECTORY_SEPARATOR . 'DocBlockMockWithDocBlock.php',
67
            DataObjectWithOldStyleTagMarkers::class => Director::baseFolder() . DIRECTORY_SEPARATOR . 'ideannotator' . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'mock' . DIRECTORY_SEPARATOR . 'DocBlockMockWithDocBlock.php',
68
            DoubleDataObjectInOneFile1::class       => Director::baseFolder() . DIRECTORY_SEPARATOR . 'ideannotator' . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'mock' . DIRECTORY_SEPARATOR . 'DoubleDataObjectInOneFile.php',
69
            DoubleDataObjectInOneFile2::class       => Director::baseFolder() . DIRECTORY_SEPARATOR . 'ideannotator' . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'mock' . DIRECTORY_SEPARATOR . 'DoubleDataObjectInOneFile.php',
70
            SubTeam::class                          => Director::baseFolder() . DIRECTORY_SEPARATOR . 'ideannotator' . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'mock' . DIRECTORY_SEPARATOR . 'DataObjectAnnotatorTest_SubTeam.php',
71
            Player::class                           => Director::baseFolder() . DIRECTORY_SEPARATOR . 'ideannotator' . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'mock' . DIRECTORY_SEPARATOR . 'DataObjectAnnotatorTest_Player.php',
72
            Team_Extension::class                   => Director::baseFolder() . DIRECTORY_SEPARATOR . 'ideannotator' . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'mock' . DIRECTORY_SEPARATOR . 'DataObjectAnnotatorTest_Team_Extension.php',
73
            Annotatable::class                      => Director::baseFolder() . DIRECTORY_SEPARATOR . 'ideannotator' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Extensions' . DIRECTORY_SEPARATOR . 'Annotatable.php',
74
            AnnotatorPageTest_Extension::class      => Director::baseFolder() . DIRECTORY_SEPARATOR . 'ideannotator' . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'mock' . DIRECTORY_SEPARATOR . 'AnnotatorPageTest.php',
75
            RootTeam::class                         => Director::baseFolder() . DIRECTORY_SEPARATOR . 'ideannotator' . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'mock' . DIRECTORY_SEPARATOR . 'RootTeam.php',
76
        ];
77
        $classes = $this->annotator->getClassesForModule('ideannotator');
78
79
        $this->assertEquals($expectedClasses, $classes);
80
    }
81
82
83
    /**
84
     * As below, as we don't want to actively change the mocks, so enable mysite
85
     */
86
    public function testAnnotateObject()
87
    {
88
        $this->assertFalse($this->annotator->annotateObject(DataObject::class));
89
90
        Config::modify()->set(DataObjectAnnotator::class, 'enabled_modules', ['ideannotator', 'mysite']);
91
        $this->assertTrue($this->annotator->annotateObject(PageController::class));
92
    }
93
94
    /**
95
     * Not testing existing modules, as it wil actively alter the mock files, so enable mysite
96
     */
97
    public function testAnnotateModule()
98
    {
99
        $noModule = $this->annotator->annotateModule('');
100
        $this->assertFalse($noModule);
101
        $noModule = $this->annotator->annotateModule('mysite');
102
        $this->assertFalse($noModule);
103
        // Enable 'mysite' for testing
104
        Config::modify()->set(DataObjectAnnotator::class, 'enabled_modules', ['ideannotator', 'mysite']);
105
106
        $module = $this->annotator->annotateModule('mysite');
107
        $this->assertTrue($module);
108
    }
109
110
    /**
111
     * Test if the correct annotations are generated
112
     * for all database fields, relations and extensions
113
     * and that the start and end tags are present
114
     */
115
    public function testFileContentWithAnnotations()
116
    {
117
        $classInfo = new AnnotateClassInfo(Team::class);
118
        $filePath = $classInfo->getClassFilePath();
119
120
        $content = $this->annotator->getGeneratedFileContent(file_get_contents($filePath), Team::class);
121
122
        // ClassName title
123
        $this->assertContains(' * Class \SilverLeague\IDEAnnotator\Tests\Team', $content);
124
125
        // database fields
126
        $this->assertContains('@property string $Title', $content);
127
        $this->assertContains('@property int $VisitCount', $content);
128
        $this->assertContains('@property float $Price', $content);
129
130
        // has_one ID
131
        $this->assertContains('@property int $CaptainID', $content);
132
        // has_one relation
133
        $this->assertContains('@method \SilverLeague\IDEAnnotator\Tests\Player Captain()', $content);
134
        // has_many relation
135
        $this->assertContains(
136
            '@method \SilverStripe\ORM\DataList|\SilverLeague\IDEAnnotator\Tests\SubTeam[] SubTeams()',
137
            $content
138
        );
139
        // many_many relation
140
        $this->assertContains(
141
            '@method \SilverStripe\ORM\ManyManyList|\SilverLeague\IDEAnnotator\Tests\Player[] Players()',
142
            $content
143
        );
144
        $this->assertContains(
145
            '@method \SilverStripe\ORM\ManyManyList|\SilverLeague\IDEAnnotator\Tests\Player[] Reserves()',
146
            $content
147
        );
148
149
        // DataExtension
150
        $this->assertContains('@mixin \SilverLeague\IDEAnnotator\Tests\Team_Extension', $content);
151
    }
152
153 View Code Duplication
    public function testInversePlayerRelationOfTeam()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155
        $classInfo = new AnnotateClassInfo(Player::class);
156
        $filePath = $classInfo->getClassFilePath();
157
158
        $content = $this->annotator->getGeneratedFileContent(file_get_contents($filePath), Player::class);
159
160
        $this->assertContains('@property boolean $IsRetired', $content);
161
        $this->assertContains('@property string $ShirtNumber', $content);
162
        $this->assertContains('@property int $FavouriteTeamID', $content);
163
        $this->assertContains('@method \SilverLeague\IDEAnnotator\Tests\Team FavouriteTeam()', $content);
164
165
        $this->assertContains(
166
            '@method \SilverStripe\ORM\ManyManyList|\SilverLeague\IDEAnnotator\Tests\Team[] TeamPlayer()',
167
            $content
168
        );
169
        $this->assertContains(
170
            '@method \SilverStripe\ORM\ManyManyList|\SilverLeague\IDEAnnotator\Tests\Team[] TeamReserve()',
171
            $content
172
        );
173
    }
174
175
    public function testExistingMethodsWillNotBeTagged()
176
    {
177
        $classInfo = new AnnotateClassInfo(Team::class);
178
        $filePath = $classInfo->getClassFilePath();
179
180
        $content = $this->annotator->getGeneratedFileContent(file_get_contents($filePath), Team::class);
181
        $this->assertNotContains(
182
            '@method \SilverStripe\ORM\ManyManyList|\SilverLeague\IDEAnnotator\Tests\SubTeam[] SecondarySubTeams()',
183
            $content
184
        );
185
    }
186
187
    /**
188
     * Test that multiple annotation runs won't generate ducplicate docblocks
189
     */
190 View Code Duplication
    public function testNothingHasChangedAfterSecondAnnotation()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
    {
192
        $classInfo = new AnnotateClassInfo(Team::class);
193
        $filePath = $classInfo->getClassFilePath();
194
        $original = file_get_contents($filePath);
195
        $firstRun = $this->annotator->getGeneratedFileContent($original, Team::class);
196
        $secondRun = $this->annotator->getGeneratedFileContent($firstRun, Team::class);
197
        $this->assertEquals($firstRun, $secondRun);
198
    }
199
200
    /**
201
     * Test that root (non-namespaced) classes get annotated
202
     */
203 View Code Duplication
    public function testRootAnnotations()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
    {
205
        $classInfo = new AnnotateClassInfo(RootTeam::class);
206
        $filePath = $classInfo->getClassFilePath();
207
        $run = $this->annotator->getGeneratedFileContent(file_get_contents($filePath), RootTeam::class);
208
        $this->assertContains('@property string $Title', $run);
209
    }
210
211
    /**
212
     * Test the generation of annotations for a DataExtension
213
     */
214 View Code Duplication
    public function testAnnotateDataExtension()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
215
    {
216
        $classInfo = new AnnotateClassInfo(Team_Extension::class);
217
        $filePath = $classInfo->getClassFilePath();
218
        $original = file_get_contents($filePath);
219
        $annotated = $this->annotator->getGeneratedFileContent($original, Team_Extension::class);
220
221
        $this->assertContains(
222
            '@property \SilverLeague\IDEAnnotator\Tests\Team|\SilverLeague\IDEAnnotator\Tests\Team_Extension $owner',
223
            $annotated
224
        );
225
        $this->assertContains('@property string $ExtendedVarcharField', $annotated);
226
        $this->assertContains('@property int $ExtendedIntField', $annotated);
227
        $this->assertContains('@property int $ExtendedHasOneRelationshipID', $annotated);
228
        $this->assertContains(
229
            '@method \SilverLeague\IDEAnnotator\Tests\Player ExtendedHasOneRelationship()',
230
            $annotated
231
        );
232
    }
233
234
    /**
235
     *
236
     */
237 View Code Duplication
    public function testTwoClassesInOneFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
238
    {
239
        $classInfo = new AnnotateClassInfo(DoubleDataObjectInOneFile1::class);
240
        $filePath = $classInfo->getClassFilePath();
241
        $original = file_get_contents($filePath);
242
        $annotated = $this->annotator->getGeneratedFileContent($original, DoubleDataObjectInOneFile1::class);
243
244
        $this->assertContains('@property string $Title', $annotated);
245
246
        $annotated = $this->annotator->getGeneratedFileContent($annotated, DoubleDataObjectInOneFile2::class);
247
248
        $this->assertContains('@property string $Name', $annotated);
249
    }
250
251
    public function tearDown()
252
    {
253
        parent::tearDown();
254
    }
255
}
256