Completed
Push — master ( fac20c...f0346e )
by Gabriel
12s queued 10s
created

StockbaseClientTest::testCreateOrder()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 66

Duplication

Lines 7
Ratio 10.61 %

Code Coverage

Tests 49
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 7
loc 66
ccs 49
cts 49
cp 1
crap 2
rs 8.7418
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
4
namespace Stockbase\Integration\Test\Unit\StockbaseApi\Client;
5
6
use DivideBV\PHPDivideIQ\DivideIQ;
7
use Magento\Sales\Api\Data\OrderInterface;
8
use PHPUnit\Framework\TestCase;
9
use Stockbase\Integration\Model\Config\StockbaseConfiguration;
10
use Stockbase\Integration\StockbaseApi\Client\StockbaseClient;
11
use Stockbase\Integration\StockbaseApi\Client\StockbaseClientException;
12
13
/**
14
 * Class StockbaseClientTest
15
 */
16
class StockbaseClientTest extends TestCase
17
{
18
    /** @var DivideIQ|\PHPUnit_Framework_MockObject_MockObject */
19
    private $divideIqClient;
20
21
    /** @var StockbaseConfiguration|\PHPUnit_Framework_MockObject_MockObject */
22
    private $stockbaseConfiguration;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 4
    public function setUp()
28
    {
29 4
        $this->divideIqClient = $this->createMock(DivideIQ::class);
30
        
31 4
        $this->stockbaseConfiguration = $this->createMock(StockbaseConfiguration::class);
32 4
    }
33
34
    /**
35
     * testGetStock
36
     */
37 1
    public function testGetStock()
38
    {
39
        $expectedData = [
40 1
            'Since' => 1497797012,
41
            'Until' => 1497797015,
42
        ];
43 1
        $this->divideIqClient->expects($this->once())->method('request')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in DivideBV\PHPDivideIQ\DivideIQ.

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...
44 1
            ->with(StockbaseClient::STOCKBASE_STOCK_ENDPOINT, $expectedData, 'GET')
45 1
            ->willReturn('TEST_ANSWER');
46
47 1
        $client = new StockbaseClient($this->divideIqClient, $this->stockbaseConfiguration);
48 1
        $this->assertEquals('TEST_ANSWER', $client->getStock(new \DateTime('@1497797012'), new \DateTime('@1497797015')));
49 1
    }
50
51
    /**
52
     * testGetImages
53
     */
54 1
    public function testGetImages()
55
    {
56
        $expectedData = [
57 1
            'ean' => '101,102,103,104,105',
58
        ];
59
        
60 1
        $this->divideIqClient->expects($this->once())->method('request')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in DivideBV\PHPDivideIQ\DivideIQ.

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...
61 1
            ->with(StockbaseClient::STOCKBASE_IMAGES_ENDPOINT, $expectedData, 'GET')
62 1
            ->willReturn('TEST_ANSWER');
63
64 1
        $client = new StockbaseClient($this->divideIqClient, $this->stockbaseConfiguration);
65 1
        $this->assertEquals('TEST_ANSWER', $client->getImages([101, 102, 103, 104, 105]));
66 1
    }
67
68
    /**
69
     * testCreateOrder
70
     */
71 1
    public function testCreateOrder()
72
    {
73 1
        $this->stockbaseConfiguration
74 1
            ->method('getOrderPrefix')
75 1
            ->willReturn('TEST_ORDER_PREFIX');
76
77 1
        $order = $this->createOrderMock();
78
        
79 1
        $reservedItems = [];
80 1 View Code Duplication
        for ($i = 0; $i < 2; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
81 1
            $reservedItem = $this->createMock(\Stockbase\Integration\Model\StockItemReserve::class);
82 1
            $reservedItem->method('getEan')->willReturn($i*100+1);
83 1
            $reservedItem->method('getAmount')->willReturn($i*100+2);
84
85 1
            $reservedItems[$i] = $reservedItem;
86
        }
87
        
88 1
        $this->divideIqClient->expects($this->once())->method('request')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in DivideBV\PHPDivideIQ\DivideIQ.

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...
89 1
            ->with(
90 1
                StockbaseClient::STOCKBASE_ORDER_REQUEST_ENDPOINT,
91 1
                $this->anything(),
92 1
                'POST'
93
            )
94 1
            ->willReturnCallback(function ($serviceName, $payload = [], $method = 'GET') use ($order, $reservedItems) {
0 ignored issues
show
Unused Code introduced by
The parameter $method 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...
95
                
96 1
                $this->assertTrue(isset($payload['OrderHeader']['OrderNumber']));
97 1
                $this->assertEquals('TEST_ORDER_PREFIX#'.$order->getRealOrderId(), $payload['OrderHeader']['OrderNumber']);
98 1
                $this->assertTrue(isset($payload['OrderHeader']['TimeStamp']));
99 1
                $this->assertRegExp('/^\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}$/', $payload['OrderHeader']['TimeStamp']);
100 1
                $this->assertTrue(isset($payload['OrderHeader']['Attention']));
101 1
                $this->assertEquals($order->getCustomerNote(), $payload['OrderHeader']['Attention']);
102
103 1
                $this->assertTrue(isset($payload['OrderLines']));
104 1
                $this->assertCount(count($reservedItems), $payload['OrderLines']);
105
106 1
                $this->assertTrue(isset($payload['OrderDelivery']['Person']['FirstName']));
107 1
                $this->assertEquals($order->getShippingAddress()->getFirstname(), $payload['OrderDelivery']['Person']['FirstName']);
108 1
                $this->assertTrue(isset($payload['OrderDelivery']['Person']['Surname']));
109 1
                $this->assertEquals($order->getShippingAddress()->getLastname(), $payload['OrderDelivery']['Person']['Surname']);
110 1
                $this->assertTrue(isset($payload['OrderDelivery']['Person']['Company']));
111 1
                $this->assertEquals($order->getShippingAddress()->getCompany(), $payload['OrderDelivery']['Person']['Company']);
112
                
113 1
                $this->assertTrue(isset($payload['OrderDelivery']['Address']['Street']));
114 1
                $this->assertEquals($order->getShippingAddress()->getStreetLine(1), $payload['OrderDelivery']['Address']['Street']);
115 1
                $this->assertTrue(isset($payload['OrderDelivery']['Address']['StreetNumber']));
116 1
                $this->assertEquals($order->getShippingAddress()->getStreetLine(2), $payload['OrderDelivery']['Address']['StreetNumber']);
117 1
                $this->assertTrue(isset($payload['OrderDelivery']['Address']['ZipCode']));
118 1
                $this->assertEquals($order->getShippingAddress()->getPostcode(), $payload['OrderDelivery']['Address']['ZipCode']);
119 1
                $this->assertTrue(isset($payload['OrderDelivery']['Address']['City']));
120 1
                $this->assertEquals($order->getShippingAddress()->getCity(), $payload['OrderDelivery']['Address']['City']);
121 1
                $this->assertTrue(isset($payload['OrderDelivery']['Address']['CountryCode']));
122 1
                $this->assertEquals($order->getShippingAddress()->getCountryId(), $payload['OrderDelivery']['Address']['CountryCode']);
123
                
124
125 1
                $response = new \stdClass();
126 1
                $response->{'StatusCode'} = 1;
127
                
128 1
                return $response;
129 1
            });
130
        
131 1
        $client = new StockbaseClient($this->divideIqClient, $this->stockbaseConfiguration);
132
        
133 1
        $result = $client->createOrder($order, $reservedItems);
134
        
135 1
        $this->assertEquals(1, $result->{'StatusCode'});
136 1
    }
137
138
    /**
139
     * testCreateOrderApiFail
140
     */
141 1
    public function testCreateOrderApiFail()
142
    {
143 1
        $this->stockbaseConfiguration
144 1
            ->method('getOrderPrefix')
145 1
            ->willReturn('TEST_ORDER_PREFIX');
146
147 1
        $order = $this->createOrderMock();
148
149 1
        $reservedItems = [];
150 1 View Code Duplication
        for ($i = 0; $i < 2; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
151 1
            $reservedItem = $this->createMock(\Stockbase\Integration\Model\StockItemReserve::class);
152 1
            $reservedItem->method('getEan')->willReturn($i*100+1);
153 1
            $reservedItem->method('getAmount')->willReturn($i*100+2);
154
155 1
            $reservedItems[$i] = $reservedItem;
156
        }
157
158 1
        $this->divideIqClient->expects($this->once())->method('request')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in DivideBV\PHPDivideIQ\DivideIQ.

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...
159 1
            ->with(
160 1
                StockbaseClient::STOCKBASE_ORDER_REQUEST_ENDPOINT,
161 1
                $this->anything(),
162 1
                'POST'
163
            )
164 1
            ->willReturnCallback(function () {
165 1
                $item = new \stdClass();
166 1
                $item->{'StatusCode'} = 2;
167 1
                $item->{'ExceptionMessage'} = 'Test exception.';
168 1
                $response = new \stdClass();
169 1
                $response->{'StatusCode'} = 2;
170 1
                $response->{'Items'} = [$item];
171
                
172 1
                return $response;
173 1
            });
174
175 1
        $client = new StockbaseClient($this->divideIqClient, $this->stockbaseConfiguration);
176
177 1
        $this->expectException(StockbaseClientException::class);
178 1
        $this->expectExceptionMessageRegExp('/Test exception\./');
179
        
180 1
        $client->createOrder($order, $reservedItems);
181
    }
182
    
183 2
    protected function createOrderMock()
184
    {
185 2
        $shippingAddress = $this->createMock(\Magento\Sales\Model\Order\Address::class);
186 2
        $shippingAddress->method('getFirstname')->willReturn('TEST_FIRST_NAME');
187 2
        $shippingAddress->method('getLastname')->willReturn('TEST_LAST_NAME');
188 2
        $shippingAddress->method('getCompany')->willReturn('TEST_COMPANY');
189 2
        $shippingAddress->method('getStreetLine')->willReturnCallback(function ($line) {
190 2
            return sprintf('STREET_LINE_%d', $line);
191 2
        });
192 2
        $shippingAddress->method('getPostcode')->willReturn('TEST_ZIP');
193 2
        $shippingAddress->method('getCity')->willReturn('TEST_CITY');
194 2
        $shippingAddress->method('getCountryId')->willReturn('US');
195
196 2
        $order = $this->createMock(\Magento\Sales\Model\Order::class);
197 2
        $order->method('getShippingAddress')->willReturn($shippingAddress);
198
199 2
        $order->method('getRealOrderId')->willReturn(123456);
200 2
        $order->method('getCustomerNote')->willReturn('TEST_CUSTOMER_NOTE');
201
        
202 2
        return $order;
203
    }
204
}
205