SyncTest::executeProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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');
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Psr\Log\LoggerInterface.

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...
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())
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 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);
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...
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