Skip to content

Commit

Permalink
5.3 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jarektkaczyk committed Aug 25, 2016
1 parent a14e490 commit 3fd8339
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 41 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.5
- 5.6
- 7.0
- hhvm
Expand Down
12 changes: 3 additions & 9 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Eloquence.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/Mappable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
}

Expand Down
7 changes: 1 addition & 6 deletions src/Metable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];

/**
Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 0 additions & 2 deletions src/Metable/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public function __construct($key = null, $value = null)
// default behaviour
if (is_array($key)) {
parent::__construct($key);

} else {
parent::__construct();

Expand Down Expand Up @@ -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)}]."
Expand Down
2 changes: 0 additions & 2 deletions src/Mutator/Mutator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down
4 changes: 2 additions & 2 deletions src/Relations/Joiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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;
}
}
Expand Down
13 changes: 8 additions & 5 deletions tests/MappableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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" > ?';
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -564,4 +567,4 @@ class BookStub extends BaseModelStub {
'name' => 'bk_name',
'pages' => 'bk_pages',
];
}
}
38 changes: 26 additions & 12 deletions tests/MetableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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 = [];

Expand Down

0 comments on commit 3fd8339

Please sign in to comment.