steamysips / steamy-sips

A full-stack LAMP e-commerce coffee shop.
https://steamysips.github.io/steamy-sips/
Creative Commons Attribution Share Alike 4.0 International
0 stars 0 forks source link

tests for Order and OrderProduct models #191

Closed Divyeshhhh closed 5 months ago

Divyeshhhh commented 5 months ago

Checklist

Confirm you have completed the following actions prior to submitting this PR.

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 :

image image

creme332 commented 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:

  1. There were errors coming from 2 different files (OrderTest and OrderProductTest).
  2. I want to debug only one file at a time so I modified the script in composer.json to consider only errors from OrderTest: "test": "phpunit tests --filter OrderTest"
  3. I checked if all the right attributes are passed to the constructor of Store in the setUp function of OrderTest. The attributes were correct.
  4. Since an exception 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.
  5. I went to the definition of the 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.
  6. Inside the first if statement (where validate() is called), I decided to throw an exception instead of returning false: throw new Exception("error here");
  7. If I run the tests again, I get the exception message "error here" which means that !$this->validate() was evaluating to True.
  8. Since the return value of $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.

Divyeshhhh commented 5 months ago

Here are the errors that i am getting :

image

creme332 commented 5 months ago

[!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.

Divyeshhhh commented 5 months ago

Run without errors :

image

Thank you for the helps.