From e6a8565cfb67fd5c2f72a14d284b59de7d4021e4 Mon Sep 17 00:00:00 2001 From: Andreas Lutro Date: Thu, 14 May 2015 11:11:18 +0200 Subject: [PATCH 1/7] misc improvements for 0.3 - reorganized tests - fixed database data consistency with empty arrays - slight json store improvements --- composer.json | 3 +- src/DatabaseSettingStore.php | 28 ++++ src/JsonSettingStore.php | 12 +- src/SettingStore.php | 11 ++ tests/DatabaseSettingFunctionalTest.php | 99 ------------- tests/DatabaseSettingStoreTest.php | 156 -------------------- tests/JsonSettingStoreTest.php | 119 --------------- tests/functional/AbstractFunctionalTest.php | 119 +++++++++++++++ tests/functional/DatabaseTest.php | 43 ++++++ tests/functional/JsonTest.php | 29 ++++ tests/tmp/.gitignore | 2 + tests/{ => unit}/ArrayUtilTest.php | 0 tests/unit/DatabaseSettingStoreTest.php | 102 +++++++++++++ tests/unit/JsonSettingStoreTest.php | 60 ++++++++ 14 files changed, 402 insertions(+), 381 deletions(-) delete mode 100644 tests/DatabaseSettingFunctionalTest.php delete mode 100644 tests/DatabaseSettingStoreTest.php delete mode 100644 tests/JsonSettingStoreTest.php create mode 100644 tests/functional/AbstractFunctionalTest.php create mode 100644 tests/functional/DatabaseTest.php create mode 100644 tests/functional/JsonTest.php create mode 100644 tests/tmp/.gitignore rename tests/{ => unit}/ArrayUtilTest.php (100%) create mode 100644 tests/unit/DatabaseSettingStoreTest.php create mode 100644 tests/unit/JsonSettingStoreTest.php diff --git a/composer.json b/composer.json index cb1122f..38a682d 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ }, "require-dev": { "mockery/mockery": "0.9.*", - "illuminate/database": ">=4.1 <6.0" + "illuminate/database": ">=4.1 <6.0", + "illuminate/filesystem": ">=4.1 <6.0" }, "autoload": { "psr-4": { diff --git a/src/DatabaseSettingStore.php b/src/DatabaseSettingStore.php index 0749ed1..3fe4745 100644 --- a/src/DatabaseSettingStore.php +++ b/src/DatabaseSettingStore.php @@ -68,6 +68,8 @@ public function setTable($table) */ public function setConstraint(\Closure $callback) { + $this->data = array(); + $this->loaded = false; $this->queryConstraint = $callback; } @@ -81,6 +83,32 @@ public function setExtraColumns(array $columns) $this->extraColumns = $columns; } + /** + * {@inheritdoc} + */ + public function forget($key) + { + parent::forget($key); + + // because the database store cannot store empty arrays, remove empty + // arrays to keep data consistent before and after saving + $segments = explode('.', $key); + array_pop($segments); + + while ($segments) { + $segment = implode('.', $segments); + + // non-empty array - exit out of the loop + if ($this->get($segment)) { + break; + } + + // remove the empty array and move on to the next segment + $this->forget($segment); + array_pop($segments); + } + } + /** * {@inheritdoc} */ diff --git a/src/JsonSettingStore.php b/src/JsonSettingStore.php index ceb5c46..f7dd3e7 100644 --- a/src/JsonSettingStore.php +++ b/src/JsonSettingStore.php @@ -32,7 +32,7 @@ public function setPath($path) { // If the file does not already exist, we will attempt to create it. if (!$this->files->exists($path)) { - $result = $this->files->put($path, json_encode(array())); + $result = $this->files->put($path, '{}'); if ($result === false) { throw new \InvalidArgumentException("Could not write to $path."); } @@ -50,10 +50,6 @@ public function setPath($path) */ protected function read() { - if (!$this->files->exists($this->path)) { - return array(); - } - $contents = $this->files->get($this->path); $data = json_decode($contents, true); @@ -70,7 +66,11 @@ protected function read() */ protected function write(array $data) { - $contents = json_encode($data); + if ($data) { + $contents = json_encode($data); + } else { + $contents = '{}'; + } $this->files->put($this->path, $contents); } diff --git a/src/SettingStore.php b/src/SettingStore.php index a981f19..90d4bce 100644 --- a/src/SettingStore.php +++ b/src/SettingStore.php @@ -95,6 +95,17 @@ public function forget($key) } } + /** + * Unset all keys in the settings data. + * + * @return void + */ + public function forgetAll() + { + $this->unsaved = true; + $this->data = []; + } + /** * Get all settings data. * diff --git a/tests/DatabaseSettingFunctionalTest.php b/tests/DatabaseSettingFunctionalTest.php deleted file mode 100644 index f04528a..0000000 --- a/tests/DatabaseSettingFunctionalTest.php +++ /dev/null @@ -1,99 +0,0 @@ - - * @license http://opensource.org/licenses/MIT - * @package l4-settings - */ - -use Mockery as m; - -class DatabaseSettingFunctionalTest extends PHPUnit_Framework_TestCase -{ - protected $container; - protected $capsule; - - 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(array( - 'driver' => 'sqlite', - 'database' => ':memory:', - 'prefix' => '', - )); - - $this->capsule->schema()->create('persistant_settings', function($t) { - $t->string('key', 64)->unique(); - $t->string('value', 4096); - }); - } - - public function tearDown() - { - $this->capsule->schema()->drop('persistant_settings'); - unset($this->capsule); - unset($this->container); - } - - public function makeSettingStore() - { - return new \anlutro\LaravelSettings\DatabaseSettingStore($this->capsule->getConnection()); - } - - /** @test */ - public function stuffWorks() - { - // batch 1 of data - $s = $this->makeSettingStore(); - $s->set(array( - 'one' => 'one_old', - 'two.one' => 'one_old', - 'two.two' => 'two_old', - 'three.one' => 'one_old', - )); - $s->save(); - - // batch 2 of data - $s = $this->makeSettingStore(); - $s->set(array( - 'one' => 'one_new', - 'two.two' => 'two_new', - 'three.two' => 'two_new', - )); - $s->save(); - - // batch 3 of data - $s = $this->makeSettingStore(); - $s->set(array( - 'one' => 'one_extra_new', - )); - $s->save(); - - // check that data is correct - $expected = array( - 'one' => 'one_extra_new', - 'two' => array( - 'one' => 'one_old', - 'two' => 'two_new', - ), - 'three' => array( - 'one' => 'one_old', - 'two' => 'two_new', - ), - ); - $s = $this->makeSettingStore(); - $this->assertEquals($expected, $s->all()); - - // remove a setting - $s = $this->makeSettingStore(); - $s->forget('two.two'); - $s->save(); - $s = $this->makeSettingStore(); - unset($expected['two']['two']); - $this->assertEquals($expected, $s->all()); - } -} diff --git a/tests/DatabaseSettingStoreTest.php b/tests/DatabaseSettingStoreTest.php deleted file mode 100644 index 3313f20..0000000 --- a/tests/DatabaseSettingStoreTest.php +++ /dev/null @@ -1,156 +0,0 @@ - - * @license http://opensource.org/licenses/MIT - * @package l4-settings - */ - -use Mockery as m; - -class DatabaseSettingStoreTest extends PHPUnit_Framework_TestCase -{ - public function tearDown() - { - m::close(); - } - - public function testGetAndSet() - { - $connection = $this->makeConnection(); - $query = $this->makeQuery($connection); - $query->shouldReceive('get')->once()->andReturn(array()); - $store = $this->makeStore($connection); - - $store->set('foo', 'bar'); - $this->assertEquals('bar', $store->get('foo')); - } - - public function testCorrectDataIsInserted() - { - $connection = $this->makeConnection(); - $query = $this->makeQuery($connection); - - $query->shouldReceive('get')->once()->andReturn(array()); - $query->shouldReceive('lists')->once()->andReturn(array()); - $query->shouldReceive('insert')->once()->with($this->getDbData()); - - $store = $this->makeStore($connection); - $store->set('foo', 'bar'); - $store->set('nest.one', 'nestone'); - $store->set('nest.two', 'nesttwo'); - $store->set('array', array('one', 'two')); - $store->save(); - } - - public function testCorrectDataIsUpdated() - { - $connection = $this->makeConnection(); - $query = $this->makeQuery($connection); - - $query->shouldReceive('get')->once()->andReturn(array(array('key' => 'nest.one', 'value' => 'old'))); - $query->shouldReceive('lists')->once()->andReturn(array('nest.one')); - $dbData = $this->getDbData(); - unset($dbData[1]); // remove the nest.one array member - $query->shouldReceive('where')->with('key', '=', 'nest.one')->andReturn(m::self())->getMock() - ->shouldReceive('update')->with(array('value' => 'nestone')); - $self = $this; // 5.3 compatibility - $query->shouldReceive('insert')->once()->andReturnUsing(function($arg) use($dbData, $self) { - $self->assertEquals(count($dbData), count($arg)); - foreach ($dbData as $key => $value) { - $self->assertContains($value, $arg); - } - }); - - $store = $this->makeStore($connection); - $store->set('foo', 'bar'); - $store->set('nest.one', 'nestone'); - $store->set('nest.two', 'nesttwo'); - $store->set('array', array('one', 'two')); - $store->save(); - } - - public function testCorrectDataIsRead() - { - $connection = $this->makeConnection(); - $query = $this->makeQuery($connection); - - $query->shouldReceive('get')->once()->andReturn($this->getDbData()); - - $store = $this->makeStore($connection); - $this->assertEquals('bar', $store->get('foo')); - $this->assertEquals('nestone', $store->get('nest.one')); - $this->assertEquals('nesttwo', $store->get('nest.two')); - $this->assertEquals(array('one', 'two'), $store->get('array')); - } - - public function testExtraColumnsAreQueried() - { - $connection = $this->makeConnection(); - $query = $this->makeQuery($connection); - $query->shouldReceive('where')->once()->with('foo', '=', 'bar') - ->andReturn(m::self())->getMock() - ->shouldReceive('get')->once()->andReturn(array(array('key' => 'foo', 'value' => 'bar'))); - - $store = $this->makeStore($connection); - $store->setExtraColumns(array('foo' => 'bar')); - $this->assertEquals('bar', $store->get('foo')); - } - - public function testExtraColumnsAreInserted() - { - $connection = $this->makeConnection(); - $query = $this->makeQuery($connection); - $query->shouldReceive('where')->times(2)->with('extracol', '=', 'extradata') - ->andReturn(m::self()); - $query->shouldReceive('get')->once()->andReturn(array()); - $query->shouldReceive('lists')->once()->andReturn(array()); - $query->shouldReceive('insert')->once()->with(array(array('key' => 'foo', 'value' => 'bar', 'extracol' => 'extradata'))); - - $store = $this->makeStore($connection); - $store->setExtraColumns(array('extracol' => 'extradata')); - $store->set('foo', 'bar'); - $store->save(); - } - - protected function getPhpData() - { - return array( - 'foo' => 'bar', - 'nest' => array( - 'one' => 'nestone', - 'two' => 'nesttwo', - ), - 'array' => array('one', 'two'), - ); - } - - protected function getDbData() - { - return array( - array('key' => 'foo', 'value' => 'bar'), - array('key' => 'nest.one', 'value' => 'nestone'), - array('key' => 'nest.two', 'value' => 'nesttwo'), - array('key' => 'array.0', 'value' => 'one'), - array('key' => 'array.1', 'value' => 'two'), - ); - } - - protected function makeConnection() - { - return m::mock('Illuminate\Database\Connection'); - } - - protected function makeQuery($connection) - { - $query = m::mock('Illuminate\Database\Query\Builder'); - $connection->shouldReceive('table')->andReturn($query); - return $query; - } - - protected function makeStore($connection) - { - return new anlutro\LaravelSettings\DatabaseSettingStore($connection); - } -} diff --git a/tests/JsonSettingStoreTest.php b/tests/JsonSettingStoreTest.php deleted file mode 100644 index 42ff869..0000000 --- a/tests/JsonSettingStoreTest.php +++ /dev/null @@ -1,119 +0,0 @@ - - * @license http://opensource.org/licenses/MIT - * @package l4-settings - */ - - -use Mockery as m; - -class JsonSettingStoreTest extends PHPUnit_Framework_TestCase -{ - /** - * @expectedException InvalidArgumentException - */ - public function testExceptionWhenNotWritable() - { - $files = m::mock('Illuminate\Filesystem\Filesystem'); - $files->shouldReceive('exists')->once()->with('fakepath')->andReturn(true); - $files->shouldReceive('isWritable')->once()->with('fakepath')->andReturn(false); - $store = $this->makeStore($files); - } - - /** - * @expectedException InvalidArgumentException - */ - public function testExceptionWhenPutFails() - { - $files = m::mock('Illuminate\Filesystem\Filesystem'); - $files->shouldReceive('exists')->once()->with('fakepath')->andReturn(false); - $files->shouldReceive('put')->once()->with('fakepath', '[]')->andReturn(false); - $store = $this->makeStore($files); - } - - public function testGetAndSet() - { - $files = $this->makeFilesystem(); - $files->shouldReceive('exists')->once()->with('fakepath')->andReturn(true); - $files->shouldReceive('isWritable')->once()->with('fakepath')->andReturn(true); - $files->shouldReceive('get')->once()->with('fakepath')->andReturn('[]'); - - $store = $this->makeStore($files); - $store->set('foo', 'bar'); - $this->assertEquals('bar', $store->get('foo')); - } - - public function testWriteData() - { - $files = $this->makeFilesystem(); - $files->shouldReceive('exists')->once()->with('fakepath')->andReturn(true); - $files->shouldReceive('isWritable')->once()->with('fakepath')->andReturn(true); - $files->shouldReceive('get')->once()->with('fakepath')->andReturn('[]'); - $files->shouldReceive('put')->once()->with('fakepath', $this->getJsonData()); - - $store = $this->makeStore($files); - $store->set('foo', 'bar'); - $store->set('nest.one', 'nestone'); - $store->set('nest.two', 'nesttwo'); - $store->set('array', array('one', 'two')); - $store->save(); - } - - public function testReadData() - { - $files = $this->makeFilesystem(); - $files->shouldReceive('exists')->once()->with('fakepath')->andReturn(true); - $files->shouldReceive('isWritable')->once()->with('fakepath')->andReturn(true); - $files->shouldReceive('get')->once()->with('fakepath')->andReturn($this->getJsonData()); - - $store = $this->makeStore($files); - $this->assertEquals('bar', $store->get('foo')); - $this->assertEquals('nestone', $store->get('nest.one')); - $this->assertEquals('nesttwo', $store->get('nest.two')); - $this->assertEquals(array('one', 'two'), $store->get('array')); - } - - /** - * @expectedException RuntimeException - */ - public function testExceptionWhenInvalidJson() - { - $files = $this->makeFilesystem(); - $files->shouldReceive('exists')->once()->with('fakepath')->andReturn(true); - $files->shouldReceive('isWritable')->once()->with('fakepath')->andReturn(true); - $files->shouldReceive('get')->once()->with('fakepath')->andReturn('[[!1!11]'); - - $store = $this->makeStore($files); - $store->get('foo'); - } - - protected function getTestData() - { - return array( - 'foo' => 'bar', - 'nest' => array( - 'one' => 'nestone', - 'two' => 'nesttwo', - ), - 'array' => array('one', 'two'), - ); - } - - protected function getJsonData() - { - return json_encode($this->getTestData()); - } - - protected function makeFilesystem($exists = true, $isWritable = true) - { - return m::mock('Illuminate\Filesystem\Filesystem'); - } - - protected function makeStore($files, $path = 'fakepath') - { - return new anlutro\LaravelSettings\JsonSettingStore($files, $path); - } -} diff --git a/tests/functional/AbstractFunctionalTest.php b/tests/functional/AbstractFunctionalTest.php new file mode 100644 index 0000000..15eb50f --- /dev/null +++ b/tests/functional/AbstractFunctionalTest.php @@ -0,0 +1,119 @@ +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 DatabaseSettingStore) { + 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, []); + } +} diff --git a/tests/functional/DatabaseTest.php b/tests/functional/DatabaseTest.php new file mode 100644 index 0000000..24b60ba --- /dev/null +++ b/tests/functional/DatabaseTest.php @@ -0,0 +1,43 @@ +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(array( + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + )); + + $this->capsule->schema()->create('persistant_settings', function($t) { + $t->string('key', 64)->unique(); + $t->string('value', 4096); + }); + } + + public function tearDown() + { + $this->capsule->schema()->drop('persistant_settings'); + unset($this->capsule); + unset($this->container); + } + + protected function createStore(array $data = array()) + { + if ($data) { + $store = $this->createStore(); + $store->set($data); + $store->save(); + unset($store); + } + + return new \anlutro\LaravelSettings\DatabaseSettingStore( + $this->capsule->getConnection() + ); + } +} diff --git a/tests/functional/JsonTest.php b/tests/functional/JsonTest.php new file mode 100644 index 0000000..6e256fd --- /dev/null +++ b/tests/functional/JsonTest.php @@ -0,0 +1,29 @@ +mockConnection(); + $query = $this->mockQuery($connection); + + $query->shouldReceive('get')->once()->andReturn(array( + array('key' => 'nest.one', 'value' => 'old'), + )); + $query->shouldReceive('lists')->once()->andReturn(array('nest.one')); + $dbData = $this->getDbData(); + unset($dbData[1]); // remove the nest.one array member + $query->shouldReceive('where')->with('key', '=', 'nest.one')->andReturn(m::self())->getMock() + ->shouldReceive('update')->with(array('value' => 'nestone')); + $self = $this; // 5.3 compatibility + $query->shouldReceive('insert')->once()->andReturnUsing(function($arg) use($dbData, $self) { + $self->assertEquals(count($dbData), count($arg)); + foreach ($dbData as $key => $value) { + $self->assertContains($value, $arg); + } + }); + + $store = $this->makeStore($connection); + $store->set('foo', 'bar'); + $store->set('nest.one', 'nestone'); + $store->set('nest.two', 'nesttwo'); + $store->set('array', array('one', 'two')); + $store->save(); + } + + public function testExtraColumnsAreQueried() + { + $connection = $this->mockConnection(); + $query = $this->mockQuery($connection); + $query->shouldReceive('where')->once()->with('foo', '=', 'bar') + ->andReturn(m::self())->getMock() + ->shouldReceive('get')->once()->andReturn(array( + array('key' => 'foo', 'value' => 'bar'), + )); + + $store = $this->makeStore($connection); + $store->setExtraColumns(array('foo' => 'bar')); + $this->assertEquals('bar', $store->get('foo')); + } + + public function testExtraColumnsAreInserted() + { + $connection = $this->mockConnection(); + $query = $this->mockQuery($connection); + $query->shouldReceive('where')->times(2)->with('extracol', '=', 'extradata') + ->andReturn(m::self()); + $query->shouldReceive('get')->once()->andReturn(array()); + $query->shouldReceive('lists')->once()->andReturn(array()); + $query->shouldReceive('insert')->once()->with(array( + array('key' => 'foo', 'value' => 'bar', 'extracol' => 'extradata'), + )); + + $store = $this->makeStore($connection); + $store->setExtraColumns(array('extracol' => 'extradata')); + $store->set('foo', 'bar'); + $store->save(); + } + + protected function getDbData() + { + return array( + array('key' => 'foo', 'value' => 'bar'), + array('key' => 'nest.one', 'value' => 'nestone'), + array('key' => 'nest.two', 'value' => 'nesttwo'), + array('key' => 'array.0', 'value' => 'one'), + array('key' => 'array.1', 'value' => 'two'), + ); + } + + protected function mockConnection() + { + return m::mock('Illuminate\Database\Connection'); + } + + protected function mockQuery($connection) + { + $query = m::mock('Illuminate\Database\Query\Builder'); + $connection->shouldReceive('table')->andReturn($query); + return $query; + } + + protected function makeStore($connection) + { + return new anlutro\LaravelSettings\DatabaseSettingStore($connection); + } +} diff --git a/tests/unit/JsonSettingStoreTest.php b/tests/unit/JsonSettingStoreTest.php new file mode 100644 index 0000000..c98bc74 --- /dev/null +++ b/tests/unit/JsonSettingStoreTest.php @@ -0,0 +1,60 @@ +mockFilesystem(); + $files->shouldReceive('exists')->once()->with('fakepath')->andReturn(true); + $files->shouldReceive('isWritable')->once()->with('fakepath')->andReturn(false); + $store = $this->makeStore($files); + } + + /** + * @test + * @expectedException InvalidArgumentException + */ + public function throws_exception_when_files_put_fails() + { + $files = $this->mockFilesystem(); + $files->shouldReceive('exists')->once()->with('fakepath')->andReturn(false); + $files->shouldReceive('put')->once()->with('fakepath', '[]')->andReturn(false); + $store = $this->makeStore($files); + } + + /** + * @test + * @expectedException RuntimeException + */ + public function throws_exception_when_file_contains_invalid_json() + { + $files = $this->mockFilesystem(); + $files->shouldReceive('exists')->once()->with('fakepath')->andReturn(true); + $files->shouldReceive('isWritable')->once()->with('fakepath')->andReturn(true); + $files->shouldReceive('get')->once()->with('fakepath')->andReturn('[[!1!11]'); + + $store = $this->makeStore($files); + $store->get('foo'); + } +} From 4b89cd9e8f1509a114e0c5fb9ed841e12fb92096 Mon Sep 17 00:00:00 2001 From: Andreas Lutro Date: Thu, 14 May 2015 11:20:34 +0200 Subject: [PATCH 2/7] test 5.3 --- .travis.yml | 1 + tests/functional/AbstractFunctionalTest.php | 48 +++---- tests/unit/ArrayUtilTest.php | 134 ++++++++++---------- tests/unit/JsonSettingStoreTest.php | 2 +- 4 files changed, 93 insertions(+), 92 deletions(-) diff --git a/.travis.yml b/.travis.yml index a6cf419..d0556ce 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ branches: language: php php: + - 5.3 - 5.4 - 5.5 - 5.6 diff --git a/tests/functional/AbstractFunctionalTest.php b/tests/functional/AbstractFunctionalTest.php index 15eb50f..9968d5c 100644 --- a/tests/functional/AbstractFunctionalTest.php +++ b/tests/functional/AbstractFunctionalTest.php @@ -43,7 +43,7 @@ public function nested_keys_are_nested() { $store = $this->createStore(); $store->set('foo.bar', 'baz'); - $this->assertStoreEquals($store, ['foo' => ['bar' => 'baz']]); + $this->assertStoreEquals($store, array('foo' => array('bar' => 'baz'))); } /** @test */ @@ -61,10 +61,10 @@ public function can_forget_key() $store = $this->createStore(); $store->set('foo', 'bar'); $store->set('bar', 'baz'); - $this->assertStoreEquals($store, ['foo' => 'bar', 'bar' => 'baz']); + $this->assertStoreEquals($store, array('foo' => 'bar', 'bar' => 'baz')); $store->forget('foo'); - $this->assertStoreEquals($store, ['bar' => 'baz']); + $this->assertStoreEquals($store, array('bar' => 'baz')); } /** @test */ @@ -74,34 +74,34 @@ public function can_forget_nested_key() $store->set('foo.bar', 'baz'); $store->set('foo.baz', 'bar'); $store->set('bar.foo', 'baz'); - $this->assertStoreEquals($store, [ - 'foo' => [ + $this->assertStoreEquals($store, array( + 'foo' => array( 'bar' => 'baz', 'baz' => 'bar', - ], - 'bar' => [ + ), + 'bar' => array( 'foo' => 'baz', - ], - ]); + ), + )); $store->forget('foo.bar'); - $this->assertStoreEquals($store, [ - 'foo' => [ + $this->assertStoreEquals($store, array( + 'foo' => array( 'baz' => 'bar', - ], - 'bar' => [ + ), + 'bar' => array( 'foo' => 'baz', - ], - ]); + ), + )); $store->forget('bar.foo'); - $expected = [ - 'foo' => [ + $expected = array( + 'foo' => array( 'baz' => 'bar', - ], - 'bar' => [ - ], - ]; + ), + 'bar' => array( + ), + ); if ($store instanceof DatabaseSettingStore) { unset($expected['bar']); } @@ -111,9 +111,9 @@ public function can_forget_nested_key() /** @test */ public function can_forget_all() { - $store = $this->createStore(['foo' => 'bar']); - $this->assertStoreEquals($store, ['foo' => 'bar']); + $store = $this->createStore(array('foo' => 'bar')); + $this->assertStoreEquals($store, array('foo' => 'bar')); $store->forgetAll(); - $this->assertStoreEquals($store, []); + $this->assertStoreEquals($store, array()); } } diff --git a/tests/unit/ArrayUtilTest.php b/tests/unit/ArrayUtilTest.php index f55e6ed..90b934d 100644 --- a/tests/unit/ArrayUtilTest.php +++ b/tests/unit/ArrayUtilTest.php @@ -15,35 +15,35 @@ public function getReturnsCorrectValue(array $data, $key, $expected) public function getGetData() { - return [ - [[], 'foo', null], - [['foo' => 'bar'], 'foo', 'bar'], - [['foo' => 'bar'], 'bar', null], - [['foo' => 'bar'], 'foo.bar', null], - [['foo' => ['bar' => 'baz']], 'foo.bar', 'baz'], - [['foo' => ['bar' => 'baz']], 'foo.baz', null], - [['foo' => ['bar' => 'baz']], 'foo', ['bar' => 'baz']], - [ - ['foo' => 'bar', 'bar' => 'baz'], - ['foo', 'bar'], - ['foo' => 'bar', 'bar' => 'baz'] - ], - [ - ['foo' => ['bar' => 'baz'], 'bar' => 'baz'], - ['foo.bar', 'bar'], - ['foo' => ['bar' => 'baz'], 'bar' => 'baz'], - ], - [ - ['foo' => ['bar' => 'baz'], 'bar' => 'baz'], - ['foo.bar'], - ['foo' => ['bar' => 'baz']], - ], - [ - ['foo' => ['bar' => 'baz'], 'bar' => 'baz'], - ['foo.bar', 'baz'], - ['foo' => ['bar' => 'baz'], 'baz' => null], - ], - ]; + return array( + array(array(), 'foo', null), + array(array('foo' => 'bar'), 'foo', 'bar'), + array(array('foo' => 'bar'), 'bar', null), + array(array('foo' => 'bar'), 'foo.bar', null), + array(array('foo' => array('bar' => 'baz')), 'foo.bar', 'baz'), + array(array('foo' => array('bar' => 'baz')), 'foo.baz', null), + array(array('foo' => array('bar' => 'baz')), 'foo', array('bar' => 'baz')), + array( + array('foo' => 'bar', 'bar' => 'baz'), + array('foo', 'bar'), + array('foo' => 'bar', 'bar' => 'baz') + ), + array( + array('foo' => array('bar' => 'baz'), 'bar' => 'baz'), + array('foo.bar', 'bar'), + array('foo' => array('bar' => 'baz'), 'bar' => 'baz'), + ), + array( + array('foo' => array('bar' => 'baz'), 'bar' => 'baz'), + array('foo.bar'), + array('foo' => array('bar' => 'baz')), + ), + array( + array('foo' => array('bar' => 'baz'), 'bar' => 'baz'), + array('foo.bar', 'baz'), + array('foo' => array('bar' => 'baz'), 'baz' => null), + ), + ); } /** @@ -58,50 +58,50 @@ public function setSetsCorrectKeyToValue(array $input, $key, $value, array $expe public function getSetData() { - return [ - [ - ['foo' => 'bar'], + return array( + array( + array('foo' => 'bar'), 'foo', 'baz', - ['foo' => 'baz'], - ], - [ - [], + array('foo' => 'baz'), + ), + array( + array(), 'foo', 'bar', - ['foo' => 'bar'], - ], - [ - [], + array('foo' => 'bar'), + ), + array( + array(), 'foo.bar', 'baz', - ['foo' => ['bar' => 'baz']], - ], - [ - ['foo' => ['bar' => 'baz']], + array('foo' => array('bar' => 'baz')), + ), + array( + array('foo' => array('bar' => 'baz')), 'foo.baz', 'foo', - ['foo' => ['bar' => 'baz', 'baz' => 'foo']], - ], - [ - ['foo' => ['bar' => 'baz']], + array('foo' => array('bar' => 'baz', 'baz' => 'foo')), + ), + array( + array('foo' => array('bar' => 'baz')), 'foo.baz.bar', 'baz', - ['foo' => ['bar' => 'baz', 'baz' => ['bar' => 'baz']]], - ], - [ - [], + array('foo' => array('bar' => 'baz', 'baz' => array('bar' => 'baz'))), + ), + array( + array(), 'foo.bar.baz', 'foo', - ['foo' => ['bar' => ['baz' => 'foo']]], - ], - ]; + array('foo' => array('bar' => array('baz' => 'foo'))), + ), + ); } /** @test */ public function setThrowsExceptionOnNonArraySegment() { - $data = ['foo' => 'bar']; + $data = array('foo' => 'bar'); $this->setExpectedException('UnexpectedValueException', 'Non-array segment encountered'); ArrayUtil::set($data, 'foo.bar', 'baz'); } @@ -117,16 +117,16 @@ public function hasReturnsCorrectly(array $input, $key, $expected) public function getHasData() { - return [ - [[], 'foo', false], - [['foo' => 'bar'], 'foo', true], - [['foo' => 'bar'], 'bar', false], - [['foo' => 'bar'], 'foo.bar', false], - [['foo' => ['bar' => 'baz']], 'foo.bar', true], - [['foo' => ['bar' => 'baz']], 'foo.baz', false], - [['foo' => ['bar' => 'baz']], 'foo', true], - [['foo' => null], 'foo', true], - [['foo' => ['bar' => null]], 'foo.bar', true], - ]; + return array( + array(array(), 'foo', false), + array(array('foo' => 'bar'), 'foo', true), + array(array('foo' => 'bar'), 'bar', false), + array(array('foo' => 'bar'), 'foo.bar', false), + array(array('foo' => array('bar' => 'baz')), 'foo.bar', true), + array(array('foo' => array('bar' => 'baz')), 'foo.baz', false), + array(array('foo' => array('bar' => 'baz')), 'foo', true), + array(array('foo' => null), 'foo', true), + array(array('foo' => array('bar' => null)), 'foo.bar', true), + ); } } diff --git a/tests/unit/JsonSettingStoreTest.php b/tests/unit/JsonSettingStoreTest.php index c98bc74..ff06a6e 100644 --- a/tests/unit/JsonSettingStoreTest.php +++ b/tests/unit/JsonSettingStoreTest.php @@ -39,7 +39,7 @@ public function throws_exception_when_files_put_fails() { $files = $this->mockFilesystem(); $files->shouldReceive('exists')->once()->with('fakepath')->andReturn(false); - $files->shouldReceive('put')->once()->with('fakepath', '[]')->andReturn(false); + $files->shouldReceive('put')->once()->with('fakepath', '{}')->andReturn(false); $store = $this->makeStore($files); } From 27db322f01bb8dc5d3eb8ad4ead66b60cc0e6e43 Mon Sep 17 00:00:00 2001 From: Andreas Lutro Date: Thu, 14 May 2015 11:21:08 +0200 Subject: [PATCH 3/7] no need for composer self-update --- .travis.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index d0556ce..5812d62 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,9 +17,6 @@ env: - COMPOSER_ARGS="" - COMPOSER_ARGS="--prefer-lowest" -before_install: - - composer self-update - install: - composer update ${COMPOSER_ARGS} --dev --no-interaction From ffbb34e9ada79b78c488d68234f5dbc0850e1fca Mon Sep 17 00:00:00 2001 From: Andreas Lutro Date: Thu, 14 May 2015 11:23:14 +0200 Subject: [PATCH 4/7] [] to array() --- tests/functional/AbstractFunctionalTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functional/AbstractFunctionalTest.php b/tests/functional/AbstractFunctionalTest.php index 9968d5c..5e9eab7 100644 --- a/tests/functional/AbstractFunctionalTest.php +++ b/tests/functional/AbstractFunctionalTest.php @@ -27,7 +27,7 @@ protected function assertStoreKeyEquals($store, $key, $expected, $message = null public function store_is_initially_empty() { $store = $this->createStore(); - $this->assertEquals([], $store->all()); + $this->assertEquals(array(), $store->all()); } /** @test */ From a0a74fb1350b3c1e19202fec17dddd44dc7f3769 Mon Sep 17 00:00:00 2001 From: Andreas Lutro Date: Thu, 14 May 2015 11:25:30 +0200 Subject: [PATCH 5/7] [] to array() --- src/SettingStore.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SettingStore.php b/src/SettingStore.php index 90d4bce..365ef5c 100644 --- a/src/SettingStore.php +++ b/src/SettingStore.php @@ -103,7 +103,7 @@ public function forget($key) public function forgetAll() { $this->unsaved = true; - $this->data = []; + $this->data = array(); } /** From 45b56d28cb0d91ac6f5be31f10ce6ce096ecfb4c Mon Sep 17 00:00:00 2001 From: Andreas Lutro Date: Thu, 14 May 2015 11:27:21 +0200 Subject: [PATCH 6/7] [] to array() --- src/ArrayUtil.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ArrayUtil.php b/src/ArrayUtil.php index affe6b1..2b3afff 100644 --- a/src/ArrayUtil.php +++ b/src/ArrayUtil.php @@ -58,7 +58,7 @@ public static function get(array $data, $key, $default = null) protected static function getArray(array $input, $keys, $default = null) { - $output = []; + $output = array(); foreach ($keys as $key) { static::set($output, $key, static::get($input, $key, $default)); @@ -108,7 +108,7 @@ public static function set(array &$data, $key, $value) // iterate through all of $segments except the last one foreach ($segments as $segment) { if (!array_key_exists($segment, $data)) { - $data[$segment] = []; + $data[$segment] = array(); } else if (!is_array($data[$segment])) { throw new \UnexpectedValueException('Non-array segment encountered'); } From f93a46c7652dbce0f6e09faad05778358a826906 Mon Sep 17 00:00:00 2001 From: Andreas Lutro Date: Thu, 14 May 2015 11:29:47 +0200 Subject: [PATCH 7/7] consistent test method naming --- tests/unit/DatabaseSettingStoreTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/unit/DatabaseSettingStoreTest.php b/tests/unit/DatabaseSettingStoreTest.php index 1505c65..c225e8a 100644 --- a/tests/unit/DatabaseSettingStoreTest.php +++ b/tests/unit/DatabaseSettingStoreTest.php @@ -39,7 +39,8 @@ public function correct_data_is_inserted_and_updated() $store->save(); } - public function testExtraColumnsAreQueried() + /** @test */ + public function extra_columns_are_queried() { $connection = $this->mockConnection(); $query = $this->mockQuery($connection); @@ -54,7 +55,8 @@ public function testExtraColumnsAreQueried() $this->assertEquals('bar', $store->get('foo')); } - public function testExtraColumnsAreInserted() + /** @test */ + public function extra_columns_are_inserted() { $connection = $this->mockConnection(); $query = $this->mockQuery($connection);