Skip to content

Commit cbb70da

Browse files
TatevikGrtatevikg1
andauthored
ISSUE-357: use phplist-api-client (#71)
* ISSUE-357: use api-client * ISSUE-357: fix config --------- Co-authored-by: Tatevik <[email protected]>
1 parent 4f8e231 commit cbb70da

File tree

5 files changed

+35
-18
lines changed

5 files changed

+35
-18
lines changed

composer.json

Lines changed: 11 additions & 2 deletions
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",
@@ -35,9 +43,10 @@
3543
},
3644
"require": {
3745
"php": "^8.1",
38-
"phplist/core": "v5.0.0-alpha8",
46+
"phplist/core": "dev-main",
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-ISSUE-357"
4150
},
4251
"require-dev": {
4352
"phpunit/phpunit": "^9.5",

config/services.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# config/services.yaml
22
parameters:
33
api_base_url: '%env(API_BASE_URL)%'
4-
env(API_BASE_URL): 'http://api.phplist.local/api/v2'
4+
env(API_BASE_URL): 'http://api.phplist.local/api/v2/'
55

66
services:
77
_defaults:
@@ -29,3 +29,10 @@ services:
2929
tags: ['controller.service_arguments']
3030

3131
Symfony\Component\HttpFoundation\Session\SessionInterface: '@session'
32+
33+
PhpList\RestApiClient\Client:
34+
$baseUrl: '%api_base_url%'
35+
36+
PhpList\RestApiClient\Endpoint\AuthClient:
37+
autoconfigure: true
38+
autowire: true

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) {

src/Service/ApiClient.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use JsonException;
1010
use RuntimeException;
1111

12+
/**
13+
* @deprecated since phplist/rest-api-client should be used instead.
14+
*/
1215
class ApiClient
1316
{
1417
private Client $client;

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)