Skip to content

ISSUE-357: use api-client #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
],
"homepage": "https://www.phplist.com/",
"license": "AGPL-3.0-or-later",
"repositories": [
{
"type": "vcs",
"url": "https://github.com/tatevikgr/phplist-api-client"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"authors": [
{
"name": "Xheni Myrtaj",
Expand All @@ -35,9 +43,10 @@
},
"require": {
"php": "^8.1",
"phplist/core": "v5.0.0-alpha8",
"phplist/core": "dev-main",
"symfony/twig-bundle": "^6.4",
"symfony/webpack-encore-bundle": "^2.2"
"symfony/webpack-encore-bundle": "^2.2",
"tatevikgr/rest-api-client": "dev-ISSUE-357"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
9 changes: 8 additions & 1 deletion config/services.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# config/services.yaml
parameters:
api_base_url: '%env(API_BASE_URL)%'
env(API_BASE_URL): 'http://api.phplist.local/api/v2'
env(API_BASE_URL): 'http://api.phplist.local/api/v2/'

services:
_defaults:
Expand Down Expand Up @@ -29,3 +29,10 @@ services:
tags: ['controller.service_arguments']

Symfony\Component\HttpFoundation\Session\SessionInterface: '@session'

PhpList\RestApiClient\Client:
$baseUrl: '%api_base_url%'

PhpList\RestApiClient\Endpoint\AuthClient:
autoconfigure: true
autowire: true
9 changes: 4 additions & 5 deletions src/Controller/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

use Exception;
use GuzzleHttp\Exception\GuzzleException;
use PhpList\WebFrontend\Service\ApiClient;
use PhpList\RestApiClient\Endpoint\AuthClient;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class AuthController extends AbstractController
{
private ApiClient $apiClient;
private AuthClient $apiClient;

public function __construct(ApiClient $apiClient)
public function __construct(AuthClient $apiClient)
{
$this->apiClient = $apiClient;
}
Expand All @@ -40,10 +40,9 @@ public function login(Request $request): Response
$password = $request->request->get('password');

try {
$authData = $this->apiClient->authenticate($username, $password);
$authData = $this->apiClient->login($username, $password);
$request->getSession()->set('auth_token', $authData['key']);
$request->getSession()->set('auth_expiry_date', $authData['key']);
$this->apiClient->setAuthToken($authData['key']);

return $this->redirectToRoute('empty_start_page');
} catch (Exception $e) {
Expand Down
3 changes: 3 additions & 0 deletions src/Service/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use JsonException;
use RuntimeException;

/**
* @deprecated since phplist/rest-api-client should be used instead.
*/
class ApiClient
{
private Client $client;
Expand Down
19 changes: 9 additions & 10 deletions tests/Unit/Controller/AuthControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace PhpList\WebFrontend\Tests\Unit\Controller;

use PhpList\WebFrontend\Controller\AuthController;
use PhpList\WebFrontend\Service\ApiClient;
use PhpList\RestApiClient\Endpoint\AuthClient;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use RuntimeException;
Expand All @@ -16,12 +16,12 @@

class AuthControllerTest extends TestCase
{
private ApiClient&MockObject $apiClient;
private AuthClient&MockObject $apiClient;
private AuthController $controller;

protected function setUp(): void
{
$this->apiClient = $this->createMock(ApiClient::class);
$this->apiClient = $this->createMock(AuthClient::class);

$this->controller = $this->getMockBuilder(AuthController::class)
->setConstructorArgs([$this->apiClient])
Expand Down Expand Up @@ -118,14 +118,10 @@ public function testLoginWithPostRequestSuccess(): void
]);
$request->setSession($session);

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

$this->apiClient->expects($this->once())
->method('setAuthToken')
->with('test-token');

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

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

$this->apiClient->method('authenticate')
$this->apiClient->method('login')
->with('testuser', 'testpass')
->willThrowException(new RuntimeException('Invalid credentials'));

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

$this->assertStringContainsString('auth/login.html.twig', $response->getContent());
$this->assertStringContainsString('Invalid credentials', $response->getContent());
$this->assertStringContainsString(
'Invalid credentials or server error: Invalid credentials',
$response->getContent(),
);
}

public function testLoginWithExistingSession(): void
Expand Down
Loading