ServiceClientTests::testSetInValidToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
namespace Mezon\Service\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
6
/**
7
 * Class ServiceClientTests
8
 *
9
 * @package ServiceClient
10
 * @subpackage ServiceClientTests
11
 * @author Dodonov A.A.
12
 * @version v.1.0 (2019/08/17)
13
 * @copyright Copyright (c) 2019, aeon.org
14
 */
15
16
/**
17
 * Common unit tests for ServiceClient and all derived client classes
18
 *
19
 * @author Dodonov A.A.
20
 * @group baseTests
21
 */
22
class ServiceClientTests extends TestCase
23
{
24
25
    /**
26
     * Client class name
27
     */
28
    protected $clientClassName = '';
29
30
    /**
31
     * Existing user's login
32
     *
33
     * @var string
34
     */
35
    protected $existingLogin = '';
36
37
    /**
38
     * Method creates client object
39
     *
40
     * @param string $password
41
     */
42
    protected function constructClient(string $password = 'root'): object
43
    {
44
        return new $this->clientClassName($this->existingLogin, $password);
45
    }
46
47
    /**
48
     * Testing API connection
49
     */
50
    public function testValidConnect(): void
51
    {
52
        $client = $this->constructClient();
53
54
        $this->assertNotEquals($client->getToken(), false, 'Connection failed');
55
        $this->assertEquals($client->getStoredLogin(), $this->existingLogin, 'Login was not saved');
56
    }
57
58
    /**
59
     * Testing invalid API connection
60
     */
61
    public function testInValidConnect(): void
62
    {
63
        $this->expectException(\Exception::class);
64
        $this->constructClient('1234567');
65
    }
66
67
    /**
68
     * Testing setting valid token
69
     */
70
    public function testSetValidToken(): void
71
    {
72
        $client = $this->constructClient();
73
74
        $newClient = new $this->clientClassName();
75
        $newClient->setToken($client->getToken());
76
77
        $this->assertNotEquals($newClient->getToken(), false, 'Token was not set(1)');
78
    }
79
80
    /**
81
     * Testing setting valid token and login
82
     */
83
    public function testSetValidTokenAndLogin(): void
84
    {
85
        $client = $this->constructClient();
86
87
        $newClient = new $this->clientClassName();
88
        $newClient->setToken($client->getToken(), '[email protected]');
89
90
        $this->assertNotEquals($newClient->getToken(), false, 'Token was not set(2)');
91
        $this->assertNotEquals($newClient->getStoredLogin(), false, 'Login was not saved');
92
    }
93
94
    /**
95
     * Testing loginAs method
96
     */
97
    public function testLoginAs(): void
98
    {
99
        // setup
100
        $client = $this->constructClient();
101
102
        // test body
103
        $client->loginAs($this->existingLogin);
104
105
        // assertions
106
        $this->addToAssertionCount(1);
107
    }
108
109
    /**
110
     * Testing loginAs method with failed call
111
     */
112
    public function testFailedLoginAs(): void
113
    {
114
        // setup
115
        $client = $this->constructClient();
116
117
        // assertions
118
        $this->expectException(\Exception::class);
119
120
        // test body
121
        $client->loginAs('[email protected]', 'login');
122
    }
123
124
    /**
125
     * Testing setting invalid token
126
     */
127
    public function testSetInValidToken(): void
128
    {
129
        // setup
130
        $client = new $this->clientClassName();
131
132
        // test body
133
        $client->setToken('unexistingtoken');
134
135
        // assertions
136
        $this->assertEquals('unexistingtoken', $client->getToken());
137
    }
138
139
    /**
140
     * Testing empty token setups
141
     */
142
    public function testSetEmptyToken(): void
143
    {
144
        // assertions
145
        $this->expectException(\Exception::class);
146
147
        // setup
148
        $client = new $this->clientClassName();
149
150
        // test body
151
        $client->setToken('');
152
    }
153
}
154