Skip to content

Revert "Add model scope support" #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Kitar/Dynamodb/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public function __call($method, $parameters)
return $this->$method(...$parameters);
}

if (! in_array($method, $allowedBuilderMethods) && ! $this->hasNamedScope($method)) {
if (! in_array($method, $allowedBuilderMethods)) {
static::throwBadMethodCallException($method);
}

Expand Down
29 changes: 0 additions & 29 deletions src/Kitar/Dynamodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,38 +472,9 @@ public function __call($method, $parameters)
return $this;
}

if ($this->hasNamedScope($method)) {
return $this->callNamedScope($method, $parameters);
}

throw new BadMethodCallException('Call to undefined method ' . static::class . "::{$method}()");
}

/**
* Determine if the given model has a scope.
*
* @param string $scope
*
* @return bool
*/
public function hasNamedScope(string $scope): bool
{
return $this->model_class && (new $this->model_class)->hasNamedScope($scope);
}

/**
* Apply the given named scope on the current builder instance.
*
* @param string $scope
* @param array $parameters
*
* @return mixed
*/
protected function callNamedScope(string $scope, array $parameters = [])
{
return (new $this->model_class)->callNamedScope($scope, array_merge([$this], $parameters));
}

/**
* @inheritdoc
*/
Expand Down
90 changes: 0 additions & 90 deletions tests/Model/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -629,94 +629,4 @@ public function it_cannot_call_disallowed_builder_method()

UserA::clientQuery();
}

/** @test */
public function it_can_call_named_scopes_on_model()
{
$connection = $this->newConnectionMock();
$connection->shouldReceive('clientQuery')->with([
'TableName' => 'User',
'KeyConditionExpression' => '#1 = :1',
'FilterExpression' => '#2 = :2',
'ExpressionAttributeNames' => [
'#1' => 'partition',
'#2' => 'status'
],
'ExpressionAttributeValues' => [
':1' => [
'S' => 'test'
],
':2' => [
'S' => 'active'
]
]
])->andReturn($this->sampleAwsResult())->once();
$this->setConnectionResolver($connection);

UserA::keyCondition('partition', '=', 'test')->active()->query();
}

/** @test */
public function it_can_call_named_scopes_with_parameters_on_model()
{
$connection = $this->newConnectionMock();
$connection->shouldReceive('clientQuery')->with([
'TableName' => 'User',
'KeyConditionExpression' => '#1 = :1',
'FilterExpression' => '#2 = :2',
'ExpressionAttributeNames' => [
'#1' => 'partition',
'#2' => 'name'
],
'ExpressionAttributeValues' => [
':1' => [
'S' => 'test'
],
':2' => [
'S' => 'John'
]
]
])->andReturn($this->sampleAwsResult())->once();
$this->setConnectionResolver($connection);

UserA::keyCondition('partition', '=', 'test')->byName('John')->query();
}

/** @test */
public function it_can_chain_multiple_scopes()
{
$connection = $this->newConnectionMock();
$connection->shouldReceive('clientQuery')->with([
'TableName' => 'User',
'KeyConditionExpression' => '#1 = :1',
'FilterExpression' => '#2 = :2 and #3 = :3',
'ExpressionAttributeNames' => [
'#1' => 'partition',
'#2' => 'status',
'#3' => 'name'
],
'ExpressionAttributeValues' => [
':1' => [
'S' => 'test'
],
':2' => [
'S' => 'active'
],
':3' => [
'S' => 'John'
]
]
])->andReturn($this->sampleAwsResult())->once();
$this->setConnectionResolver($connection);

UserA::keyCondition('partition', '=', 'test')->active()->byName('John')->query();
}

/** @test */
public function it_cannot_call_non_existent_scope_on_model()
{
$this->expectException(BadMethodCallException::class);

UserA::nonExistentScope();
}
}
12 changes: 1 addition & 11 deletions tests/Model/UserA.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ class UserA extends Model implements AuthenticatableContract
protected $table = 'User';
protected $primaryKey = 'partition';
protected $fillable = [
'partition', 'name', 'password', 'status',
'partition', 'name', 'password'
];

public function scopeActive($query)
{
return $query->filter('status', '=', 'active');
}

public function scopeByName($query, $name)
{
return $query->filter('name', '=', $name);
}
}