Passed
Branch real-time-api (954386)
by James
06:12
created

AbstractServiceTest.php$0 ➔ getDomain()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: james
5
 * Date: 20/07/2018
6
 * Time: 20:24
7
 */
8
9
namespace CwsOps\LivePerson\Tests;
10
11
use CwsOps\LivePerson\Account\Config;
12
use CwsOps\LivePerson\Services\AbstractService;
13
use CwsOps\LivePerson\Services\RequestNotSentException;
14
use PHPUnit\Framework\MockObject\MockObject;
15
use PHPUnit\Framework\TestCase;
16
17
/**
18
 * Class AbstractServiceTest
19
 *
20
 * @package CwsOps\LivePerson\Tests
21
 */
22
class AbstractServiceTest extends TestCase
23
{
24
25
    /** @var MockObject|AbstractService */
26
    private $mock;
27
    /** @var Config */
28
    private $config;
29
30
    public function setUp()
31
    {
32
        $accountId = 'foo';
33
        $consumerKey = 'bar';
34
        $consumerSecret = 'biz';
35
        $token = 'baz';
36
        $tokenSecret = 'bee';
37
        $username = 'noo';
38
        $this->config = new Config($accountId, $consumerKey, $consumerSecret, $token, $tokenSecret, $username);
39
40
        $this->mock = $this->getMockBuilder(AbstractService::class)
41
            ->disableOriginalConstructor()
42
            ->setMethods(['getStatus', 'getResponse'])
43
            ->getMockForAbstractClass();
44
45
    }
46
47
    /**
48
     * @covers \CwsOps\LivePerson\Services\AbstractService::__construct
49
     */
50
    public function testCanInitWithOptions()
51
    {
52
        $mock = $this->getMockBuilder(AbstractService::class)
53
            ->setConstructorArgs([$this->config, 5, null])
54
            ->getMockForAbstractClass();
55
56
        $this->assertInstanceOf(AbstractService::class, $mock);
57
    }
58
59
    /**
60
     * @covers \CwsOps\LivePerson\Services\AbstractService::__construct
61
     */
62
    public function testWillThrowInvalidArgumentOnInvalidRetryLimit()
63
    {
64
        $this->expectException(\InvalidArgumentException::class);
65
        $this->expectExceptionMessage('Maximum $retryLimit is 5 you tried setting 10, try setting a value between 0 and 5');
66
67
        new class($this->config, 10) extends AbstractService
68
        {
69
            protected function getDomain(): string
70
            {
71
                return 'foo';
72
            }
73
        };
74
    }
75
76
    /**
77
     * @covers \CwsOps\LivePerson\Services\AbstractService::getStatus()
78
     */
79
    public function testGetStatus()
80
    {
81
        $status = new \stdClass();
82
        $status->live = true;
83
84
        $this->mock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CwsOps\LivePerson\Services\AbstractService. ( Ignorable by Annotation )

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

84
        $this->mock->/** @scrutinizer ignore-call */ 
85
                     expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
            ->method('getStatus')
86
            ->willReturn($status);
87
88
        $result = $this->mock->getStatus();
0 ignored issues
show
Bug introduced by
The method getStatus() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

88
        /** @scrutinizer ignore-call */ 
89
        $result = $this->mock->getStatus();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
90
        $this->assertInstanceOf(\stdClass::class, $result);
91
        $this->assertTrue($status->live);
92
    }
93
94
    /**
95
     * @covers \CwsOps\LivePerson\Services\AbstractService::getResponse()
96
     *
97
     * @throws RequestNotSentException
98
     */
99
    public function testGetResponse()
100
    {
101
        $response = new \stdClass();
102
        $response->data = ['foo' => 'bar'];
103
        $response->bool = false;
104
105
        $this->mock->expects($this->once())
106
            ->method('getResponse')
107
            ->willReturn($response);
108
109
        $result = $this->mock->getResponse();
0 ignored issues
show
Bug introduced by
The method getResponse() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

109
        /** @scrutinizer ignore-call */ 
110
        $result = $this->mock->getResponse();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
111
        $this->assertInstanceOf(\stdClass::class, $result);
112
        $this->assertFalse($result->bool);
113
        $this->assertArrayHasKey('foo', $result->data);
114
115
    }
116
117
    /**
118
     *
119
     * @covers \CwsOps\LivePerson\Services\AbstractService::getResponse()
120
     */
121
    public function testWillThrowNotBuiltOnNoRequest()
122
    {
123
        /** @var AbstractService|MockObject $mock */
124
        $mock = $this->getMockBuilder(AbstractService::class)
125
            ->disableOriginalConstructor()
126
            ->getMockForAbstractClass();
127
128
        $this->expectException(RequestNotSentException::class);
129
130
        $mock->getResponse();
131
    }
132
133
    public function tearDown()
134
    {
135
        $this->mock = null;
136
    }
137
138
139
}
140