Skip to content

Commit

Permalink
tests: add tests for the change picture endpoint for user
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryde committed Jul 7, 2024
1 parent 256362b commit 72591f0
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 5 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"require-dev": {
"dama/doctrine-test-bundle": "^8",
"deployer/deployer": "^7.3",
"league/flysystem-memory": "^3.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^1.8.2",
"phpstan/phpstan-doctrine": "^1.3.12",
Expand Down
50 changes: 49 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion config/packages/oneup_flysystem.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ when@dev:
adapters:
musicall.flysystem_adapter:
local:
location: "%kernel.project_dir%/public/"
location: "%kernel.project_dir%/public/"
when@test:
# for dev env we use the local adapter (to not upload on the S3)
oneup_flysystem:
adapters:
musicall.flysystem_adapter:
memory: ~
1 change: 0 additions & 1 deletion src/State/Processor/User/UserProfilePictureProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function __construct(
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
{
dump($data);
/** @var User $user */
$user = $this->security->getUser();
$previousProfilePicture = $user->getProfilePicture() ?: null;
Expand Down
62 changes: 62 additions & 0 deletions tests/Api/User/UserChangePictureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Api\User;

use App\Tests\ApiTestCase;
use App\Tests\Factory\User\UserFactory;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Response;

class UserChangePictureTest extends ApiTestCase
{
public function test_change_picture(): void
{
$user1 = UserFactory::new()->asBaseUser()->create()->_real();
$file = new UploadedFile(__DIR__ . '/fixtures/image-ok.jpeg', 'image-ok.jpeg');

$this->assertNull($user1->getProfilePicture());

$this->client->loginUser($user1);
$this->client->request('POST', '/api/user_profile_pictures', [], ['imageFile' => $file], [
'CONTENT_TYPE' => 'multipart/form-data',
], );
$this->assertResponseStatusCodeSame(Response::HTTP_CREATED);
$this->assertNotNull($user1->getProfilePicture());
}

public function test_change_picture_too_big(): void
{
$user1 = UserFactory::new()->asBaseUser()->create()->_real();
$file = new UploadedFile(__DIR__ . '/fixtures/image-too-big.jpg', 'image-too-big.jpg');

$this->client->loginUser($user1);
$this->client->request('POST', '/api/user_profile_pictures', [], ['imageFile' => $file], [
'CONTENT_TYPE' => 'multipart/form-data',
], );
$this->assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY);
$this->assertSame('{"@id":"\/api\/validation_errors\/7f87163d-878f-47f5-99ba-a8eb723a1ab2","@type":"ConstraintViolationList","status":422,"violations":[{"propertyPath":"image_file","message":"La largeur de l\u0027image est trop grande (4100px). La largeur maximale autorisée est de 4000px.","code":"7f87163d-878f-47f5-99ba-a8eb723a1ab2"}],"detail":"image_file: La largeur de l\u0027image est trop grande (4100px). La largeur maximale autorisée est de 4000px.","hydra:title":"An error occurred","hydra:description":"image_file: La largeur de l\u0027image est trop grande (4100px). La largeur maximale autorisée est de 4000px.","type":"\/validation_errors\/7f87163d-878f-47f5-99ba-a8eb723a1ab2","title":"An error occurred"}', $this->client->getResponse()->getContent());
}

public function test_change_picture_too_small(): void
{
$user1 = UserFactory::new()->asBaseUser()->create()->_real();
$file = new UploadedFile(__DIR__ . '/fixtures/image-too-small.jpeg', 'image-too-small.jpeg');

$this->client->loginUser($user1);
$this->client->request('POST', '/api/user_profile_pictures', [], ['imageFile' => $file], [
'CONTENT_TYPE' => 'multipart/form-data',
], );
$this->assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY);
$this->assertSame('{"@id":"\/api\/validation_errors\/9afbd561-4f90-4a27-be62-1780fc43604a","@type":"ConstraintViolationList","status":422,"violations":[{"propertyPath":"image_file","message":"La largeur de l\u0027image est trop petite (200px). La largeur minimale attendue est de 450px.","code":"9afbd561-4f90-4a27-be62-1780fc43604a"}],"detail":"image_file: La largeur de l\u0027image est trop petite (200px). La largeur minimale attendue est de 450px.","hydra:title":"An error occurred","hydra:description":"image_file: La largeur de l\u0027image est trop petite (200px). La largeur minimale attendue est de 450px.","type":"\/validation_errors\/9afbd561-4f90-4a27-be62-1780fc43604a","title":"An error occurred"}', $this->client->getResponse()->getContent());
}

public function test_change_picture_not_logged(): void
{
$file = new UploadedFile(__DIR__ . '/fixtures/image-ok.jpeg', 'image-ok.jpeg');
$this->client->request('POST', '/api/user_profile_pictures', [], ['file' => $file], [
'CONTENT_TYPE' => 'multipart/form-data',
], );
$this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED);
$this->assertSame('{"code":401,"message":"JWT Token not found"}', $this->client->getResponse()->getContent());
}
}
4 changes: 2 additions & 2 deletions tests/Api/User/UserGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class UserGetTest extends ApiTestCase
use ResetDatabase, Factories;
use ApiTestAssertionsTrait;

public function test_get_self_not_logged()
public function test_get_self_not_logged(): void
{
$this->client->request('GET', '/api/users/self');
$this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED);
Expand All @@ -24,7 +24,7 @@ public function test_get_self_not_logged()
]);
}

public function test_get_self()
public function test_get_self(): void
{
$user1 = UserFactory::new()->asBaseUser()->create()->object();

Expand Down
Binary file added tests/Api/User/fixtures/image-ok.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/Api/User/fixtures/image-too-big.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/Api/User/fixtures/image-too-small.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 72591f0

Please sign in to comment.