Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 3 | class FileCollectorTest extends FileSystemTest |
||
| 4 | { |
||
| 5 | |||
| 6 | /** |
||
| 7 | * The FileCollector object we'l be testing in this class. |
||
| 8 | * @var Bmitch\Envsync\Collectors\FileCollector\FileCollector |
||
| 9 | */ |
||
| 10 | protected $fileCollector; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @test |
||
| 14 | */ |
||
| 15 | public function it_can_get_a_php_file_from_a_folder() |
||
| 16 | { |
||
| 17 | file_put_contents("{$this->path}/foobar.php", 'foobar.txt'); |
||
| 18 | $files = $this->fileCollector |
||
| 19 | ->get('.php') |
||
| 20 | ->from($this->path); |
||
| 21 | |||
| 22 | $this->assertCount(1, $files); |
||
| 23 | $this->assertContains('foobar.php', $files[0]); |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @test |
||
| 28 | */ |
||
| 29 | public function it_can_get_a_env_file_from_a_folder() |
||
| 30 | { |
||
| 31 | file_put_contents("{$this->path}/.env", 'foobar.txt'); |
||
| 32 | $files = $this->fileCollector |
||
| 33 | ->get('.env') |
||
| 34 | ->from($this->path); |
||
| 35 | |||
| 36 | $this->assertCount(1, $files); |
||
| 37 | $this->assertContains('.env', $files[0]); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @test |
||
| 42 | */ |
||
| 43 | public function it_can_get_a_env_example_file_from_a_folder() |
||
| 44 | { |
||
| 45 | file_put_contents("{$this->path}/.env.example", 'foobar.txt'); |
||
| 46 | $files = $this->fileCollector |
||
| 47 | ->get('.env.example') |
||
| 48 | ->from($this->path); |
||
| 49 | |||
| 50 | $this->assertCount(1, $files); |
||
| 51 | $this->assertContains('.env.example', $files[0]); |
||
| 52 | } |
||
| 53 | /** |
||
| 54 | * @test |
||
| 55 | */ |
||
| 56 | public function it_can_get_multiple_files_with_same_extension_from_a_folder() |
||
| 57 | { |
||
| 58 | file_put_contents("{$this->path}/foo.php", 'foo.txt'); |
||
| 59 | file_put_contents("{$this->path}/bar.php", 'bar.txt'); |
||
| 60 | file_put_contents("{$this->path}/baz.php", 'baz.txt'); |
||
| 61 | $files = $this->fileCollector |
||
| 62 | ->get('.php') |
||
| 63 | ->from($this->path); |
||
| 64 | |||
| 65 | $this->assertCount(3, $files); |
||
| 66 | $this->assertContains('foo.php', $files[0]); |
||
| 67 | $this->assertContains('bar.php', $files[1]); |
||
| 68 | $this->assertContains('baz.php', $files[2]); |
||
| 69 | } |
||
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * @test |
||
| 74 | */ |
||
| 75 | public function if_no_files_found_returns_empty_array() |
||
| 76 | { |
||
| 77 | $files = $this->fileCollector |
||
| 78 | ->get('.php') |
||
| 79 | ->from($this->path); |
||
| 80 | |||
| 81 | $this->assertCount(0, $files); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function setup() |
||
| 85 | { |
||
| 86 | parent::setup(); |
||
| 87 | $this->fileCollector = new Bmitch\Envsync\Collectors\FileCollector; |
||
| 88 | } |
||
| 89 | } |
||
| 90 |