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

$stmt->rowCount doesn't use actual results #34

Open
JTimNolan opened this issue Jul 16, 2020 · 2 comments
Open

$stmt->rowCount doesn't use actual results #34

JTimNolan opened this issue Jul 16, 2020 · 2 comments

Comments

@JTimNolan
Copy link

JTimNolan commented Jul 16, 2020

When creating a mocked select result from an array, the rowCount stays at 0. e.g.

$p->mock($query, [['id' => 1],['id' => 2],['id' => 3]]);
$stmt = $p->query($query);
echo $stmt->rowCount(); // Outputs 0, expected 3

Assuming I'm not just doing something wrong, I'd be happy to submit a PR for this, though it'd be helpful to be pointed in the right direction 😃

@jimbojsb
Copy link
Owner

jimbojsb commented Jul 16, 2020

Currently, the row count is "dumb". Assuming you're mocking an insert or update here, it does not know how to translate that into a sane affected row count. I'm not entirely sure it could or should, as there'd really be now way to guess what a database engine would answer back. If you want a value returned there, you have to set it using a setter on the result object:

https://github.com/jimbojsb/pseudo/blob/master/src/Pseudo/Result.php#L157

Would be open to a PR to improve this, just not quite sure what it'd look like.

@JTimNolan
Copy link
Author

To clarify, this is a select statement. I'd agree for an insert or update statement, you'd likely need to keep it "dumb". For select statements though, it'd be nice if it just returned the count of the rows in the mock results. Currently to make this happen it's fairly verbose:

$query = "SELECT * FROM test";
$rows = [['id' => 1],['id' => 2],['id' => 3]];
$result = new Result($rows);
$result->setAffectedRowCount(count($rows));
$p->mock($query, $result);

For a fix, I'd think it'd need to be something along the lines of "if affectedRowCount has been manually set, return that, else return count($rows)", to avoid breaking existing usages. The part I could use some guidance on is on is how to make that work with parameterized queries, as I'm just getting started with this library and I'm not 100% clear on how they work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants