Skip to content

Commit

Permalink
Fix scopes methods and add test (#14078)
Browse files Browse the repository at this point in the history
  • Loading branch information
sileence authored and taylorotwell committed Jun 20, 2016
1 parent 8397913 commit 838a5bd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ public function scopes(array $scopes)
}

$builder = $builder->callScope(
'scope'.ucfirst($scope), (array) $parameters
[$this->model, 'scope'.ucfirst($scope)], (array) $parameters
);
}

Expand Down
33 changes: 33 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Mockery as m;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;

class DatabaseEloquentModelTest extends PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -1348,6 +1349,22 @@ public function testStringIdTypePreserved()
$this->assertEquals('string id', $model->id);
}

public function testScopesMethod()
{
$model = new EloquentModelStub;
$this->addMockConnection($model);

$scopes = [
'published',
'category' => 'Laravel',
'framework' => ['Laravel', '5.2'],
];

$this->assertInstanceOf(Builder::class, $model->scopes($scopes));

$this->assertSame($scopes, $model->scopesCalled);
}

protected function addMockConnection($model)
{
$model->setConnectionResolver($resolver = m::mock('Illuminate\Database\ConnectionResolverInterface'));
Expand All @@ -1371,6 +1388,7 @@ public function saved()
class EloquentModelStub extends Model
{
public $connection;
public $scopesCalled = [];
protected $table = 'stub';
protected $guarded = [];
protected $morph_to_stub_type = 'EloquentModelSaveStub';
Expand Down Expand Up @@ -1429,6 +1447,21 @@ public function getAppendableAttribute()
{
return 'appended';
}

public function scopePublished(Builder $builder)
{
$this->scopesCalled[] = 'published';
}

public function scopeCategory(Builder $builder, $category)
{
$this->scopesCalled['category'] = $category;
}

public function scopeFramework(Builder $builder, $framework, $version)
{
$this->scopesCalled['framework'] = [$framework, $version];
}
}

class EloquentModelCamelStub extends EloquentModelStub
Expand Down

0 comments on commit 838a5bd

Please sign in to comment.