|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stockbase\Integration\Test\Unit\Cron; |
|
4
|
|
|
|
|
5
|
|
|
use Magento\Framework\ObjectManagerInterface; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use Stockbase\Integration\Cron\Sync; |
|
9
|
|
|
use Stockbase\Integration\Model\Config\StockbaseConfiguration; |
|
10
|
|
|
use Stockbase\Integration\Model\ResourceModel\StockItem; |
|
11
|
|
|
use Stockbase\Integration\StockbaseApi\Client\StockbaseClient; |
|
12
|
|
|
use Stockbase\Integration\StockbaseApi\Client\StockbaseClientFactory; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @author Gabriel Somoza <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class SyncTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */ |
|
20
|
|
|
private $logger; |
|
21
|
|
|
/** @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ |
|
22
|
|
|
private $om; |
|
23
|
|
|
/** @var StockbaseClientFactory|\PHPUnit_Framework_MockObject_MockObject */ |
|
24
|
|
|
private $clientFactory; |
|
25
|
|
|
/** @var StockbaseConfiguration|\PHPUnit_Framework_MockObject_MockObject */ |
|
26
|
|
|
private $config; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* setUp |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
4 |
|
public function setUp() |
|
33
|
|
|
{ |
|
34
|
4 |
|
$this->logger = $this->getMockBuilder(LoggerInterface::class) |
|
35
|
4 |
|
->setMethods(['log', 'warning', 'notice', 'debug', 'info', 'emergency', 'alert', 'critical', 'error']) |
|
36
|
4 |
|
->getMock(); |
|
37
|
|
|
|
|
38
|
4 |
|
$this->om = $this->createMock(ObjectManagerInterface::class); |
|
39
|
4 |
|
$this->config = $this->createMock(StockbaseConfiguration::class); |
|
40
|
|
|
|
|
41
|
4 |
|
$getStockResponseMock = $this->createMock(\stdClass::class); |
|
42
|
4 |
|
$client = $this->createMock(StockbaseClient::class); |
|
43
|
4 |
|
$client->expects($this->any())->method('getStock')->willReturn( |
|
44
|
4 |
|
$getStockResponseMock |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
4 |
|
$this->clientFactory = $this->createMock(StockbaseClientFactory::class); |
|
48
|
4 |
|
$this->clientFactory->expects($this->any())->method('create')->willReturn($client); |
|
49
|
4 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* testExecuteDisabled |
|
53
|
|
|
* @return void |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function testExecuteDisabled() |
|
56
|
|
|
{ |
|
57
|
1 |
|
$this->logger->expects(self::never())->method('info'); |
|
|
|
|
|
|
58
|
1 |
|
$sync = new Sync($this->logger, $this->om, $this->clientFactory, $this->config); |
|
59
|
1 |
|
$sync->execute(); |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* testExecute |
|
64
|
|
|
* @param int $updates Number of stock updates received from API |
|
65
|
|
|
* @return void |
|
66
|
|
|
* @dataProvider executeProvider |
|
67
|
|
|
*/ |
|
68
|
3 |
|
public function testExecuteEnabled(int $updates) |
|
69
|
|
|
{ |
|
70
|
|
|
/** @var StockItem|\PHPUnit_Framework_MockObject_MockObject $stockItemResource */ |
|
71
|
3 |
|
$stockItemResource = $this->createMock(StockItem::class); |
|
72
|
3 |
|
$stockItemResource->expects($this->once()) |
|
|
|
|
|
|
73
|
3 |
|
->method('updateFromStockObject') |
|
74
|
3 |
|
->with(self::isInstanceOf(\stdClass::class)) |
|
75
|
3 |
|
->willReturn($updates); |
|
76
|
|
|
|
|
77
|
3 |
|
$this->config->expects($this->once())->method('isModuleEnabled')->willReturn(true); |
|
|
|
|
|
|
78
|
3 |
|
$this->om->expects($this->once())->method('create') |
|
79
|
3 |
|
->with(StockItem::class) |
|
80
|
3 |
|
->willReturn($stockItemResource); |
|
81
|
|
|
|
|
82
|
3 |
|
$sync = new Sync($this->logger, $this->om, $this->clientFactory, $this->config); |
|
83
|
3 |
|
$sync->execute(); |
|
84
|
3 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* executeProvider |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
public function executeProvider() |
|
91
|
|
|
{ |
|
92
|
|
|
return [ |
|
93
|
|
|
[0], |
|
94
|
|
|
[1], |
|
95
|
|
|
[3], |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: