HandshakeControllerTest::testUpdateTenant()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 61
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 53
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 61
rs 9.0254

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace AtlassianConnectBundle\Tests\Controller;
6
7
use AtlassianConnectBundle\Controller\HandshakeController;
8
use AtlassianConnectBundle\Entity\Tenant;
9
use AtlassianConnectBundle\Repository\TenantRepositoryInterface;
10
use Firebase\JWT\JWT;
11
use PHPUnit\Framework\TestCase;
12
use Psr\Log\NullLogger;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpKernel\Log\Logger;
15
16
class HandshakeControllerTest extends TestCase
17
{
18
    public function testCreateTenant(): void
19
    {
20
        $repository = $this->createMock(TenantRepositoryInterface::class);
21
        $repository->expects($this->once())
22
            ->method('findByClientKey')
23
            ->with('test')
24
            ->willReturn(null);
25
        $repository
26
            ->expects($this->once())
27
            ->method('initializeTenant')
28
            ->willReturn(new Tenant());
29
30
        $expectedTenant = new Tenant();
31
        $expectedTenant
32
            ->setAddonKey('test')
33
            ->setClientKey('test')
34
            ->setPublicKey('test')
35
            ->setSharedSecret('test')
36
            ->setServerVersion('test')
37
            ->setPluginsVersion('test')
38
            ->setBaseUrl('test')
39
            ->setProductType('test')
40
            ->setDescription('test')
41
            ->setEventType('test')
42
            ->setOauthClientId('test');
43
44
        $repository->expects($this->once())
45
            ->method('save')
46
            ->with($expectedTenant);
47
48
        $controller = new HandshakeController($repository, new Logger());
49
        $request = new Request([], [], [], [], [], [], json_encode([
50
            'key' => 'test',
51
            'clientKey' => 'test',
52
            'publicKey' => 'test',
53
            'sharedSecret' => 'test',
54
            'serverVersion' => 'test',
55
            'pluginsVersion' => 'test',
56
            'baseUrl' => 'test',
57
            'productType' => 'test',
58
            'description' => 'test',
59
            'eventType' => 'test',
60
            'oauthClientId' => 'test',
61
        ]));
62
63
        $response = $controller->registerAction($request);
64
        self::assertEquals(200, $response->getStatusCode());
65
        self::assertEquals('OK', $response->getContent());
66
    }
67
68
    public function testUpdateTenant(): void
69
    {
70
        $tenant = new Tenant();
71
        $tenant
72
            ->setAddonKey('test')
73
            ->setClientKey('test')
74
            ->setPublicKey('test')
75
            ->setSharedSecret('test')
76
            ->setServerVersion('test')
77
            ->setPluginsVersion('test')
78
            ->setBaseUrl('test')
79
            ->setProductType('test')
80
            ->setDescription('test')
81
            ->setEventType('test')
82
            ->setOauthClientId('test');
83
84
        $repository = $this->createMock(TenantRepositoryInterface::class);
85
        $repository->expects($this->once())
86
            ->method('findByClientKey')
87
            ->with('test')
88
            ->willReturn($tenant);
89
        $repository
90
            ->expects($this->never())
91
            ->method('initializeTenant');
92
93
        $expectedTenant = new Tenant();
94
        $expectedTenant
95
            ->setAddonKey('test2')
96
            ->setClientKey('test')
97
            ->setPublicKey('test2')
98
            ->setSharedSecret('test')
99
            ->setServerVersion('test2')
100
            ->setPluginsVersion('test')
101
            ->setBaseUrl('test2')
102
            ->setProductType('test2')
103
            ->setDescription('test')
104
            ->setEventType('test')
105
            ->setOauthClientId('test');
106
107
        $repository->expects($this->once())
108
            ->method('save')
109
            ->with($expectedTenant);
110
111
        $controller = new HandshakeController($repository, new Logger());
112
        $request = new Request([], [], [], [], [], ['HTTP_AUTHORIZATION' => 'Bearer '.JWT::encode([], 'test', 'HS256')], json_encode([
113
            'key' => 'test2',
114
            'clientKey' => 'test',
115
            'publicKey' => 'test2',
116
            'sharedSecret' => 'test',
117
            'serverVersion' => 'test2',
118
            'pluginsVersion' => 'test',
119
            'baseUrl' => 'test2',
120
            'productType' => 'test2',
121
            'description' => 'test',
122
            'eventType' => 'test',
123
            'oauthClientId' => 'test',
124
        ]));
125
126
        $response = $controller->registerAction($request);
127
        self::assertEquals(200, $response->getStatusCode());
128
        self::assertEquals('OK', $response->getContent());
129
    }
130
131
    public function testUnauthorized(): void
132
    {
133
        $tenant = new Tenant();
134
        $tenant
135
            ->setAddonKey('test')
136
            ->setClientKey('test')
137
            ->setPublicKey('test')
138
            ->setSharedSecret('differentsharedsecret')
139
            ->setServerVersion('test')
140
            ->setPluginsVersion('test')
141
            ->setBaseUrl('test')
142
            ->setProductType('test')
143
            ->setDescription('test')
144
            ->setEventType('test')
145
            ->setOauthClientId('test');
146
147
        $repository = $this->createMock(TenantRepositoryInterface::class);
148
        $repository->expects($this->once())
149
            ->method('findByClientKey')
150
            ->with('test')
151
            ->willReturn($tenant);
152
153
        $repository
154
            ->expects($this->never())
155
            ->method('initializeTenant');
156
157
        $repository->expects($this->never())
158
            ->method('save');
159
160
        $controller = new HandshakeController($repository, new NullLogger());
161
        $request = new Request([], [], [], [], [], ['HTTP_AUTHORIZATION' => 'Bearer '.JWT::encode([], 'test', 'HS256')], json_encode([
162
            'key' => 'test2',
163
            'clientKey' => 'test',
164
            'publicKey' => 'test2',
165
            'sharedSecret' => 'test',
166
            'serverVersion' => 'test2',
167
            'pluginsVersion' => 'test',
168
            'baseUrl' => 'test2',
169
            'productType' => 'test2',
170
            'description' => 'test',
171
            'eventType' => 'test',
172
            'oauthClientId' => 'test',
173
        ]));
174
175
        $response = $controller->registerAction($request);
176
        self::assertEquals(401, $response->getStatusCode());
177
        self::assertEquals('Unauthorized', $response->getContent());
178
    }
179
}
180