1
|
|
|
<?php |
2
|
|
|
class DeploynautLogFileTest extends SapphireTest { |
|
|
|
|
3
|
|
|
|
4
|
|
|
protected $basePath; |
5
|
|
|
|
6
|
|
|
public function setUp() { |
7
|
|
|
parent::setUp(); |
8
|
|
|
|
9
|
|
|
$this->basePath = '/tmp/deploynautlogstest'; |
10
|
|
|
mkdir($this->basePath); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
public function testLogFileNameSanitised() { |
14
|
|
|
$log = new DeploynautLogFile('SomeSortOf Filename (UAT).log', $this->basePath); |
15
|
|
|
$this->assertEquals($log->getSanitisedLogFilePath(), $this->basePath . '/somesortof-filename-uat.log'); |
|
|
|
|
16
|
|
|
|
17
|
|
|
$log = new DeploynautLogFile('..SomeSortOf Filename_(UAT).log', $this->basePath); |
18
|
|
|
$this->assertEquals($log->getSanitisedLogFilePath(), $this->basePath . '/somesortof-filename-uat.log'); |
|
|
|
|
19
|
|
|
|
20
|
|
|
$log = new DeploynautLogFile('test-project.1823.log', $this->basePath); |
21
|
|
|
$this->assertEquals($log->getSanitisedLogFilePath(), $this->basePath . '/test-project.1823.log'); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testUnsanitisedLogFileFallback() { |
25
|
|
|
touch($this->basePath . '/' . 'SomeSortOf Filename (UAT).log'); |
26
|
|
|
|
27
|
|
|
$log = new DeploynautLogFile('SomeSortOf Filename (UAT).log', $this->basePath); |
28
|
|
|
$this->assertEquals($log->getLogFilePath(), $this->basePath . '/SomeSortOf Filename (UAT).log'); |
|
|
|
|
29
|
|
|
$this->assertTrue($log->exists()); |
|
|
|
|
30
|
|
|
|
31
|
|
|
// writing into the logs still works, and we reference the old file path still. |
32
|
|
|
$log->write('Some test content'); |
33
|
|
|
$this->assertEquals($log->getLogFilePath(), $this->basePath . '/SomeSortOf Filename (UAT).log'); |
|
|
|
|
34
|
|
|
$this->assertContains('Some test content', $log->content()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testLogWriting() { |
38
|
|
|
$log = new DeploynautLogFile('SomeSortOf Filename (UAT).log', $this->basePath); |
39
|
|
|
$this->assertFalse($log->exists()); |
|
|
|
|
40
|
|
|
$this->assertNull($log->getLogFilePath()); |
|
|
|
|
41
|
|
|
|
42
|
|
|
$log->write('This is some content'); |
43
|
|
|
$this->assertTrue($log->exists()); |
|
|
|
|
44
|
|
|
$this->assertEquals($this->basePath . '/somesortof-filename-uat.log', $log->getLogFilePath()); |
|
|
|
|
45
|
|
|
$this->assertContains('This is some content', $log->content()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testLogDoesntExistMessage() { |
49
|
|
|
$log = new DeploynautLogFile('SomeSortOf Filename (UAT).log', $this->basePath); |
50
|
|
|
$this->assertNull($log->getLogFilePath()); |
|
|
|
|
51
|
|
|
$this->assertEquals('Log has not been created yet.', $log->content()); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function tearDown() { |
55
|
|
|
parent::tearDown(); |
56
|
|
|
|
57
|
|
|
exec(sprintf('rm -rf %s', $this->basePath)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.