From 3fd8339bfc6e5cd7c4b7d07bd7cf63d010001ee6 Mon Sep 17 00:00:00 2001 From: Jarek Tkaczyk Date: Thu, 25 Aug 2016 10:45:11 +0800 Subject: [PATCH] 5.3 fixes --- .travis.yml | 1 - src/Builder.php | 12 +++--------- src/Eloquence.php | 2 +- src/Mappable.php | 2 +- src/Metable.php | 7 +------ src/Metable/Attribute.php | 2 -- src/Mutator/Mutator.php | 2 -- src/Relations/Joiner.php | 4 ++-- tests/MappableTest.php | 13 ++++++++----- tests/MetableTest.php | 38 ++++++++++++++++++++++++++------------ 10 files changed, 42 insertions(+), 41 deletions(-) diff --git a/.travis.yml b/.travis.yml index 78c42aa..e53ebb7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 5.5 - 5.6 - 7.0 - hhvm diff --git a/src/Builder.php b/src/Builder.php index 183514c..d6496a9 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -132,7 +132,9 @@ protected function addSearchClauses( $whereBindings = $this->searchSelect($subquery, $columns, $words, $threshold); // For morphOne/morphMany support we need to port the bindings from JoinClauses. - $joinBindings = array_flatten(array_pluck((array) $subquery->getQuery()->joins, 'bindings')); + $joinBindings = collect($subquery->getQuery()->joins)->flatMap(function ($join) { + $join->getBindings(); + })->all(); $this->addBinding($joinBindings, 'select'); @@ -268,25 +270,18 @@ protected function countBindings(array $where, $type) { if ($this->isHasWhere($where, $type)) { return substr_count($where['column'] . $where['value'], '?'); - } elseif ($type === 'basic') { return (int) !$where['value'] instanceof Expression; - } elseif (in_array($type, ['basic', 'date', 'year', 'month', 'day'])) { return (int) !$where['value'] instanceof Expression; - } elseif (in_array($type, ['null', 'notnull'])) { return 0; - } elseif ($type === 'between') { return 2; - } elseif (in_array($type, ['in', 'notin'])) { return count($where['values']); - } elseif ($type === 'raw') { return substr_count($where['sql'], '?'); - } elseif (in_array($type, ['nested', 'sub', 'exists', 'notexists', 'insub', 'notinsub'])) { return count($where['query']->getBindings()); } @@ -457,7 +452,6 @@ protected function joinForSearch($mappings, $subquery) $columns->add( new Column($grammar, $related->getTable(), $column, $mapping, $weight) ); - } else { $columns->add( new Column($grammar, $this->model->getTable(), $mapping, $mapping, $weight) diff --git a/src/Eloquence.php b/src/Eloquence.php index 2106aa9..15ea16b 100644 --- a/src/Eloquence.php +++ b/src/Eloquence.php @@ -16,7 +16,7 @@ * It also provides hasColumn and getColumnListing helper methods * so you can easily list or check columns in the model's table. * - * @version 5.1 + * @version 5.3 * * @method \Illuminate\Database\Connection getConnection() * @method string getTable() diff --git a/src/Mappable.php b/src/Mappable.php index 023a113..6ef6d46 100644 --- a/src/Mappable.php +++ b/src/Mappable.php @@ -107,7 +107,7 @@ protected function mappedSelect(Builder $query, ArgumentBag $args) $columns[$key] = "{$table}.{$mapped}"; - if ($as !== $mapped) { + if ($as !== $column) { $columns[$key] .= " as {$as}"; } diff --git a/src/Metable.php b/src/Metable.php index e41980a..bd60909 100644 --- a/src/Metable.php +++ b/src/Metable.php @@ -20,7 +20,7 @@ trait Metable protected $metaQueryable = [ 'where', 'whereBetween', 'whereIn', 'whereNull', 'whereDate', 'whereYear', 'whereMonth', 'whereDay', - 'orderBy', 'pluck', 'value', 'aggregate', 'lists' + 'orderBy', 'pluck', 'value', 'aggregate', ]; /** @@ -144,11 +144,6 @@ protected function orderByMeta(Builder $query, $args, $alias) return $query; } - protected function listsMeta(Builder $query, ArgumentBag $args, $alias) - { - return $this->pluckMeta($query, $args, $alias); - } - /** * Get an array with the values of given meta attribute. * diff --git a/src/Metable/Attribute.php b/src/Metable/Attribute.php index 8c7d66c..3fba183 100644 --- a/src/Metable/Attribute.php +++ b/src/Metable/Attribute.php @@ -85,7 +85,6 @@ public function __construct($key = null, $value = null) // default behaviour if (is_array($key)) { parent::__construct($key); - } else { parent::__construct(); @@ -221,7 +220,6 @@ public function setValue($value) if ($this->hasMutator($value, 'setter')) { $value = $this->mutateValue($value, 'setter'); - } elseif (!$this->isStringable($value) && !is_null($value)) { throw new InvalidTypeException( "Unsupported meta value type [{$this->getValueType($value)}]." diff --git a/src/Mutator/Mutator.php b/src/Mutator/Mutator.php index 2ad8aff..32ca26c 100644 --- a/src/Mutator/Mutator.php +++ b/src/Mutator/Mutator.php @@ -50,10 +50,8 @@ protected function parse($callable) if ($this->isClassMethod($callable)) { $callable = $this->parseClassMethod($callable); - } elseif ($this->isMutatorMethod($callable)) { $callable = [$this, $callable]; - } elseif (!function_exists($callable)) { throw new InvalidCallableException("Function [{$callable}] not found."); } diff --git a/src/Relations/Joiner.php b/src/Relations/Joiner.php index 842b29f..f1dc8d4 100644 --- a/src/Relations/Joiner.php +++ b/src/Relations/Joiner.php @@ -132,7 +132,7 @@ protected function getJoinClause(Model $parent, Relation $relation, $table, $typ { list($fk, $pk) = $this->getJoinKeys($relation); - $join = (new Join($type, $table))->on($fk, '=', $pk); + $join = (new Join($this->query, $type, $table))->on($fk, '=', $pk); if ($relation instanceof MorphOneOrMany) { $join->where($relation->getMorphType(), '=', $parent->getMorphClass()); @@ -161,7 +161,7 @@ protected function joinIntermediate(Model $parent, Relation $relation, $type) $pk = $parent->getQualifiedKeyName(); - if (!$this->alreadyJoined($join = (new Join($type, $table))->on($fk, '=', $pk))) { + if (!$this->alreadyJoined($join = (new Join($this->query, $type, $table))->on($fk, '=', $pk))) { $this->query->joins[] = $join; } } diff --git a/tests/MappableTest.php b/tests/MappableTest.php index e7b150c..d1c6570 100644 --- a/tests/MappableTest.php +++ b/tests/MappableTest.php @@ -3,14 +3,18 @@ namespace Sofa\Eloquence\Tests; use Mockery as m; -use Illuminate\Database\Eloquent\Model; -use Sofa\Eloquence\Eloquence; use Sofa\Eloquence\Mappable; +use Sofa\Eloquence\Eloquence; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\Relation; class MappableTest extends \PHPUnit_Framework_TestCase { public function setUp() { + Relation::morphMap([ + 'UserMorph' => MappableEloquentStub::class, + ]); $this->model = new MappableStub; } @@ -386,7 +390,7 @@ public function it_sets_mapped_value() /** * @test */ - public function it_inherits_base_model_proper() + public function it_inherits_base_model_properly() { $q = $this->getModel(new UserStub)->select(['id', 'name'])->where('id', '>', 0); $expectedSql = 'select "user_stubs"."usr_id", "user_stubs"."usr_name" from "user_stubs" where "usr_id" > ?'; @@ -491,7 +495,6 @@ class MappableEloquentStub extends Model { use Eloquence, Mappable; protected $table = 'users'; - protected $morphClass = 'UserMorph'; protected $maps = [ 'first_name' => 'profile.first_name', 'profile' => ['last_name', 'age'], @@ -564,4 +567,4 @@ class BookStub extends BaseModelStub { 'name' => 'bk_name', 'pages' => 'bk_pages', ]; -} \ No newline at end of file +} diff --git a/tests/MetableTest.php b/tests/MetableTest.php index 943f4c7..e443db5 100644 --- a/tests/MetableTest.php +++ b/tests/MetableTest.php @@ -3,17 +3,25 @@ namespace Sofa\Eloquence\Tests; use Mockery as m; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Query\Builder as Query; -use Illuminate\Database\Query\Grammars\Grammar; -use Illuminate\Database\Query\Expression; use Sofa\Eloquence\Builder; -use Sofa\Eloquence\Eloquence; use Sofa\Eloquence\Metable; +use Sofa\Eloquence\Eloquence; use Sofa\Eloquence\ArgumentBag; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Query\Expression; +use Illuminate\Database\Query\Builder as Query; +use Illuminate\Database\Query\Grammars\Grammar; +use Illuminate\Database\Eloquent\Relations\Relation; class MetableTest extends \PHPUnit_Framework_TestCase { + public function setUp() + { + Relation::morphMap([ + 'Metable' => MetableEloquentStub::class, + ]); + } + public function tearDown() { m::close(); @@ -465,18 +473,18 @@ public function meta_where() public function it_saves_meta_attributes() { $size = m::mock('StdClass'); - $size->shouldReceive('getValue')->andReturn(null); + $size->shouldReceive('getValue')->once()->andReturn(null); $size->shouldReceive('delete')->once(); $color = m::mock('StdClass'); - $color->shouldReceive('getValue')->andReturn('red'); + $color->shouldReceive('getValue')->once()->andReturn('red'); $relation = m::mock('StdClass'); - $relation->shouldReceive('save')->with($color); + $relation->shouldReceive('save')->once()->with($color); $model = m::mock('\Sofa\Eloquence\Tests\MetableEloquentStub')->makePartial(); - $model->shouldReceive('getMetaAttributes')->andReturn([$color, $size]); - $model->shouldReceive('metaAttributes')->andReturn($relation); + $model->shouldReceive('getMetaAttributes')->once()->andReturn([$color, $size]); + $model->shouldReceive('metaAttributes')->once()->andReturn($relation); $model->exists = true; $model->save(['timestamps' => false, 'touch' => false]); @@ -611,11 +619,17 @@ public function getModel() } } -class MetableEloquentStub extends Model { +class ParentModel extends Model { + public function save() + { + return true; + } +} + +class MetableEloquentStub extends ParentModel { use Eloquence, Metable; protected $table = 'metables'; - protected $morphClass = 'Metable'; public $aliases = [];