-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDashboardControllerTest.php
More file actions
156 lines (132 loc) · 6.05 KB
/
Copy pathDashboardControllerTest.php
File metadata and controls
156 lines (132 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
declare(strict_types=1);
namespace PhpList\WebFrontend\Tests\Integration\Controller;
use PhpList\RestApiClient\Endpoint\StatisticsClient;
use PhpList\RestApiClient\Exception\AuthenticationException;
use PhpList\RestApiClient\Exception\AuthorizationException;
use PhpList\RestApiClient\Response\Statistics\DashboardStatisticsResponse;
use PhpList\WebFrontend\Controller\DashboardController;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\Routing\RouterInterface;
class DashboardControllerTest extends KernelTestCase
{
public function testDashboardRouteIsRegistered(): void
{
self::bootKernel();
/** @var RouterInterface $router */
$router = static::getContainer()->get('router');
self::assertSame('/', $router->generate('home'));
}
public function testDashboardRendersSpaPayloadWithStats(): void
{
self::bootKernel();
$apiBaseUrl = (string) static::getContainer()->getParameter('app.api_base_url');
$statsClient = $this->createMock(StatisticsClient::class);
$statsClient->expects(self::once())
->method('getDashboardStats')
->willReturn($this->createDashboardStatsResponse());
$controller = new DashboardController($statsClient);
$controller->setContainer(static::getContainer());
$request = Request::create('/');
$session = new Session(new MockArraySessionStorage());
$session->set('auth_token', 'integration-token');
$request->setSession($session);
static::getContainer()->get(RequestStack::class)->push($request);
$response = $controller->index($request);
$content = (string) $response->getContent();
self::assertSame(200, $response->getStatusCode());
self::assertStringContainsString('<title>phpList - Dashboard</title>', $content);
self::assertStringContainsString('data-api-token="integration-token"', $content);
self::assertStringContainsString(sprintf('data-api-base-url="%s"', $apiBaseUrl), $content);
self::assertStringContainsString('data-dashboard-stats=', $content);
self::assertStringContainsString('"recent_campaigns"', $content);
self::assertStringContainsString('Weekly Digest', $content);
self::assertStringContainsString('data-dashboard-error=""', $content);
}
public function testDashboardPropagatesAuthenticationExceptionForLoginRedirect(): void
{
self::bootKernel();
$statsClient = $this->createMock(StatisticsClient::class);
$statsClient->expects(self::once())
->method('getDashboardStats')
->willThrowException(new AuthenticationException('Session expired'));
$controller = new DashboardController($statsClient);
$controller->setContainer(static::getContainer());
$request = Request::create('/');
$session = new Session(new MockArraySessionStorage());
$session->set('auth_token', 'integration-token');
$request->setSession($session);
// An expired session must not be rendered inline: the controller lets the
// AuthenticationException propagate so UnauthorizedSubscriber redirects to /login.
$this->expectException(AuthenticationException::class);
$this->expectExceptionMessage('Session expired');
$controller->index($request);
}
public function testDashboardRendersDashboardErrorWhenAuthorizationFails(): void
{
self::bootKernel();
$statsClient = $this->createMock(StatisticsClient::class);
$statsClient->expects(self::once())
->method('getDashboardStats')
->willThrowException(new AuthorizationException(
'No valid session key was provided as basic auth password.',
403
));
$controller = new DashboardController($statsClient);
$controller->setContainer(static::getContainer());
$request = Request::create('/');
$session = new Session(new MockArraySessionStorage());
$session->set('auth_token', 'integration-token');
$request->setSession($session);
$response = $controller->index($request);
$content = (string) $response->getContent();
$error = 'data-dashboard-error="No valid session key was provided '
. 'as basic auth password."';
self::assertSame(200, $response->getStatusCode());
self::assertStringContainsString($error, $content);
self::assertStringContainsString('data-dashboard-stats="[]"', $content);
}
private function createDashboardStatsResponse(): DashboardStatisticsResponse
{
return new DashboardStatisticsResponse([
'summary_statistics' => [
'total_subscribers' => [
'value' => 1000,
'change_vs_last_month' => 8.2,
],
'active_campaigns' => [
'value' => 3,
'change_vs_last_month' => 1.0,
],
'open_rate' => [
'value' => 47.6,
'change_vs_last_month' => 2.1,
],
'bounce_rate' => [
'value' => 1.2,
'change_vs_last_month' => -0.2,
],
],
'recent_campaigns' => [
[
'name' => 'Weekly Digest',
'status' => 'sent',
'date' => '2026-04-10',
'open_rate' => 53.2,
'click_rate' => 12.3,
],
],
'campaign_performance' => [
[
'date' => '2026-04-09',
'opens' => 120,
'clicks' => 24,
],
],
]);
}
}