Skip to content

Commit

Permalink
Merge pull request #10 from drbyte/add-tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
denisdulici authored Jun 1, 2018
2 parents 80c75e6 + 841fba6 commit ebe1552
Show file tree
Hide file tree
Showing 11 changed files with 620 additions and 4 deletions.
30 changes: 30 additions & 0 deletions .travis.yml
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
15 changes: 11 additions & 4 deletions composer.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@
],
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.* || 5.3.* || 5.4.* || 5.5.*"
"laravel/framework": ">=5.2 <6.0"
},
"autoload": {
"psr-4": {
"require-dev": {
"phpunit/phpunit": ">=4.8, <6",
"mockery/mockery": "0.9.*",
"laravel/framework": ">=5.2 <6.0"
},
"autoload": {
"psr-4": {
"Akaunting\\Setting\\": "./src"
},
"files": [
Expand All @@ -32,5 +37,7 @@
"Setting": "Akaunting\\Setting\\Facade"
}
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
18 changes: 18 additions & 0 deletions phpunit.xml
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>
118 changes: 118 additions & 0 deletions tests/functional/AbstractFunctionalTest.php
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, []);
}
}
43 changes: 43 additions & 0 deletions tests/functional/DatabaseTest.php
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()
);
}
}
29 changes: 29 additions & 0 deletions tests/functional/JsonTest.php
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);
}
}
21 changes: 21 additions & 0 deletions tests/functional/MemoryTest.php
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);
}
}
Loading

0 comments on commit ebe1552

Please sign in to comment.