1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TextFile\Tests\Writer; |
4
|
|
|
|
5
|
|
|
use TextFile\Tests\TextFileTestCase; |
6
|
|
|
use TextFile\Writer\PrependingWriter; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class PrependingWriterTest |
10
|
|
|
* |
11
|
|
|
* @package TextFile\Tests\Writer |
12
|
|
|
*/ |
13
|
|
View Code Duplication |
class PrependingWriterTest extends TextFileTestCase |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @covers TextFile\Writer\PrependingWriter::write |
17
|
|
|
*/ |
18
|
|
|
public function testWriteBeginning() |
19
|
|
|
{ |
20
|
|
|
$prependingWriter = new PrependingWriter(); |
21
|
|
|
|
22
|
|
|
$filePath = $this->createTestFileFromFixtures('complex_singleline_file.txt'); |
23
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
24
|
|
|
|
25
|
|
|
$prependingWriter->write($file, 'test'); |
26
|
|
|
|
27
|
|
|
$file->rewind(); |
28
|
|
|
|
29
|
|
|
$this->assertEquals('testfirstsecondthirdfourthfifth', trim($file->current())); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @covers TextFile\Writer\PrependingWriter::write |
34
|
|
|
*/ |
35
|
|
|
public function testWriteMiddle() |
36
|
|
|
{ |
37
|
|
|
$prependingWriter = new PrependingWriter(); |
38
|
|
|
|
39
|
|
|
$filePath = $this->createTestFileFromFixtures('complex_singleline_file.txt'); |
40
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
41
|
|
|
|
42
|
|
|
$file->fseek(5); |
43
|
|
|
|
44
|
|
|
$prependingWriter->write($file, 'test'); |
45
|
|
|
|
46
|
|
|
$file->rewind(); |
47
|
|
|
|
48
|
|
|
$this->assertEquals('firsttestsecondthirdfourthfifth', trim($file->current())); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @covers TextFile\Writer\ErasingWriter::write |
53
|
|
|
*/ |
54
|
|
|
public function testWriteMiddleNewLine() |
55
|
|
|
{ |
56
|
|
|
$prependingWriter = new PrependingWriter(); |
57
|
|
|
|
58
|
|
|
$filePath = $this->createTestFileFromFixtures('complex_singleline_file.txt'); |
59
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
60
|
|
|
|
61
|
|
|
$file->fseek(5); |
62
|
|
|
|
63
|
|
|
$prependingWriter->write($file, 'test', true); |
64
|
|
|
|
65
|
|
|
$file->rewind(); |
66
|
|
|
|
67
|
|
|
$this->assertEquals('firsttest', trim($file->current())); |
68
|
|
|
|
69
|
|
|
$file->next(); |
70
|
|
|
|
71
|
|
|
$this->assertEquals('secondthirdfourthfifth', trim($file->current())); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @covers TextFile\Writer\PrependingWriter::write |
76
|
|
|
*/ |
77
|
|
|
public function testWriteBeginningNewLine() |
78
|
|
|
{ |
79
|
|
|
$prependingWriter = new PrependingWriter(); |
80
|
|
|
|
81
|
|
|
$filePath = $this->createTestFileFromFixtures('complex_singleline_file.txt'); |
82
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
83
|
|
|
|
84
|
|
|
$prependingWriter->write($file, 'test', true); |
85
|
|
|
|
86
|
|
|
$file->rewind(); |
87
|
|
|
|
88
|
|
|
$this->assertEquals('test', trim($file->current())); |
89
|
|
|
|
90
|
|
|
$file->next(); |
91
|
|
|
|
92
|
|
|
$this->assertEquals('firstsecondthirdfourthfifth', trim($file->current())); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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.