Skip to content

Commit a553700

Browse files
committed
ISSUE-345: test fix
1 parent 90fe1d1 commit a553700

12 files changed

+132
-80
lines changed

Diff for: src/Controller/SessionController.php

+2-12
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
1212
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1313
use PhpList\Core\Domain\Model\Identity\AdministratorToken;
14-
use PhpList\Core\Domain\Repository\Identity\AdministratorRepository;
1514
use PhpList\Core\Security\Authentication;
1615
use PhpList\RestBundle\Controller\Traits\AuthenticationTrait;
1716
use Symfony\Component\HttpFoundation\JsonResponse;
1817
use Symfony\Component\HttpFoundation\Request;
1918
use Symfony\Component\HttpFoundation\Response;
2019
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
2120
use Symfony\Component\Routing\Attribute\Route;
22-
use Symfony\Component\Serializer\SerializerInterface;
2321
use OpenApi\Attributes as OA;
2422

2523
/**
@@ -33,19 +31,11 @@ class SessionController extends AbstractController
3331
{
3432
use AuthenticationTrait;
3533

36-
private AdministratorRepository $administratorRepository;
37-
private SerializerInterface $serializer;
3834
private SessionManager $sessionManager;
3935

40-
public function __construct(
41-
Authentication $authentication,
42-
AdministratorRepository $administratorRepository,
43-
SerializerInterface $serializer,
44-
SessionManager $sessionManager,
45-
) {
36+
public function __construct(Authentication $authentication, SessionManager $sessionManager)
37+
{
4638
$this->authentication = $authentication;
47-
$this->administratorRepository = $administratorRepository;
48-
$this->serializer = $serializer;
4939
$this->sessionManager = $sessionManager;
5040
}
5141

Diff for: src/Controller/SubscriberController.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use OpenApi\Attributes as OA;
88
use PhpList\Core\Domain\Model\Subscription\Subscriber;
9-
use PhpList\Core\Domain\Repository\Subscription\SubscriberRepository;
109
use PhpList\Core\Security\Authentication;
1110
use PhpList\RestBundle\Controller\Traits\AuthenticationTrait;
1211
use PhpList\RestBundle\Entity\CreateSubscriberRequest;
@@ -33,16 +32,11 @@ class SubscriberController extends AbstractController
3332
{
3433
use AuthenticationTrait;
3534

36-
private SubscriberRepository $subscriberRepository;
3735
private SubscriberManager $subscriberManager;
3836

39-
public function __construct(
40-
Authentication $authentication,
41-
SubscriberRepository $repository,
42-
SubscriberManager $subscriberManager,
43-
) {
37+
public function __construct(Authentication $authentication, SubscriberManager $subscriberManager)
38+
{
4439
$this->authentication = $authentication;
45-
$this->subscriberRepository = $repository;
4640
$this->subscriberManager = $subscriberManager;
4741
}
4842

Diff for: src/Entity/UpdateSubscriberRequest.php

-12
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,9 @@ class UpdateSubscriberRequest implements RequestInterface
2525
#[Assert\Type(type: 'bool')]
2626
public bool $htmlEmail;
2727

28-
#[Assert\Type(type: 'number')]
29-
public ?int $rssFrequency = null; // todo check what is this
30-
3128
#[Assert\Type(type: 'bool')]
3229
public bool $disabled;
3330

3431
#[Assert\Type(type: 'string')]
3532
public string $additionalData;
36-
37-
#[Assert\Type(type: 'string')]
38-
public ?string $woonplaats = null; // todo check what is this
39-
40-
#[Assert\Type(type: 'string')]
41-
public ?string $foreignKey = null; // todo check what is this
42-
43-
#[Assert\Type(type: 'string')]
44-
public ?string $country = null; // todo check what is this
4533
}

Diff for: src/OpenApi/SwaggerSchemas.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
new OA\Property(property: 'id', type: 'integer', example: 2),
1313
new OA\Property(property: 'name', type: 'string', example: 'Newsletter'),
1414
new OA\Property(property: 'description', type: 'string', example: 'Monthly updates'),
15-
new OA\Property(property: 'creation_date', type: 'string', format: 'date-time', example: '2022-12-01T10:00:00Z'),
15+
new OA\Property(
16+
property: 'creation_date',
17+
type: 'string',
18+
format: 'date-time',
19+
example: '2022-12-01T10:00:00Z'
20+
),
1621
new OA\Property(property: 'public', type: 'boolean', example: true),
1722
],
1823
type: 'object'
@@ -22,7 +27,12 @@
2227
properties: [
2328
new OA\Property(property: 'id', type: 'integer', example: 1),
2429
new OA\Property(property: 'email', type: 'string', example: '[email protected]'),
25-
new OA\Property(property: 'creation_date', type: 'string', format: 'date-time', example: '2023-01-01T12:00:00Z'),
30+
new OA\Property(
31+
property: 'creation_date',
32+
type: 'string',
33+
format: 'date-time',
34+
example: '2023-01-01T12:00:00Z',
35+
),
2636
new OA\Property(property: 'confirmed', type: 'boolean', example: true),
2737
new OA\Property(property: 'blacklisted', type: 'boolean', example: false),
2838
new OA\Property(property: 'bounce_count', type: 'integer', example: 0),
@@ -37,4 +47,6 @@
3747
],
3848
type: 'object'
3949
)]
40-
class SwaggerSchemas {}
50+
class SwaggerSchemas
51+
{
52+
}

Diff for: src/Serializer/AdministratorTokenNormalizer.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
class AdministratorTokenNormalizer implements NormalizerInterface
1111
{
12+
/**
13+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
14+
*/
1215
public function normalize($object, string $format = null, array $context = []): array
1316
{
1417
if (!$object instanceof AdministratorToken) {
@@ -18,7 +21,7 @@ public function normalize($object, string $format = null, array $context = []):
1821
return [
1922
'id' => $object->getId(),
2023
'key' => $object->getKey(),
21-
'expiry' => $object->getExpiry()->format('Y-m-d\TH:i:sP'),
24+
'expiry_date' => $object->getExpiry()->format('Y-m-d\TH:i:sP'),
2225
];
2326
}
2427

Diff for: src/Validator/RequestValidator.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpList\RestBundle\Entity\RequestInterface;
88
use Symfony\Component\HttpFoundation\Request;
9+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
910
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
1011
use Symfony\Component\Serializer\SerializerInterface;
1112
use Symfony\Component\Validator\Validator\ValidatorInterface;
@@ -28,7 +29,7 @@ public function validate(Request $request, string $dtoClass): RequestInterface
2829
'json'
2930
);
3031
} catch (Throwable $e) {
31-
throw new UnprocessableEntityHttpException('Invalid JSON: ' . $e->getMessage());
32+
throw new BadRequestHttpException('Invalid JSON: ' . $e->getMessage());
3233
}
3334

3435
return $this->validateDto($dto);

Diff for: tests/Integration/Controller/SessionControllerTest.php

+14-23
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,27 @@ public function testPostSessionsWithNoJsonReturnsError400()
4646
$this->jsonRequest('post', '/api/v2/sessions');
4747

4848
$this->assertHttpBadRequest();
49-
$this->assertJsonResponseContentEquals(
50-
[
51-
'message' => 'Empty JSON data',
52-
]
53-
);
49+
$data = $this->getDecodedJsonResponseContent();
50+
$this->assertStringContainsString('Invalid JSON:', $data['message']);
5451
}
5552

5653
public function testPostSessionsWithInvalidJsonWithJsonContentTypeReturnsError400()
5754
{
5855
$this->jsonRequest('post', '/api/v2/sessions', [], [], [], 'Here be dragons, but no JSON.');
5956

6057
$this->assertHttpBadRequest();
61-
$this->assertJsonResponseContentEquals(
62-
[
63-
'message' => 'Could not decode request body.',
64-
]
65-
);
58+
$data = $this->getDecodedJsonResponseContent();
59+
$this->assertStringContainsString('Invalid JSON:', $data['message']);
6660
}
6761

68-
public function testPostSessionsWithValidEmptyJsonWithOtherTypeReturnsError400()
62+
public function testPostSessionsWithValidEmptyJsonWithOtherTypeReturnsError422()
6963
{
7064
self::getClient()->request('post', '/api/v2/sessions', [], [], ['CONTENT_TYPE' => 'application/xml'], '[]');
7165

72-
$this->assertHttpBadRequest();
66+
$this->assertHttpUnprocessableEntity();
7367
$this->assertJsonResponseContentEquals(
7468
[
75-
'message' => 'Incomplete credentials',
69+
'message' => "loginName: This value should not be blank.\npassword: This value should not be blank.",
7670
]
7771
);
7872
}
@@ -84,7 +78,7 @@ public static function incompleteCredentialsDataProvider(): array
8478
{
8579
return [
8680
'neither login_name nor password' => ['{}'],
87-
'login_name, but no password' => ['{"login_name": "[email protected]"}'],
81+
'login_name, but no password' => ['{"loginName": "[email protected]"}'],
8882
'password, but no login_name' => ['{"password": "t67809oibuzfq2qg3"}'],
8983
];
9084
}
@@ -96,12 +90,9 @@ public function testPostSessionsWithValidIncompleteJsonReturnsError400(string $j
9690
{
9791
$this->jsonRequest('post', '/api/v2/sessions', [], [], [], $jsonData);
9892

99-
$this->assertHttpBadRequest();
100-
$this->assertJsonResponseContentEquals(
101-
[
102-
'message' => 'Incomplete credentials',
103-
]
104-
);
93+
$this->assertHttpUnprocessableEntity();
94+
$data = $this->getDecodedJsonResponseContent();
95+
$this->assertStringContainsString('This value should not be blank', $data['message']);
10596
}
10697

10798
public function testPostSessionsWithInvalidCredentialsReturnsNotAuthorized()
@@ -110,7 +101,7 @@ public function testPostSessionsWithInvalidCredentialsReturnsNotAuthorized()
110101

111102
$loginName = 'john.doe';
112103
$password = 'a sandwich and a cup of coffee';
113-
$jsonData = ['login_name' => $loginName, 'password' => $password];
104+
$jsonData = ['loginName' => $loginName, 'password' => $password];
114105

115106
$this->jsonRequest('post', '/api/v2/sessions', [], [], [], json_encode($jsonData));
116107

@@ -128,7 +119,7 @@ public function testPostSessionsActionWithValidCredentialsReturnsCreatedHttpStat
128119

129120
$loginName = 'john.doe';
130121
$password = 'Bazinga!';
131-
$jsonData = ['login_name' => $loginName, 'password' => $password];
122+
$jsonData = ['loginName' => $loginName, 'password' => $password];
132123

133124
$this->jsonRequest('post', '/api/v2/sessions', [], [], [], json_encode($jsonData));
134125

@@ -139,7 +130,7 @@ public function testPostSessionsActionWithValidCredentialsCreatesToken()
139130
{
140131
$administratorId = 1;
141132
$this->loadFixtures([AdministratorFixture::class, AdministratorTokenFixture::class]);
142-
$jsonData = ['login_name' => 'john.doe', 'password' => 'Bazinga!'];
133+
$jsonData = ['loginName' => 'john.doe', 'password' => 'Bazinga!'];
143134

144135
$this->jsonRequest('post', '/api/v2/sessions', [], [], [], json_encode($jsonData));
145136

Diff for: tests/Integration/Controller/SubscriberControllerTest.php

+28-9
Original file line numberDiff line numberDiff line change
@@ -95,33 +95,52 @@ public function testPostSubscribersWithValidSessionKeyAndExistingEmailAddressCre
9595
$this->assertHttpConflict();
9696
}
9797

98+
/**
99+
* @dataProvider invalidSubscriberDataProvider
100+
* @param array[] $jsonData
101+
*/
102+
public function testPostSubscribersWithInvalidDataCreatesUnprocessableEntityStatus(array $jsonData)
103+
{
104+
$this->authenticatedJsonRequest('post', '/api/v2/subscribers', [], [], [], json_encode($jsonData));
105+
106+
$this->assertHttpUnprocessableEntity();
107+
}
108+
98109
/**
99110
* @return array[][]
100111
*/
101112
public static function invalidSubscriberDataProvider(): array
102113
{
103114
return [
104115
'no data' => [[]],
105-
'email is null' => [['email' => null]],
106116
'email is an empty string' => [['email' => '']],
107117
'email is invalid string' => [['email' => 'coffee and cigarettes']],
108-
'email as boolean' => [['email' => true]],
109-
'html_email as integer' => [['email' => '[email protected]', 'htmlEmail' => 1]],
110-
'html_email as string' => [['email' => '[email protected]', 'htmlEmail' => 'yes']],
111-
'request_confirmation as string' => [['email' => '[email protected]', 'requestConfirmation' => 'needed']],
112-
'disabled as string' => [['email' => '[email protected]', 'requestConfirmation' => 1]],
113118
];
114119
}
115120

116121
/**
117-
* @dataProvider invalidSubscriberDataProvider
122+
* @dataProvider invalidDataProvider
118123
* @param array[] $jsonData
119124
*/
120-
public function testPostSubscribersWithInvalidDataCreatesUnprocessableEntityStatus(array $jsonData)
125+
public function testPostSubscribersWithInvalidJsonCreatesHttpBadRequestStatus(array $jsonData)
121126
{
122127
$this->authenticatedJsonRequest('post', '/api/v2/subscribers', [], [], [], json_encode($jsonData));
123128

124-
$this->assertHttpUnprocessableEntity();
129+
$this->assertHttpBadRequest();
130+
}
131+
132+
/**
133+
* @return array[][]
134+
*/
135+
public static function invalidDataProvider(): array
136+
{
137+
return [
138+
'email is null' => [['email' => null]],
139+
'email as boolean' => [['email' => true]],
140+
'html_email as integer' => [['email' => '[email protected]', 'htmlEmail' => 1]],
141+
'html_email as string' => [['email' => '[email protected]', 'htmlEmail' => 'yes']],
142+
'request_confirmation as string' => [['email' => '[email protected]', 'requestConfirmation' => 'needed']],
143+
];
125144
}
126145

127146
public function testPostSubscribersWithValidSessionKeyAssignsProvidedSubscriberData()

Diff for: tests/Integration/Service/Manager/SubscriberManagerTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpList\RestBundle\Tests\Integration\Service\Manager;
66

7+
use Doctrine\ORM\EntityManagerInterface;
78
use PHPUnit\Framework\TestCase;
89
use PhpList\RestBundle\Service\Manager\SubscriberManager;
910
use PhpList\Core\Domain\Repository\Subscription\SubscriberRepository;
@@ -15,6 +16,7 @@ class SubscriberManagerTest extends TestCase
1516
public function testCreateSubscriberPersistsAndReturnsProperlyInitializedEntity(): void
1617
{
1718
$repoMock = $this->createMock(SubscriberRepository::class);
19+
$emMock = $this->createMock(EntityManagerInterface::class);
1820
$repoMock
1921
->expects($this->once())
2022
->method('save')
@@ -26,7 +28,7 @@ public function testCreateSubscriberPersistsAndReturnsProperlyInitializedEntity(
2628
&& $sub->isDisabled() === false;
2729
}));
2830

29-
$manager = new SubscriberManager($repoMock);
31+
$manager = new SubscriberManager($repoMock, $emMock);
3032

3133
$dto = new CreateSubscriberRequest();
3234
$dto->email = '[email protected]';

Diff for: tests/Integration/Validator/RequestValidatorTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPUnit\Framework\TestCase;
1212
use RuntimeException;
1313
use Symfony\Component\HttpFoundation\Request;
14+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1415
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
1516
use Symfony\Component\Serializer\SerializerInterface;
1617
use Symfony\Component\Validator\ConstraintViolation;
@@ -66,7 +67,7 @@ public function testValidateThrowsOnInvalidJson(): void
6667
->method('deserialize')
6768
->willThrowException(new RuntimeException('Syntax error'));
6869

69-
$this->expectException(UnprocessableEntityHttpException::class);
70+
$this->expectException(BadRequestHttpException::class);
7071
$this->expectExceptionMessage('Invalid JSON: Syntax error');
7172

7273
$this->requestValidator->validate($request, DummyRequestDto::class);

0 commit comments

Comments
 (0)