Skip to content

Commit

Permalink
fix: count function without parameter bug
Browse files Browse the repository at this point in the history
  • Loading branch information
SiavashBamshadnia committed Feb 1, 2024
1 parent 87dcd29 commit 2018232
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/Numeric/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace sbamtr\LaravelQueryEnrich\Numeric;

use sbamtr\LaravelQueryEnrich\DBFunction;
use sbamtr\LaravelQueryEnrich\QE;

/**
* Returns the number of records returned by a select query.
Expand All @@ -13,7 +14,11 @@ class Count extends DBFunction

public function __construct(mixed $parameter = '*')
{
$this->parameter = $parameter;
if ($parameter === '*') {
$this->parameter = QE::raw('*');
} else {
$this->parameter = $parameter;
}
}

protected function getQuery(): string
Expand Down
3 changes: 2 additions & 1 deletion workbench/BaseTest/BaseBasicFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Facades\DB;
use sbamtr\LaravelQueryEnrich\QE;
use Workbench\App\Models\Author;
use function sbamtr\LaravelQueryEnrich\c;

abstract class BaseBasicFunctionsTest extends BaseTest
{
Expand All @@ -18,7 +19,7 @@ public function testEscapeCondition()
]);

$queryResult = Author::whereRaw(
QE::condition('first_name', 'like', '%%')->toSql()
QE::condition(c('first_name'), 'like', '%%')
)->count();

self::assertEquals(0, $queryResult);
Expand Down
49 changes: 48 additions & 1 deletion workbench/BaseTest/BaseProjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use sbamtr\LaravelQueryEnrich\QE;
use Workbench\App\Models\Author;
use Workbench\App\Models\Book;

use function sbamtr\LaravelQueryEnrich\c;

abstract class BaseProjectionTest extends BaseTest
Expand Down Expand Up @@ -691,6 +690,54 @@ public function testCount()
self::assertEquals($expected_2, $actual_2);
}

public function testCountWithoutParameter()
{
$author_1 = Author::create([
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
'email' => $this->faker->email,
]);
$author_2 = Author::create([
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
'email' => $this->faker->email,
]);
$count = $this->faker->numberBetween(2, 100);
$booksToInsert = [];
for ($i = 0; $i < $count; $i++) {
$booksToInsert[] = [
'title' => $this->faker->title,
'description' => $this->faker->text,
'price' => 100,
'year' => $this->faker->year,
];
if ($i % 2 == 0) {
$booksToInsert[$i]['author_id'] = $author_1->id;
} else {
$booksToInsert[$i]['author_id'] = $author_2->id;
}
}
Book::insert($booksToInsert);

$books = Book::select(
QE::count()->as('result')
)->groupBy(
'author_id'
)->orderBy(
'author_id'
)->get();

$actual_1 = $books[0]->result;
$expected_1 = Book::where('author_id', 1)->count();

self::assertEquals($expected_1, $actual_1);

$actual_2 = $books[1]->result;
$expected_2 = Book::where('author_id', 2)->count();

self::assertEquals($expected_2, $actual_2);
}

public function testRadianToDegrees()
{
$number = $this->faker->randomFloat();
Expand Down

0 comments on commit 2018232

Please sign in to comment.