From 8f1856b51720ff5edce0d9b22c64608337f87879 Mon Sep 17 00:00:00 2001 From: Kevin Laude Date: Sun, 27 Dec 2015 20:40:44 -0600 Subject: [PATCH] Run tests in Laravel 5.0, 5.1, and 5.2 Provide a little more compatibility with Eloquent 5.0, verify unit tests run and pass when Eloquents 5.0 and 5.1 are loaded, and attempt to test 5.0, 5.1, and 5.2 in Travis. --- .travis.yml | 9 +++++++++ src/HasPreferences.php | 4 ++-- tests/HasPreferenceTest.php | 24 +++++++++++++++++++----- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0179f0c..8baeec5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,5 +6,14 @@ php: - "7.0" - hhvm +env: + - LARAVEL_VERSION="5.0.*" + - LARAVEL_VERSION="5.1.*" + - LARAVEL_VERSION="5.2.*" + before_script: - 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} diff --git a/src/HasPreferences.php b/src/HasPreferences.php index a9b4a8b..6308c4e 100644 --- a/src/HasPreferences.php +++ b/src/HasPreferences.php @@ -70,8 +70,8 @@ public function setPreference($preference, $value) // Serialize date and JSON-like preference values. if ($this->isPreferenceDateCastable($preference)) { $value = $this->fromDateTime($value); - } elseif ($this->isPreferenceJsonCastable($preference) && method_exists($this, 'asJson')) { - $value = $this->asJson($value); + } elseif ($this->isPreferenceJsonCastable($preference)) { + $value = method_exists($this, 'asJson') ? $this->asJson($value) : json_encode($value); } /** @var Preference $savedPreference */ diff --git a/tests/HasPreferenceTest.php b/tests/HasPreferenceTest.php index ec8d8c6..8fd8635 100644 --- a/tests/HasPreferenceTest.php +++ b/tests/HasPreferenceTest.php @@ -207,7 +207,8 @@ public function provideInternalTypesInputsAndOutputs() { $date = Carbon::now(); - return [ + // Eloquent 5.0 and 5.1 compatible casts. + $provide = [ 'int cast to int' => ['int-preference', 1234, 'int', 1234], 'string cast to int' => ['int-preference', '1234', 'int', 1234], 'integer' => ['integer-preference', 1234, 'int', 1234], @@ -220,9 +221,15 @@ public function provideInternalTypesInputsAndOutputs() 'int true cast to bool' => ['bool-preference', 1, 'bool', true], 'array cast to array' => ['array-preference', [1, 2], 'array', [1, 2]], 'json cast to array' => ['json-preference', [1, 2], 'array', [1, 2]], - 'timestamp' => ['timestamp-preference', $date, 'int', $date->timestamp], 'unknown types don\'t get cast' => ['undefined-type-preference', '1234', 'string', '1234'], ]; + + // Eloquent 5.2 compatible casts. + if (method_exists(new Preference, 'asTimeStamp')) { + $provide['timestamp'] = ['timestamp-preference', $date, 'int', $date->timestamp]; + } + + return $provide; } /** @@ -251,12 +258,19 @@ public function provideObjectTypesInputsAndOutputs() $collection = new Collection(['foo']); $date = Carbon::now(); - return [ + // Eloquent 5.0 compatible casts. + $provide = [ 'object' => ['object-preference', $object, 'stdClass', $object], 'collection' => ['collection-preference', $collection, Collection::class, $collection], - 'date' => ['date-preference', $date, Carbon::class, $date], - 'datetime' => ['datetime-preference', $date, Carbon::class, $date], ]; + + // Eloquent 5.1 compatible casts. + if (method_exists(new Preference, 'asDateTime')) { + $provide['date'] = ['date-preference', $date, Carbon::class, $date]; + $provide['datetime'] = ['datetime-preference', $date, Carbon::class, $date]; + } + + return $provide; } /**