| @@ 28-242 (lines=215) @@ | ||
| 25 | use Symfony\Component\Process\Exception\ProcessFailedException; |
|
| 26 | use Symfony\Component\Process\Process; |
|
| 27 | ||
| 28 | class HeadTest extends AbstractFileTestCase |
|
| 29 | { |
|
| 30 | /** |
|
| 31 | * @var Head |
|
| 32 | */ |
|
| 33 | protected $head; |
|
| 34 | ||
| 35 | /** |
|
| 36 | * @var ProcessFactory|MockInterface |
|
| 37 | */ |
|
| 38 | protected $processFactory; |
|
| 39 | ||
| 40 | public function setUp() |
|
| 41 | { |
|
| 42 | $this->processFactory = m::mock(ProcessFactory::class)->makePartial(); |
|
| 43 | $this->head = new Head(); |
|
| 44 | $this->head->setProcessFactory($this->processFactory); |
|
| 45 | } |
|
| 46 | ||
| 47 | public function testInstanceOf() |
|
| 48 | { |
|
| 49 | static::assertInstanceOf(FileModifierInterface::class, $this->head); |
|
| 50 | } |
|
| 51 | ||
| 52 | public function testCanModifyAcceptsLocalFile() |
|
| 53 | { |
|
| 54 | $localFile = m::mock(LocalFile::class); |
|
| 55 | $localFile->shouldReceive('exists')->andReturn(true, false); |
|
| 56 | ||
| 57 | static::assertTrue($this->head->canModify($localFile)); |
|
| 58 | static::assertFalse( |
|
| 59 | $this->head->canModify($localFile), |
|
| 60 | "CanExtend should return false if the file does not exist" |
|
| 61 | ); |
|
| 62 | ||
| 63 | $randomThing = m::mock(FileNodeInterface::class); |
|
| 64 | ||
| 65 | static::assertFalse($this->head->canModify($randomThing)); |
|
| 66 | } |
|
| 67 | ||
| 68 | public function testBasicReadingTheFirstNLines() |
|
| 69 | { |
|
| 70 | $file = $this->createFile('first_five_lines'); |
|
| 71 | ||
| 72 | $newFile = $this->head->head($file, 5); |
|
| 73 | ||
| 74 | static::assertEquals( |
|
| 75 | [ |
|
| 76 | "Line 1", |
|
| 77 | "Line 2", |
|
| 78 | "Line 3", |
|
| 79 | "Line 4", |
|
| 80 | "Line 5", |
|
| 81 | ], |
|
| 82 | $newFile->getContents() |
|
| 83 | ); |
|
| 84 | ||
| 85 | $newFile = $this->head->head($file, 2); |
|
| 86 | ||
| 87 | static::assertEquals( |
|
| 88 | [ |
|
| 89 | "Line 1", |
|
| 90 | "Line 2", |
|
| 91 | ], |
|
| 92 | $newFile->getContents() |
|
| 93 | ); |
|
| 94 | } |
|
| 95 | ||
| 96 | /** |
|
| 97 | * @param string $path |
|
| 98 | * |
|
| 99 | * @return LocalFile |
|
| 100 | */ |
|
| 101 | private function createFile($path) |
|
| 102 | { |
|
| 103 | $file = new LocalFile(static::$dir . $path); |
|
| 104 | $file->put( |
|
| 105 | "Line 1 |
|
| 106 | Line 2 |
|
| 107 | Line 3 |
|
| 108 | Line 4 |
|
| 109 | Line 5 |
|
| 110 | Line 6 |
|
| 111 | Line 7 |
|
| 112 | Line 8 |
|
| 113 | Line 9 |
|
| 114 | Line 10 |
|
| 115 | " |
|
| 116 | ); |
|
| 117 | ||
| 118 | return $file; |
|
| 119 | } |
|
| 120 | ||
| 121 | public function testOutputLinesUpToN() |
|
| 122 | { |
|
| 123 | $file = $this->createFile('from.second.line.onwards'); |
|
| 124 | ||
| 125 | $newFile = $this->head->head($file, '-2'); |
|
| 126 | ||
| 127 | static::assertEquals( |
|
| 128 | [ |
|
| 129 | "Line 1", |
|
| 130 | "Line 2", |
|
| 131 | "Line 3", |
|
| 132 | "Line 4", |
|
| 133 | "Line 5", |
|
| 134 | "Line 6", |
|
| 135 | "Line 7", |
|
| 136 | "Line 8", |
|
| 137 | ], |
|
| 138 | $newFile->getContents() |
|
| 139 | ); |
|
| 140 | } |
|
| 141 | ||
| 142 | public function testAddingAPostfixToTheEndOfTheFile() |
|
| 143 | { |
|
| 144 | $file = $this->createFile('postfix_test.test'); |
|
| 145 | ||
| 146 | $newFile = $this->head->head($file, 4, ['postfix' => 'pfixtest']); |
|
| 147 | ||
| 148 | static::assertNotNull($newFile); |
|
| 149 | static::assertEquals('postfix_test-pfixtest.test', $newFile->getFilename()); |
|
| 150 | } |
|
| 151 | ||
| 152 | public function testCallingWithBlankPostfixWillReplaceInLine() |
|
| 153 | { |
|
| 154 | $file = $this->createFile('inline_tail.test'); |
|
| 155 | ||
| 156 | $newFile = $this->head->head($file, 2, ['postfix' => '']); |
|
| 157 | ||
| 158 | static::assertNotNull($newFile); |
|
| 159 | static::assertEquals($file->getFilename(), $newFile->getFilename()); |
|
| 160 | } |
|
| 161 | ||
| 162 | public function testSettingKeepOldFileToFalseWillDeleteTheOldFile() |
|
| 163 | { |
|
| 164 | $file = $this->createFile('inline_replace.test'); |
|
| 165 | ||
| 166 | $newFile = $this->head->head($file, 5, ['keepOldFile' => false]); |
|
| 167 | ||
| 168 | static::assertTrue($newFile->exists()); |
|
| 169 | static::assertFalse($file->exists()); |
|
| 170 | } |
|
| 171 | ||
| 172 | public function testCallingModifyDoesTail() |
|
| 173 | { |
|
| 174 | $file = $this->createFile('simple_tail.test'); |
|
| 175 | ||
| 176 | $newFile = $this->head->modify($file, ['lines' => 4]); |
|
| 177 | ||
| 178 | static::assertEquals( |
|
| 179 | [ |
|
| 180 | "Line 1", |
|
| 181 | "Line 2", |
|
| 182 | "Line 3", |
|
| 183 | "Line 4", |
|
| 184 | ], |
|
| 185 | $newFile->getContents() |
|
| 186 | ); |
|
| 187 | } |
|
| 188 | ||
| 189 | public function testCallingModifyWillPassThroughOptions() |
|
| 190 | { |
|
| 191 | $file = $this->createFile('option_pass_through.test'); |
|
| 192 | ||
| 193 | $newFile = $this->head->modify( |
|
| 194 | $file, |
|
| 195 | [ |
|
| 196 | 'lines' => 2, |
|
| 197 | 'postfix' => 'pass', |
|
| 198 | 'keepOldFile' => false, |
|
| 199 | ] |
|
| 200 | ); |
|
| 201 | ||
| 202 | static::assertTrue($newFile->exists()); |
|
| 203 | static::assertFalse($file->exists()); |
|
| 204 | static::assertNotNull($newFile); |
|
| 205 | static::assertEquals('option_pass_through-pass.test', $newFile->getFilename()); |
|
| 206 | } |
|
| 207 | ||
| 208 | public function testCallingModifyWithoutLinesWillThrowAnException() |
|
| 209 | { |
|
| 210 | $file = $this->createFile('option_pass_through.test'); |
|
| 211 | ||
| 212 | $this->expectException(InvalidArgumentException::class); |
|
| 213 | ||
| 214 | $this->head->modify($file); |
|
| 215 | } |
|
| 216 | ||
| 217 | public function testCallingModifyWithANonLocalFileWillThrowAnException() |
|
| 218 | { |
|
| 219 | $file = m::mock(FileNodeInterface::class); |
|
| 220 | $file->shouldReceive('__toString') |
|
| 221 | ->andReturn('some/file/here'); |
|
| 222 | ||
| 223 | $this->expectException(InvalidArgumentException::class); |
|
| 224 | ||
| 225 | $this->head->modify($file, ['lines' => 1]); |
|
| 226 | } |
|
| 227 | ||
| 228 | public function testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding() |
|
| 229 | { |
|
| 230 | $process = m::mock(Process::class)->makePartial(); |
|
| 231 | $process->shouldReceive('isSuccessful')->andReturn(false); |
|
| 232 | $this->processFactory->shouldReceive('createProcess') |
|
| 233 | ->andReturn($process); |
|
| 234 | ||
| 235 | $file = new LocalFile(static::$dir . 'failed_tail.test'); |
|
| 236 | $file->put('nothing interesting here'); |
|
| 237 | ||
| 238 | $this->expectException(ProcessFailedException::class); |
|
| 239 | ||
| 240 | $this->head->head($file, 3); |
|
| 241 | } |
|
| 242 | } |
|
| 243 | ||
| @@ 28-243 (lines=216) @@ | ||
| 25 | use Symfony\Component\Process\Exception\ProcessFailedException; |
|
| 26 | use Symfony\Component\Process\Process; |
|
| 27 | ||
| 28 | class TailTest extends AbstractFileTestCase |
|
| 29 | { |
|
| 30 | /** |
|
| 31 | * @var Tail |
|
| 32 | */ |
|
| 33 | protected $tail; |
|
| 34 | ||
| 35 | /** |
|
| 36 | * @var ProcessFactory|MockInterface |
|
| 37 | */ |
|
| 38 | protected $processFactory; |
|
| 39 | ||
| 40 | public function setUp() |
|
| 41 | { |
|
| 42 | $this->processFactory = m::mock(ProcessFactory::class)->makePartial(); |
|
| 43 | $this->tail = new Tail(); |
|
| 44 | $this->tail->setProcessFactory($this->processFactory); |
|
| 45 | } |
|
| 46 | ||
| 47 | public function testInstanceOf() |
|
| 48 | { |
|
| 49 | static::assertInstanceOf(FileModifierInterface::class, $this->tail); |
|
| 50 | } |
|
| 51 | ||
| 52 | public function testCanModifyAcceptsLocalFile() |
|
| 53 | { |
|
| 54 | $localFile = m::mock(LocalFile::class); |
|
| 55 | $localFile->shouldReceive('exists')->andReturn(true, false); |
|
| 56 | ||
| 57 | static::assertTrue($this->tail->canModify($localFile)); |
|
| 58 | static::assertFalse( |
|
| 59 | $this->tail->canModify($localFile), |
|
| 60 | "CanExtend should return false if the file does not exist" |
|
| 61 | ); |
|
| 62 | ||
| 63 | $randomThing = m::mock(FileNodeInterface::class); |
|
| 64 | ||
| 65 | static::assertFalse($this->tail->canModify($randomThing)); |
|
| 66 | } |
|
| 67 | ||
| 68 | public function testBasicReadingTheLastNLines() |
|
| 69 | { |
|
| 70 | $file = $this->createFile('last_five_lines'); |
|
| 71 | ||
| 72 | $newFile = $this->tail->tail($file, 5); |
|
| 73 | ||
| 74 | static::assertEquals( |
|
| 75 | [ |
|
| 76 | "Line 6", |
|
| 77 | "Line 7", |
|
| 78 | "Line 8", |
|
| 79 | "Line 9", |
|
| 80 | "Line 10", |
|
| 81 | ], |
|
| 82 | $newFile->getContents() |
|
| 83 | ); |
|
| 84 | ||
| 85 | $newFile = $this->tail->tail($file, 2); |
|
| 86 | ||
| 87 | static::assertEquals( |
|
| 88 | [ |
|
| 89 | "Line 9", |
|
| 90 | "Line 10", |
|
| 91 | ], |
|
| 92 | $newFile->getContents() |
|
| 93 | ); |
|
| 94 | } |
|
| 95 | ||
| 96 | /** |
|
| 97 | * @param string $path |
|
| 98 | * |
|
| 99 | * @return LocalFile |
|
| 100 | */ |
|
| 101 | private function createFile($path) |
|
| 102 | { |
|
| 103 | $file = new LocalFile(static::$dir . $path); |
|
| 104 | $file->put( |
|
| 105 | "Line 1 |
|
| 106 | Line 2 |
|
| 107 | Line 3 |
|
| 108 | Line 4 |
|
| 109 | Line 5 |
|
| 110 | Line 6 |
|
| 111 | Line 7 |
|
| 112 | Line 8 |
|
| 113 | Line 9 |
|
| 114 | Line 10 |
|
| 115 | " |
|
| 116 | ); |
|
| 117 | ||
| 118 | return $file; |
|
| 119 | } |
|
| 120 | ||
| 121 | public function testOutputLinesStartingFromN() |
|
| 122 | { |
|
| 123 | $file = $this->createFile('from.second.line.onwards'); |
|
| 124 | ||
| 125 | $newFile = $this->tail->tail($file, '+2'); |
|
| 126 | ||
| 127 | static::assertEquals( |
|
| 128 | [ |
|
| 129 | "Line 2", |
|
| 130 | "Line 3", |
|
| 131 | "Line 4", |
|
| 132 | "Line 5", |
|
| 133 | "Line 6", |
|
| 134 | "Line 7", |
|
| 135 | "Line 8", |
|
| 136 | "Line 9", |
|
| 137 | "Line 10", |
|
| 138 | ], |
|
| 139 | $newFile->getContents() |
|
| 140 | ); |
|
| 141 | } |
|
| 142 | ||
| 143 | public function testAddingAPostfixToTheEndOfTheFile() |
|
| 144 | { |
|
| 145 | $file = $this->createFile('postfix_test.test'); |
|
| 146 | ||
| 147 | $newFile = $this->tail->tail($file, 4, ['postfix' => 'pfixtest']); |
|
| 148 | ||
| 149 | static::assertNotNull($newFile); |
|
| 150 | static::assertEquals('postfix_test-pfixtest.test', $newFile->getFilename()); |
|
| 151 | } |
|
| 152 | ||
| 153 | public function testCallingWithBlankPostfixWillReplaceInLine() |
|
| 154 | { |
|
| 155 | $file = $this->createFile('inline_tail.test'); |
|
| 156 | ||
| 157 | $newFile = $this->tail->tail($file, 2, ['postfix' => '']); |
|
| 158 | ||
| 159 | static::assertNotNull($newFile); |
|
| 160 | static::assertEquals($file->getFilename(), $newFile->getFilename()); |
|
| 161 | } |
|
| 162 | ||
| 163 | public function testSettingKeepOldFileToFalseWillDeleteTheOldFile() |
|
| 164 | { |
|
| 165 | $file = $this->createFile('inline_replace.test'); |
|
| 166 | ||
| 167 | $newFile = $this->tail->tail($file, 5, ['keepOldFile' => false]); |
|
| 168 | ||
| 169 | static::assertTrue($newFile->exists()); |
|
| 170 | static::assertFalse($file->exists()); |
|
| 171 | } |
|
| 172 | ||
| 173 | public function testCallingModifyDoesTail() |
|
| 174 | { |
|
| 175 | $file = $this->createFile('simple_tail.test'); |
|
| 176 | ||
| 177 | $newFile = $this->tail->modify($file, ['lines' => 4]); |
|
| 178 | ||
| 179 | static::assertEquals( |
|
| 180 | [ |
|
| 181 | "Line 7", |
|
| 182 | "Line 8", |
|
| 183 | "Line 9", |
|
| 184 | "Line 10", |
|
| 185 | ], |
|
| 186 | $newFile->getContents() |
|
| 187 | ); |
|
| 188 | } |
|
| 189 | ||
| 190 | public function testCallingModifyWillPassThroughOptions() |
|
| 191 | { |
|
| 192 | $file = $this->createFile('option_pass_through.test'); |
|
| 193 | ||
| 194 | $newFile = $this->tail->modify( |
|
| 195 | $file, |
|
| 196 | [ |
|
| 197 | 'lines' => 2, |
|
| 198 | 'postfix' => 'pass', |
|
| 199 | 'keepOldFile' => false, |
|
| 200 | ] |
|
| 201 | ); |
|
| 202 | ||
| 203 | static::assertTrue($newFile->exists()); |
|
| 204 | static::assertFalse($file->exists()); |
|
| 205 | static::assertNotNull($newFile); |
|
| 206 | static::assertEquals('option_pass_through-pass.test', $newFile->getFilename()); |
|
| 207 | } |
|
| 208 | ||
| 209 | public function testCallingModifyWithoutLinesWillThrowAnException() |
|
| 210 | { |
|
| 211 | $file = $this->createFile('option_pass_through.test'); |
|
| 212 | ||
| 213 | $this->expectException(InvalidArgumentException::class); |
|
| 214 | ||
| 215 | $this->tail->modify($file); |
|
| 216 | } |
|
| 217 | ||
| 218 | public function testCallingModifyWithANonLocalFileWillThrowAnException() |
|
| 219 | { |
|
| 220 | $file = m::mock(FileNodeInterface::class); |
|
| 221 | $file->shouldReceive('__toString') |
|
| 222 | ->andReturn('some/file/here'); |
|
| 223 | ||
| 224 | $this->expectException(InvalidArgumentException::class); |
|
| 225 | ||
| 226 | $this->tail->modify($file, ['lines' => 1]); |
|
| 227 | } |
|
| 228 | ||
| 229 | public function testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding() |
|
| 230 | { |
|
| 231 | $process = m::mock(Process::class)->makePartial(); |
|
| 232 | $process->shouldReceive('isSuccessful')->andReturn(false); |
|
| 233 | $this->processFactory->shouldReceive('createProcess') |
|
| 234 | ->andReturn($process); |
|
| 235 | ||
| 236 | $file = new LocalFile(static::$dir . 'failed_tail.test'); |
|
| 237 | $file->put('nothing interesting here'); |
|
| 238 | ||
| 239 | $this->expectException(ProcessFailedException::class); |
|
| 240 | ||
| 241 | $this->tail->tail($file, 3); |
|
| 242 | } |
|
| 243 | } |
|
| 244 | ||