Completed
Push — 1.7 ( 1627e8...895c05 )
by Kamil
73:45 queued 48:32
created

OrderItemsSubtotalCalculatorTest::getOrderMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\ShopBundle\Tests\Calculator;
15
16
use Mockery;
17
use Mockery\Adapter\Phpunit\MockeryTestCase;
18
use Sylius\Bundle\ShopBundle\Calculator\OrderItemsSubtotalCalculator;
19
use Sylius\Bundle\ShopBundle\Calculator\OrderItemsSubtotalCalculatorInterface;
20
use Sylius\Component\Core\Model\OrderInterface;
21
use Sylius\Component\Core\Model\OrderItemInterface;
22
23
final class OrderItemsSubtotalCalculatorTest extends MockeryTestCase
24
{
25
    /**
26
     * @test
27
     */
28
    public function it_can_be_instantiated(): OrderItemsSubtotalCalculator
29
    {
30
        $calculator = new OrderItemsSubtotalCalculator();
31
        $this->assertInstanceOf(OrderItemsSubtotalCalculatorInterface::class, $calculator);
32
33
        return $calculator;
34
    }
35
36
    /**
37
     * @test
38
     * @depends it_can_be_instantiated
39
     * @param OrderItemsSubtotalCalculator $calculator
40
     */
41
    public function it_can_calculate_the_subtotal_of_order_items(OrderItemsSubtotalCalculator $calculator): void
42
    {
43
        $subTotal = $calculator->getSubtotal($this->getOrderMock([
44
            $this->getOrderItemMock(1000),
45
            $this->getOrderItemMock(1000)
46
        ]));
47
        $this->assertEquals(2000, $subTotal);
48
    }
49
50
    /**
51
     * @test
52
     * @depends it_can_be_instantiated
53
     * @param OrderItemsSubtotalCalculator $calculator
54
     */
55
    public function it_can_calculate_a_subtotal_if_there_are_no_order_items(
56
        OrderItemsSubtotalCalculator $calculator
57
    ): void {
58
        $subTotal = $calculator->getSubtotal($this->getOrderMock([]));
59
        $this->assertEquals(0, $subTotal);
60
    }
61
62
    private function getOrderItemMock(int $subTotal): OrderItemInterface
63
    {
64
        $orderItem = Mockery::mock(OrderItemInterface::class);
65
        $orderItem
66
            ->shouldReceive('getSubTotal')
67
            ->once()
68
            ->andReturn($subTotal)
69
        ;
70
71
        return $orderItem;
72
    }
73
74
    private function getOrderMock(array $orderItems): OrderInterface
75
    {
76
        $order = Mockery::mock(OrderInterface::class);
77
        $order
78
            ->shouldReceive('getItems->toArray')
79
            ->once()
80
            ->andReturn($orderItems);
81
82
        return $order;
83
    }
84
}
85