Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

PHPORM-33 Add tests on Query\Builder methods #14

Merged
merged 5 commits into from
Jul 26, 2023
Merged

PHPORM-33 Add tests on Query\Builder methods #14

merged 5 commits into from
Jul 26, 2023

Conversation

GromNaN
Copy link
Owner

@GromNaN GromNaN commented Jul 17, 2023

Fix PHPORM-33
Replace #4

  • Add tests on query builder methods that don't need to be fixed.
  • Throw exception when calling unsupported methods: whereIntegerInRaw, orWhereIntegerInRaw, whereIntegerNotInRaw, orWhereIntegerNotInRaw
  • Throw an exception when Query\Builder::where is called with only a column name

@GromNaN GromNaN requested review from jmikola and alcaeus July 17, 2023 15:28
['find' => [['foo' => ['$in' => ['bar', 'baz']]], []]],
fn (Builder $builder) => $builder->whereIn('foo', ['bar', 'baz']),
];

yield 'whereIn associative array' => [
['find' => [['id' => ['$in' => [45582, 2, 3]]], []]],
fn (Builder $builder) => $builder->whereIn('id', ['issue' => 45582, 'id' => 2, 3]),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we consider raising an exception if the value is not a list, or is that likely to ruffle some feathers among users? Passing an associative array this way seems incorrect, and MongoDB itself would rightfully complain if a BSON object was provided where an array is expected.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception added. I agree that we need to be more strict than Laravel, with didactic error messages.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where was the exception added? I only error tests for "find with single string argument" and various "IntegerInRaw" cases, neither of which are related to this.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I reverted. I inadvertently made the change in laravel/framework before realising it would be complex to do in laravel-mongodb. If you insist I can try to add this exception in an other PR.

Copy link
Collaborator

@jmikola jmikola Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's revisit this in a separate PR -- feel free to create a new issue to track it.

I'm very much in favor of raising exceptions for poorly formatted values. As annoying as it may be for some users, I think it will ultimately avoid unexpected edge cases and bugs down the line.

@GromNaN GromNaN force-pushed the PHPORM-33-1 branch 3 times, most recently from 437d9cf to 44baa9b Compare July 19, 2023 14:11
['find' => [['foo' => ['$in' => ['bar', 'baz']]], []]],
fn (Builder $builder) => $builder->whereIn('foo', ['bar', 'baz']),
];

yield 'whereIn associative array' => [
['find' => [['id' => ['$in' => [45582, 2, 3]]], []]],
fn (Builder $builder) => $builder->whereIn('id', ['issue' => 45582, 'id' => 2, 3]),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where was the exception added? I only error tests for "find with single string argument" and various "IntegerInRaw" cases, neither of which are related to this.


yield 'offset limit negative' => [
['find' => [[], []]],
fn (Builder $builder) => $builder->offset(-5)->limit(-10),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I missed this in a previous review or if this was recently added, but I'd propose throwing an exception if negative values are passed for offset and limit. That seems preferable to ignoring them outright.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a Laravel feature. I'm not sure we should care about changing this behavior.
https://github.com/laravel/framework/blob/10.x/src/Illuminate/Database/Query/Builder.php#L2435-L2437

Copy link
Collaborator

@jmikola jmikola Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted. We can leave this as-is.


yield 'offset 0 limit 0' => [
['find' => [[], []]],
fn (Builder $builder) => $builder->offset(0)->limit(0),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Offhand, do you know if zero is handled exactly the same as "null (reset)"?

If so, I'd be in favor of the following changes:

  • Rename this test to "offset limit zero (unset)"
  • Create a new test below named "offset limit null (unset)", based on this test. The point of these tests would be to demonstrate that zero and null values both result in the options being unspecified.
  • Create a new test here named "offset limit zero (reset)", based on the "offset limit null (reset)" test below. Like that, it would demonstrate that zero and null both reset the options back to an unset state

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment might have been missed.

I'll defer to you if you'd like to make the suggested test changes. Otherwise, feel free to resolve.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test updated. Thanks for the reminder.

@GromNaN GromNaN force-pushed the PHPORM-33-1 branch 2 times, most recently from 7e0f82d to 43dfe24 Compare July 25, 2023 07:31
@@ -332,7 +332,7 @@ public function testTransaction(): void
$count = User::count();
$this->assertEquals(2, $count);

$this->assertTrue(User::where('alcaeus')->exists());
$this->assertTrue(User::where('name', 'alcaeus')->exists());
Copy link
Owner Author

@GromNaN GromNaN Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being too permissive didn't allow to detect this incorrect query.

- { "alcaeus": null }
+ { "name": "alcaeus" }

The new exception made it surface.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to stop using my name in tests. It makes blaming me for my mistakes too easy 😅

@GromNaN GromNaN requested a review from jmikola July 25, 2023 07:42
Copy link
Collaborator

@alcaeus alcaeus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@@ -923,6 +923,10 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't comment on the if itself, but I'm wondering if the condition should read func_num_args() >= 3 - we wouldn't be removing the $ from an operator if $boolean was defined as well 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me how $boolean is used, but I see it's passed on to the parent method below. I think that's right, and this suggests another lingering bug.

I'll defer to you guys whether to fix here (if so, add a regression test that passes a fourth parameter to where() (I suppose an explicit 'and' string is fine) or report a new JIRA issue and address in a subsequent PR. A new issue probably makes more sense for visibility in the release notes.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #20

@@ -1376,4 +1380,28 @@ public function havingBetween($column, iterable $values, $boolean = 'and', $not
{
throw new \BadMethodCallException('This method is not supported by MongoDB');
}

/** @internal This method is not supported by MongoDB. */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good strategy to mark these as internal to have the IDE notify users already 👍

@@ -332,7 +332,7 @@ public function testTransaction(): void
$count = User::count();
$this->assertEquals(2, $count);

$this->assertTrue(User::where('alcaeus')->exists());
$this->assertTrue(User::where('name', 'alcaeus')->exists());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to stop using my name in tests. It makes blaming me for my mistakes too easy 😅

Copy link
Collaborator

@jmikola jmikola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two follow-up comments.

LGTM to merge as-is if you'd rather not make the test changes. In that case, the issue @alcaeus mentioned in https://github.com/GromNaN/laravel-mongodb/pull/14/files#r1273137525 should be tracked in a separate ticket.

@@ -923,6 +923,10 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me how $boolean is used, but I see it's passed on to the parent method below. I think that's right, and this suggests another lingering bug.

I'll defer to you guys whether to fix here (if so, add a regression test that passes a fourth parameter to where() (I suppose an explicit 'and' string is fine) or report a new JIRA issue and address in a subsequent PR. A new issue probably makes more sense for visibility in the release notes.


yield 'offset 0 limit 0' => [
['find' => [[], []]],
fn (Builder $builder) => $builder->offset(0)->limit(0),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment might have been missed.

I'll defer to you if you'd like to make the suggested test changes. Otherwise, feel free to resolve.

@GromNaN GromNaN merged commit b0b796c into master Jul 26, 2023
@GromNaN GromNaN deleted the PHPORM-33-1 branch July 26, 2023 07:28
GromNaN added a commit that referenced this pull request Aug 22, 2023
- Add tests on query builder methods that don't need to be fixed.
- Throw exception when calling unsupported methods: whereIntegerInRaw, orWhereIntegerInRaw, whereIntegerNotInRaw, orWhereIntegerNotInRaw
- Throw an exception when Query\Builder::where is called with only a column name
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants