Issues (14)

tests/Services/AbstractServiceTest.php (3 issues)

Labels
Severity
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 GuzzleHttp\Client;
15
use PHPUnit\Framework\MockObject\MockObject;
16
use PHPUnit\Framework\TestCase;
17
18
/**
19
 * Class AbstractServiceTest
20
 *
21
 * @package CwsOps\LivePerson\Tests
22
 */
23
class AbstractServiceTest extends TestCase
24
{
25
26
    /** @var MockObject|AbstractService */
27
    private $mock;
28
    /** @var Config */
29
    private $config;
30
    /** @var Client */
31
    private $client;
32
33
    public function setUp()
34
    {
35
        $accountId = 'foo';
36
        $consumerKey = 'bar';
37
        $consumerSecret = 'biz';
38
        $token = 'baz';
39
        $tokenSecret = 'bee';
40
        $username = 'noo';
41
        $this->config = new Config($accountId, $consumerKey, $consumerSecret, $token, $tokenSecret, $username);
42
43
        $this->mock = $this->getMockBuilder(AbstractService::class)
44
            ->disableOriginalConstructor()
45
            ->setMethods(['getStatus', 'getResponse'])
46
            ->getMockForAbstractClass();
47
48
    }
49
50
    /**
51
     * @covers \CwsOps\LivePerson\Services\AbstractService::__construct
52
     */
53
    public function testCanInitWithOptions()
54
    {
55
        $mock = $this->getMockBuilder(AbstractService::class)
56
            ->setConstructorArgs([$this->config, 5, null])
57
            ->getMockForAbstractClass();
58
59
        $this->assertInstanceOf(AbstractService::class, $mock);
60
    }
61
62
    /**
63
     * @covers \CwsOps\LivePerson\Services\AbstractService::__construct
64
     */
65
    public function testWillThrowInvalidArgumentOnInvalidRetryLimit()
66
    {
67
        $this->expectException(\InvalidArgumentException::class);
68
        $this->expectExceptionMessage('Maximum $retryLimit is 5 you tried setting 10, try setting a value between 0 and 5');
69
70
        new class($this->config, 10) extends AbstractService
71
        {
72
            protected function getDomain(): string
73
            {
74
                return 'foo';
75
            }
76
77
            /**
78
             * Should provide the Live Person service the service will query against.
79
             *
80
             * @return string
81
             */
82
            protected function getService(): string
83
            {
84
                return '';
85
            }
86
        };
87
88
89
    }
90
91
    /**
92
     * @covers \CwsOps\LivePerson\Services\AbstractService::getStatus()
93
     */
94
    public function testGetStatus()
95
    {
96
        $status = new \stdClass();
97
        $status->live = true;
98
99
        $this->mock->expects($this->once())
0 ignored issues
show
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

99
        $this->mock->/** @scrutinizer ignore-call */ 
100
                     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...
100
            ->method('getStatus')
101
            ->willReturn($status);
102
103
        $result = $this->mock->getStatus();
0 ignored issues
show
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

103
        /** @scrutinizer ignore-call */ 
104
        $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...
104
105
        $this->assertInstanceOf(\stdClass::class, $result);
106
        $this->assertTrue($status->live);
107
    }
108
109
    /**
110
     * @covers \CwsOps\LivePerson\Services\AbstractService::getResponse()
111
     *
112
     * @throws RequestNotSentException
113
     */
114
    public function testGetResponse()
115
    {
116
        $response = new \stdClass();
117
        $response->data = ['foo' => 'bar'];
118
        $response->bool = false;
119
120
        $this->mock->expects($this->once())
121
            ->method('getResponse')
122
            ->willReturn($response);
123
124
        $result = $this->mock->getResponse();
0 ignored issues
show
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

124
        /** @scrutinizer ignore-call */ 
125
        $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...
125
126
        $this->assertInstanceOf(\stdClass::class, $result);
127
        $this->assertFalse($result->bool);
128
        $this->assertArrayHasKey('foo', $result->data);
129
130
    }
131
132
    /**
133
     * @covers \CwsOps\LivePerson\Services\RequestNotSentException
134
     * @covers \CwsOps\LivePerson\Services\AbstractService::getResponse()
135
     */
136
    public function testWillThrowNotBuiltOnNoRequest()
137
    {
138
        /** @var AbstractService|MockObject $mock */
139
        $mock = $this->getMockBuilder(AbstractService::class)
140
            ->disableOriginalConstructor()
141
            ->getMockForAbstractClass();
142
143
        try {
144
            $mock->getResponse();
145
        } catch (\Exception $e) {
146
            $this->assertInstanceOf(RequestNotSentException::class, $e);
147
            $this->assertEquals('No request has been sent, you need call a service first.', $e->getMessage());
148
        }
149
150
    }
151
152
    public function tearDown()
153
    {
154
        $this->mock = null;
155
        $this->client = null;
156
    }
157
158
159
}
160