Php laravel test not loading relationships

380 views Asked by At

I've got the following method in my controller:

public function assignCustomer(string $orderID) : JsonResponse
    {
        $customersID = \request()->get('customers_id');

        try {
            $order = AddCustomerToOrderAction::execute($orderID, $customersID);
            Log::info("Order $orderID successfully updated with customer $customersID");
            return (new OrdersResource($order->loadMissing('orderItems', 'orderItems.product', 'customer')))
                ->response()
                ->setStatusCode(ResponseAlias::HTTP_OK);
        } catch (ModelNotFoundException) {
            return response()->json(['message' => "Record Not Found. Check that either Customer's ID or Order ID are valid"], ResponseAlias::HTTP_UNPROCESSABLE_ENTITY);


        } catch (Exception $e) {
            Log::error("failed to add customer $customersID to Order $orderID : $e");
            return response()->json(['message' => "An error has happened. Please try again or contact IT service for help"], ResponseAlias::HTTP_INTERNAL_SERVER_ERROR);
        }
    }

where AddCustomerToOrderActions is:

class AddCustomerToOrderAction
{
    public static function execute($orderID, $customersID)
    {
        $order = null;

        // validate customer
        $customer = ViewCustomer::findOrFail($customersID);

        if ($customer) {
            $order = Order::find($orderID);
            $order->customers_id = $customer->id;
            $order->save();
        }

        return $order;
    }
}

If I perform a call to POST http://127.0.0.1:8000/api/orders/982007c0-78e8-4f2a-bfcc-8297c3731dbb/customers with a body such as:

{
    "customers_id": "982007a0-420b-41c0-b3e3-c07ad1901b7f"
}

I'm getting back:

{
    "data": {
        "id": "982007c0-78e8-4f2a-bfcc-8297c3731dbb",
        "type": "orders",
        "attributes": {
            "status": "paid",
            "payment_type": "cash",
            "payment_transaction_no": "",
            "customer": {
                "id": "982007a0-420b-41c0-b3e3-c07ad1901b7f",
                "type": "customers",
                "attributes": {
                    "customer_type": "individuals",
                    "identification": "9832342123",
                    "identification_type": "cc",
                    "name": "SEBASTIAN  BONILLA ",
                    "primary_phone_number": "6464698219",
                    "created_at": "2023-01-02T04:22:23.000000Z"
                }
            },
            "items": [
                {
                    "id": 1,
                    "order_id": "982007c0-78e8-4f2a-bfcc-8297c3731dbb",
                    "product": {
                        "id": "98200742-2994-429b-8915-5591716ea249",
                        "type": "products",
                        "attributes": {
                            "barcode": "3234423423909",
                            "name": "Test 1",
                            "slug": "test-1",
                            "sku": null,
                            "description": null,
                            "type": "goods",
                            "wholesale_price": null,
                            "retail_price": 20000,
                            "base_picture": null,
                            "current_stock_level": 0,
                            "active": 1
                        }
                    },
                    "qty": 2,
                    "price": 20000,
                    "total": 40000,
                    "created_at": "2023-01-02T04:22:44.000000Z",
                    "updated_at": "2023-01-02T04:22:44.000000Z"
                },
                {
                    "id": 2,
                    "order_id": "982007c0-78e8-4f2a-bfcc-8297c3731dbb",
                    "product": {
                        "id": "98200770-9d13-4e9b-ab36-56a66f632324",
                        "type": "products",
                        "attributes": {
                            "barcode": "3234423423121",
                            "name": "Test 2",
                            "slug": "test-2",
                            "sku": null,
                            "description": null,
                            "type": "goods",
                            "wholesale_price": null,
                            "retail_price": 15500,
                            "base_picture": null,
                            "current_stock_level": 0,
                            "active": 1
                        }
                    },
                    "qty": 1,
                    "price": 15500,
                    "total": 15500,
                    "created_at": "2023-01-02T04:22:54.000000Z",
                    "updated_at": "2023-01-02T04:22:54.000000Z"
                }
            ],
            "subtotal": 55500,
            "taxes": 0,
            "total": 55500,
            "created_at": "2023-01-02T04:22:44.000000Z",
            "updated_at": "2023-01-02T04:23:09.000000Z"
        }
    }
}

Notice how customer is a nested object that contains the customer's info.

Now, i've got the following test:

it('should return customer\'s information nested in the response when a customer is assigned to an order', function () {

    // create a customer
    $individual = Individual::factory()->create();

    // create an order with a customer
    $order = createOrderWithProducts(3);

    $customerData = ['customers_id' => $individual->id];

    $url = ORDERS_URI . "/{$order->id}/customers";

    $response = $this->postJson($url, $customerData);

    dd($response->json());
})->only();

When I run it I'm getting:

"data" => array:3 [
    "id" => "712a4b1c-3fd2-393c-9f4d-bf0fa52149c8"
    "type" => "orders"
    "attributes" => array:10 [
      "status" => "new"
      "payment_type" => ""
      "payment_transaction_no" => ""
      "customer" => null
      "items" => array:3 [
...

(omitted the rest of the response for simplicity). Notice how customers is empty.

I've added multiple debug statements and everything seems ok, until I added the following one:

$order = AddCustomerToOrderAction::execute($orderID, $customersID);
            Log::info("Order $orderID successfully updated with customer $customersID");
            $tmp = new OrdersResource($order->loadMissing('orderItems', 'orderItems.product', 'customer'));
            dd($tmp);

When I run a call from the rest client and from the test itself the results are consistent except for this piece:

Call from rest client:

#relations: array:2 [▼
      "orderItems" => Illuminate\Database\Eloquent\Collection {#1353 ▶}
      "customer" => Domain\Customer\Models\ViewCustomer {#1373 ▶}
    ]

Call from test:

#relations: array:1 [
              "product" => Domain\Product\Models\Product^
]

Notice how the loaded relationships are empty.

I checked multiple times and yes, the customer is being created in the test. Also, if I print the order before passing it to the resource, it does contain the customers_id, it is when passed to the resource that it is not loading the relationship.

What am i missing?

thanks

0

There are 0 answers