StockbaseStockManagementTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 1
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
4
namespace Stockbase\Integration\Test\Unit\Model\Inventory;
5
6
use Magento\Catalog\Api\ProductRepositoryInterface;
7
use Magento\CatalogInventory\Api\StockRegistryInterface;
8
use Magento\Framework\ObjectManagerInterface;
9
use Magento\Framework\TestFramework\Unit\Matcher\MethodInvokedAtIndex;
10
use Magento\Quote\Model\Quote\Item as QuoteItem;
11
use PHPUnit\Framework\TestCase;
12
use Stockbase\Integration\Model\Config\StockbaseConfiguration;
13
use Stockbase\Integration\Model\Inventory\StockbaseStockManagement;
14
use Stockbase\Integration\Model\ResourceModel\StockItem as StockItemResource;
15
use Stockbase\Integration\Model\ResourceModel\StockItemReserve\Collection as StockItemReserveCollection;
16
use Stockbase\Integration\Model\StockItemReserve;
17
18
/**
19
 * Class StockbaseStockManagementTest
20
 */
21
class StockbaseStockManagementTest extends TestCase
22
{
23
24
    /** @var StockRegistryInterface|\PHPUnit_Framework_MockObject_MockObject */
25
    private $stockRegistry;
26
27
    /** @var StockbaseConfiguration|\PHPUnit_Framework_MockObject_MockObject */
28
    private $config;
29
30
    /** @var ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
31
    private $productRepository;
32
33
    /** @var StockItemResource|\PHPUnit_Framework_MockObject_MockObject */
34
    private $stockItemResource;
35
36
    /** @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
37
    private $objectManager;
38
39
    /** @var \Magento\CatalogInventory\Model\Stock\Item|\PHPUnit_Framework_MockObject_MockObject */
40
    private $stockItem;
41
42
    /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */
43
    private $product;
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 9
    public function setUp()
49
    {
50 9
        $this->stockItem = $this->createMock(\Magento\CatalogInventory\Model\Stock\Item::class);
51
        
52 9
        $this->stockRegistry = $this->createMock(StockRegistryInterface::class);
53
        
54 9
        $this->config = $this->createMock(StockbaseConfiguration::class);
55
        
56 9
        $this->product = $this->createMock(\Magento\Catalog\Model\Product::class);
57
        
58 9
        $this->productRepository = $this->createMock(ProductRepositoryInterface::class);
59
        
60 9
        $this->stockItemResource = $this->createMock(StockItemResource::class);
61
        
62 9
        $this->objectManager = $this->createMock(ObjectManagerInterface::class);
63 9
    }
64
65
    /**
66
     * testGetStockbaseStockAmount
67
     */
68 1
    public function testGetStockbaseStockAmount()
69
    {
70 1
        $this->configureStockbaseEan(101, '12345');
71
72 1
        $this->stockItemResource->expects($this->once())->method('getNotReservedStockAmount')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Stockbase\Integration\Mo...ResourceModel\StockItem.

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...
73 1
            ->with('12345')
74 1
            ->willReturn(5.0);
75
        
76 1
        $model = $this->createModel();
77 1
        $result = $model->getStockbaseStockAmount(101);
78
        
79 1
        $this->assertEquals(5.0, $result);
80 1
    }
81
82
    /**
83
     * testGetStockbaseEan
84
     */
85 1 View Code Duplication
    public function testGetStockbaseEan()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87 1
        $this->configureStockbaseEan(101, '12345');
88
        
89 1
        $model = $this->createModel();
90 1
        $result = $model->getStockbaseEan(101);
91
        
92 1
        $this->assertEquals('12345', $result);
93 1
    }
94
95
    /**
96
     * testIsStockbaseProduct
97
     */
98 1 View Code Duplication
    public function testIsStockbaseProduct()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100 1
        $this->configureStockbaseEan(101, '12345');
101
        
102 1
        $model = $this->createModel();
103 1
        $result = $model->isStockbaseProduct(101);
104
        
105 1
        $this->assertEquals(true, $result);
106 1
    }
107
108
    /**
109
     * testUpdateStockAmount
110
     * @dataProvider updateStockAmountProvider
111
     *
112
     * @param mixed $ean
113
     * @param mixed $amount
114
     * @param mixed $operation
115
     */
116 2
    public function testUpdateStockAmount($ean, $amount, $operation)
117
    {
118 2
        $this->stockItemResource->expects($this->once())->method('updateStockAmount')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Stockbase\Integration\Mo...ResourceModel\StockItem.

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...
119 2
            ->with($ean, $amount, $operation);
120
        
121 2
        $model = $this->createModel();
122 2
        $model->updateStockAmount($ean, $amount, $operation);
123 2
    }
124
125
    /**
126
     * @return array
127
     */
128
    public function updateStockAmountProvider()
129
    {
130
        return [
131
            ['101', 5, '+'],
132
            ['102', 7, '-'],
133
        ];
134
    }
135
136
    /**
137
     * testCreateReserve
138
     */
139 1
    public function testCreateReserve()
140
    {
141 1
        $this->configureStockbaseEan(101, '12345');
142
143
144 1
        $quoteItem = $this->getMockBuilder(QuoteItem::class)
145 1
            ->disableOriginalConstructor()
146 1
            ->setMethods(['getProductId', 'getId'])
147 1
            ->getMock();
148 1
        $quoteItem->method('getProductId')->willReturn(101);
149 1
        $quoteItem->method('getId')->willReturn(201);
150
151 1
        $stockItemReserve = $this->createMock(StockItemReserve::class);
152
        
153 1
        $this->objectManager->expects($this->once())->method('create')->with(StockItemReserve::class)
154 1
            ->willReturn($stockItemReserve);
155
156 1
        $stockItemReserve->expects($this->at(0))->method('setData')->willReturnCallback(function ($data) {
157 1
            $this->assertArrayHasKey('ean', $data);
158 1
            $this->assertArrayHasKey('amount', $data);
159 1
            $this->assertArrayHasKey('magento_stock_amount', $data);
160 1
            $this->assertArrayHasKey('quote_item_id', $data);
161 1
            $this->assertArrayHasKey('product_id', $data);
162 1
            $this->assertArrayHasKey('created_at', $data);
163
            
164 1
            $this->assertEquals('12345', $data['ean']);
165 1
            $this->assertEquals(5, $data['amount']);
166 1
            $this->assertEquals(7, $data['magento_stock_amount']);
167 1
            $this->assertEquals(201, $data['quote_item_id']);
168 1
            $this->assertEquals(101, $data['product_id']);
169 1
            $this->assertRegExp('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $data['created_at']);
170 1
        });
171 1
        $stockItemReserve->expects($this->at(1))->method('save');
172
        
173 1
        $model = $this->createModel();
174 1
        $result = $model->createReserve($quoteItem, 5, 7);
175
        
176 1
        $this->assertEquals($stockItemReserve, $result);
177 1
    }
178
179
    /**
180
     * testReleaseReserve
181
     */
182 1
    public function testReleaseReserve()
183
    {
184 1
        $reserve = $this->createMock(StockItemReserve::class);
185 1
        $reserve->expects($this->once())->method('delete');
186
        
187 1
        $model = $this->createModel();
188 1
        $model->releaseReserve($reserve);
189 1
    }
190
191
    /**
192
     * testGetReserveForProduct
193
     */
194 1 View Code Duplication
    public function testGetReserveForProduct()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
195
    {
196 1
        $reserveCollection = $this->createMock(StockItemReserveCollection::class);
197
        
198 1
        $this->objectManager->expects($this->once())->method('create')
199 1
            ->with(StockItemReserveCollection::class)
200 1
            ->willReturn($reserveCollection);
201
202 1
        $reserve = $this->createMock(StockItemReserve::class);
203
        
204 1
        $reserveCollection->expects($this->any())->method('addFieldToFilter')
205 1
            ->with('product_id', ['in' => [101]]);
206
        
207 1
        $reserveCollection->expects($this->once())->method('getItems')
208 1
            ->willReturn([$reserve]);
209
        
210 1
        $model = $this->createModel();
211 1
        $result = $model->getReserveForProduct(101);
212
        
213 1
        $this->assertEquals([$reserve], $result);
214 1
    }
215
216
    /**
217
     * testGetReserveForQuoteItem
218
     */
219 1 View Code Duplication
    public function testGetReserveForQuoteItem()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
220
    {
221 1
        $reserveCollection = $this->createMock(StockItemReserveCollection::class);
222
        
223 1
        $this->objectManager->expects($this->once())->method('create')
224 1
            ->with(StockItemReserveCollection::class)
225 1
            ->willReturn($reserveCollection);
226
227 1
        $reserve = $this->createMock(StockItemReserve::class);
228
        
229 1
        $reserveCollection->expects($this->any())->method('addFieldToFilter')
230 1
            ->with('quote_item_id', ['in' => [101]]);
231
        
232 1
        $reserveCollection->expects($this->once())->method('getItems')
233 1
            ->willReturn([$reserve]);
234
        
235 1
        $model = $this->createModel();
236 1
        $result = $model->getReserveForQuoteItem(101);
237
            
238 1
        $this->assertEquals([$reserve], $result);
239 1
    }
240
    
241 9
    protected function createModel()
242
    {
243 9
        return new StockbaseStockManagement(
244 9
            $this->stockRegistry,
245 9
            $this->config,
246 9
            $this->productRepository,
247 9
            $this->stockItemResource,
248 9
            $this->objectManager
249
        );
250
    }
251
    
252 4
    protected function configureStockbaseEan($productId, $ean)
253
    {
254 4
        $this->stockRegistry->method('getStockItem')->with($productId)->willReturn($this->stockItem);
255
256 4
        $this->config->expects($this->once())->method('isModuleEnabled')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Stockbase\Integration\Mo...\StockbaseConfiguration.

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...
257 4
        $this->config->expects($this->once())->method('getEanFieldName')->willReturn('ean');
258 4
        $this->stockItem->expects($this->once())->method('getManageStock')->willReturn(true);
259 4
        $this->stockItem->expects($this->once())->method('getBackorders')
260 4
            ->willReturn(\Magento\CatalogInventory\Model\Stock::BACKORDERS_NO);
261
262 4
        $this->productRepository->expects($this->once())->method('getById')->with($productId)
263 4
            ->willReturn($this->product);
264
        
265 4
        $this->product->expects(new MethodInvokedAtIndex(0))->method('getData')->with('ean')->willReturn($ean);
266 4
        $this->product->expects(new MethodInvokedAtIndex(1))->method('getData')->with('stockbase_product')
267 4
            ->willReturn(true);
268 4
    }
269
}
270