PHP code example of nopolabs / mock-with-expectations

1. Go to this page and download the library: Download nopolabs/mock-with-expectations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nopolabs / mock-with-expectations example snippets


public function refundOrder($orderId)
{
    $order = $this->orderRepository->findOrder($orderId);
    if ($order->isRefundable()) {
        $this->orderRefunder->refund($order);
    }
}

public function testRefundOrder()
{
    $orderId = 1337;
    $order = $this->createMock(Order::class);
    $orderRepository = $this->createMock(OrderRepository::class);
    $orderRefunder = $this->createMock(OrderRefunder::class);
    
    $orderRepository->expects($this->once())
        ->method('findOrder')
        ->with($orderId)
        ->willReturn($order);
     
    $order->expects($this->once())
        ->method('isRefundable')
        ->willReturn(true);
        
    $orderRefunder->expects($this->once())
        ->method('refund')
        ->with($order);
        
    $manager = new OrderManager($orderRepository, $orderRefunder);
    
    $manager->refundOrder($orderId);
}

use MockWithExpectationsTrait;

public function testRefundOrder()
{
    $orderId = 1337;

    $order = $this->createMockWithExpectations(Order::class, [
        ['isRefundable', [], true],
    ]);

    $orderRepository = $this->createMockWithExpectations(OrderRepository::class, [
        ['findOrder', [$orderId], $order],
    ]);

    $orderRefunder = $this->createMockWithExpectations(OrderRefunder::class, [
        ['refund', [$order]],
    ]);

    $manager = new OrderManager($orderRepository, $orderRefunder);

    $manager->refundOrder($orderId);
}

public function refundOrder($orderId)
{
    $order = $this->findOrder($orderId);
    if ($this->isRefundable($order)) {
        $this->refund($order);
    }
}

protected function findOrder($orderId) : Order
{
    return $this->orderRepository->findOrder($orderId);
}

protected function isRefundable(Order $order) : bool
{
    return $order->isRefundable();
}

protected function refund(Order $order)
{
    return $this->orderRefunder->refund($order);
}

use MockWithExpectationsTrait;

public function testRefundOrder()
{
    $orderId = 1337;
    $order = $this->createMock(Order::class);
    $manager = $this->createMockWithExpectations(OrderManager::class, [
        ['findOrder', [$orderId], $order],
        ['isRefundable', [$order], true],
        ['refund', [$order]],
    ]);
    $manager->refundOrder($orderId);
}

public function testRefundOrderNotRefundable()
{
    $orderId = 1337;
    $order = $this->createMock(Order::class);
    $manager = $this->createMockWithExpectations(OrderManager::class, [
        ['findOrder', [$orderId], $order],
        ['isRefundable', [$order], false],
        ['refund', 'never'],
    ]);
    $manager->refundOrder($orderId);
}

class OrderManagement
{
    private $orderRepository;
    private $orderRefunder;
    
    public __construct(OrderRepository $orderRepository, OrderRefunder $orderRefunder)
    {
        $this->orderRepository = $orderRepository;
        $this->orderRefunder = $orderRefunder;
    }
    
    public function findOrder($orderId) : Order
    {
        return $this->orderRepository->findOrder($orderId);
    }
    
    public function isRefundable(Order $order) : bool
    {
        return $order->isRefundable();
    }
    
    public function refund(Order $order)
    {
        return $this->orderRefunder->refund($order);
    }
}

class OrderManager
{
    private $orderManagement;
    
    public __construct(OrderManagement $orderManagement)
    {
        $this->orderManagement = $orderManagement;
    }
    
    public function refundOrder($orderId)
    {
        $order = $this->orderManagement->findOrder($orderId);
        if ($this->orderManagement->isRefundable($order)) {
            $this->orderManagement->refund($order);
        }
    }
}

class OrderManagerTest extends TestCase
{
    use MockWithExpectationsTrait;
    
    public function testRefundOrder()
    {
        $orderId = 1337;
        $order = $this->createMock(Order::class);
        $management = $this->createMockWithExpectations(OrderManagement::class, [
            ['findOrder', [$orderId], $order],
            ['isRefundable', [$order], true]
            ['refund', [$order]],
        ]);
        $manager = new OrderManager($management);
        
        $manager->refundOrder($orderId);
    }
}