Completed
Push — master ( 37a41b...3a7e3e )
by Maxim
07:11
created

AmoCRMClientTest::testSessionException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
use GuzzleHttp\ClientInterface;
3
use GuzzleHttp\Psr7\Request;
4
use GuzzleHttp\Psr7\Response;
5
use mb24dev\AmoCRM\Exception;
6
use mb24dev\AmoCRM\Method\MethodInterface;
7
use mb24dev\AmoCRM\ResponseTransformer\ResponseTransformerInterface;
8
use mb24dev\AmoCRM\Session\Session;
9
use mb24dev\AmoCRM\Session\SessionDoesNotExistException;
10
use mb24dev\AmoCRM\Session\SessionDoNotSavedException;
11
use mb24dev\AmoCRM\SessionStorage\SessionStorageInterface;
12
use mb24dev\AmoCRM\User\User;
13
14
/**
15
 * Class AmoCRMClientTest
16
 */
17
class AmoCRMClientTest extends PHPUnit_Framework_TestCase
18
{
19
20
    /** @var  ClientInterface|PHPUnit_Framework_MockObject_MockObject */
21
    private $client;
22
23
    /** @var  SessionStorageInterface|PHPUnit_Framework_MockObject_MockObject */
24
    private $sessionStorage;
25
26
    /** @var  ResponseTransformerInterface|PHPUnit_Framework_MockObject_MockObject */
27
    private $responseTransformer;
28
29
    private function validSessionStorage()
30
    {
31
        $this->sessionStorage->method('save')->willReturn(null);
32
        $this->sessionStorage->method('getActive')->willReturn(new Session('session_id'));
33
    }
34
35
    private function invalidSessionStorage()
36
    {
37
        $this->sessionStorage->method('save')->willThrowException(new SessionDoNotSavedException());
38
        $this->sessionStorage->method('getActive')->willThrowException(
39
            new SessionDoesNotExistException(new User('domain', 'login', 'hash'))
40
        );
41
    }
42
43
    /**
44
     * Sets up the fixture, for example, open a network connection.
45
     * This method is called before a test is executed.
46
     */
47
    protected function setUp()
48
    {
49
        $this->client = $this->getMockForAbstractClass(ClientInterface::class);
50
        $this->client->method('send')->willReturn(
51
            new Response(200)
52
        );
53
54
        $this->sessionStorage = $this->getMockForAbstractClass(
55
            SessionStorageInterface::class
56
        );
57
58
        $this->responseTransformer = $this->getMockForAbstractClass(ResponseTransformerInterface::class);
59
        $this->responseTransformer->method(
60
            'transform'
61
        )->willReturn([]);
62
    }
63
64
    public function testExecAbstractMethod()
65
    {
66
        $this->validSessionStorage();
67
        $amoCRMClient = new \mb24dev\AmoCRM\AmoCRMClient(
68
            $this->client, $this->sessionStorage, $this->responseTransformer
69
        );
70
71
        $method = $this->getMockForAbstractClass(MethodInterface::class);
72
        $method->method('getUser')->willReturn(
73
            new User('domain', 'login', 'hash')
74
        );
75
76
        $method->method('buildRequest')->willReturn(new Request('post', 'url'));
77
        $method->method('getResponseTransformer')->willReturn(null);
78
79
        $this->assertEquals([], $amoCRMClient->exec($method));
80
    }
81
82
    public function testSessionException()
83
    {
84
        $this->invalidSessionStorage();
85
        $amoCRMClient = new \mb24dev\AmoCRM\AmoCRMClient(
86
            $this->client, $this->sessionStorage, $this->responseTransformer
87
        );
88
89
        $method = $this->getMockForAbstractClass(MethodInterface::class);
90
        $method->method('getUser')->willReturn(
91
            new User('domain', 'login', 'hash')
92
        );
93
94
        $method->method('buildRequest')->willReturn(new Request('post', 'url'));
95
        $method->method('getResponseTransformer')->willReturn(null);
96
        $this->expectException(Exception::class);
97
        $amoCRMClient->exec($method);
98
    }
99
100
}
101