Skip to content
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

feat(AbstractInScope): support scalar value add test #105

Merged
merged 1 commit into from
Dec 2, 2024
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"require-dev": {
"ext-pdo": "*",
"ext-sqlite3": "*",
"jetbrains/phpstorm-attributes": "^1.2",
"mockery/mockery": "^1.5.0",
"nette/php-generator": "v4.0.5",
"nikic/php-parser": "v4.15.2",
Expand Down
17 changes: 0 additions & 17 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ parameters:
path: src/Testing/Actions/ParsePhpDocAction.php

# forward compatibility
-
message: "#^Attribute class PHPUnit\\\\Framework\\\\Attributes\\\\Before does not exist\\.$#"
count: 1
path: src/Testing/Assert/AssertExpectationTestCase.php

-
message: "#^Attribute class PHPUnit\\\\Framework\\\\Attributes\\\\PostCondition does not exist\\.$#"
count: 1
path: src/Testing/Assert/AssertExpectationTestCase.php

-
message: "#^Method LaraStrict\\\\Testing\\\\Assert\\\\AssertExpectationTestCase\\:\\:beforeStartAssertExpectationManager\\(\\) has no return type specified\\.$#"
count: 1
Expand All @@ -51,10 +41,3 @@ parameters:
message: "#^Method Tests\\\\LaraStrict\\\\Feature\\\\Translations\\\\InvalidServiceProviderTranslations\\:\\:getProviderClass\\(\\) should return class\\-string\\<LaraStrict\\\\Providers\\\\AbstractServiceProvider\\> but returns string\\.$#"
count: 1
path: tests/Feature/Translations/InvalidServiceProviderTranslations.php

# forward compatibility
-
message: "#^Attribute class PHPUnit\\\\Framework\\\\Attributes\\\\BeforeClass does not exist\\.$#"
count: 1
path: tests/Unit/Database/Services/ChunkWriteServiceTest.php
# forward compatibility
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ parameters:
count: 1
path: src/Database/Models/Casts/FloatCast.php
reportUnmatched: false
# forward compatibility
- "#^Attribute class PHPUnit\\\\Framework\\\\Attributes\\\\.*?does not exist\\.$#"

14 changes: 10 additions & 4 deletions src/Database/Scopes/AbstractInScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use JetBrains\PhpStorm\Deprecated;

/**
* Adds whereIn or where condition for given values.
Expand All @@ -26,10 +27,11 @@ abstract class AbstractInScope extends AbstractScope
* 'or' value
*/
public function __construct(
private array $values,
private array|int|float|string|bool|null $values,
#[Deprecated(reason: 'Use right parameter instead of this magic.')]
string|bool|null $booleanOrTableOrNot = null,
string $table = '',
bool $not = false
bool $not = false,
) {
if (is_bool($booleanOrTableOrNot)) {
$not = $booleanOrTableOrNot;
Expand All @@ -49,8 +51,12 @@ public function __construct(
public function apply(Builder $builder, Model $model): void
{
$column = $this->table === '' ? $this->getColumn($model) : $this->table . '.' . $this->getColumn($model);
if (count($this->values) === 1) {
$builder->where($column, $this->not ? '<>' : '=', reset($this->values), $this->boolean);
if (is_array($this->values) && count($this->values) === 1) {
$this->values = reset($this->values);
}

if (is_array($this->values) === false) {
$builder->where($column, $this->not ? '!=' : '=', $this->values, $this->boolean);

return;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Database/Scopes/WhereIdsScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
namespace LaraStrict\Database\Scopes;

use Illuminate\Database\Eloquent\Model;
use JetBrains\PhpStorm\Deprecated;

class WhereIdsScope extends AbstractInScope
{
public function __construct(
array $ids,
array|int $ids,
private readonly ?string $key = null,
#[Deprecated(reason: 'Use right parameter instead of this magic.')]
string|bool|null $booleanOrTableOrNot = null,
string $table = '',
bool $not = false
Expand Down
91 changes: 91 additions & 0 deletions tests/Unit/Database/Scopes/WhereIdsScopeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace Tests\LaraStrict\Unit\Database\Scopes;

use Closure;
use LaraStrict\Database\Scopes\WhereIdsScope;
use LaraStrict\Tests\Traits\SqlTestEnable;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Tests\LaraStrict\Unit\Database\Services\TestModel;

class WhereIdsScopeTest extends TestCase
{
use SqlTestEnable;

/**
* @return array<string|int, array{0: Closure(static):void}>
*/
public static function data(): array
{
return [
[
static function (self $self) {
$self->assert(new WhereIdsScope(1), 'select * from "test_models" where "id" = ?');
},
],
[
static function (self $self) {
$self->assert(new WhereIdsScope([1]), 'select * from "test_models" where "id" = ?');
},
],
[
static function (self $self) {
$self->assert(
new WhereIdsScope([1], not: true),
'select * from "test_models" where "id" != ?',
);
},
],
[
static function (self $self) {
$self->assert(new WhereIdsScope([1, 2]), 'select * from "test_models" where "id" in (?, ?)');
},
],
[
static function (self $self) {
$self->assert(
new WhereIdsScope([1, 2], 'foo'),
'select * from "test_models" where "foo" in (?, ?)',
);
},
],
[
static function (self $self) {
$self->assert(
new WhereIdsScope([1, 2], table: 'foo'),
'select * from "test_models" where "foo"."id" in (?, ?)',
);
},
],
[
static function (self $self) {
$self->assert(
new WhereIdsScope([1, 2], not: true),
'select * from "test_models" where "id" not in (?, ?)',
);
},
],
];
}

/**
* @param Closure(static):void $assert
* @dataProvider data
*/
public function test(Closure $assert): void
{
$assert($this);
}

public function assert(WhereIdsScope $scope, string $expected): void
{
$builder = TestModel::query();
$scope->apply($builder, new TestModel());

$sql = $builder->toSql();
Assert::assertSame($expected, $sql);
}
}