1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CSoellinger\SilverStripe\ModelAnnotations\Test\Unit\Handler; |
4
|
|
|
|
5
|
|
|
use CSoellinger\SilverStripe\ModelAnnotations\Handler\DataClassFileHandler; |
6
|
|
|
use CSoellinger\SilverStripe\ModelAnnotations\Task\ModelAnnotationsTask; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use SilverStripe\Core\Config\Config; |
9
|
|
|
use SilverStripe\Core\Injector\Injector; |
10
|
|
|
use SilverStripe\Dev\SapphireTest; |
11
|
|
|
|
12
|
|
|
define('PHP_OPEN', '<?php'); |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @internal |
16
|
|
|
* |
17
|
|
|
* @covers \CSoellinger\SilverStripe\ModelAnnotations\Handler\DataClassFileHandler |
18
|
|
|
*/ |
19
|
|
|
class DataClassFileHandlerTest extends SapphireTest |
20
|
|
|
{ |
21
|
|
|
protected static DataClassFileHandler $fileHandler; |
22
|
|
|
|
23
|
|
|
protected function setUp(): void |
24
|
|
|
{ |
25
|
|
|
parent::setUp(); |
26
|
|
|
|
27
|
|
|
/** @var DataClassFileHandler $fileHandler */ |
28
|
|
|
$fileHandler = Injector::inst()->createWithArgs(DataClassFileHandler::class, [__FILE__]); |
29
|
|
|
|
30
|
|
|
self::$fileHandler = $fileHandler; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @covers \CSoellinger\SilverStripe\ModelAnnotations\Handler\DataClassFileHandler::__construct |
35
|
|
|
*/ |
36
|
|
|
public function testInitialize(): void |
37
|
|
|
{ |
38
|
|
|
$fileHandlerInstance = new DataClassFileHandler(__FILE__); |
39
|
|
|
|
40
|
|
|
self::assertInstanceOf(DataClassFileHandler::class, $fileHandlerInstance); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @covers \CSoellinger\SilverStripe\ModelAnnotations\Handler\DataClassFileHandler::__construct |
45
|
|
|
* |
46
|
|
|
* @throws InvalidArgumentException |
47
|
|
|
* @group ExpectedOutput |
48
|
|
|
*/ |
49
|
|
|
public function testInitializeWrongPath(): void |
50
|
|
|
{ |
51
|
|
|
$this->expectException(InvalidArgumentException::class); |
52
|
|
|
$this->expectExceptionMessage('Error with file at path "/no-file-exists-here.php"'); |
53
|
|
|
|
54
|
|
|
new DataClassFileHandler('/no-file-exists-here.php'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @covers \CSoellinger\SilverStripe\ModelAnnotations\Handler\DataClassFileHandler::getPath |
59
|
|
|
*/ |
60
|
|
|
public function testGetPath(): void |
61
|
|
|
{ |
62
|
|
|
self::assertEquals(__FILE__, self::$fileHandler->getPath()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @covers \CSoellinger\SilverStripe\ModelAnnotations\Handler\DataClassFileHandler::getContent |
67
|
|
|
*/ |
68
|
|
|
public function testGetContent(): void |
69
|
|
|
{ |
70
|
|
|
self::assertEquals(file_get_contents(__FILE__), self::$fileHandler->getContent()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @covers \CSoellinger\SilverStripe\ModelAnnotations\Handler\DataClassFileHandler::getAst |
75
|
|
|
*/ |
76
|
|
|
public function testGetAst(): void |
77
|
|
|
{ |
78
|
|
|
self::assertEquals(\ast\parse_file(__FILE__, 80), self::$fileHandler->getAst()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @covers \CSoellinger\SilverStripe\ModelAnnotations\Handler\DataClassFileHandler::getNamespaceAst |
83
|
|
|
*/ |
84
|
|
|
public function testGetNamespaceAst(): void |
85
|
|
|
{ |
86
|
|
|
self::assertEquals(\ast\parse_file(__FILE__, 80)->children[0], self::$fileHandler->getNamespaceAst()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @covers \CSoellinger\SilverStripe\ModelAnnotations\Handler\DataClassFileHandler::getNamespaceAst |
91
|
|
|
*/ |
92
|
|
|
public function testGetNamespaceAstFromNoNamespaceFile(): void |
93
|
|
|
{ |
94
|
|
|
$filePath = implode(DIRECTORY_SEPARATOR, [ |
95
|
|
|
__DIR__, |
96
|
|
|
'..', |
97
|
|
|
'..', |
98
|
|
|
'..', |
99
|
|
|
'_config.php', |
100
|
|
|
]); |
101
|
|
|
|
102
|
|
|
/** @var DataClassFileHandler $fileHandlerInstance */ |
103
|
|
|
$fileHandlerInstance = Injector::inst()->createWithArgs(DataClassFileHandler::class, [$filePath]); |
104
|
|
|
|
105
|
|
|
self::$fileHandler = $fileHandlerInstance; |
106
|
|
|
|
107
|
|
|
self::assertNull(self::$fileHandler->getNamespaceAst()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @covers \CSoellinger\SilverStripe\ModelAnnotations\Handler\DataClassFileHandler::getClassAst |
112
|
|
|
*/ |
113
|
|
|
public function testGetClassAst(): void |
114
|
|
|
{ |
115
|
|
|
self::assertEquals(\ast\parse_file(__FILE__, 80)->children[8], self::$fileHandler->getClassAst(self::class)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @covers \CSoellinger\SilverStripe\ModelAnnotations\Handler\DataClassFileHandler::getUseStatementsFromAst |
120
|
|
|
*/ |
121
|
|
|
public function testGetUseStatementsFromAst(): void |
122
|
|
|
{ |
123
|
|
|
$useStatements = [ |
124
|
|
|
\ast\parse_file(__FILE__, 80)->children[1]->children[0], |
125
|
|
|
\ast\parse_file(__FILE__, 80)->children[2]->children[0], |
126
|
|
|
\ast\parse_file(__FILE__, 80)->children[3]->children[0], |
127
|
|
|
\ast\parse_file(__FILE__, 80)->children[4]->children[0], |
128
|
|
|
\ast\parse_file(__FILE__, 80)->children[5]->children[0], |
129
|
|
|
\ast\parse_file(__FILE__, 80)->children[6]->children[0], |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
self::assertEquals($useStatements, self::$fileHandler->getUseStatementsFromAst()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @covers \CSoellinger\SilverStripe\ModelAnnotations\Handler\DataClassFileHandler::addText |
137
|
|
|
*/ |
138
|
|
|
public function testAddText(): void |
139
|
|
|
{ |
140
|
|
|
$newContent = str_replace( |
141
|
|
|
PHP_OPEN . PHP_EOL, |
142
|
|
|
PHP_OPEN . PHP_EOL . '\/** TEST *\/' . PHP_EOL, |
143
|
|
|
(string) file_get_contents(__FILE__) |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
self::$fileHandler->addText('\/** TEST *\/', 2); |
147
|
|
|
|
148
|
|
|
self::assertEquals($newContent, self::$fileHandler->getContent()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @covers \CSoellinger\SilverStripe\ModelAnnotations\Handler\DataClassFileHandler::addText |
153
|
|
|
*/ |
154
|
|
|
public function testAddTextAtLineZero(): void |
155
|
|
|
{ |
156
|
|
|
$newContent = str_replace( |
157
|
|
|
PHP_OPEN . PHP_EOL, |
158
|
|
|
'<!-- TEST -->' . PHP_EOL . PHP_OPEN . PHP_EOL, |
159
|
|
|
(string) file_get_contents(__FILE__) |
160
|
|
|
); |
161
|
|
|
|
162
|
|
|
self::$fileHandler->addText('<!-- TEST -->', 0); |
163
|
|
|
|
164
|
|
|
self::assertEquals($newContent, self::$fileHandler->getContent()); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @covers \CSoellinger\SilverStripe\ModelAnnotations\Handler\DataClassFileHandler::contentReplace |
169
|
|
|
*/ |
170
|
|
|
public function testContentReplace(): void |
171
|
|
|
{ |
172
|
|
|
$newContent = str_replace( |
173
|
|
|
PHP_OPEN . PHP_EOL, |
174
|
|
|
PHP_OPEN . ' \/** TEST *\/' . PHP_EOL, |
175
|
|
|
(string) file_get_contents(__FILE__) |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
self::$fileHandler->contentReplace(PHP_OPEN . PHP_EOL, PHP_OPEN . ' \/** TEST *\/' . PHP_EOL); |
179
|
|
|
|
180
|
|
|
self::assertEquals($newContent, self::$fileHandler->getContent()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @covers \CSoellinger\SilverStripe\ModelAnnotations\Handler\DataClassFileHandler::write |
185
|
|
|
*/ |
186
|
|
|
public function testWriteFile(): void |
187
|
|
|
{ |
188
|
|
|
self::$fileHandler->write(); |
189
|
|
|
self::assertEquals(file_get_contents(__FILE__), self::$fileHandler->getContent()); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @covers \CSoellinger\SilverStripe\ModelAnnotations\Handler\DataClassFileHandler::write |
194
|
|
|
*/ |
195
|
|
|
public function testWriteFileWithBackupFile(): void |
196
|
|
|
{ |
197
|
|
|
Config::forClass(ModelAnnotationsTask::class)->set('createBackupFile', true); |
198
|
|
|
|
199
|
|
|
self::$fileHandler->write(); |
200
|
|
|
self::assertEquals(file_get_contents(__FILE__), self::$fileHandler->getContent()); |
201
|
|
|
self::assertFileExists(__FILE__ . '.bck'); |
202
|
|
|
|
203
|
|
|
self::$fileHandler->write(); |
204
|
|
|
self::assertEquals(file_get_contents(__FILE__), self::$fileHandler->getContent()); |
205
|
|
|
self::assertFileExists(__FILE__ . '.1.bck'); |
206
|
|
|
|
207
|
|
|
unlink(__FILE__ . '.bck'); |
208
|
|
|
unlink(__FILE__ . '.1.bck'); |
209
|
|
|
|
210
|
|
|
Config::forClass(ModelAnnotationsTask::class)->set('createBackupFile', false); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|