From 7bbcc965603d7b75271cf64077c2282f0b82ec29 Mon Sep 17 00:00:00 2001 From: Satoshi Kita Date: Mon, 9 Jun 2025 00:47:32 +0900 Subject: [PATCH] Revert "Add model scope support" --- src/Kitar/Dynamodb/Model/Model.php | 2 +- src/Kitar/Dynamodb/Query/Builder.php | 29 --------- tests/Model/ModelTest.php | 90 ---------------------------- tests/Model/UserA.php | 12 +--- 4 files changed, 2 insertions(+), 131 deletions(-) diff --git a/src/Kitar/Dynamodb/Model/Model.php b/src/Kitar/Dynamodb/Model/Model.php index 6eb4b57..1698f56 100644 --- a/src/Kitar/Dynamodb/Model/Model.php +++ b/src/Kitar/Dynamodb/Model/Model.php @@ -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); } diff --git a/src/Kitar/Dynamodb/Query/Builder.php b/src/Kitar/Dynamodb/Query/Builder.php index 5cb9e2e..76c5c5a 100644 --- a/src/Kitar/Dynamodb/Query/Builder.php +++ b/src/Kitar/Dynamodb/Query/Builder.php @@ -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 */ diff --git a/tests/Model/ModelTest.php b/tests/Model/ModelTest.php index 1421d59..90dda42 100644 --- a/tests/Model/ModelTest.php +++ b/tests/Model/ModelTest.php @@ -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(); - } } diff --git a/tests/Model/UserA.php b/tests/Model/UserA.php index fc57b7d..94c2d7d 100644 --- a/tests/Model/UserA.php +++ b/tests/Model/UserA.php @@ -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); - } }