Closed Divyeshhhh closed 5 months ago
When you face an issue and you need help, you should summarize what you have tried, what you investigated, and the results you had after implementing any possible solutions.
here are the steps that I took to identify the issue:
"test": "phpunit tests --filter OrderTest"
Unable to save store to database. Attributes seem to be ok as per validate().
was being generated, this implies that the return value of $this->dummy_store->save()
is false.save()
function in the Store model. There are only two ways for this function to return false: either (!$this->validate())
is true or ($stm->rowCount() === 0)
is true.throw new Exception("error here");
!$this->validate()
was evaluating to True. $this->validate()
is an array, !$this->validate()
always evaluates to true. The correct statement should have been:
if (!empty($this->validate())) {
return false;
}
This issue was already fixed in another commit: b6a12c483bb89e80718134e7b55bdb41c357bc43.
Here are the errors that i am getting :
[!IMPORTANT] When you face an issue and you need help, you should summarize what you have tried, what you investigated, and the results you had after implementing any possible solutions.
Here are the steps I took (and that you should have taken) to solve the invalid line item
exception in OrderProductTest:
You should first understand the exception and where it is coming from:
1) OrderProductTest::testValidate
Exception: Invalid line item:{}
The exception is generated inside of setUp
because testValidate
does not throw any exception.
The next step is to identify the line of code in setUp
that is generating the exception. By inspecting the Order model, you will see that this particular exception can only be generated in either addLineItem()
or save()
. In the setUp
function, here are the possible culprits:
// Add line items to the order
foreach ($this->line_items as $line_item) {
$this->dummy_order->addLineItem($line_item); <---------
}
$success = $this->dummy_order->save(); <---------
if (!$success) {
throw new Exception('Unable to save order');
}
If you comment the second block of code (everything including and after save()), you will get a single error (different from the first error):
1) OrderProductTest::testGetByID
Failed asserting that null is not null.
This means that the true culprit is $success = $this->dummy_order->save();
Here is the piece of code inside the save()
method that generates the exception:
if (!$line_item->validate()) {
// line item contains invalid attributes
$conn->rollBack();
$conn = null;
throw new Exception("Invalid line item:" . json_encode($line_item));
}
validate()
returns an array of errors and returns an empty array when there are no errorrs. Therefore !$line_item->validate()
always returns true. The correct statement should have been !empty($line_item->validate())
. I pushed a commit on your branch to fix this.
The new exceptions are now:
There were 2 errors:
1) OrderProductTest::testValidate
Exception: Store with ID 78 has insufficient stock for product 151
/var/www/html/steamy-sips/src/models/Order.php:148
/var/www/html/steamy-sips/tests/OrderProductTest.php:102
2) OrderProductTest::testGetByID
Exception: Store with ID 79 has insufficient stock for product 152
These are issues with your tests.
Run without errors :
Thank you for the helps.
Checklist
Confirm you have completed the following actions prior to submitting this PR.
composer test
does not throw any error.Issue Resolution
Tell us which issue this PR fixes.
This pull request Fixes #182
Additional Information
Can you help me to dubug it?
I am getting these errors :