Skip to content

Commit

Permalink
Add database query builder test
Browse files Browse the repository at this point in the history
  • Loading branch information
onursimsek committed Sep 8, 2024
1 parent 2bf2963 commit b3d2b7d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Workbench\App\Models\Product;

it('boundary constraints', function ($method, $column, $value, $sql) {
expect(Product::query()->{$method}($column, $value)->toRawSql())->toEqual($sql);
})->with([
// Greater
['whereGreaterThan', 'price', 500, 'select * from "products" where "price" > 500'],
['whereGreaterThanOrEqual', 'price', 500, 'select * from "products" where "price" >= 500'],
['whereColumnGreaterThan', 'price', 'amount', 'select * from "products" where "price" > "amount"'],
['whereColumnGreaterThanOrEqual', 'price', "amount", 'select * from "products" where "price" >= "amount"'],
// Less
['whereLessThan', 'price', 500, 'select * from "products" where "price" < 500'],
['whereLessThanOrEqual', 'price', 500, 'select * from "products" where "price" <= 500'],
['whereColumnLessThan', 'price', 'amount', 'select * from "products" where "price" < "amount"'],
['whereColumnLessThanOrEqual', 'price', "amount", 'select * from "products" where "price" <= "amount"'],
]);

it('conditional constraints', function ($value, $column, $sql) {
expect(Product::query()->whenWhere($value, $column)->toRawSql())->toEqual($sql);
})->with([
[true, 'is_active', 'select * from "products" where "is_active" = 1'],
[false, 'is_active', 'select * from "products"'],
]);
9 changes: 9 additions & 0 deletions workbench/app/Models/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Workbench\App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
}

0 comments on commit b3d2b7d

Please sign in to comment.