diff --git a/.travis.yml b/.travis.yml index e5aad52..73ccb4a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,10 @@ language: php +cache: + directories: + - vendor + - $HOME/.composer/cache + # Test in supported Laravel versions and Laravel's supported PHP versions. # # * Laravel 5.3+ requires PHP 5.6+. @@ -101,12 +106,17 @@ matrix: - php: "7.3" env: LARAVEL_VERSION="6.0.*" +# Code coverage testing isn't necessary, so remove xdebug to speed things up. +before_install: + - phpenv config-rm xdebug.ini + before_script: + # Install the right version of Laravel components based on the LARAVEL_VERSION + # environment variable. + - 'sed -i -e "s/\"illuminate\/database\": \"\(.*\)\"/\"illuminate\/database\": \"$LARAVEL_VERSION\"/" composer.json' + - 'sed -i -e "s/\"illuminate\/support\": \"\(.*\)\"/\"illuminate\/support\": \"$LARAVEL_VERSION\"/" composer.json' + - 'sed -i -e "s/\"illuminate\/events\": \"\(.*\)\"/\"illuminate\/events\": \"$LARAVEL_VERSION\"/" composer.json' - composer install --dev - - composer remove --update-with-dependencies illuminate/database illuminate/support - - composer remove --dev --update-with-dependencies illuminate/events - - composer require illuminate/database:${LARAVEL_VERSION} illuminate/support:${LARAVEL_VERSION} - - composer require --dev illuminate/events:${LARAVEL_VERSION} script: - vendor/bin/phpunit diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index eb6e712..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,18 +0,0 @@ -# Change Log - -## Version 0.2.0 -**Released 23 April, 2016** -* Add support for hiding preference attributes from JSON export ([#2](https://github.com/klaude/eloquent-preferences/issues/2) - Thanks, [@timothyallan](https://github.com/timothyallan)!) - -## Version 0.1.1 -**Released 23 February, 2016** -* Fix a typo in an internal method name ([#1](https://github.com/klaude/eloquent-preferences/issues/1) - Thanks, [@timothyallan](https://github.com/timothyallan)!) -* Replace a deprecated PHPUnit configuration option -* Add contributing guidelines and a change log - -## Version 0.1.0 -**Released 27 December, 2015** -* Initial release -* Basic support for preference retrieval, creation, updating, and deletion -* Support for preference value type casting -* Support for default preference values diff --git a/composer.json b/composer.json index cb63969..90fa0af 100644 --- a/composer.json +++ b/composer.json @@ -12,8 +12,9 @@ } ], "require": { - "illuminate/database": "~5.0", - "illuminate/support": "~5.0" + "ext-json": "*", + "illuminate/database": "~5|~6.0", + "illuminate/support": "~5|~6.0" }, "autoload": { "psr-4": { @@ -21,7 +22,8 @@ } }, "require-dev": { - "illuminate/events": "~5.0", + "ext-pdo": "*", + "illuminate/events": "~5|~6.0", "phpunit/phpunit": "~5.0|~4.8" }, "autoload-dev": { diff --git a/database/migrations/2015_12_20_000000_create_model_preferences_table.php b/database/migrations/2015_12_20_000000_create_model_preferences_table.php index 86b0c91..58c1a97 100644 --- a/database/migrations/2015_12_20_000000_create_model_preferences_table.php +++ b/database/migrations/2015_12_20_000000_create_model_preferences_table.php @@ -17,7 +17,7 @@ public function up() Model::getConnectionResolver() ->connection() ->getSchemaBuilder() - ->create((new Preference)->getQualifiedTableName(), function(Blueprint $table) { + ->create((new Preference())->getQualifiedTableName(), function (Blueprint $table) { $table->increments('id'); $table->string('preference'); $table->string('value'); @@ -36,6 +36,6 @@ public function down() Model::getConnectionResolver() ->connection() ->getSchemaBuilder() - ->drop((new Preference)->getQualifiedTableName()); + ->drop((new Preference())->getQualifiedTableName()); } } diff --git a/src/EloquentPreferencesServiceProvider.php b/src/EloquentPreferencesServiceProvider.php index 981784f..9e6d314 100644 --- a/src/EloquentPreferencesServiceProvider.php +++ b/src/EloquentPreferencesServiceProvider.php @@ -14,7 +14,8 @@ class EloquentPreferencesServiceProvider extends ServiceProvider public function register() { $this->mergeConfigFrom( - dirname(__DIR__) . '/config/eloquent-preferences.php', 'eloquent-preferences' + dirname(__DIR__) . '/config/eloquent-preferences.php', + 'eloquent-preferences' ); } diff --git a/src/HasPreferences.php b/src/HasPreferences.php index 0e2952a..45e4b33 100644 --- a/src/HasPreferences.php +++ b/src/HasPreferences.php @@ -37,7 +37,7 @@ public function getPreference($preference, $defaultValue = null) { $savedPreference = $this->preferences()->where('preference', $preference)->first(); - $value = is_null($savedPreference) + $value = $savedPreference === null ? $this->getDefaultValue($preference, $defaultValue) : $savedPreference->value; @@ -77,7 +77,7 @@ public function setPreference($preference, $value) /** @var Preference $savedPreference */ $savedPreference = $this->preferences()->where('preference', $preference)->first(); - if (is_null($savedPreference)) { + if ($savedPreference === null) { $this->preferences()->save( new Preference(['preference' => $preference, 'value' => $value]) ); @@ -242,7 +242,7 @@ protected function isPreferenceJsonCastable($preference) protected function getPreferenceCastType($preference) { return $this->hasPreferenceCast($preference) - ? trim(strtolower($this->preference_casts[$preference])) + ? strtolower(trim($this->preference_casts[$preference])) : null; } @@ -274,7 +274,7 @@ protected function castPreferenceValue($preference, $value) case 'object': return method_exists($this, 'fromJson') ? $this->fromJson($value, true) - : json_decode($value); + : json_decode($value, false); case 'array': case 'json': return method_exists($this, 'fromJson') @@ -298,11 +298,13 @@ protected function castPreferenceValue($preference, $value) } // Cast Eloquent >= 5.2 compatible types. - if (method_exists($this, 'asTimeStamp')) { - switch ($castTo) { - case 'timestamp': - return $this->asTimeStamp($value); - } + if ($castTo === 'timestamp' && method_exists($this, 'asTimeStamp')) { + return $this->asTimeStamp($value); + } + + // Case Eloquent >= 5.7 compatible types + if (method_exists($this, 'asDecimal') && strpos($castTo, 'decimal:') === 0) { + return $this->asDecimal($value, explode(':', $castTo, 2)[1]); } return $value; diff --git a/tests/HasPreferenceTest.php b/tests/HasPreferenceTest.php index 2564d3f..57d5902 100644 --- a/tests/HasPreferenceTest.php +++ b/tests/HasPreferenceTest.php @@ -37,8 +37,8 @@ class HasPreferenceTest extends PHPUnit_Framework_TestCase */ public static function setUpBeforeClass() { - Eloquent::setConnectionResolver(new ConnectionResolver); - Eloquent::setEventDispatcher(new Dispatcher); + Eloquent::setConnectionResolver(new ConnectionResolver()); + Eloquent::setEventDispatcher(new Dispatcher()); } /** @@ -60,7 +60,7 @@ public function setUp() $table->string('email'); }); - (new CreateModelPreferencesTable)->up(); + (new CreateModelPreferencesTable())->up(); $this->testUser = TestUser::create(['id' => 1, 'email' => 'johndoe@example.org']); } @@ -225,10 +225,15 @@ public function provideInternalTypesInputsAndOutputs() ]; // Eloquent >= 5.2 compatible casts. - if (method_exists(new Preference, 'asTimeStamp')) { + if (method_exists(new Preference(), 'asTimeStamp')) { $provide['timestamp'] = ['timestamp-preference', $date, 'int', $date->timestamp]; } + // Eloquent >= 5.7 compatible casts. + if (method_exists(new Preference(), 'asDecimal')) { + $provide['decimal'] = ['decimal-preference', 12.345, 'string', '12.35']; + } + return $provide; } @@ -253,7 +258,7 @@ public function testCastInternalTypeValues($preference, $input, $expectedInterna */ public function provideObjectTypesInputsAndOutputs() { - $object = new stdClass; + $object = new stdClass(); $object->foo = 'bar'; $collection = new Collection(['foo']); @@ -271,7 +276,7 @@ public function provideObjectTypesInputsAndOutputs() ]; // Eloquent 5.1 compatible casts. - if (method_exists(new Preference, 'asDateTime')) { + if (method_exists(new Preference(), 'asDateTime')) { $provide['date'] = ['date-preference', $date, Carbon::class, $date]; $provide['datetime'] = ['datetime-preference', $date, Carbon::class, $date]; } diff --git a/tests/Models/TestUser.php b/tests/Models/TestUser.php index 3f2f29a..456c64b 100644 --- a/tests/Models/TestUser.php +++ b/tests/Models/TestUser.php @@ -41,5 +41,6 @@ class TestUser extends Model 'datetime-preference' => 'datetime', 'timestamp-preference' => 'timestamp', 'undefined-type-preference' => 'undefined', + 'decimal-preference' => 'decimal:2', ]; } diff --git a/tests/PreferenceTest.php b/tests/PreferenceTest.php index d66038e..f54bdcf 100644 --- a/tests/PreferenceTest.php +++ b/tests/PreferenceTest.php @@ -9,7 +9,7 @@ class PreferenceTest extends PHPUnit_Framework_TestCase { public function testSetTheDefaultTableName() { - $this->assertEquals(Preference::DEFAULT_MODEL_PREFERENCE_TABLE, (new Preference)->getTable()); + $this->assertEquals(Preference::DEFAULT_MODEL_PREFERENCE_TABLE, (new Preference())->getTable()); } /** @@ -19,7 +19,7 @@ public function testSetTheTableNameByConstant() { define('MODEL_PREFERENCE_TABLE', 'foo-constant'); - $this->assertEquals('foo-constant', (new Preference)->getTable()); + $this->assertEquals('foo-constant', (new Preference())->getTable()); } /** @@ -30,12 +30,12 @@ public function testSetTheTableNameByLaravelConfig() // Define config() in the global namespace require_once __DIR__ . '/Support/helpers.php'; - $this->assertEquals('foo-function', (new Preference)->getTable()); + $this->assertEquals('foo-function', (new Preference())->getTable()); } public function testPreferencesHaveNoHiddenAttributesByDefault() { - $this->assertEquals([], (new Preference)->getHidden()); + $this->assertEquals([], (new Preference())->getHidden()); } /** @@ -45,7 +45,7 @@ public function testSetHiddenAttributesByConstant() { define('MODEL_PREFERENCE_HIDDEN_ATTRIBUTES', 'foo,constant'); - $this->assertEquals(['foo', 'constant'], (new Preference)->getHidden()); + $this->assertEquals(['foo', 'constant'], (new Preference())->getHidden()); } /** @@ -56,6 +56,6 @@ public function testSetHiddenAttributesByLaravelConfig() // Define config() in the global namespace require_once __DIR__ . '/Support/helpers.php'; - $this->assertEquals(['foo', 'function'], (new Preference)->getHidden()); + $this->assertEquals(['foo', 'function'], (new Preference())->getHidden()); } } diff --git a/tests/Support/helpers.php b/tests/Support/helpers.php index ca9b7b8..20b37e1 100644 --- a/tests/Support/helpers.php +++ b/tests/Support/helpers.php @@ -6,7 +6,7 @@ * * @param string $key * @param string $default - * @return string + * @return string|array */ function config($key = null, $default = null) {