Skip to content

Commit 56538bf

Browse files
committed
ISSUE-357: use api-client
1 parent 4f8e231 commit 56538bf

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

composer.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
],
1212
"homepage": "https://www.phplist.com/",
1313
"license": "AGPL-3.0-or-later",
14+
"repositories": [
15+
{
16+
"type": "vcs",
17+
"url": "https://github.com/tatevikgr/phplist-api-client"
18+
}
19+
],
20+
"minimum-stability": "dev",
21+
"prefer-stable": true,
1422
"authors": [
1523
{
1624
"name": "Xheni Myrtaj",
@@ -37,7 +45,8 @@
3745
"php": "^8.1",
3846
"phplist/core": "v5.0.0-alpha8",
3947
"symfony/twig-bundle": "^6.4",
40-
"symfony/webpack-encore-bundle": "^2.2"
48+
"symfony/webpack-encore-bundle": "^2.2",
49+
"tatevikgr/rest-api-client": "dev-master"
4150
},
4251
"require-dev": {
4352
"phpunit/phpunit": "^9.5",

src/Controller/AuthController.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66

77
use Exception;
88
use GuzzleHttp\Exception\GuzzleException;
9-
use PhpList\WebFrontend\Service\ApiClient;
9+
use PhpList\RestApiClient\Endpoint\AuthClient;
1010
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1111
use Symfony\Component\HttpFoundation\Request;
1212
use Symfony\Component\HttpFoundation\Response;
1313
use Symfony\Component\Routing\Attribute\Route;
1414

1515
class AuthController extends AbstractController
1616
{
17-
private ApiClient $apiClient;
17+
private AuthClient $apiClient;
1818

19-
public function __construct(ApiClient $apiClient)
19+
public function __construct(AuthClient $apiClient)
2020
{
2121
$this->apiClient = $apiClient;
2222
}
@@ -40,10 +40,9 @@ public function login(Request $request): Response
4040
$password = $request->request->get('password');
4141

4242
try {
43-
$authData = $this->apiClient->authenticate($username, $password);
43+
$authData = $this->apiClient->login($username, $password);
4444
$request->getSession()->set('auth_token', $authData['key']);
4545
$request->getSession()->set('auth_expiry_date', $authData['key']);
46-
$this->apiClient->setAuthToken($authData['key']);
4746

4847
return $this->redirectToRoute('empty_start_page');
4948
} catch (Exception $e) {

tests/Unit/Controller/AuthControllerTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace PhpList\WebFrontend\Tests\Unit\Controller;
66

77
use PhpList\WebFrontend\Controller\AuthController;
8-
use PhpList\WebFrontend\Service\ApiClient;
8+
use PhpList\RestApiClient\Endpoint\AuthClient;
99
use PHPUnit\Framework\MockObject\MockObject;
1010
use PHPUnit\Framework\TestCase;
1111
use RuntimeException;
@@ -16,12 +16,12 @@
1616

1717
class AuthControllerTest extends TestCase
1818
{
19-
private ApiClient&MockObject $apiClient;
19+
private AuthClient&MockObject $apiClient;
2020
private AuthController $controller;
2121

2222
protected function setUp(): void
2323
{
24-
$this->apiClient = $this->createMock(ApiClient::class);
24+
$this->apiClient = $this->createMock(AuthClient::class);
2525

2626
$this->controller = $this->getMockBuilder(AuthController::class)
2727
->setConstructorArgs([$this->apiClient])
@@ -118,14 +118,10 @@ public function testLoginWithPostRequestSuccess(): void
118118
]);
119119
$request->setSession($session);
120120

121-
$this->apiClient->method('authenticate')
121+
$this->apiClient->method('login')
122122
->with('testuser', 'testpass')
123123
->willReturn(['key' => 'test-token']);
124124

125-
$this->apiClient->expects($this->once())
126-
->method('setAuthToken')
127-
->with('test-token');
128-
129125
$response = $this->controller->login($request);
130126

131127
$this->assertInstanceOf(RedirectResponse::class, $response);
@@ -147,14 +143,17 @@ public function testLoginWithPostRequestFailure(): void
147143
]);
148144
$request->setSession($session);
149145

150-
$this->apiClient->method('authenticate')
146+
$this->apiClient->method('login')
151147
->with('testuser', 'testpass')
152148
->willThrowException(new RuntimeException('Invalid credentials'));
153149

154150
$response = $this->controller->login($request);
155151

156152
$this->assertStringContainsString('auth/login.html.twig', $response->getContent());
157-
$this->assertStringContainsString('Invalid credentials', $response->getContent());
153+
$this->assertStringContainsString(
154+
'Invalid credentials or server error: Invalid credentials',
155+
$response->getContent(),
156+
);
158157
}
159158

160159
public function testLoginWithExistingSession(): void

0 commit comments

Comments
 (0)