1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverLeague\IDEAnnotator\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
6
|
|
|
use SilverLeague\IDEAnnotator\AnnotatePermissionChecker; |
7
|
|
|
use SilverLeague\IDEAnnotator\DataObjectAnnotator; |
8
|
|
|
use SilverLeague\IDEAnnotator\AnnotateClassInfo; |
9
|
|
|
use SilverStripe\Core\Injector\Injector; |
10
|
|
|
use SilverStripe\Core\Config\Config; |
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 DataObjectAnnotatorTest extends SapphireTest |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var MockDataObjectAnnotator |
25
|
|
|
*/ |
26
|
|
|
private $annotator; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var AnnotatePermissionChecker $permissionChecker |
30
|
|
|
*/ |
31
|
|
|
private $permissionChecker; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Setup Defaults |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
parent::setUp(); |
39
|
|
|
Config::modify()->set(DataObjectAnnotator::class, 'enabled', true); |
40
|
|
|
Config::modify()->set(DataObjectAnnotator::class, 'enabled_modules', ['ideannotator']); |
41
|
|
|
|
42
|
|
|
$this->annotator = Injector::inst()->get(MockDataObjectAnnotator::class); |
43
|
|
|
$this->permissionChecker = Injector::inst()->get(AnnotatePermissionChecker::class); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Test if the correct annotations are generated |
48
|
|
|
* for all database fields, relations and extensions |
49
|
|
|
* and that the start and end tags are present |
50
|
|
|
*/ |
51
|
|
|
public function testFileContentWithAnnotations() |
52
|
|
|
{ |
53
|
|
|
$classInfo = new AnnotateClassInfo(Team::class); |
54
|
|
|
$filePath = $classInfo->getClassFilePath(); |
55
|
|
|
|
56
|
|
|
$content = $this->annotator->getGeneratedFileContent(file_get_contents($filePath), Team::class); |
57
|
|
|
|
58
|
|
|
// ClassName title |
59
|
|
|
$this->assertContains(' * Class \SilverLeague\IDEAnnotator\Tests\Team', $content); |
60
|
|
|
|
61
|
|
|
// database fields |
62
|
|
|
$this->assertContains('@property string $Title', $content); |
63
|
|
|
$this->assertContains('@property int $VisitCount', $content); |
64
|
|
|
$this->assertContains('@property float $Price', $content); |
65
|
|
|
|
66
|
|
|
// has_one ID |
67
|
|
|
$this->assertContains('@property int $CaptainID', $content); |
68
|
|
|
// has_one relation |
69
|
|
|
$this->assertContains('@method \SilverLeague\IDEAnnotator\Tests\Player Captain()', $content); |
70
|
|
|
// has_many relation |
71
|
|
|
$this->assertContains( |
72
|
|
|
'@method \SilverStripe\ORM\DataList|\SilverLeague\IDEAnnotator\Tests\SubTeam[] SubTeams()', |
73
|
|
|
$content |
74
|
|
|
); |
75
|
|
|
// many_many relation |
76
|
|
|
$this->assertContains( |
77
|
|
|
'@method \SilverStripe\ORM\ManyManyList|\SilverLeague\IDEAnnotator\Tests\Player[] Players()', |
78
|
|
|
$content |
79
|
|
|
); |
80
|
|
|
$this->assertContains( |
81
|
|
|
'@method \SilverStripe\ORM\ManyManyList|\SilverLeague\IDEAnnotator\Tests\Player[] Reserves()', |
82
|
|
|
$content |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
// DataExtension |
86
|
|
|
$this->assertContains('@mixin \SilverLeague\IDEAnnotator\Tests\Team_Extension', $content); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
View Code Duplication |
public function testInversePlayerRelationOfTeam() |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$classInfo = new AnnotateClassInfo(Player::class); |
92
|
|
|
$filePath = $classInfo->getClassFilePath(); |
93
|
|
|
|
94
|
|
|
$content = $this->annotator->getGeneratedFileContent(file_get_contents($filePath), Player::class); |
95
|
|
|
|
96
|
|
|
$this->assertContains('@property boolean $IsRetired', $content); |
97
|
|
|
$this->assertContains('@property string $ShirtNumber', $content); |
98
|
|
|
$this->assertContains('@property int $FavouriteTeamID', $content); |
99
|
|
|
$this->assertContains('@method \SilverLeague\IDEAnnotator\Tests\Team FavouriteTeam()', $content); |
100
|
|
|
|
101
|
|
|
$this->assertContains( |
102
|
|
|
'@method \SilverStripe\ORM\ManyManyList|\SilverLeague\IDEAnnotator\Tests\Team[] TeamPlayer()', |
103
|
|
|
$content |
104
|
|
|
); |
105
|
|
|
$this->assertContains( |
106
|
|
|
'@method \SilverStripe\ORM\ManyManyList|\SilverLeague\IDEAnnotator\Tests\Team[] TeamReserve()', |
107
|
|
|
$content |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testExistingMethodsWillNotBeTagged() |
112
|
|
|
{ |
113
|
|
|
$classInfo = new AnnotateClassInfo(Team::class); |
114
|
|
|
$filePath = $classInfo->getClassFilePath(); |
115
|
|
|
|
116
|
|
|
$content = $this->annotator->getGeneratedFileContent(file_get_contents($filePath), Team::class); |
117
|
|
|
$this->assertNotContains( |
118
|
|
|
'@method \SilverStripe\ORM\ManyManyList|\SilverLeague\IDEAnnotator\Tests\SubTeam[] SecondarySubTeams()', |
119
|
|
|
$content |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Test that multiple annotation runs won't generate ducplicate docblocks |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function testNothingHasChangedAfterSecondAnnotation() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$classInfo = new AnnotateClassInfo(Team::class); |
129
|
|
|
$filePath = $classInfo->getClassFilePath(); |
130
|
|
|
$original = file_get_contents($filePath); |
131
|
|
|
$firstRun = $this->annotator->getGeneratedFileContent($original, Team::class); |
132
|
|
|
$secondRun = $this->annotator->getGeneratedFileContent($firstRun, Team::class); |
133
|
|
|
$this->assertEquals($firstRun, $secondRun); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Test the generation of annotations for a DataExtension |
138
|
|
|
*/ |
139
|
|
View Code Duplication |
public function testAnnotateDataExtension() |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$classInfo = new AnnotateClassInfo(Team_Extension::class); |
142
|
|
|
$filePath = $classInfo->getClassFilePath(); |
143
|
|
|
$original = file_get_contents($filePath); |
144
|
|
|
$annotated = $this->annotator->getGeneratedFileContent($original, Team_Extension::class); |
145
|
|
|
|
146
|
|
|
$this->assertContains( |
147
|
|
|
'@property \SilverLeague\IDEAnnotator\Tests\Team|\SilverLeague\IDEAnnotator\Tests\Team_Extension $owner', |
148
|
|
|
$annotated |
149
|
|
|
); |
150
|
|
|
$this->assertContains('@property string $ExtendedVarcharField', $annotated); |
151
|
|
|
$this->assertContains('@property int $ExtendedIntField', $annotated); |
152
|
|
|
$this->assertContains('@property int $ExtendedHasOneRelationshipID', $annotated); |
153
|
|
|
$this->assertContains( |
154
|
|
|
'@method \SilverLeague\IDEAnnotator\Tests\Player ExtendedHasOneRelationship()', |
155
|
|
|
$annotated |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* |
161
|
|
|
*/ |
162
|
|
View Code Duplication |
public function testTwoClassesInOneFile() |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
$classInfo = new AnnotateClassInfo(DoubleDataObjectInOneFile1::class); |
165
|
|
|
$filePath = $classInfo->getClassFilePath(); |
166
|
|
|
$original = file_get_contents($filePath); |
167
|
|
|
$annotated = $this->annotator->getGeneratedFileContent($original, DoubleDataObjectInOneFile1::class); |
168
|
|
|
|
169
|
|
|
$this->assertContains('@property string $Title', $annotated); |
170
|
|
|
|
171
|
|
|
$annotated = $this->annotator->getGeneratedFileContent($annotated, DoubleDataObjectInOneFile2::class); |
172
|
|
|
|
173
|
|
|
$this->assertContains('@property string $Name', $annotated); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function tearDown() |
177
|
|
|
{ |
178
|
|
|
parent::tearDown(); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
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.