Skip to content

Commit

Permalink
refact: improve queryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoFerrazfs committed Aug 25, 2023
1 parent 2d86b5c commit 45eedef
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 59 deletions.
15 changes: 11 additions & 4 deletions src/DataMapper/DataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,11 @@ public function where(

$model = new $this->schema->entityClass;

$query = $this->withTrashed ?
QueryBuilder::prepareValueForQueryCompatibility($query) :
QueryBuilder::prepareValueForSoftDeleteCompatibility($query, $model);
$query = QueryBuilder::resolveQuery(
$query,
$model,
$this->withTrashed
);

return new $cursorClass(
$this->schema,
Expand Down Expand Up @@ -309,8 +311,13 @@ public function first(

$model = new $this->schema->entityClass;

$query = QueryBuilder::resolveQuery(
$query,
$model,
);

$document = $this->getCollection()->findOne(
QueryBuilder::prepareValueForSoftDeleteCompatibility($query, $model),
$query,
['projection' => $this->prepareProjection($projection)]
);

Expand Down
16 changes: 11 additions & 5 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,11 @@ public function where(ModelInterface $model, $query = [], array $projection = []
{
$cursor = $useCache ? CacheableCursor::class : Cursor::class;

$query = $this->withTrashed ?
QueryBuilder::prepareValueForQueryCompatibility($query) :
QueryBuilder::prepareValueForSoftDeleteCompatibility($query, $model);

$query = QueryBuilder::resolveQuery(
$query,
$model,
$this->withTrashed
);
return new $cursor(
$model->getCollection(),
'find',
Expand Down Expand Up @@ -252,8 +253,13 @@ public function first(ModelInterface $model, $query = [], array $projection = []
return $this->where($model, $query, $projection, $useCache)->first();
}

$query = QueryBuilder::resolveQuery(
$query,
$model,
);

return $model->getCollection()->findOne(
QueryBuilder::prepareValueForSoftDeleteCompatibility($query, $model),
$query,
['projection' => $this->prepareProjection($projection)],
);
}
Expand Down
20 changes: 15 additions & 5 deletions src/Util/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

class QueryBuilder
{
public static function prepareValueForSoftDeleteCompatibility(mixed $query, ModelInterface $model): array
public static function resolveQuery(mixed $query, ModelInterface $model, bool $withTrashed = false): array
{
$query = self::prepareValueForQueryCompatibility($query);

return self::addSoftDeleteFilterIfRequired($query, $model);
return $withTrashed ?
QueryBuilder::prepareValueForQueryCompatibility($query) :
QueryBuilder::prepareValueForSoftDeleteCompatibility(
$query,
$model
);
}

public static function getDeletedAtColumn(ModelInterface $model): string
Expand All @@ -23,7 +26,7 @@ public static function getDeletedAtColumn(ModelInterface $model): string
: 'deleted_at';
}

public static function prepareValueForQueryCompatibility(mixed $query): array
private static function prepareValueForQueryCompatibility(mixed $query): array
{
if (!is_array($query)) {
$query = ['_id' => $query];
Expand All @@ -47,6 +50,13 @@ public static function prepareValueForQueryCompatibility(mixed $query): array
return $query;
}

private static function prepareValueForSoftDeleteCompatibility(mixed $query, ModelInterface $model): array
{
$query = self::prepareValueForQueryCompatibility($query);

return self::addSoftDeleteFilterIfRequired($query, $model);
}

private static function addSoftDeleteFilterIfRequired(array $query, ModelInterface $model): array
{
if ($model->isSoftDeleteEnabled) {
Expand Down
2 changes: 0 additions & 2 deletions tests/Integration/PersistLegacyModelWithSoftDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

final class PersistLegacyModelWithSoftDeleteTest extends IntegrationTestCase
{
private ObjectId $_id;

public function testShouldFindUndeletedProduct(): void
{
// Set
Expand Down
65 changes: 22 additions & 43 deletions tests/Unit/Util/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ final class QueryBuilderTest extends TestCase
* @dataProvider queryValueScenarios
*/
public function testShouldPrepareQueryValue(
mixed $value,
mixed $query,
bool $isSoftDeleteEnabled,
array $expectation
): void {
// Actions
$result = QueryBuilder::prepareValueForQueryCompatibility($value);
$model = m::mock(ModelInterface::class);
$model->isSoftDeleteEnabled = $isSoftDeleteEnabled;
$result = QueryBuilder::resolveQuery($query, $model, false);

// Assertions
$this->assertMongoQueryEquals($expectation, $result);
Expand All @@ -28,77 +31,52 @@ public function queryValueScenarios(): array
{
return [
'An array' => [
'value' => ['age' => ['$gt' => 25]],
'query' => ['age' => ['$gt' => 25]],
'isSoftDeleteEnabled' => false,
'expectation' => ['age' => ['$gt' => 25]],
],
'An ObjectId string' => [
'value' => '507f1f77bcf86cd799439011',
'query' => '507f1f77bcf86cd799439011',
'isSoftDeleteEnabled' => false,
'expectation' => [
'_id' => new ObjectID(
'_id' => new ObjectId(
'507f1f77bcf86cd799439011'
),
],
],
'An ObjectId string within a query' => [
'value' => ['_id' => '507f1f77bcf86cd799439011'],
'query' => ['_id' => '507f1f77bcf86cd799439011'],
'isSoftDeleteEnabled' => false,
'expectation' => [
'_id' => new ObjectID(
'_id' => new ObjectId(
'507f1f77bcf86cd799439011'
),
],
],
'Other type of _id, sequence for example' => [
'value' => 7,
'expectation' => ['_id' => 7],
],
'Series of string _ids as the $in parameter' => [
'value' => ['_id' => ['$in' => ['507f1f77bcf86cd799439011', '507f1f77bcf86cd799439012']]],
'query' => ['_id' => ['$in' => ['507f1f77bcf86cd799439011', '507f1f77bcf86cd799439012']]],
'isSoftDeleteEnabled' => false,
'expectation' => [
'_id' => [
'$in' => [
new ObjectID('507f1f77bcf86cd799439011'),
new ObjectID('507f1f77bcf86cd799439012'),
new ObjectId('507f1f77bcf86cd799439011'),
new ObjectId('507f1f77bcf86cd799439012'),
],
],
],
],
'Series of string _ids as the $in parameter' => [
'value' => ['_id' => ['$nin' => ['507f1f77bcf86cd799439011']]],
'Series of string _ids as the $nin parameter' => [
'query' => ['_id' => ['$nin' => ['507f1f77bcf86cd799439011']]],
'isSoftDeleteEnabled' => false,
'expectation' => [
'_id' => [
'$nin' => [new ObjectID(
'$nin' => [new ObjectId(
'507f1f77bcf86cd799439011'
),
],
],
],
],
];
}

/**
* @dataProvider getQuery
*/
public function testShouldPrepareValueForSoftDeleteCompatibility(
mixed $query,
bool $isSoftDeleteEnabled,
array $expected
): void {
// Set
$model = m::mock(ModelInterface::class);
$model->isSoftDeleteEnabled = $isSoftDeleteEnabled;

// Actions
$actual = QueryBuilder::prepareValueForSoftDeleteCompatibility($query, $model);

// Assertions
$this->assertSame($expected, $actual);
}

public function getQuery(): array
{
$objectId = new ObjectId('64e8963b1de34f08a40502e0');
return [
'When query is a string and softDelete is enabled' => [
'query' => '123',
'isSoftDeleteEnabled' => true,
Expand Down Expand Up @@ -147,6 +125,7 @@ public function getQuery(): array
];
}


/**
* @dataProvider setDefaultClass
*/
Expand Down

0 comments on commit 45eedef

Please sign in to comment.