CancelOrderItemObserverTest::createHandler()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Stockbase\Integration\Test\Unit\Model\Observer;
5
6
use Magento\Catalog\Model\Indexer\Product\Price\Processor as ProductPriceProcessor;
7
use Magento\CatalogInventory\Api\StockManagementInterface;
8
use Magento\Store\Api\Data\StoreInterface;
9
use PHPUnit\Framework\TestCase;
10
use Stockbase\Integration\Model\Inventory\StockbaseStockManagement;
11
use Stockbase\Integration\Model\Observer\CancelOrderItemObserver;
12
use Stockbase\Integration\Model\StockItemReserve;
13
14
/**
15
 * Class CancelOrderItemObserverTest
16
 */
17
class CancelOrderItemObserverTest extends TestCase
18
{
19
    /** @var StockManagementInterface|\PHPUnit_Framework_MockObject_MockObject */
20
    private $stockManagement;
21
22
    /** @var ProductPriceProcessor|\PHPUnit_Framework_MockObject_MockObject */
23
    private $priceIndexer;
24
25
    /** @var StockbaseStockManagement|\PHPUnit_Framework_MockObject_MockObject */
26
    private $stockbaseStockManagement;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 3
    public function setUp()
32
    {
33 3
        $this->stockManagement = $this->createMock(StockManagementInterface::class);
34 3
        $this->priceIndexer = $this->createMock(ProductPriceProcessor::class);
35 3
        $this->stockbaseStockManagement = $this->createMock(StockbaseStockManagement::class);
36 3
    }
37
38
    /**
39
     * testExecute
40
     * @dataProvider itemProvider
41
     *
42
     * @param mixed $qty
43
     * @param mixed $stockbaseQty
44
     * @param mixed $magentoQty
45
     */
46 3
    public function testExecute($qty, $stockbaseQty, $magentoQty)
47
    {
48 3
        $store = $this->createMock(StoreInterface::class);
49 3
        $store->method('getWebsiteId')->willReturn(1);
50
        
51 3
        $item = $this->createMock(\Magento\Sales\Model\Order\Item::class);
52 3
        $item->method('getQtyOrdered')->willReturn($qty);
53 3
        $item->method('getQtyShipped')->willReturn(0);
54 3
        $item->method('getQtyInvoiced')->willReturn(0);
55 3
        $item->method('getQtyCanceled')->willReturn(0);
56 3
        $item->method('getId')->willReturn(101);
57 3
        $item->method('getProductId')->willReturn(201);
58 3
        $item->method('getQuoteItemId')->willReturn(301);
59 3
        $item->method('getStore')->willReturn($store);
60
        
61 3
        $observer = new \Magento\Framework\Event\Observer([
62 3
            'event' => new \Magento\Framework\Event([
63 3
                'item' => $item,
64
            ]),
65
        ]);
66
        
67 3
        $reserve = $this->createMock(StockItemReserve::class);
68 3
        $reserve->method('getAmount')->willReturn($stockbaseQty);
69 3
        $reserve->method('getMagentoStockAmount')->willReturn($magentoQty);
70
71 3
        $this->stockbaseStockManagement->method('getReserveForQuoteItem')->with(301)->willReturn([
72 3
            301 => $reserve,
73
        ]);
74
75 3
        $this->stockbaseStockManagement->expects($this->once())->method('releaseReserve')->with($reserve);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Stockbase\Integration\Mo...tockbaseStockManagement.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
76 3
        $this->stockManagement->expects($this->once())->method('backItemQty')->with(201, $magentoQty, 1);
77 3
        $this->priceIndexer->expects($this->once())->method('reindexRow')->with(201);
78
        
79 3
        $handler = $this->createHandler();
80 3
        $handler->execute($observer);
81 3
    }
82
83
    /**
84
     * @return array
85
     */
86
    public function itemProvider()
87
    {
88
        return [
89
            [5, 3, 2],
90
            [10, 0, 10],
91
            [7, 7, 0],
92
        ];
93
    }
94
    
95 3
    protected function createHandler()
96
    {
97 3
        return new CancelOrderItemObserver(
98 3
            $this->stockManagement,
99 3
            $this->priceIndexer,
100 3
            $this->stockbaseStockManagement
101
        );
102
    }
103
}
104