-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from drbyte/add-tests
Add tests
- Loading branch information
Showing
11 changed files
with
620 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
branches: | ||
only: | ||
- develop | ||
- master | ||
|
||
language: php | ||
|
||
php: | ||
- 5.5 | ||
- 5.6 | ||
- 7.1 | ||
- 7.2 | ||
|
||
matrix: | ||
allow_failures: | ||
- php: 7.2 | ||
env: COMPOSER_ARGS="--prefer-lowest" | ||
|
||
env: | ||
matrix: | ||
- COMPOSER_ARGS="" | ||
- COMPOSER_ARGS="--prefer-lowest" | ||
|
||
install: | ||
- composer update ${COMPOSER_ARGS} --no-interaction | ||
|
||
script: | ||
- vendor/bin/phpunit --verbose | ||
|
||
sudo: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
> | ||
<testsuites> | ||
<testsuite name="Package Test Suite"> | ||
<directory suffix=".php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
use Akaunting\Setting\Drivers\Database; | ||
|
||
abstract class AbstractFunctionalTest extends PHPUnit_Framework_TestCase | ||
{ | ||
abstract protected function createStore(array $data = []); | ||
|
||
protected function assertStoreEquals($store, $expected, $message = null) | ||
{ | ||
$this->assertEquals($expected, $store->all(), $message); | ||
$store->save(); | ||
$store = $this->createStore(); | ||
$this->assertEquals($expected, $store->all(), $message); | ||
} | ||
|
||
protected function assertStoreKeyEquals($store, $key, $expected, $message = null) | ||
{ | ||
$this->assertEquals($expected, $store->get($key), $message); | ||
$store->save(); | ||
$store = $this->createStore(); | ||
$this->assertEquals($expected, $store->get($key), $message); | ||
} | ||
|
||
/** @test */ | ||
public function store_is_initially_empty() | ||
{ | ||
$store = $this->createStore(); | ||
$this->assertEquals([], $store->all()); | ||
} | ||
|
||
/** @test */ | ||
public function written_changes_are_saved() | ||
{ | ||
$store = $this->createStore(); | ||
$store->set('foo', 'bar'); | ||
$this->assertStoreKeyEquals($store, 'foo', 'bar'); | ||
} | ||
|
||
/** @test */ | ||
public function nested_keys_are_nested() | ||
{ | ||
$store = $this->createStore(); | ||
$store->set('foo.bar', 'baz'); | ||
$this->assertStoreEquals($store, ['foo' => ['bar' => 'baz']]); | ||
} | ||
|
||
/** @test */ | ||
public function cannot_set_nested_key_on_non_array_member() | ||
{ | ||
$store = $this->createStore(); | ||
$store->set('foo', 'bar'); | ||
$this->setExpectedException('UnexpectedValueException', 'Non-array segment encountered'); | ||
$store->set('foo.bar', 'baz'); | ||
} | ||
|
||
/** @test */ | ||
public function can_forget_key() | ||
{ | ||
$store = $this->createStore(); | ||
$store->set('foo', 'bar'); | ||
$store->set('bar', 'baz'); | ||
$this->assertStoreEquals($store, ['foo' => 'bar', 'bar' => 'baz']); | ||
|
||
$store->forget('foo'); | ||
$this->assertStoreEquals($store, ['bar' => 'baz']); | ||
} | ||
|
||
/** @test */ | ||
public function can_forget_nested_key() | ||
{ | ||
$store = $this->createStore(); | ||
$store->set('foo.bar', 'baz'); | ||
$store->set('foo.baz', 'bar'); | ||
$store->set('bar.foo', 'baz'); | ||
$this->assertStoreEquals($store, [ | ||
'foo' => [ | ||
'bar' => 'baz', | ||
'baz' => 'bar', | ||
], | ||
'bar' => [ | ||
'foo' => 'baz', | ||
], | ||
]); | ||
|
||
$store->forget('foo.bar'); | ||
$this->assertStoreEquals($store, [ | ||
'foo' => [ | ||
'baz' => 'bar', | ||
], | ||
'bar' => [ | ||
'foo' => 'baz', | ||
], | ||
]); | ||
|
||
$store->forget('bar.foo'); | ||
$expected = [ | ||
'foo' => [ | ||
'baz' => 'bar', | ||
], | ||
'bar' => [ | ||
], | ||
]; | ||
if ($store instanceof Database) { | ||
unset($expected['bar']); | ||
} | ||
$this->assertStoreEquals($store, $expected); | ||
} | ||
|
||
/** @test */ | ||
public function can_forget_all() | ||
{ | ||
$store = $this->createStore(['foo' => 'bar']); | ||
$this->assertStoreEquals($store, ['foo' => 'bar']); | ||
$store->forgetAll(); | ||
$this->assertStoreEquals($store, []); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
class DatabaseTest extends AbstractFunctionalTest | ||
{ | ||
public function setUp() | ||
{ | ||
$this->container = new \Illuminate\Container\Container(); | ||
$this->capsule = new \Illuminate\Database\Capsule\Manager($this->container); | ||
$this->capsule->setAsGlobal(); | ||
$this->container['db'] = $this->capsule; | ||
$this->capsule->addConnection([ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
'prefix' => '', | ||
]); | ||
|
||
$this->capsule->schema()->create('settings', function ($t) { | ||
$t->string('key', 64)->unique(); | ||
$t->string('value', 4096); | ||
}); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
$this->capsule->schema()->drop('settings'); | ||
unset($this->capsule); | ||
unset($this->container); | ||
} | ||
|
||
protected function createStore(array $data = []) | ||
{ | ||
if ($data) { | ||
$store = $this->createStore(); | ||
$store->set($data); | ||
$store->save(); | ||
unset($store); | ||
} | ||
|
||
return new \Akaunting\Setting\Drivers\Database( | ||
$this->capsule->getConnection() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
class JsonTest extends AbstractFunctionalTest | ||
{ | ||
protected function createStore(array $data = null) | ||
{ | ||
$path = dirname(__DIR__).'/tmp/store.json'; | ||
|
||
if ($data !== null) { | ||
if ($data) { | ||
$json = json_encode($data); | ||
} else { | ||
$json = '{}'; | ||
} | ||
|
||
file_put_contents($path, $json); | ||
} | ||
|
||
return new \Akaunting\Setting\Drivers\Json( | ||
new \Illuminate\Filesystem\Filesystem(), $path | ||
); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
$path = dirname(__DIR__).'/tmp/store.json'; | ||
unlink($path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
class MemoryTest extends AbstractFunctionalTest | ||
{ | ||
protected function assertStoreEquals($store, $expected, $message = null) | ||
{ | ||
$this->assertEquals($expected, $store->all(), $message); | ||
// removed persistance test assertions | ||
} | ||
|
||
protected function assertStoreKeyEquals($store, $key, $expected, $message = null) | ||
{ | ||
$this->assertEquals($expected, $store->get($key), $message); | ||
// removed persistance test assertions | ||
} | ||
|
||
protected function createStore(array $data = null) | ||
{ | ||
return new \Akaunting\Setting\Drivers\Memory($data); | ||
} | ||
} |
Oops, something went wrong.