Completed
Push — master ( ec1dbf...510cdb )
by Gabriel
04:59 queued 51s
created

StockbaseStockManagementTest::createModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace Stockbase\Integration\Test\Unit\Model\Inventory;
5
6
use Magento\Catalog\Model\ProductFactory;
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 ProductFactory|\PHPUnit_Framework_MockObject_MockObject */
31
    private $productFactory;
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->productFactory = $this->getMockBuilder('\Magento\Catalog\Model\ProductFactory')
59 9
            ->setMethods(['create'])
60 9
            ->getMock();
61 9
        $this->productFactory->method('create')->willReturn($this->product);
62
        
63 9
        $this->stockItemResource = $this->createMock(StockItemResource::class);
64
        
65 9
        $this->objectManager = $this->createMock(ObjectManagerInterface::class);
66 9
    }
67
68
    /**
69
     * testGetStockbaseStockAmount
70
     */
71 1
    public function testGetStockbaseStockAmount()
72
    {
73 1
        $this->configureStockbaseEan(101, '12345');
74
75 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...
76 1
            ->with('12345')
77 1
            ->willReturn(5.0);
78
        
79 1
        $model = $this->createModel();
80 1
        $result = $model->getStockbaseStockAmount(101);
81
        
82 1
        $this->assertEquals(5.0, $result);
83 1
    }
84
85
    /**
86
     * testGetStockbaseEan
87
     */
88 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...
89
    {
90 1
        $this->configureStockbaseEan(101, '12345');
91
        
92 1
        $model = $this->createModel();
93 1
        $result = $model->getStockbaseEan(101);
94
        
95 1
        $this->assertEquals('12345', $result);
96 1
    }
97
98
    /**
99
     * testIsStockbaseProduct
100
     */
101 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...
102
    {
103 1
        $this->configureStockbaseEan(101, '12345');
104
        
105 1
        $model = $this->createModel();
106 1
        $result = $model->isStockbaseProduct(101);
107
        
108 1
        $this->assertEquals(true, $result);
109 1
    }
110
111
    /**
112
     * testUpdateStockAmount
113
     * @dataProvider updateStockAmountProvider
114
     *
115
     * @param mixed $ean
116
     * @param mixed $amount
117
     * @param mixed $operation
118
     */
119 2
    public function testUpdateStockAmount($ean, $amount, $operation)
120
    {
121 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...
122 2
            ->with($ean, $amount, $operation);
123
        
124 2
        $model = $this->createModel();
125 2
        $model->updateStockAmount($ean, $amount, $operation);
126 2
    }
127
128
    /**
129
     * @return array
130
     */
131
    public function updateStockAmountProvider()
132
    {
133
        return [
134
            ['101', 5, '+'],
135
            ['102', 7, '-'],
136
        ];
137
    }
138
139
    /**
140
     * testCreateReserve
141
     */
142 1
    public function testCreateReserve()
143
    {
144 1
        $this->configureStockbaseEan(101, '12345');
145
146
147 1
        $quoteItem = $this->getMockBuilder(QuoteItem::class)
148 1
            ->disableOriginalConstructor()
149 1
            ->setMethods(['getProductId', 'getId'])
150 1
            ->getMock();
151 1
        $quoteItem->method('getProductId')->willReturn(101);
152 1
        $quoteItem->method('getId')->willReturn(201);
153
154 1
        $stockItemReserve = $this->createMock(StockItemReserve::class);
155
        
156 1
        $this->objectManager->expects($this->once())->method('create')->with(StockItemReserve::class)
157 1
            ->willReturn($stockItemReserve);
158
159 1
        $stockItemReserve->expects($this->at(0))->method('setData')->willReturnCallback(function ($data) {
160 1
            $this->assertArrayHasKey('ean', $data);
161 1
            $this->assertArrayHasKey('amount', $data);
162 1
            $this->assertArrayHasKey('magento_stock_amount', $data);
163 1
            $this->assertArrayHasKey('quote_item_id', $data);
164 1
            $this->assertArrayHasKey('product_id', $data);
165 1
            $this->assertArrayHasKey('created_at', $data);
166
            
167 1
            $this->assertEquals('12345', $data['ean']);
168 1
            $this->assertEquals(5, $data['amount']);
169 1
            $this->assertEquals(7, $data['magento_stock_amount']);
170 1
            $this->assertEquals(201, $data['quote_item_id']);
171 1
            $this->assertEquals(101, $data['product_id']);
172 1
            $this->assertRegExp('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $data['created_at']);
173 1
        });
174 1
        $stockItemReserve->expects($this->at(1))->method('save');
175
        
176 1
        $model = $this->createModel();
177 1
        $result = $model->createReserve($quoteItem, 5, 7);
178
        
179 1
        $this->assertEquals($stockItemReserve, $result);
180 1
    }
181
182
    /**
183
     * testReleaseReserve
184
     */
185 1
    public function testReleaseReserve()
186
    {
187 1
        $reserve = $this->createMock(StockItemReserve::class);
188 1
        $reserve->expects($this->once())->method('delete');
189
        
190 1
        $model = $this->createModel();
191 1
        $model->releaseReserve($reserve);
192 1
    }
193
194
    /**
195
     * testGetReserveForProduct
196
     */
197 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...
198
    {
199 1
        $reserveCollection = $this->createMock(StockItemReserveCollection::class);
200
        
201 1
        $this->objectManager->expects($this->once())->method('create')
202 1
            ->with(StockItemReserveCollection::class)
203 1
            ->willReturn($reserveCollection);
204
205 1
        $reserve = $this->createMock(StockItemReserve::class);
206
        
207 1
        $reserveCollection->expects($this->any())->method('addFieldToFilter')
208 1
            ->with('product_id', ['in' => [101]]);
209
        
210 1
        $reserveCollection->expects($this->once())->method('getItems')
211 1
            ->willReturn([$reserve]);
212
        
213 1
        $model = $this->createModel();
214 1
        $result = $model->getReserveForProduct(101);
215
        
216 1
        $this->assertEquals([$reserve], $result);
217 1
    }
218
219
    /**
220
     * testGetReserveForQuoteItem
221
     */
222 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...
223
    {
224 1
        $reserveCollection = $this->createMock(StockItemReserveCollection::class);
225
        
226 1
        $this->objectManager->expects($this->once())->method('create')
227 1
            ->with(StockItemReserveCollection::class)
228 1
            ->willReturn($reserveCollection);
229
230 1
        $reserve = $this->createMock(StockItemReserve::class);
231
        
232 1
        $reserveCollection->expects($this->any())->method('addFieldToFilter')
233 1
            ->with('quote_item_id', ['in' => [101]]);
234
        
235 1
        $reserveCollection->expects($this->once())->method('getItems')
236 1
            ->willReturn([$reserve]);
237
        
238 1
        $model = $this->createModel();
239 1
        $result = $model->getReserveForQuoteItem(101);
240
            
241 1
        $this->assertEquals([$reserve], $result);
242 1
    }
243
    
244 9
    protected function createModel()
245
    {
246 9
        return new StockbaseStockManagement(
247 9
            $this->stockRegistry,
248 9
            $this->config,
249 9
            $this->productFactory,
250 9
            $this->stockItemResource,
251 9
            $this->objectManager
252
        );
253
    }
254
    
255 4
    protected function configureStockbaseEan($productId, $ean)
256
    {
257 4
        $this->stockRegistry->method('getStockItem')->with($productId)->willReturn($this->stockItem);
258
259 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...
260 4
        $this->config->expects($this->once())->method('getEanFieldName')->willReturn('ean');
261 4
        $this->stockItem->expects($this->once())->method('getManageStock')->willReturn(true);
262 4
        $this->stockItem->expects($this->once())->method('getBackorders')
263 4
            ->willReturn(\Magento\CatalogInventory\Model\Stock::BACKORDERS_NO);
264
265 4
        $this->product->expects($this->once())->method('load')->with($productId);
266 4
        $this->product->expects(new MethodInvokedAtIndex(0))->method('getData')->with('ean')->willReturn($ean);
267 4
        $this->product->expects(new MethodInvokedAtIndex(1))->method('getData')->with('stockbase_product')
268 4
            ->willReturn(true);
269 4
    }
270
}
271