1
|
|
|
<?php |
2
|
|
|
namespace Robo; |
3
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
use Robo\Traits\TestTasksTrait; |
6
|
|
|
|
7
|
|
|
class WriteFileTest extends TestCase |
8
|
|
|
{ |
9
|
|
|
use TestTasksTrait; |
10
|
|
|
use Task\File\loadTasks; |
11
|
|
|
|
12
|
|
|
protected $fixtures; |
13
|
|
|
|
14
|
|
|
public function setUp() |
15
|
|
|
{ |
16
|
|
|
$this->fixtures = new Fixtures(); |
17
|
|
|
$this->initTestTasksTrait(); |
18
|
|
|
$this->fixtures->createAndCdToSandbox(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function tearDown() |
22
|
|
|
{ |
23
|
|
|
$this->fixtures->cleanup(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
View Code Duplication |
public function writeFewLines() |
27
|
|
|
{ |
28
|
|
|
// write lines with WriteToFile task |
29
|
|
|
$result = $this->taskWriteToFile('blogpost.md') |
30
|
|
|
->line('****') |
31
|
|
|
->line('hello world') |
32
|
|
|
->line('****') |
33
|
|
|
->run(); |
34
|
|
|
$this->assertTrue($result->wasSuccessful(), $result->getMessage()); |
35
|
|
|
$this->assertFileExists('blogpost.md'); |
36
|
|
|
$contents = file_get_contents('blogpost.md'); |
37
|
|
|
$expreded = <<<HERE |
38
|
|
|
**** |
39
|
|
|
hello world |
40
|
|
|
**** |
41
|
|
|
|
42
|
|
|
HERE; |
43
|
|
|
$this->assertContains($expected, $contents); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function appendToFile() |
47
|
|
|
{ |
48
|
|
|
$result = $this->taskWriteToFile('a.txt') |
49
|
|
|
->append() |
50
|
|
|
->line('hello world') |
51
|
|
|
->run(); |
52
|
|
|
$this->assertTrue($result->wasSuccessful(), $result->getMessage()); |
53
|
|
|
$this->assertFileExists('a.txt'); |
54
|
|
|
$contents = file_get_contents('a.txt'); |
55
|
|
|
$expected = <<<HERE |
56
|
|
|
Ahello world |
57
|
|
|
|
58
|
|
|
HERE; |
59
|
|
|
$this->assertContains($expected, $contents); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testWouldChange() |
63
|
|
|
{ |
64
|
|
|
$writeTask = $this->taskWriteToFile('a.txt') |
65
|
|
|
->append(); |
66
|
|
|
$this->assertEquals(false, $writeTask->wouldChange(), "No changes to test file."); |
67
|
|
|
$writeTask->line('hello world'); |
68
|
|
|
$this->assertEquals(true, $writeTask->wouldChange(), "Test file would change."); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
public function insertFile() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$result = $this->taskWriteToFile('a.txt') |
74
|
|
|
->line('****') |
75
|
|
|
->textFromFile('b.txt') |
76
|
|
|
->line("C") |
77
|
|
|
->run(); |
78
|
|
|
$this->assertTrue($result->wasSuccessful(), $result->getMessage()); |
79
|
|
|
$this->assertFileExists('a.txt'); |
80
|
|
|
$contents = file_get_contents('a.txt'); |
81
|
|
|
$expected = <<<HERE |
82
|
|
|
**** |
83
|
|
|
BC |
84
|
|
|
|
85
|
|
|
HERE; |
86
|
|
|
$this->assertContains($expected, $contents); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function appendIfMatch() |
90
|
|
|
{ |
91
|
|
|
// append lines with WriteToFile task, but only if pattern does not match |
92
|
|
|
$result = $this->taskWriteToFile('blogpost.md') |
93
|
|
|
->line('****') |
94
|
|
|
->line('hello world') |
95
|
|
|
->line('****') |
96
|
|
|
->appendUnlessMatches('/hello/', 'Should not add this') |
97
|
|
|
->appendUnlessMatches('/goodbye/', 'Should add this') |
98
|
|
|
->appendIfMatches('/hello/', ' and should also add this') |
99
|
|
|
->appendIfMatches('/goodbye/', ' but should not add this') |
100
|
|
|
->appendIfMatches('/should/', '!') |
101
|
|
|
->run(); |
102
|
|
|
$this->assertTrue($result->wasSuccessful(), $result->getMessage()); |
103
|
|
|
$this->assertFileExists('blogpost.md'); |
104
|
|
|
$contents = file_get_contents('blogpost.md'); |
105
|
|
|
$expected = <<<HERE |
106
|
|
|
**** |
107
|
|
|
hello world |
108
|
|
|
**** |
109
|
|
|
Should add this and should also add this! |
110
|
|
|
HERE; |
111
|
|
|
$this->assertContains($expected, $contents); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function replaceInFile() |
115
|
|
|
{ |
116
|
|
|
$result = $this->taskReplaceInFile('a.txt') |
117
|
|
|
->from('A') |
118
|
|
|
->to('B') |
119
|
|
|
->run(); |
120
|
|
|
$this->assertTrue($result->wasSuccessful(), $result->getMessage()); |
121
|
|
|
$this->assertFileExists('a.txt'); |
122
|
|
|
$contents = file_get_contents('a.txt'); |
123
|
|
|
$this->assertContains('B', $contents); |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function replaceMultipleInFile() |
128
|
|
|
{ |
129
|
|
|
$result = $this->taskReplaceInFile('box/robo.txt') |
130
|
|
|
->from(array('HELLO', 'ROBO')) |
131
|
|
|
->to(array('Hello ', 'robo.li!')) |
132
|
|
|
->run(); |
133
|
|
|
$this->assertTrue($result->wasSuccessful(), $result->getMessage()); |
134
|
|
|
$this->assertFileExists('box/robo.txt'); |
135
|
|
|
$contents = file_get_contents('box/robo.txt'); |
136
|
|
|
$this->assertContains('Hello robo.li!', $contents); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
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.