|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\Tests\Unit\Infrastructure; |
|
6
|
|
|
|
|
7
|
|
|
use FileFetcher\ErrorLoggingFileFetcher; |
|
8
|
|
|
use FileFetcher\InMemoryFileFetcher; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use Psr\Log\NullLogger; |
|
11
|
|
|
use WMDE\Fundraising\Frontend\Infrastructure\WordListFileReader; |
|
12
|
|
|
|
|
13
|
|
|
class WordListFileReaderTest extends TestCase { |
|
14
|
|
|
|
|
15
|
|
|
public function testGivenEmptyString_anEmptyListIsReturned(): void { |
|
16
|
|
|
$stringList = new WordListFileReader( $this->getFileFetcherWithContent( [ 'empty.txt' => '' ] ), 'empty.txt' ); |
|
17
|
|
|
$this->assertSame( [], $stringList->toArray() ); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function testGivenEmptyFilename_anEmptyListIsReturned(): void { |
|
21
|
|
|
$stringList = new WordListFileReader( $this->getFileFetcherWithContent( [ 'empty.txt' => '' ] ), '' ); |
|
22
|
|
|
$this->assertSame( [], $stringList->toArray() ); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testGivenMissingFile_fileFetchingExceptionIsCaught(): void { |
|
26
|
|
|
$stringList = new WordListFileReader( $this->getFileFetcherWithContent( [] ), 'utopia.txt' ); |
|
27
|
|
|
$this->assertSame( [], $stringList->toArray() ); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testGivenMultilineFile_eachLineIsReturned(): void { |
|
31
|
|
|
$stringList = new WordListFileReader( |
|
32
|
|
|
$this->getFileFetcherWithContent( [ 'words.txt' => "one\ntwo\nthree\ntechno" ] ), |
|
33
|
|
|
'words.txt' |
|
34
|
|
|
); |
|
35
|
|
|
$this->assertSame( [ 'one', 'two', 'three', 'techno' ], $stringList->toArray() ); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testGivenFileWithEmptyLinesAndTabs_whitespaceIsStripped(): void { |
|
39
|
|
|
$stringList = new WordListFileReader( |
|
40
|
|
|
$this->getFileFetcherWithContent( [ 'words.txt' => "one \n\ttwo\n\t\tthree\n\n\t\t\ttechno\r\n" ] ), |
|
41
|
|
|
'words.txt' |
|
42
|
|
|
); |
|
43
|
|
|
$this->assertEquals( [ 'one', 'two', 'three', 'techno' ], $stringList->toArray() ); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function getFileFetcherWithContent( array $content ): ErrorLoggingFileFetcher { |
|
47
|
|
|
return new ErrorLoggingFileFetcher( new InMemoryFileFetcher( $content ), new NullLogger() ); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|