forked from ARCANEDEV/LogViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilesystemTest.php
234 lines (184 loc) · 5.81 KB
/
FilesystemTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php namespace Arcanedev\LogViewer\Tests\Utilities;
use Arcanedev\LogViewer\Tests\TestCase;
use Arcanedev\LogViewer\Utilities\Filesystem;
/**
* Class FilesystemTest
*
* @package Arcanedev\LogViewer\Tests\Utilities
* @author ARCANEDEV <[email protected]>
*/
class FilesystemTest extends TestCase
{
/* -----------------------------------------------------------------
| Properties
| -----------------------------------------------------------------
*/
/** @var \Arcanedev\LogViewer\Utilities\Filesystem */
private $filesystem;
/* -----------------------------------------------------------------
| Main Methods
| -----------------------------------------------------------------
*/
protected function setUp(): void
{
parent::setUp();
$this->filesystem = $this->filesystem();
}
protected function tearDown(): void
{
unset($this->filesystem);
parent::tearDown();
}
/* -----------------------------------------------------------------
| Tests
| -----------------------------------------------------------------
*/
/** @test */
public function it_can_be_instantiated()
{
static::assertInstanceOf(Filesystem::class, $this->filesystem);
}
/** @test */
public function it_can_get_filesystem_instance()
{
static::assertInstanceOf(
\Illuminate\Filesystem\Filesystem::class,
$this->filesystem->getInstance()
);
}
/** @test */
public function it_can_get_all_valid_log_files()
{
static::assertCount(2, $this->filesystem->logs());
}
/** @test */
public function it_can_get_all_custom_log_files()
{
$files = $this->filesystem
->setPrefixPattern('laravel-cli-')
->logs();
static::assertCount(1, $files);
}
/** @test */
public function it_can_get_all_log_files()
{
$files = $this->filesystem->all();
static::assertCount(5, $files);
foreach ($files as $file) {
static::assertStringEndsWith('.log', $file);
}
}
/** @test */
public function it_can_read_file()
{
$file = $this->filesystem->read($date = '2015-01-01');
static::assertNotEmpty($file);
static::assertStringStartsWith('['.$date, $file);
}
/** @test */
public function it_can_delete_file()
{
static::createDummyLog($date = date('Y-m-d'));
// Assert log exists
$file = $this->filesystem->read($date);
static::assertNotEmpty($file);
// Assert log deletion
try {
$deleted = $this->filesystem->delete($date);
$message = '';
}
catch (\Exception $e) {
$deleted = false;
$message = $e->getMessage();
}
static::assertTrue($deleted, $message);
}
/** @test */
public function it_can_get_files()
{
$files = $this->filesystem->logs();
static::assertCount(2, $files);
foreach ($files as $file) {
static::assertFileExists($file);
}
}
/** @test */
public function it_can_set_a_custom_path()
{
$this->filesystem->setPath(storage_path('custom-path-logs'));
$files = $this->filesystem->logs();
static::assertCount(1, $files);
foreach ($files as $file) {
static::assertFileExists($file);
}
}
/** @test */
public function it_can_get_file_path_by_date()
{
static::assertFileExists(
$this->filesystem->path('2015-01-01')
);
}
/** @test */
public function it_can_get_dates_from_log_files()
{
static::assertDates(
$this->filesystem->dates()
);
}
/** @test */
public function it_can_get_dates_with_paths_from_log_files()
{
foreach ($this->filesystem->dates(true) as $date => $path) {
static::assertDate($date);
static::assertFileExists($path);
}
}
/** @test */
public function it_must_throw_a_filesystem_exception_on_read()
{
$this->expectException(\Arcanedev\LogViewer\Exceptions\FilesystemException::class);
$this->filesystem->read('2222-11-11'); // Future FTW
}
/** @test */
public function it_must_throw_a_filesystem_exception_on_delete()
{
$this->expectException(\Arcanedev\LogViewer\Exceptions\FilesystemException::class);
$this->filesystem->delete('2222-11-11'); // Future FTW
}
/** @test */
public function it_can_set_and_get_pattern()
{
static::assertSame(
'laravel-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].log',
$this->filesystem->getPattern()
);
$this->filesystem->setExtension('');
static::assertSame(
'laravel-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]',
$this->filesystem->getPattern()
);
$this->filesystem->setPrefixPattern('laravel-cli-');
static::assertSame(
'laravel-cli-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]',
$this->filesystem->getPattern()
);
$this->filesystem->setDatePattern('[0-9][0-9][0-9][0-9]');
static::assertSame(
'laravel-cli-[0-9][0-9][0-9][0-9]',
$this->filesystem->getPattern()
);
$this->filesystem->setPattern();
static::assertSame(
'laravel-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].log',
$this->filesystem->getPattern()
);
$this->filesystem->setPattern(
'laravel-', '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]', '.log'
);
static::assertSame(
'laravel-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].log',
$this->filesystem->getPattern()
);
}
}