D
D
Dark_Dante2021-11-25 09:19:46
PHP
Dark_Dante, 2021-11-25 09:19:46

Why is only one Mock object created?

The situation is this. For testing, you need several Mock objects of the same class.
We create a method:

protected function createBagMock(
        ?delivery $delivery = null,
        ?string $barcode = null,
        ?int $weight = null,
        array $products =[]
    ): Bag {
        $bag = $this->getMockBuilder(Bag::class)
            ->setConstructorArgs([
                'pickUpDelivery' => is_null($delivery) ? $this->dliveryMock() : $delivery,
                'barcode' => is_null($barcode) ? TestValues::TEST_BARCODE : $barcode,
                'weight' => is_null($weight) ? 1000 : $weight,
                'products' => empty($products) ? [] : $products,
            ])->getMock();

        return $bag;
    }


We call:
dump($this->createBagMock()); //тут Mock_Bag_edb40126
dump($this->createBagMock()); //и тут Mock_Bag_edb40126
dump($this->createBagMock()); //и тут тоже Mock_Bag_edb40126


In all cases, the same object was returned to us, and I would have three different ones. It seems that on the first call, the object was created, and on subsequent calls, the previously created mock-object is returned. How can I get multiple mock objects of the same class?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question