From 2371d63ea6b448e3c26a9344c95ff2649cc63d2d Mon Sep 17 00:00:00 2001 From: fire015 Date: Tue, 12 Mar 2019 10:51:44 +0000 Subject: [PATCH 1/2] Update to PHP 7.0 min version, move to PHPUnit 6 --- .travis.yml | 1 - README.md | 42 ++++----------- composer.json | 50 +++++++++--------- src/Flintstone.php | 2 +- src/Formatter/FormatterInterface.php | 13 +---- src/Formatter/JsonFormatter.php | 41 ++++++++++----- src/Formatter/SerializeFormatter.php | 16 ++---- tests/Cache/ArrayCacheTest.php | 2 +- tests/ConfigTest.php | 2 +- tests/DatabaseTest.php | 2 +- tests/FlintstoneTest.php | 2 +- tests/Formatter/JsonFormatterTest.php | 60 +++++++++++++++++++--- tests/Formatter/SerializeFormatterTest.php | 31 ++++++++--- tests/LineTest.php | 2 +- tests/ValidationTest.php | 2 +- 15 files changed, 151 insertions(+), 117 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0f06489..2ba4294 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ language: php sudo: false php: - - 5.6 - 7.0 - 7.1 - 7.2 diff --git a/README.md b/README.md index d9ae9ce..a3596b4 100644 --- a/README.md +++ b/README.md @@ -35,17 +35,11 @@ $users = new Flintstone('users', ['dir' => '/path/to/database/dir/']); ### Requirements -- PHP 5.6+ +- PHP 7.0+ ### Data types -Flintstone can store the following data types: - -* Strings -* Integers -* Floats -* Arrays -* Null +Flintstone can store any data type that can be formatted into a string. By default this uses `serialize()`. See [Changing the formatter](#changing-the-formatter) for more details. ### Options @@ -64,40 +58,24 @@ Flintstone can store the following data types: ```php '/path/to/database/dir/']; - -// Load the databases -$users = new Flintstone('users', $options); -$settings = new Flintstone('settings', $options); +// Load a database +$users = new Flintstone('users', ['dir' => '/path/to/database/dir/']); -// Set keys +// Set a key $users->set('bob', ['email' => 'bob@site.com', 'password' => '123456']); -$users->set('joe', ['email' => 'joe@site.com', 'password' => 'test']); -$settings->set('site_offline', 1); -$settings->set('site_back', '3 days'); -// Retrieve keys +// Get a key $user = $users->get('bob'); echo 'Bob, your email is ' . $user['email']; -$offline = $settings->get('site_offline'); -if ($offline == 1) { - echo 'Sorry, the website is offline
'; - echo 'We will be back in ' . $settings->get('site_back'); -} - // Retrieve all key names -$keys = $users->getKeys(); // returns array('bob', 'joe', ...) +$keys = $users->getKeys(); // returns array('bob') -foreach ($keys as $username) { - $user = $users->get($username); - echo $username.', your email is ' . $user['email']; - echo $username.', your password is ' . $user['password']; -} +// Retrieve all data +$data = $users->getAll(); // returns array('bob' => array('email' => 'bob@site.com', 'password' => '123456')); // Delete a key -$users->delete('joe'); +$users->delete('bob'); // Flush the database $users->flush(); diff --git a/composer.json b/composer.json index cf6f57c..61da233 100644 --- a/composer.json +++ b/composer.json @@ -1,25 +1,25 @@ -{ - "name": "fire015/flintstone", - "type": "library", - "description": "A key/value database store using flat files for PHP", - "keywords": ["flintstone", "database", "cache", "files", "memory"], - "homepage": "http://www.xeweb.net/flintstone/", - "license": "MIT", - "authors": [ - { - "name": "Jason M", - "email": "emailfire@gmail.com" - } - ], - "require": { - "php": ">=5.6" - }, - "autoload": { - "psr-4": { - "Flintstone\\": "src/" - } - }, - "require-dev" : { - "phpunit/phpunit": "^5.0" - } -} \ No newline at end of file +{ + "name": "fire015/flintstone", + "type": "library", + "description": "A key/value database store using flat files for PHP", + "keywords": ["flintstone", "database", "cache", "files", "memory"], + "homepage": "http://www.xeweb.net/flintstone/", + "license": "MIT", + "authors": [ + { + "name": "Jason M", + "email": "emailfire@gmail.com" + } + ], + "require": { + "php": ">=7.0" + }, + "autoload": { + "psr-4": { + "Flintstone\\": "src/" + } + }, + "require-dev" : { + "phpunit/phpunit": "^6" + } +} diff --git a/src/Flintstone.php b/src/Flintstone.php index f753a67..4cb5413 100644 --- a/src/Flintstone.php +++ b/src/Flintstone.php @@ -18,7 +18,7 @@ class Flintstone * * @var string */ - const VERSION = '2.1.1'; + const VERSION = '2.2'; /** * Database class. diff --git a/src/Formatter/FormatterInterface.php b/src/Formatter/FormatterInterface.php index 71602f9..8666dd6 100644 --- a/src/Formatter/FormatterInterface.php +++ b/src/Formatter/FormatterInterface.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE.md - * file that was distributed with this source code. - */ - namespace Flintstone\Formatter; interface FormatterInterface @@ -20,7 +11,7 @@ interface FormatterInterface * * @return string */ - public function encode($data); + public function encode($data): string; /** * Decode a string into data. @@ -29,5 +20,5 @@ public function encode($data); * * @return mixed */ - public function decode($data); + public function decode(string $data); } diff --git a/src/Formatter/JsonFormatter.php b/src/Formatter/JsonFormatter.php index 6f6e322..c94abb2 100644 --- a/src/Formatter/JsonFormatter.php +++ b/src/Formatter/JsonFormatter.php @@ -1,31 +1,46 @@ - * - * For the full copyright and license information, please view the LICENSE.md - * file that was distributed with this source code. - */ - namespace Flintstone\Formatter; +use Flintstone\Exception; + class JsonFormatter implements FormatterInterface { + /** + * @var bool + */ + private $assoc; + + public function __construct(bool $assoc = true) + { + $this->assoc = $assoc; + } + /** * {@inheritdoc} */ - public function encode($data) + public function encode($data): string { - return json_encode($data); + $result = json_encode($data); + + if (json_last_error() === JSON_ERROR_NONE) { + return $result; + } + + throw new Exception(json_last_error_msg()); } /** * {@inheritdoc} */ - public function decode($data) + public function decode(string $data) { - return json_decode($data, true); + $result = json_decode($data, $this->assoc); + + if (json_last_error() === JSON_ERROR_NONE) { + return $result; + } + + throw new Exception(json_last_error_msg()); } } diff --git a/src/Formatter/SerializeFormatter.php b/src/Formatter/SerializeFormatter.php index 13e1fa2..c16087e 100644 --- a/src/Formatter/SerializeFormatter.php +++ b/src/Formatter/SerializeFormatter.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE.md - * file that was distributed with this source code. - */ - namespace Flintstone\Formatter; class SerializeFormatter implements FormatterInterface @@ -16,7 +7,7 @@ class SerializeFormatter implements FormatterInterface /** * {@inheritdoc} */ - public function encode($data) + public function encode($data): string { return serialize($this->preserveLines($data, false)); } @@ -24,7 +15,7 @@ public function encode($data) /** * {@inheritdoc} */ - public function decode($data) + public function decode(string $data) { return $this->preserveLines(unserialize($data), true); } @@ -37,10 +28,11 @@ public function decode($data) * * @return mixed */ - protected function preserveLines($data, $reverse) + protected function preserveLines($data, bool $reverse) { $search = ["\n", "\r"]; $replace = ['\\n', '\\r']; + if ($reverse) { $search = ['\\n', '\\r']; $replace = ["\n", "\r"]; diff --git a/tests/Cache/ArrayCacheTest.php b/tests/Cache/ArrayCacheTest.php index 29a630b..a9e0561 100644 --- a/tests/Cache/ArrayCacheTest.php +++ b/tests/Cache/ArrayCacheTest.php @@ -2,7 +2,7 @@ use Flintstone\Cache\ArrayCache; -class ArrayCacheTest extends PHPUnit_Framework_TestCase +class ArrayCacheTest extends \PHPUnit\Framework\TestCase { /** * @var ArrayCache diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index a40de2d..5af11ae 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -3,7 +3,7 @@ use Flintstone\Config; use Flintstone\Formatter\JsonFormatter; -class ConfigTest extends PHPUnit_Framework_TestCase +class ConfigTest extends \PHPUnit\Framework\TestCase { public function testDefaultConfig() { diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index d100721..cc83433 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -3,7 +3,7 @@ use Flintstone\Config; use Flintstone\Database; -class DatabaseTest extends PHPUnit_Framework_TestCase +class DatabaseTest extends \PHPUnit\Framework\TestCase { /** * @var Database diff --git a/tests/FlintstoneTest.php b/tests/FlintstoneTest.php index cee004f..af0687d 100644 --- a/tests/FlintstoneTest.php +++ b/tests/FlintstoneTest.php @@ -3,7 +3,7 @@ use Flintstone\Flintstone; use Flintstone\Formatter\JsonFormatter; -class FlintstoneTest extends PHPUnit_Framework_TestCase +class FlintstoneTest extends \PHPUnit\Framework\TestCase { public function testGetDatabaseAndConfig() { diff --git a/tests/Formatter/JsonFormatterTest.php b/tests/Formatter/JsonFormatterTest.php index e602d4c..d97fede 100644 --- a/tests/Formatter/JsonFormatterTest.php +++ b/tests/Formatter/JsonFormatterTest.php @@ -2,7 +2,7 @@ use Flintstone\Formatter\JsonFormatter; -class JsonFormatterTest extends PHPUnit_Framework_TestCase +class JsonFormatterTest extends \PHPUnit\Framework\TestCase { /** * @var JsonFormatter @@ -14,16 +14,60 @@ protected function setUp() $this->formatter = new JsonFormatter(); } - public function testEncode() + /** + * @test + * @dataProvider validData + */ + public function encodesValidData($originalValue, $encodedValue) + { + $this->assertSame($encodedValue, $this->formatter->encode($originalValue)); + } + + /** + * @test + * @dataProvider validData + */ + public function decodesValidData($originalValue, $encodedValue) + { + $this->assertSame($originalValue, $this->formatter->decode($encodedValue)); + } + + /** + * @test + */ + public function decodesAnObject() + { + $originalValue = (object)['foo' => 'bar']; + $formatter = new JsonFormatter(false); + $encodedValue = $formatter->encode($originalValue); + $this->assertEquals($originalValue, $formatter->decode($encodedValue)); + } + + /** + * @test + * @expectedException \Flintstone\Exception + */ + public function encodingInvalidDataThrowsException() + { + $this->formatter->encode(chr(241)); + } + + /** + * @test + * @expectedException \Flintstone\Exception + */ + public function decodingInvalidDataThrowsException() { - $data = $this->formatter->encode(["test", "new\nline"]); - $this->assertEquals('["test","new\nline"]', $data); + $this->formatter->decode('{'); } - public function testDecode() + public function validData(): array { - $data = $this->formatter->decode('["test","new\nline"]'); - $this->assertTrue(is_array($data)); - $this->assertEquals(["test", "new\nline"], $data); + return [ + [null, 'null'], + [1, '1'], + ['foo', '"foo"'], + [["test", "new\nline"], '["test","new\nline"]'], + ]; } } diff --git a/tests/Formatter/SerializeFormatterTest.php b/tests/Formatter/SerializeFormatterTest.php index 48caa2d..cf2fd87 100644 --- a/tests/Formatter/SerializeFormatterTest.php +++ b/tests/Formatter/SerializeFormatterTest.php @@ -2,7 +2,7 @@ use Flintstone\Formatter\SerializeFormatter; -class SerializeFormatterTest extends PHPUnit_Framework_TestCase +class SerializeFormatterTest extends \PHPUnit\Framework\TestCase { /** * @var SerializeFormatter @@ -14,16 +14,31 @@ protected function setUp() $this->formatter = new SerializeFormatter(); } - public function testEncode() + /** + * @test + * @dataProvider validData + */ + public function encodesValidData($originalValue, $encodedValue) + { + $this->assertSame($encodedValue, $this->formatter->encode($originalValue)); + } + + /** + * @test + * @dataProvider validData + */ + public function decodesValidData($originalValue, $encodedValue) { - $data = $this->formatter->encode(["test", "new\nline"]); - $this->assertEquals('a:2:{i:0;s:4:"test";i:1;s:9:"new\nline";}', $data); + $this->assertSame($originalValue, $this->formatter->decode($encodedValue)); } - public function testDecode() + public function validData(): array { - $data = $this->formatter->decode('a:2:{i:0;s:4:"test";i:1;s:9:"new\nline";}'); - $this->assertTrue(is_array($data)); - $this->assertEquals(["test", "new\nline"], $data); + return [ + [null, 'N;'], + [1, 'i:1;'], + ['foo', 's:3:"foo";'], + [["test", "new\nline"], 'a:2:{i:0;s:4:"test";i:1;s:9:"new\nline";}'], + ]; } } diff --git a/tests/LineTest.php b/tests/LineTest.php index 766fe91..9a06b8c 100644 --- a/tests/LineTest.php +++ b/tests/LineTest.php @@ -2,7 +2,7 @@ use Flintstone\Line; -class LineTest extends PHPUnit_Framework_TestCase +class LineTest extends \PHPUnit\Framework\TestCase { /** * @var Line diff --git a/tests/ValidationTest.php b/tests/ValidationTest.php index daeca2a..aa9d28f 100644 --- a/tests/ValidationTest.php +++ b/tests/ValidationTest.php @@ -2,7 +2,7 @@ use Flintstone\Validation; -class ValidationTest extends PHPUnit_Framework_TestCase +class ValidationTest extends \PHPUnit\Framework\TestCase { /** * @expectedException Flintstone\Exception From 4cd00b2fb11fe4f370d5f83c085cd3debff9f363 Mon Sep 17 00:00:00 2001 From: fire015 Date: Tue, 12 Mar 2019 12:08:42 +0000 Subject: [PATCH 2/2] Removed data type validation for storing and added param and return types --- CHANGELOG.md | 6 ++++++ phpunit.xml.dist | 1 - src/Cache/ArrayCache.php | 9 --------- src/Cache/CacheInterface.php | 9 --------- src/Config.php | 37 +++++++++++++--------------------- src/Database.php | 30 ++++++++++----------------- src/Exception.php | 9 --------- src/Flintstone.php | 32 ++++++++++------------------- src/Line.php | 20 ++++-------------- src/Validation.php | 18 ++--------------- tests/Cache/ArrayCacheTest.php | 15 +++++++++++--- tests/ConfigTest.php | 36 +++++++++++++++++++++++---------- tests/DatabaseTest.php | 33 ++++++++++++++++++++++-------- tests/FlintstoneTest.php | 22 +++++++++----------- tests/LineTest.php | 20 ++++++++++++++---- tests/ValidationTest.php | 15 ++++---------- 16 files changed, 139 insertions(+), 173 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23aa08d..1b222ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ Change Log ========== +### 12/03/2019 - 2.2 +* Bump minimum PHP version to 7.0 +* Update PHPUnit to version 6 +* Removed data type validation for storing +* Added param and return types + ### 09/06/2017 - 2.1.1 * Update `Database::writeTempToFile` to correctly close the file pointer and free up memory diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 133ba89..a9c62fe 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,7 +8,6 @@ convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" - syntaxCheck="true" verbose="true" > diff --git a/src/Cache/ArrayCache.php b/src/Cache/ArrayCache.php index c2d01a3..5d2fcdc 100644 --- a/src/Cache/ArrayCache.php +++ b/src/Cache/ArrayCache.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE.md - * file that was distributed with this source code. - */ - namespace Flintstone\Cache; class ArrayCache implements CacheInterface diff --git a/src/Cache/CacheInterface.php b/src/Cache/CacheInterface.php index d58dbe4..9459e01 100644 --- a/src/Cache/CacheInterface.php +++ b/src/Cache/CacheInterface.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE.md - * file that was distributed with this source code. - */ - namespace Flintstone\Cache; interface CacheInterface diff --git a/src/Config.php b/src/Config.php index b455747..aa13210 100644 --- a/src/Config.php +++ b/src/Config.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE.md - * file that was distributed with this source code. - */ - namespace Flintstone; use Flintstone\Cache\ArrayCache; @@ -48,7 +39,7 @@ public function __construct(array $config = []) * * @return array */ - protected function normalizeConfig(array $config) + protected function normalizeConfig(array $config): array { $defaultConfig = [ 'dir' => getcwd(), @@ -56,7 +47,7 @@ protected function normalizeConfig(array $config) 'gzip' => false, 'cache' => true, 'formatter' => null, - 'swap_memory_limit' => 2097152, // 2MB + 'swap_memory_limit' => 2097152, // 2MB ]; return array_replace($defaultConfig, $config); @@ -67,7 +58,7 @@ protected function normalizeConfig(array $config) * * @return string */ - public function getDir() + public function getDir(): string { return $this->config['dir']; } @@ -79,7 +70,7 @@ public function getDir() * * @throws Exception */ - public function setDir($dir) + public function setDir(string $dir) { if (!is_dir($dir)) { throw new Exception('Directory does not exist: ' . $dir); @@ -93,7 +84,7 @@ public function setDir($dir) * * @return string */ - public function getExt() + public function getExt(): string { if ($this->useGzip()) { return $this->config['ext'] . '.gz'; @@ -107,9 +98,9 @@ public function getExt() * * @param string $ext */ - public function setExt($ext) + public function setExt(string $ext) { - if ('.' != substr($ext, 0, 1)) { + if (substr($ext, 0, 1) !== '.') { $ext = '.' . $ext; } @@ -121,7 +112,7 @@ public function setExt($ext) * * @return bool */ - public function useGzip() + public function useGzip(): bool { return $this->config['gzip']; } @@ -131,9 +122,9 @@ public function useGzip() * * @param bool $gzip */ - public function setGzip($gzip) + public function setGzip(bool $gzip) { - $this->config['gzip'] = (bool)$gzip; + $this->config['gzip'] = $gzip; } /** @@ -171,7 +162,7 @@ public function setCache($cache) * * @return FormatterInterface */ - public function getFormatter() + public function getFormatter(): FormatterInterface { return $this->config['formatter']; } @@ -201,7 +192,7 @@ public function setFormatter($formatter) * * @return int */ - public function getSwapMemoryLimit() + public function getSwapMemoryLimit(): int { return $this->config['swap_memory_limit']; } @@ -211,8 +202,8 @@ public function getSwapMemoryLimit() * * @param int $limit */ - public function setSwapMemoryLimit($limit) + public function setSwapMemoryLimit(int $limit) { - $this->config['swap_memory_limit'] = (int)$limit; + $this->config['swap_memory_limit'] = $limit; } } diff --git a/src/Database.php b/src/Database.php index 95e361b..08deb52 100644 --- a/src/Database.php +++ b/src/Database.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE.md - * file that was distributed with this source code. - */ - namespace Flintstone; use SplFileObject; @@ -77,7 +68,7 @@ class Database * @param string $name * @param Config|null $config */ - public function __construct($name, Config $config = null) + public function __construct(string $name, Config $config = null) { $this->setName($name); @@ -91,7 +82,7 @@ public function __construct($name, Config $config = null) * * @return string */ - public function getName() + public function getName(): string { return $this->name; } @@ -103,7 +94,7 @@ public function getName() * * @throws Exception */ - public function setName($name) + public function setName(string $name) { Validation::validateDatabaseName($name); $this->name = $name; @@ -114,7 +105,7 @@ public function setName($name) * * @return Config */ - public function getConfig() + public function getConfig(): Config { return $this->config; } @@ -134,7 +125,7 @@ public function setConfig(Config $config) * * @return string */ - public function getPath() + public function getPath(): string { return $this->config->getDir() . $this->getName() . $this->config->getExt(); } @@ -148,7 +139,7 @@ public function getPath() * * @return SplFileObject */ - protected function openFile($mode) + protected function openFile(int $mode): SplFileObject { $path = $this->getPath(); @@ -167,11 +158,12 @@ protected function openFile($mode) $res = $this->fileAccessMode[$mode]; $file = new SplFileObject($path, $res['mode']); - if (self::FILE_READ == $mode) { + if ($mode === self::FILE_READ) { $file->setFlags(SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY | SplFileObject::READ_AHEAD); } if (!$this->getConfig()->useGzip() && !$file->flock($res['operation'])) { + $file = null; throw new Exception('Could not lock file: ' . $path); } @@ -183,7 +175,7 @@ protected function openFile($mode) * * @return SplTempFileObject */ - public function openTempFile() + public function openTempFile(): SplTempFileObject { return new SplTempFileObject($this->getConfig()->getSwapMemoryLimit()); } @@ -210,7 +202,7 @@ protected function closeFile(SplFileObject &$file) * * @return \Generator */ - public function readFromFile() + public function readFromFile(): \Generator { $file = $this->openFile(static::FILE_READ); @@ -228,7 +220,7 @@ public function readFromFile() * * @param string $line */ - public function appendToFile($line) + public function appendToFile(string $line) { $file = $this->openFile(static::FILE_APPEND); $file->fwrite($line); diff --git a/src/Exception.php b/src/Exception.php index 14f687b..e784197 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE.md - * file that was distributed with this source code. - */ - namespace Flintstone; class Exception extends \Exception diff --git a/src/Flintstone.php b/src/Flintstone.php index 4cb5413..e2c21e9 100644 --- a/src/Flintstone.php +++ b/src/Flintstone.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE.md - * file that was distributed with this source code. - */ - namespace Flintstone; class Flintstone @@ -59,7 +50,7 @@ public function __construct($database, $config) * * @return Database */ - public function getDatabase() + public function getDatabase(): Database { return $this->database; } @@ -79,7 +70,7 @@ public function setDatabase(Database $database) * * @return Config */ - public function getConfig() + public function getConfig(): Config { return $this->config; } @@ -102,7 +93,7 @@ public function setConfig(Config $config) * * @return mixed */ - public function get($key) + public function get(string $key) { Validation::validateKey($key); @@ -139,10 +130,9 @@ public function get($key) * @param string $key * @param mixed $data */ - public function set($key, $data) + public function set(string $key, $data) { Validation::validateKey($key); - Validation::validateData($data); // If the key already exists we need to replace it if ($this->get($key) !== false) { @@ -164,7 +154,7 @@ public function set($key, $data) * * @param string $key */ - public function delete($key) + public function delete(string $key) { Validation::validateKey($key); @@ -191,7 +181,7 @@ public function flush() * * @return array */ - public function getKeys() + public function getKeys(): array { $keys = []; $file = $this->getDatabase()->readFromFile(); @@ -209,7 +199,7 @@ public function getKeys() * * @return array */ - public function getAll() + public function getAll(): array { $data = []; $file = $this->getDatabase()->readFromFile(); @@ -228,7 +218,7 @@ public function getAll() * @param string $key * @param mixed $data */ - protected function replace($key, $data) + protected function replace(string $key, $data) { // Write a new database to a temporary file $tmpFile = $this->getDatabase()->openTempFile(); @@ -264,7 +254,7 @@ protected function replace($key, $data) * * @return string */ - protected function getLineString($key, $data) + protected function getLineString(string $key, $data): string { return $key . '=' . $this->encodeData($data) . "\n"; } @@ -276,7 +266,7 @@ protected function getLineString($key, $data) * * @return mixed */ - protected function decodeData($data) + protected function decodeData(string $data) { return $this->getConfig()->getFormatter()->decode($data); } @@ -288,7 +278,7 @@ protected function decodeData($data) * * @return string */ - protected function encodeData($data) + protected function encodeData($data): string { return $this->getConfig()->getFormatter()->encode($data); } diff --git a/src/Line.php b/src/Line.php index 18eca59..c9f52e0 100644 --- a/src/Line.php +++ b/src/Line.php @@ -14,35 +14,23 @@ class Line */ protected $pieces = []; - /** - * @param string $line - */ - public function __construct($line) + public function __construct(string $line) { $this->line = $line; $this->pieces = explode('=', $line, 2); } - /** - * @return string - */ - public function getLine() + public function getLine(): string { return $this->line; } - /** - * @return string - */ - public function getKey() + public function getKey(): string { return $this->pieces[0]; } - /** - * @return string - */ - public function getData() + public function getData(): string { return $this->pieces[1]; } diff --git a/src/Validation.php b/src/Validation.php index 5eca1bf..29e901e 100644 --- a/src/Validation.php +++ b/src/Validation.php @@ -11,27 +11,13 @@ class Validation * * @throws Exception */ - public static function validateKey($key) + public static function validateKey(string $key) { if (empty($key) || !preg_match('/^[\w-]+$/', $key)) { throw new Exception('Invalid characters in key'); } } - /** - * Check the data type is valid. - * - * @param mixed $data - * - * @throws Exception - */ - public static function validateData($data) - { - if (!is_null($data) && !is_string($data) && !is_int($data) && !is_float($data) && !is_array($data)) { - throw new Exception('Invalid data type'); - } - } - /** * Check the database name is valid. * @@ -39,7 +25,7 @@ public static function validateData($data) * * @throws Exception */ - public static function validateDatabaseName($name) + public static function validateDatabaseName(string $name) { if (empty($name) || !preg_match('/^[\w-]+$/', $name)) { throw new Exception('Invalid characters in database name'); diff --git a/tests/Cache/ArrayCacheTest.php b/tests/Cache/ArrayCacheTest.php index a9e0561..868796a 100644 --- a/tests/Cache/ArrayCacheTest.php +++ b/tests/Cache/ArrayCacheTest.php @@ -14,21 +14,30 @@ protected function setUp() $this->cache = new ArrayCache(); } - public function testGetAndSet() + /** + * @test + */ + public function canGetAndSet() { $this->cache->set('foo', 'bar'); $this->assertTrue($this->cache->contains('foo')); $this->assertEquals('bar', $this->cache->get('foo')); } - public function testDelete() + /** + * @test + */ + public function canDelete() { $this->cache->set('foo', 'bar'); $this->cache->delete('foo'); $this->assertFalse($this->cache->contains('foo')); } - public function testFlush() + /** + * @test + */ + public function canFlush() { $this->cache->set('foo', 'bar'); $this->cache->flush(); diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 5af11ae..c941ab4 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -1,22 +1,30 @@ assertEquals(getcwd().DIRECTORY_SEPARATOR, $config->getDir()); $this->assertEquals('.dat', $config->getExt()); $this->assertFalse($config->useGzip()); - $this->assertInstanceOf(\Flintstone\Cache\ArrayCache::class, $config->getCache()); - $this->assertInstanceOf(\Flintstone\Formatter\SerializeFormatter::class, $config->getFormatter()); + $this->assertInstanceOf(ArrayCache::class, $config->getCache()); + $this->assertInstanceOf(SerializeFormatter::class, $config->getFormatter()); $this->assertEquals(2097152, $config->getSwapMemoryLimit()); } - public function testConfigConstructorOptions() + /** + * @test + */ + public function constructorConfigOverride() { $config = new Config([ 'dir' => __DIR__, @@ -31,11 +39,14 @@ public function testConfigConstructorOptions() $this->assertEquals('.test.gz', $config->getExt()); $this->assertTrue($config->useGzip()); $this->assertFalse($config->getCache()); - $this->assertInstanceOf(\Flintstone\Formatter\SerializeFormatter::class, $config->getFormatter()); + $this->assertInstanceOf(SerializeFormatter::class, $config->getFormatter()); $this->assertEquals(100, $config->getSwapMemoryLimit()); } - public function testConfigSetFormatter() + /** + * @test + */ + public function setValidFormatter() { $config = new Config(); $config->setFormatter(new JsonFormatter()); @@ -43,27 +54,30 @@ public function testConfigSetFormatter() } /** + * @test * @expectedException Flintstone\Exception */ - public function testConfigInvalidDir() + public function setInvalidFormatter() { $config = new Config(); - $config->setDir('/x/y/z/foo'); + $config->setFormatter(new self()); } /** + * @test * @expectedException Flintstone\Exception */ - public function testConfigInvalidFormatter() + public function invalidDirSet() { $config = new Config(); - $config->setFormatter(new self()); + $config->setDir('/x/y/z/foo'); } /** + * @test * @expectedException Flintstone\Exception */ - public function testConfigInvalidCache() + public function invalidCacheSet() { $config = new Config(); $config->setCache(new self()); diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index cc83433..6120170 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -2,6 +2,7 @@ use Flintstone\Config; use Flintstone\Database; +use Flintstone\Line; class DatabaseTest extends \PHPUnit\Framework\TestCase { @@ -27,48 +28,64 @@ protected function tearDown() } /** + * @test * @expectedException Flintstone\Exception * @expectedExceptionMessage Invalid characters in database name */ - public function testDatabaseInvalidName() + public function databaseHasInvalidName() { $config = new Config(); - $db = new Database('test!123', $config); + new Database('test!123', $config); } - public function testGetDatabaseAndConfig() + /** + * @test + */ + public function canGetDatabaseAndConfig() { $this->assertEquals('test', $this->db->getName()); $this->assertInstanceOf(Config::class, $this->db->getConfig()); $this->assertEquals(__DIR__ . DIRECTORY_SEPARATOR . 'test.dat', $this->db->getPath()); } - public function testAppendToFile() + /** + * @test + */ + public function canAppendToFile() { $this->db->appendToFile('foo=bar'); $this->assertEquals('foo=bar', file_get_contents($this->db->getPath())); } - public function testFlushFile() + /** + * @test + */ + public function canFlushFile() { $this->db->appendToFile('foo=bar'); $this->db->flushFile(); $this->assertEmpty(file_get_contents($this->db->getPath())); } - public function testReadFromFile() + /** + * @test + */ + public function canReadFromFile() { $this->db->appendToFile('foo=bar'); $file = $this->db->readFromFile(); foreach ($file as $line) { - $this->assertInstanceOf(\Flintstone\Line::class, $line); + $this->assertInstanceOf(Line::class, $line); $this->assertEquals('foo', $line->getKey()); $this->assertEquals('bar', $line->getData()); } } - public function testWriteTempToFile() + /** + * @test + */ + public function canWriteTempToFile() { $tmpFile = new SplTempFileObject(); $tmpFile->fwrite('foo=bar'); diff --git a/tests/FlintstoneTest.php b/tests/FlintstoneTest.php index af0687d..474f49a 100644 --- a/tests/FlintstoneTest.php +++ b/tests/FlintstoneTest.php @@ -1,5 +1,7 @@ false, ]); - $this->assertInstanceOf(\Flintstone\Database::class, $db->getDatabase()); - $this->assertInstanceOf(\Flintstone\Config::class, $db->getConfig()); + $this->assertInstanceOf(Database::class, $db->getDatabase()); + $this->assertInstanceOf(Config::class, $db->getConfig()); } /** + * @test * @expectedException \Flintstone\Exception * @expectedExceptionMessage Invalid characters in key */ - public function testKeyInvalidName() + public function keyHasInvalidName() { $db = new Flintstone('test', []); $db->get('test!123'); } /** - * @expectedException \Flintstone\Exception - * @expectedExceptionMessage Invalid data type + * @test */ - public function testKeyInvalidData() - { - $db = new Flintstone('test', []); - $db->set('test', new self()); - } - - public function testOperations() + public function canRunAllOperations() { $this->runOperationsTests([ 'dir' => __DIR__, @@ -58,7 +54,7 @@ public function testOperations() ]); } - protected function runOperationsTests($config) + private function runOperationsTests(array $config) { $db = new Flintstone('test', $config); $arr = ['foo' => "new\nline"]; diff --git a/tests/LineTest.php b/tests/LineTest.php index 9a06b8c..e29bc50 100644 --- a/tests/LineTest.php +++ b/tests/LineTest.php @@ -14,22 +14,34 @@ protected function setUp() $this->line = new Line('foo=bar'); } - public function testGetLine() + /** + * @test + */ + public function canGetLine() { $this->assertEquals('foo=bar', $this->line->getLine()); } - public function testGetKey() + /** + * @test + */ + public function canGetKey() { $this->assertEquals('foo', $this->line->getKey()); } - public function testGetData() + /** + * @test + */ + public function canGetData() { $this->assertEquals('bar', $this->line->getData()); } - public function testMultipleEquals() + /** + * @test + */ + public function canGetKeyAndDataWithMultipleEquals() { $line = new Line('foo=bar=baz'); $this->assertEquals('foo', $line->getKey()); diff --git a/tests/ValidationTest.php b/tests/ValidationTest.php index aa9d28f..b11f912 100644 --- a/tests/ValidationTest.php +++ b/tests/ValidationTest.php @@ -5,28 +5,21 @@ class ValidationTest extends \PHPUnit\Framework\TestCase { /** + * @test * @expectedException Flintstone\Exception * @expectedExceptionMessage Invalid characters in key */ - public function testValidateKey() + public function validateKey() { Validation::validateKey('test!123'); } /** - * @expectedException Flintstone\Exception - * @expectedExceptionMessage Invalid data type - */ - public function testValidateData() - { - Validation::validateData(new self()); - } - - /** + * @test * @expectedException Flintstone\Exception * @expectedExceptionMessage Invalid characters in database name */ - public function testValidateDatabaseName() + public function validateDatabaseName() { Validation::validateDatabaseName('test!123'); }