Passed
Pull Request — master (#59)
by Raúl
04:04
created

CreateOrderMethodTest::testGetOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.7998
1
<?php
2
3
namespace Test\Pagantis\OrdersApiClient\Method;
4
5
use Faker\Factory;
6
use Httpful\Http;
7
use Httpful\Request;
8
use Pagantis\OrdersApiClient\Exception\ClientException;
9
use Pagantis\OrdersApiClient\Method\CreateOrderMethod;
10
use Pagantis\OrdersApiClient\Model\ApiConfiguration;
11
use Pagantis\OrdersApiClient\Model\Order;
12
use Test\Pagantis\OrdersApiClient\AbstractTest;
13
14
/**
15
 * Class CreateOrderMethodTest
16
 *
17
 * @package Test\Pagantis\OrdersApiClient\Method;
18
 */
19
class CreateOrderMethodTest extends AbstractTest
20
{
21
    /**
22
     * testEndpointConstant
23
     */
24
    public function testEndpointConstant()
25
    {
26
        $constant = CreateOrderMethod::ENDPOINT;
27
        $this->assertEquals('/orders', $constant);
28
    }
29
30
    /**
31
     * testSetOrderId
32
     *
33
     * @throws \ReflectionException
34
     */
35
    public function testSetOrderId()
36
    {
37
        $order = new Order();
38
        $apiConfigurationMock = $this->getMock('Pagantis\OrdersApiClient\Model\ApiConfiguration');
39
        $createOrderMethod = new CreateOrderMethod($apiConfigurationMock);
40
        $createOrderMethod->setOrder($order);
41
        $reflectCreateOrderMethod = new \ReflectionClass('Pagantis\OrdersApiClient\Method\CreateOrderMethod');
42
        $property = $reflectCreateOrderMethod->getProperty('order');
43
        $property->setAccessible(true);
44
        $this->assertSame($order, $property->getValue($createOrderMethod));
45
    }
46
47
    /**
48
     * testGetOrder
49
     *
50
     * @throws \ReflectionException
51
     * @throws \Exception
52
     */
53
    public function testGetOrder()
54
    {
55
        $orderJson = file_get_contents($this->resourcePath.'Order.json');
56
        $responseMock = $this->getMockBuilder('Httpful\Response')->disableOriginalConstructor()->getMock();
57
        $responseMockReflect = new \ReflectionClass('Httpful\Response');
58
        $property = $responseMockReflect->getProperty('body');
59
        $property->setAccessible(true);
60
        $property->setValue($responseMock, json_decode($orderJson));
61
62
        $apiConfigurationMock = $this->getMock('Pagantis\OrdersApiClient\Model\ApiConfiguration');
63
        $createOrderMethod = new CreateOrderMethod($apiConfigurationMock);
64
        $this->assertFalse($createOrderMethod->getOrder());
0 ignored issues
show
Bug introduced by
$createOrderMethod->getOrder() of type Pagantis\OrdersApiClient\Model\Order is incompatible with the type boolean expected by parameter $condition of PHPUnit_Framework_Assert::assertFalse(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        $this->assertFalse(/** @scrutinizer ignore-type */ $createOrderMethod->getOrder());
Loading history...
65
        $reflectCreateOrderMethod = new \ReflectionClass('Pagantis\OrdersApiClient\Method\CreateOrderMethod');
66
        $property = $reflectCreateOrderMethod->getProperty('response');
67
        $property->setAccessible(true);
68
        $property->setValue($createOrderMethod, $responseMock);
69
70
        $this->assertInstanceOf('Pagantis\OrdersApiClient\Model\Order', $createOrderMethod->getOrder());
71
    }
72
73
    /**
74
     * testPrepareRequest
75
     *
76
     * @throws \ReflectionException
77
     * @throws ClientException
78
     */
79
    public function testPrepareRequest()
80
    {
81
        $faker = Factory::create();
82
        $url = $faker->url;
83
        $order = $this->getMock('Pagantis\OrdersApiClient\Model\Order');
84
        $apiConfiguration = new ApiConfiguration();
85
        $apiConfiguration->setBaseUri($url);
86
        $createOrderMethod = new CreateOrderMethod($apiConfiguration);
87
        $reflectCreateOrderMethod = new \ReflectionClass('Pagantis\OrdersApiClient\Method\CreateOrderMethod');
88
        $method = $reflectCreateOrderMethod->getMethod('prepareRequest');
89
        $method->setAccessible(true);
90
        $property = $reflectCreateOrderMethod->getProperty('request');
91
        $property->setAccessible(true);
92
        $this->assertNull($property->getValue($createOrderMethod));
93
        $createOrderMethod->setOrder($order);
94
        $method->invoke($createOrderMethod);
95
        /** @var Request $request */
96
        $request = $property->getValue($createOrderMethod);
97
        $this->assertInstanceOf('Httpful\Request', $request);
98
        $this->assertSame(Http::POST, $request->method);
99
        $uri =
100
            $url .
101
            CreateOrderMethod::ENDPOINT
102
        ;
103
        $this->assertSame($uri, $request->uri);
104
    }
105
106
    /**
107
     * testCall
108
     *
109
     * @throws \Httpful\Exception\ConnectionErrorException
110
     * @throws \Pagantis\OrdersApiClient\Exception\HttpException
111
     */
112
    public function testCall()
113
    {
114
        $apiConfigurationMock = $this->getMock('Pagantis\OrdersApiClient\Model\ApiConfiguration');
115
        $createOrderMethod = new CreateOrderMethod($apiConfigurationMock);
116
        try {
117
            $createOrderMethod->call();
118
            $this->assertTrue(false);
119
        } catch (ClientException $exception) {
120
            $this->assertInstanceOf('Pagantis\OrdersApiClient\Exception\ClientException', $exception);
121
        }
122
    }
123
}
124