Skip to content

Commit

Permalink
Merge pull request #5 from klaude/add-decimal-casting
Browse files Browse the repository at this point in the history
Add a missing decimal cast feature and perform cleanup
  • Loading branch information
klaude authored Sep 5, 2019
2 parents 5d37276 + 0635e25 commit 5ed16bf
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 50 deletions.
18 changes: 14 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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+.
Expand Down Expand Up @@ -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
18 changes: 0 additions & 18 deletions CHANGELOG.md

This file was deleted.

8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
}
],
"require": {
"illuminate/database": "~5.0",
"illuminate/support": "~5.0"
"ext-json": "*",
"illuminate/database": "~5|~6.0",
"illuminate/support": "~5|~6.0"
},
"autoload": {
"psr-4": {
"KLaude\\EloquentPreferences\\": "src/"
}
},
"require-dev": {
"illuminate/events": "~5.0",
"ext-pdo": "*",
"illuminate/events": "~5|~6.0",
"phpunit/phpunit": "~5.0|~4.8"
},
"autoload-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -36,6 +36,6 @@ public function down()
Model::getConnectionResolver()
->connection()
->getSchemaBuilder()
->drop((new Preference)->getQualifiedTableName());
->drop((new Preference())->getQualifiedTableName());
}
}
3 changes: 2 additions & 1 deletion src/EloquentPreferencesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}

Expand Down
20 changes: 11 additions & 9 deletions src/HasPreferences.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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])
);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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')
Expand All @@ -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;
Expand Down
17 changes: 11 additions & 6 deletions tests/HasPreferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand All @@ -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']);
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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']);

Expand All @@ -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];
}
Expand Down
1 change: 1 addition & 0 deletions tests/Models/TestUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ class TestUser extends Model
'datetime-preference' => 'datetime',
'timestamp-preference' => 'timestamp',
'undefined-type-preference' => 'undefined',
'decimal-preference' => 'decimal:2',
];
}
12 changes: 6 additions & 6 deletions tests/PreferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}
}
2 changes: 1 addition & 1 deletion tests/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* @param string $key
* @param string $default
* @return string
* @return string|array
*/
function config($key = null, $default = null)
{
Expand Down

0 comments on commit 5ed16bf

Please sign in to comment.