PHP/Laravel: Error: Call to a member function __call() on null

I’m currently working on a Laravel project and I’m facing an issue with setting up a factory for a pivot table. Specifically, I’m trying to create a factory for a table that serves as a pivot between Order and Product models. The table is named order_product and includes fields such as order_id, product_id, quantity, and price.

The test looks like this:

public function testCalculateOrderTotal(): void
    {
        $address = Address::factory()->create();
        $client = Client::factory()->create(['address_id' => $address->id]);
        $order = Order::factory()->create(['client_id' => $client->id]);
        $product1 = Product::factory()->create();
        $product2 = Product::factory()->create();

        $orderProductData1 = [
            'order_id' => $order->id,
            'product_id' => $product1->id,
            'quantity' => 2,
            'price' => 100.00
        ];
        $orderProductData2 = [
            'order_id' => $order->id,
            'product_id' => $product2->id,
            'quantity' => 1,
            'price' => 150.00
        ];

        $orderProduct1 = OrderProduct::factory()->create($orderProductData1);
        $orderProduct2 = OrderProduct::factory()->create($orderProductData2);

        $this->orderProductRepository
            ->shouldReceive('findByOrder')
            ->with($order->id)
            ->once()
            ->andReturn(new Collection([$orderProduct1, $orderProduct2]));

         $expectedTotal = ($orderProductData1['quantity'] * $orderProductData1['price']) +
             ($orderProductData2['quantity'] * $orderProductData2['price']);

         $total = $this->orderProductService->calculateOrderTotal($order->id);

         $this->assertIsFloat($total);
         $this->assertEquals($expectedTotal, $total);
    }

and this is the service method which I’m trying to test

    public function calculateOrderTotal($order_id): float
    {
        try {
            $orderProducts = $this->orderProductRepository->findByOrder($order_id);
            return $orderProducts->sum(fn($item) => $item->quantity * $item->price);
        } catch (Throwable $th) {
            throw $th;
        }
    }

and when I run all the tests (./vendor/bin/sail exec -T laravel.test ./vendor/bin/phpunit --coverage-html=coverage/) it keeps returning:

 FAILED  TestsUnitServicesOrderProductServiceTest > calculate order total                                                                                                                             Error   
  Call to a member function __call() on null

  at tests/Unit/Services/OrderProductServiceTest.php:56
  ➜  56▕ $orderProduct1 = OrderProduct::factory()->create($orderProductData1);
     57▕ $orderProduct2 = OrderProduct::factory()->create($orderProductData2);
     58▕ 

1 tests/Unit/Services/OrderProductServiceTest.php:56

My OrderProductFactory looks like this:

public function definition(): array
{
return [
    'order_id' => Order::factory(),
    'product_id' => Product::factory(),
    'quantity' => $this->faker->numberBetween(1, 10),
    'price' => $this->faker->randomFloat(2, 5, 100),
];
}

I’ve tried to update my OrderProductFactory but that didn’t work.

Any help or guidance would be greatly appreciated!

Thanks in advance!

8

This does not look hard. But before helping you resolve this problem, I got a few questions of my own:

  1. How do you instantiate that $orderProductService variable? via the service container or just new up the object?

  2. How do you instantiate that $orderProductRepository inside the OrderProductService class? Is that a dependency like this?

class OrderProductService
{
    protected $orderProductRepository;

    public function __construct(OrderProductRepository $orderProductRepository)
    {
        $this->orderProductRepository = $orderProductRepository;
    }
}
  1. I see you are mocking the OrderProductRepository class. Reading [the docs][1], it has to be mocked this way:
$this->mock(OrderProductRepository::class, function ($mock) use ($order) {
    $mock->shouldReceive('findByOrder')
        ->with($order->id)
        ->once()
        ->andReturn(new Collection([$orderProduct1, $orderProduct2]));
});

Still, I find mocking in this context unnecessary. That findByOrder method is simple and should return the correct results anyway. You can just remove this line.

Let us know, so we can help you resolve.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật