Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
svenluijten committed Mar 23, 2016
1 parent c64206e commit 173b000
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 39 deletions.
31 changes: 31 additions & 0 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Sven\FlexEnv\Tests;

use Sven\FlexEnv\EnvEditor;

abstract class BaseTest extends \Orchestra\Testbench\TestCase
{
/**
* The EnvEditor instance.
*
* @var \Sven\FlexEnv\EnvEditor
*/
protected $flex;

/**
* Set up the testing suite.
*/
public function setUp()
{
$this->flex = new EnvEditor(__DIR__ . '/assets/.env');
}

/**
* Tear down the testing suite.
*/
public function tearDown()
{
unlink(__DIR__.'/assets/.env');
}
}
53 changes: 17 additions & 36 deletions tests/EnvEditorTest.php
Original file line number Diff line number Diff line change
@@ -1,82 +1,63 @@
<?php

namespace Sven\FlexEnv\Tests;

class EnvEditorTest extends Orchestra\Testbench\TestCase
class EnvEditorTest extends BaseTest
{
/** @test */
public function it_creates_an_env_file_if_none_exists()
{
@unlink(__DIR__.'/assets/.env');

$f = new Sven\FlexEnv\EnvEditor(__DIR__.'/assets/.env');

$this->assertEquals(__DIR__.'/assets/.env', $f->getPath());
$this->assertEquals(__DIR__ . '/assets/.env', $this->flex->getPath());
}

/** @test */
public function it_can_read_entries()
{
$f = new Sven\FlexEnv\EnvEditor(__DIR__.'/assets/.env');

file_put_contents(__DIR__.'/assets/.env', 'TEST=hello-world');
file_put_contents(__DIR__.'/assets/.env', "\nFOO_BAR=something else", FILE_APPEND);
file_put_contents(__DIR__ . '/assets/.env', 'TEST=hello-world');
file_put_contents(__DIR__ . '/assets/.env', "\nFOO_BAR=something else", FILE_APPEND);

$this->assertEquals('hello-world', $f->get('TEST'));
$this->assertEquals('something else', $f->get('FOO_BAR'));
$this->assertEquals('hello-world', $this->flex->get('TEST'));
$this->assertEquals('something else', $this->flex->get('FOO_BAR'));
}

/** @test */
public function it_returns_an_empty_string_if_value_not_found()
{
$f = new Sven\FlexEnv\EnvEditor(__DIR__.'/assets/.env');

$this->assertEquals('', $f->get('I_DO_NOT_EXIST'));
$this->assertEquals('', $this->flex->get('I_DO_NOT_EXIST'));
}

/** @test */
public function it_can_add_values()
{
@unlink(__DIR__.'/assets/.env');

$f = new Sven\FlexEnv\EnvEditor(__DIR__.'/assets/.env');

$result = $f->set('HELLO_WORLD', 'this-is-a-test')
->get('HELLO_WORLD');
$result = $this->flex->set('HELLO_WORLD', 'this-is-a-test')
->get('HELLO_WORLD');

$this->assertEquals('this-is-a-test', $result);
}

/** @test */
public function it_removes_an_entry()
{
@unlink(__DIR__.'/assets/.env');

$f = new Sven\FlexEnv\EnvEditor(__DIR__.'/assets/.env');

$result1 = $f->set('FOO_BAR', 'biz-baz')
->get('FOO_BAR');
$result1 = $this->flex->set('FOO_BAR', 'biz-baz')
->get('FOO_BAR');

$this->assertEquals('biz-baz', $result1);

$result2 = $f->delete('FOO_BAR')
->get('FOO_BAR');
$result2 = $this->flex->delete('FOO_BAR')
->get('FOO_BAR');

$this->assertEquals('', $result2);
}

/** @test */
public function it_lists_all_entries()
{
@unlink(__DIR__.'/assets/.env');

$f = new Sven\FlexEnv\EnvEditor(__DIR__.'/assets/.env');

$f->set('TESTING', 'hello-world')
->set('FOO_BAR', 'biz-baz');
$this->flex->set('TESTING', 'hello-world')
->set('FOO_BAR', 'biz-baz');

$this->assertEquals(
['TESTING' => 'hello-world', 'FOO_BAR' => 'biz-baz'],
$f->all()
$this->flex->all()
);
}
}
3 changes: 0 additions & 3 deletions tests/assets/.env

This file was deleted.

0 comments on commit 173b000

Please sign in to comment.