Skip to content

Commit 50d8e6e

Browse files
committed
Implement Auth tests
1 parent 5b2114d commit 50d8e6e

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed

tests/Unit/AuthTest.php

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit;
6+
7+
use CodeMash\Auth;
8+
use CodeMash\Client;
9+
use CodeMash\Exceptions\RequestValidationException;
10+
use Generator;
11+
use PHPUnit\Framework\MockObject\MockObject;
12+
use PHPUnit\Framework\TestCase;
13+
14+
final class AuthTest extends TestCase
15+
{
16+
/** @var Client|MockObject */
17+
private $client;
18+
19+
/** @var Auth */
20+
private $auth;
21+
22+
protected function setUp(): void
23+
{
24+
parent::setUp();
25+
26+
$this->client = $this->getMockBuilder(Client::class)
27+
->disableOriginalConstructor()
28+
->onlyMethods(['request'])
29+
->getMock();
30+
31+
$this->auth = new Auth($this->client);
32+
}
33+
34+
/**
35+
* @dataProvider provideTestAuthenticateFail
36+
*/
37+
public function testAuthenticateFail(
38+
array $params,
39+
string $expectedException,
40+
string $expectedExceptionMessage
41+
): void {
42+
$this->expectException($expectedException);
43+
$this->expectExceptionMessage($expectedExceptionMessage);
44+
45+
$this->auth->authenticate($params);
46+
}
47+
48+
public static function provideTestAuthenticateFail(): Generator
49+
{
50+
yield 'Empty params' => [
51+
'$params' => [],
52+
'$expectedException' => RequestValidationException::class,
53+
'$expectedExceptionMessage' => '"password" is required!',
54+
];
55+
56+
yield 'Missing userName' => [
57+
'$params' => ['password' => 'fooBar'],
58+
'$expectedException' => RequestValidationException::class,
59+
'$expectedExceptionMessage' => '"userName" is required!',
60+
];
61+
}
62+
63+
public function testAuthenticate(): void
64+
{
65+
$params = [
66+
'password' => 'testPassword',
67+
'userName' => 'testUserName',
68+
];
69+
70+
$expectedResult = ['foo' => 'bar'];
71+
72+
$this->client
73+
->expects($this->once())
74+
->method('request')
75+
->with(
76+
'POST',
77+
'v2/auth/credentials',
78+
[
79+
'headers' => [
80+
'Accept' => 'application/json',
81+
'Content-Type' => 'application/json',
82+
],
83+
'body' => '{"password":"testPassword","userName":"testUserName"}',
84+
]
85+
)->willReturn(['result' => $expectedResult]);
86+
87+
$actualResult = $this->auth->authenticate($params);
88+
89+
$this->assertSame($expectedResult, $actualResult);
90+
}
91+
92+
public function testCheckAuth(): void
93+
{
94+
$expectedResult = ['foo' => 'bar'];
95+
96+
$this->client
97+
->expects($this->once())
98+
->method('request')
99+
->with(
100+
'POST',
101+
'v2/auth/',
102+
[
103+
'headers' => [
104+
'Accept' => 'application/json',
105+
'Content-Type' => 'application/json',
106+
],
107+
]
108+
)->willReturn(['result' => $expectedResult]);
109+
110+
$actualResult = $this->auth->checkAuth();
111+
112+
$this->assertSame($expectedResult, $actualResult);
113+
}
114+
115+
/**
116+
* @dataProvider provideTestLogoutFail
117+
*/
118+
public function testLogoutFail(
119+
array $params,
120+
string $expectedException,
121+
string $expectedExceptionMessage
122+
): void {
123+
$this->expectException($expectedException);
124+
$this->expectExceptionMessage($expectedExceptionMessage);
125+
126+
$this->auth->logout($params);
127+
}
128+
129+
public static function provideTestLogoutFail(): Generator
130+
{
131+
yield 'Empty params' => [
132+
'$params' => [],
133+
'$expectedException' => RequestValidationException::class,
134+
'$expectedExceptionMessage' => '"bearerToken" is required!',
135+
];
136+
}
137+
138+
public function testLogout(): void
139+
{
140+
$params = [
141+
'bearerToken' => 'testBearerToken',
142+
];
143+
144+
$expectedResult = [
145+
'result' => [
146+
'foo' => 'bar'
147+
],
148+
];
149+
150+
$this->client
151+
->expects($this->once())
152+
->method('request')
153+
->with(
154+
'POST',
155+
'auth/logout',
156+
[
157+
'headers' => [
158+
'Accept' => 'application/json',
159+
'Content-Type' => 'application/json',
160+
'Authorization' => 'Bearer testBearerToken',
161+
],
162+
]
163+
)->willReturn($expectedResult);
164+
165+
$actualResult = $this->auth->logout($params);
166+
167+
$this->assertSame($expectedResult, $actualResult);
168+
}
169+
}

0 commit comments

Comments
 (0)