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 |
||
| 5 | class EnvironmentFinderTest extends FileSystemTest |
||
| 6 | { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * The EnvironmentFinder class we'll be testing in this class. |
||
| 10 | * @var EnvironmentFinder |
||
| 11 | */ |
||
| 12 | protected $envFinder; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @test |
||
| 16 | */ |
||
| 17 | public function it_can_find_an_environment_varaible_in_a_dot_env_file() |
||
| 18 | { |
||
| 19 | file_put_contents("{$this->path}/.env", 'FOOBAR=BAZ'); |
||
| 20 | $result = $this->envFinder->getFromFile("{$this->path}/.env"); |
||
| 21 | $this->assertCount(1, $result); |
||
| 22 | $this->assertEquals('FOOBAR', $result[0]); |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @test |
||
| 27 | */ |
||
| 28 | public function it_can_find_multiple_environment_varaibles_in_a_dot_env_file() |
||
| 29 | { |
||
| 30 | file_put_contents("{$this->path}/.env", "FOOBAR=BAZ\nBAZBAR=FOO"); |
||
| 31 | $result = $this->envFinder->getFromFile("{$this->path}/.env"); |
||
| 32 | $this->assertCount(2, $result); |
||
| 33 | $this->assertEquals('FOOBAR', $result[0]); |
||
| 34 | $this->assertEquals('BAZBAR', $result[1]); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @test |
||
| 39 | */ |
||
| 40 | public function it_can_find_zero_environment_varaibles_in_a_dot_env_file() |
||
| 41 | { |
||
| 42 | file_put_contents("{$this->path}/.env", ""); |
||
| 43 | $result = $this->envFinder->getFromFile("{$this->path}/.env"); |
||
| 44 | $this->assertCount(0, $result); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @test |
||
| 49 | */ |
||
| 50 | public function it_can_find_an_environment_varaible_in_a_dot_env_example_file() |
||
| 51 | { |
||
| 52 | file_put_contents("{$this->path}/.env.example", 'FOOBAR=BAZ'); |
||
| 53 | $result = $this->envFinder->getFromFile("{$this->path}/.env.example"); |
||
| 54 | $this->assertCount(1, $result); |
||
| 55 | $this->assertEquals('FOOBAR', $result[0]); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @test |
||
| 60 | */ |
||
| 61 | public function it_can_find_multiple_environment_varaibles_in_a_dot_env_example_file() |
||
| 62 | { |
||
| 63 | file_put_contents("{$this->path}/.env.example", "FOOBAR=BAZ\nBAZBAR=FOO"); |
||
| 64 | $result = $this->envFinder->getFromFile("{$this->path}/.env.example"); |
||
| 65 | $this->assertCount(2, $result); |
||
| 66 | $this->assertEquals('FOOBAR', $result[0]); |
||
| 67 | $this->assertEquals('BAZBAR', $result[1]); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @test |
||
| 72 | */ |
||
| 73 | public function it_can_find_zero_environment_varaibles_in_a_dot_env_example_file() |
||
| 74 | { |
||
| 75 | file_put_contents("{$this->path}/.env.example", ""); |
||
| 76 | $result = $this->envFinder->getFromFile("{$this->path}/.env.example"); |
||
| 77 | $this->assertCount(0, $result); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @test |
||
| 82 | */ |
||
| 83 | public function it_can_find_an_environment_varaible_in_a_php_file() |
||
| 84 | { |
||
| 85 | file_put_contents("{$this->path}/foobar.php", "env('FOOBAR');"); |
||
| 86 | $result = $this->envFinder->getFromFile("{$this->path}/foobar.php"); |
||
| 87 | $this->assertCount(1, $result); |
||
| 88 | $this->assertEquals('FOOBAR', $result[0]); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @test |
||
| 93 | */ |
||
| 94 | public function it_can_find_multiple_environment_varaibles_in_a_php_file() |
||
| 95 | { |
||
| 96 | file_put_contents("{$this->path}/foobar.php", "env('FOOBAR'); env('BARBAZ');"); |
||
| 97 | $result = $this->envFinder->getFromFile("{$this->path}/foobar.php"); |
||
| 98 | $this->assertCount(2, $result); |
||
| 99 | $this->assertEquals('FOOBAR', $result[0]); |
||
| 100 | $this->assertEquals('BARBAZ', $result[1]); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @test |
||
| 105 | */ |
||
| 106 | public function it_can_find_zero_environment_varaibles_in_a_php_file() |
||
| 107 | { |
||
| 108 | file_put_contents("{$this->path}/foobar.php", ""); |
||
| 109 | $result = $this->envFinder->getFromFile("{$this->path}/foobar.php"); |
||
| 110 | $this->assertCount(0, $result); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @test |
||
| 115 | */ |
||
| 116 | public function if_the_php_file_doesnt_exist_then_no_results_are_found() |
||
| 117 | { |
||
| 118 | $result = $this->envFinder->getFromFile("{$this->path}/foobar.php"); |
||
| 119 | $this->assertCount(0, $result); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @test |
||
| 124 | */ |
||
| 125 | public function if_the_dot_env_file_doesnt_exist_then_no_results_are_found() |
||
| 126 | { |
||
| 127 | $result = $this->envFinder->getFromFile("{$this->path}/.env"); |
||
| 128 | $this->assertCount(0, $result); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @test |
||
| 133 | */ |
||
| 134 | public function if_the_dont_env_example_file_doesnt_exist_then_no_results_are_found() |
||
| 135 | { |
||
| 136 | $result = $this->envFinder->getFromFile("{$this->path}/.env.example"); |
||
| 137 | $this->assertCount(0, $result); |
||
| 138 | } |
||
| 139 | |||
| 140 | public function setup() |
||
| 141 | { |
||
| 142 | parent::setup(); |
||
| 143 | $this->envFinder = new EnvironmentFinder; |
||
| 144 | } |
||
| 145 | } |
||
| 146 |