class PaymentGateway { public function processPayment($amount) { // process payment and return status } } $gateway = new PaymentGateway(); $spy = Mockery::spy($gateway); // invoke method on the spy $spy->processPayment(100); // check if method was called $spy->shouldHaveReceived('processPayment')->once();
class ShoppingCart { private $items = []; public function addItem($item) { $this->items[] = $item; } public function getItemCount() { return count($this->items); } } $cart = new ShoppingCart(); $spy = Mockery::spy($cart); // invoke method on the spy $spy->addItem('product 1'); $spy->addItem('product 2'); // check if method was called and with what arguments $spy->shouldHaveReceived('addItem')->twice()->with(Mockery::type('string')); // check for property modification $spy->__toString()->contains('items');In this example, we create a shopping cart object and then create a spy object using `Mockery::spy()`. We then invoke the `addItem()` method twice on the spy and use `shouldHaveReceived()` to check if the `addItem()` method was called twice with arguments of type string. We also use the spy object to verify that the `items` property of the shopping cart object was modified by checking the `__toString()` method output. Mockery is a PHP package library used for creating test doubles such as mocks, stubs, and spies.