StockItemTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 250
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.52%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 250
ccs 99
cts 107
cp 0.9252
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 17 1
A testGetNotReservedStockAmount() 0 34 1
A getNotReservedStockAmountProvider() 0 7 1
A testUpdateStockAmount() 0 15 1
A updateStockAmountProvider() 0 9 1
A testGetLastModifiedItemDate() 0 23 1
A getLastModifiedItemDateProvider() 0 17 1
A testUpdateFromStockObject() 0 42 1
A testUpdateFromStockObjectEmpty() 0 12 1
A createResourceModel() 0 4 1
1
<?php
2
3
4
namespace Stockbase\Integration\Test\Unit\Model\ResourceModel;
5
6
use PHPUnit\Framework\TestCase;
7
use Stockbase\Integration\Model\ResourceModel\StockItem;
8
9
/**
10
 * Class StockItemTest
11
 */
12
class StockItemTest extends TestCase
13
{
14
    const TEST_PREFIX = 'TEST_';
15
    
16
    /** @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject */
17
    private $resources;
18
19
    /** @var \Magento\Framework\Model\ResourceModel\Db\Context|\PHPUnit_Framework_MockObject_MockObject */
20
    private $context;
21
22
    /** @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject */
23
    private $connection;
24
25
    /** @var \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject */
26
    private $select;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 11
    public function setUp()
32
    {
33 11
        $this->select = $this->createMock(\Magento\Framework\DB\Select::class);
34
        
35 11
        $this->connection = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
36 11
        $this->connection->method('select')->willReturn($this->select);
37
        
38 11
        $this->resources = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
39 11
        $this->resources->method('getConnection')->willReturn($this->connection);
40 11
        $this->resources->method('getTableName')
41 11
            ->willReturnCallback(function ($table, $connectionName) {
0 ignored issues
show
Unused Code introduced by
The parameter $connectionName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42 10
                return self::TEST_PREFIX.$table;
43 11
            });
44
        
45 11
        $this->context = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
46 11
        $this->context->method('getResources')->willReturn($this->resources);
47 11
    }
48
49
    /**
50
     * testGetNotReservedStockAmount
51
     * @dataProvider getNotReservedStockAmountProvider
52
     *
53
     * @param mixed $request
54
     * @param array $data
55
     * @param mixed $expectedResult
56
     */
57 2
    public function testGetNotReservedStockAmount($request, array $data, $expectedResult)
58
    {
59 2
        $this->select->expects($this->once())->method('from')
60 2
            ->with(
61 2
                ['s' => self::TEST_PREFIX.'stockbase_stock'],
62 2
                ['s.ean', 'amount' => new \Zend_Db_Expr('IF(s.noos = 1, 1000000000, s.amount - COALESCE(SUM(r.amount), 0))')]
63
            )
64 2
            ->willReturnSelf();
65
        
66 2
        $this->select->expects($this->once())->method('joinLeft')
67 2
            ->with(
68 2
                ['r' => self::TEST_PREFIX.'stockbase_stock_reserve'],
69 2
                'r.ean = s.ean',
70 2
                null
71
            )
72 2
            ->willReturnSelf();
73
        
74 2
        $this->select->expects($this->once())->method('where')
75 2
            ->with('s.ean in (?)', $request)
76 2
            ->willReturnSelf();
77
        
78 2
        $this->select->expects($this->once())->method('group')
79 2
            ->with('s.ean')
80 2
            ->willReturnSelf();
81
        
82 2
        $this->connection->expects($this->once())->method('fetchPairs')
83 2
            ->with($this->select)
84 2
            ->willReturn($data);
85
        
86 2
        $resourceModel = $this->createResourceModel();
87
        
88 2
        $result = $resourceModel->getNotReservedStockAmount($request);
89 2
        $this->assertEquals($expectedResult, $result);
90 2
    }
91
92
    /**
93
     * @return array
94
     */
95
    public function getNotReservedStockAmountProvider()
96
    {
97
        return [
98
            [111, [111 => 2], 2],
99
            [[101, 102], [101 => 5, 102 => 0], [101 => 5, 102 => 0]],
100
        ];
101
    }
102
103
    /**
104
     * testUpdateStockAmount
105
     * @dataProvider updateStockAmountProvider
106
     *
107
     * @param mixed $ean
108
     * @param mixed $amount
109
     * @param mixed $operation
110
     * @param mixed $amountExpr
111
     * @param mixed $numRows
112
     * @param mixed $expectedResult
113
     */
114 4
    public function testUpdateStockAmount($ean, $amount, $operation, $amountExpr, $numRows, $expectedResult)
115
    {
116 4
        $this->connection->expects($this->once())->method('update')
117 4
            ->with(
118 4
                self::TEST_PREFIX.'stockbase_stock',
119 4
                ['amount' => new \Zend_Db_Expr($amountExpr)],
120 4
                ['ean = ?' => $ean]
121
            )
122 4
            ->willReturn($numRows);
123
        
124 4
        $resourceModel = $this->createResourceModel();
125
126 4
        $result = $resourceModel->updateStockAmount($ean, $amount, $operation);
127 4
        $this->assertEquals($expectedResult, $result);
128 4
    }
129
130
    /**
131
     * @return array
132
     */
133
    public function updateStockAmountProvider()
134
    {
135
        return [
136
            [101, 5, '+', 'amount + 5', 1, true],
137
            [102, 2, '-', 'amount - 2', 1, true],
138
            [103, 2, null, 'amount + 2', 1, true],
139
            [104, '2.5', '-', 'amount - 2.5', 0, false],
140
        ];
141
    }
142
143
    /**
144
     * testGetLastModifiedItemDate
145
     * @dataProvider getLastModifiedItemDateProvider
146
     *
147
     * @param mixed $data
148
     * @param mixed $expectedResult
149
     */
150 3
    public function testGetLastModifiedItemDate($data, $expectedResult)
151
    {
152 3
        $this->select->expects($this->once())->method('from')
153 3
            ->with(self::TEST_PREFIX.'stockbase_stock', 'timestamp')
154 3
            ->willReturnSelf();
155
        
156 3
        $this->select->expects($this->once())->method('order')
157 3
            ->with('timestamp DESC')
158 3
            ->willReturnSelf();
159
        
160 3
        $this->select->expects($this->once())->method('limit')
161 3
            ->with(1)
162 3
            ->willReturnSelf();
163
164 3
        $this->connection->expects($this->once())->method('fetchCol')
165 3
            ->with($this->select)
166 3
            ->willReturn($data);
167
168 3
        $resourceModel = $this->createResourceModel();
169
170 3
        $result = $resourceModel->getLastModifiedItemDate();
171 3
        $this->assertEquals($expectedResult, $result);
172 3
    }
173
174
    /**
175
     * @return array
176
     */
177
    public function getLastModifiedItemDateProvider()
178
    {
179
        return [
180
            [
181
                [],
182
                null,
183
            ],
184
            [
185
                ['2001-02-03'],
186
                new \DateTime('2001-02-03'),
187
            ],
188
            [
189
                ['2001-02-03', '2001-03-04'],
190
                new \DateTime('2001-02-03'),
191
            ],
192
        ];
193
    }
194
195
    /**
196
     * testUpdateFromStockObject
197
     */
198 1
    public function testUpdateFromStockObject()
199
    {
200 1
        $item = new \stdClass();
201 1
        $item->{'EAN'} = '12345';
202 1
        $item->{'Amount'} = 5;
203 1
        $item->{'NOOS'} = false;
204 1
        $item->{'Timestamp'} = 1497797012;
205
        
206 1
        $group = new \stdClass();
207 1
        $group->{'Brand'} = 'TEST_BRAND';
208 1
        $group->{'Code'} = 'TEST_CODE';
209 1
        $group->{'SupplierCode'} = 'TEST_SUPPLIER_CODE';
210 1
        $group->{'SupplierGLN'} = 'TEST_SUPPLIER_GLN';
211 1
        $group->{'Items'} = [$item];
212
        
213 1
        $data = new \stdClass();
214 1
        $data->{'Groups'} = [$group];
215
        
216
        $expectedData = [
217
            [
218 1
                'ean' => '12345',
219
                'brand' => 'TEST_BRAND',
220
                'code' => 'TEST_CODE',
221
                'supplier_code' => 'TEST_SUPPLIER_CODE',
222
                'supplier_gln' => 'TEST_SUPPLIER_GLN',
223
                'amount' => 5,
224
                'noos' => false,
225
                'timestamp' => '2017-06-18 16:43:32',
226
            ],
227
        ];
228
        
229 1
        $this->connection->expects($this->exactly(1))->method('beginTransaction');
230
        
231 1
        $this->connection->expects($this->exactly(1))->method('insertOnDuplicate')
232 1
            ->with(self::TEST_PREFIX.'stockbase_stock', $expectedData);
233 1
        $this->connection->expects($this->exactly(1))->method('commit');
234
235 1
        $resourceModel = $this->createResourceModel();
236
237 1
        $result = $resourceModel->updateFromStockObject($data);
238 1
        $this->assertEquals(1, $result);
239 1
    }
240
241
    /**
242
     * testUpdateFromStockObjectEmpty
243
     */
244 1
    public function testUpdateFromStockObjectEmpty()
245
    {
246 1
        $data = new \stdClass();
247 1
        $data->{'Groups'} = [];
248
249 1
        $this->connection->expects($this->never())->method('insertOnDuplicate');
250
251 1
        $resourceModel = $this->createResourceModel();
252
253 1
        $result = $resourceModel->updateFromStockObject($data);
254 1
        $this->assertEquals(0, $result);
255 1
    }
256
    
257 11
    protected function createResourceModel()
258
    {
259 11
        return new StockItem($this->context);
260
    }
261
}
262