Skip to content

Commit

Permalink
CS Fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
KoukiFactori committed Feb 15, 2023
1 parent 9dcc6af commit 2fe2d0a
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 196 deletions.
14 changes: 7 additions & 7 deletions src/Entity/Rating.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace App\Entity;

use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Post;
use Doctrine\DBAL\Types\Types;
use ApiPlatform\Metadata\Patch;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\RatingRepository;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use App\Repository\RatingRepository;
use App\Validator\IsAuthenticatedUser;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity(repositoryClass: RatingRepository::class)]
#[UniqueEntity(fields: ['user', 'bookmark'])]
Expand Down
15 changes: 9 additions & 6 deletions src/Validator/IsAuthenticatedUserValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@
namespace App\Validator;

use App\Entity\User;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class IsAuthenticatedUserValidator extends ConstraintValidator
{
public function __construct(private Security $security) { }
public function __construct(private Security $security)
{
}

/**
* @param User $value
* @param User $value
* @param IsAuthenticatedUser $constraint
* */
public function validate($value, Constraint $constraint)
{

{
if (null === $value || '' === $value) {
return;
}

if ($value == $this->security->getUser()) return;
if ($value == $this->security->getUser()) {
return;
}

// TODO: implement the validation here
$this->context->buildViolation($constraint->message)
Expand Down
114 changes: 57 additions & 57 deletions tests/Api/Rating/RatingCreateCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,70 @@

class RatingCreateCest
{
public function postRatingAnonymous(ApiTester $I): void
{
BookmarkFactory::createOne();
UserFactory::createOne(['login' => 'user1', 'password' => 'test']);

$I->sendPost('/api/ratings', [
'user' => '/api/users/1',
'bookmark' => '/api/bookmarks/1',
'value' => 5
]);
public function postRatingAnonymous(ApiTester $I): void
{
BookmarkFactory::createOne();
UserFactory::createOne(['login' => 'user1', 'password' => 'test']);

$I->seeResponseCodeIs(401);
}
$I->sendPost('/api/ratings', [
'user' => '/api/users/1',
'bookmark' => '/api/bookmarks/1',
'value' => 5,
]);

public function postRatingAuthenticated(ApiTester $I): void
{
$bookmark = BookmarkFactory::createOne();
$user = UserFactory::createOne(['login' => 'user1', 'password' => 'test'])->object();
$I->amLoggedInAs($user);
$I->seeResponseCodeIs(401);
}

$I->sendPost('/api/ratings', [
'user' => '/api/users/' . $user->getId(),
'bookmark' => '/api/bookmarks/' . $bookmark->getId(),
'value' => 5
]);
public function postRatingAuthenticated(ApiTester $I): void
{
$bookmark = BookmarkFactory::createOne();
$user = UserFactory::createOne(['login' => 'user1', 'password' => 'test'])->object();
$I->amLoggedInAs($user);

$I->seeResponseCodeIs(201);
$I->seeResponseIsJson();
}
$I->sendPost('/api/ratings', [
'user' => '/api/users/'.$user->getId(),
'bookmark' => '/api/bookmarks/'.$bookmark->getId(),
'value' => 5,
]);

public function postRatingAgain(ApiTester $I): void
{
$bookmark = BookmarkFactory::createOne();
$user = UserFactory::createOne(['login' => 'user1', 'password' => 'test'])->object();
$I->amLoggedInAs($user);
$I->seeResponseCodeIs(201);
$I->seeResponseIsJson();
}

$I->sendPost('/api/ratings', [
'user' => '/api/users/' . $user->getId(),
'bookmark' => '/api/bookmarks/' . $bookmark->getId(),
'value' => 5
]);
$I->seeResponseCodeIs(201);
public function postRatingAgain(ApiTester $I): void
{
$bookmark = BookmarkFactory::createOne();
$user = UserFactory::createOne(['login' => 'user1', 'password' => 'test'])->object();
$I->amLoggedInAs($user);

$I->sendPost('/api/ratings', [
'user' => '/api/users/' . $user->getId(),
'bookmark' => '/api/bookmarks/' . $bookmark->getId(),
'value' => 5
]);
$I->seeResponseCodeIs(422);
}
$I->sendPost('/api/ratings', [
'user' => '/api/users/'.$user->getId(),
'bookmark' => '/api/bookmarks/'.$bookmark->getId(),
'value' => 5,
]);
$I->seeResponseCodeIs(201);

public function postRatingForSomeoneElse(ApiTester $I): void
{
$target = UserFactory::createOne(); //User we want to use for post
$I->sendPost('/api/ratings', [
'user' => '/api/users/'.$user->getId(),
'bookmark' => '/api/bookmarks/'.$bookmark->getId(),
'value' => 5,
]);
$I->seeResponseCodeIs(422);
}

$bookmark = BookmarkFactory::createOne();
$user = UserFactory::createOne(['login' => 'user1', 'password' => 'test'])->object();
$I->amLoggedInAs($user);
public function postRatingForSomeoneElse(ApiTester $I): void
{
$target = UserFactory::createOne(); // User we want to use for post

$I->sendPost('/api/ratings', [
'user' => '/api/users/' . $target->getId(),
'bookmark' => '/api/bookmarks/' . $bookmark->getId(),
'value' => 5
]);
$I->seeResponseCodeIs(422);
}
}
$bookmark = BookmarkFactory::createOne();
$user = UserFactory::createOne(['login' => 'user1', 'password' => 'test'])->object();
$I->amLoggedInAs($user);

$I->sendPost('/api/ratings', [
'user' => '/api/users/'.$target->getId(),
'bookmark' => '/api/bookmarks/'.$bookmark->getId(),
'value' => 5,
]);
$I->seeResponseCodeIs(422);
}
}
63 changes: 31 additions & 32 deletions tests/Api/Rating/RatingDeleteCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,54 @@

namespace App\Tests\Api\Rating;

use App\Entity\Rating;
use App\Factory\BookmarkFactory;
use App\Factory\RatingFactory;
use App\Factory\UserFactory;
use App\Tests\Support\ApiTester;

class RatingDeleteCest
{
public function deleteRatingAnonymous(ApiTester $I)
{
$bookmark = BookmarkFactory::createOne();
$user = UserFactory::createOne();
public function deleteRatingAnonymous(ApiTester $I)
{
$bookmark = BookmarkFactory::createOne();
$user = UserFactory::createOne();

RatingFactory::createOne([
'user' => $user,
'bookmark' => $bookmark
]);
RatingFactory::createOne([
'user' => $user,
'bookmark' => $bookmark,
]);

$I->sendDelete('/api/ratings/1');
$I->sendDelete('/api/ratings/1');

$I->seeResponseCodeIs(401);
}
$I->seeResponseCodeIs(401);
}

public function deleteRatingNotOwning(ApiTester $I)
{
RatingFactory::createOne();
public function deleteRatingNotOwning(ApiTester $I)
{
RatingFactory::createOne();

$user = UserFactory::createOne(['login' => 'user1'])->object();
$I->amLoggedInAs($user);
$user = UserFactory::createOne(['login' => 'user1'])->object();
$I->amLoggedInAs($user);

$I->sendDelete('/api/ratings/1');
$I->sendDelete('/api/ratings/1');

$I->seeResponseCodeIs(403);
}
$I->seeResponseCodeIs(403);
}

public function putRatingOwning(ApiTester $I)
{
$bookmark = BookmarkFactory::createOne();
public function putRatingOwning(ApiTester $I)
{
$bookmark = BookmarkFactory::createOne();

$user = UserFactory::createOne(['login' => 'user1'])->object();
$I->amLoggedInAs($user);
$user = UserFactory::createOne(['login' => 'user1'])->object();
$I->amLoggedInAs($user);

RatingFactory::createOne([
'user' => $user,
'bookmark' => $bookmark
]);
RatingFactory::createOne([
'user' => $user,
'bookmark' => $bookmark,
]);

$I->sendDelete('/api/ratings/1');
$I->sendDelete('/api/ratings/1');

$I->seeResponseCodeIs(204);
}
$I->seeResponseCodeIs(204);
}
}
85 changes: 42 additions & 43 deletions tests/Api/Rating/RatingPatchCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,66 @@

namespace App\Tests\Api\Rating;

use App\Entity\Bookmark;
use App\Factory\BookmarkFactory;
use App\Factory\RatingFactory;
use App\Factory\UserFactory;
use App\Tests\Support\ApiTester;

class RatingPatchCest
{
public function patchRatingAnonymous(ApiTester $I)
{
$bookmark = BookmarkFactory::createOne();
$user = UserFactory::createOne();
public function patchRatingAnonymous(ApiTester $I)
{
$bookmark = BookmarkFactory::createOne();
$user = UserFactory::createOne();

RatingFactory::createOne([
'user' => $user,
'bookmark' => $bookmark
]);
RatingFactory::createOne([
'user' => $user,
'bookmark' => $bookmark,
]);

$I->sendPatch('/api/ratings/1', [
'value' => 3
]);
$I->sendPatch('/api/ratings/1', [
'value' => 3,
]);

$I->seeResponseCodeIs(401);
}
$I->seeResponseCodeIs(401);
}

public function patchRatingNotOwning(ApiTester $I)
{
$bookmark = BookmarkFactory::createOne();
$owner = UserFactory::createOne();
public function patchRatingNotOwning(ApiTester $I)
{
$bookmark = BookmarkFactory::createOne();
$owner = UserFactory::createOne();

RatingFactory::createOne([
'user' => $owner,
'bookmark' => $bookmark
]);
RatingFactory::createOne([
'user' => $owner,
'bookmark' => $bookmark,
]);

$user = UserFactory::createOne(['login' => 'user1'])->object();
$I->amLoggedInAs($user);
$user = UserFactory::createOne(['login' => 'user1'])->object();
$I->amLoggedInAs($user);

$I->sendPatch('/api/ratings/' . $bookmark->getId(), [
'value' => 3
]);
$I->sendPatch('/api/ratings/'.$bookmark->getId(), [
'value' => 3,
]);

$I->seeResponseCodeIs(403);
}
$I->seeResponseCodeIs(403);
}

public function patchRatingOwning(ApiTester $I)
{
$bookmark = BookmarkFactory::createOne();
public function patchRatingOwning(ApiTester $I)
{
$bookmark = BookmarkFactory::createOne();

$user = UserFactory::createOne(['login' => 'user1'])->object();
$I->amLoggedInAs($user);
$user = UserFactory::createOne(['login' => 'user1'])->object();
$I->amLoggedInAs($user);

RatingFactory::createOne([
'user' => $user,
'bookmark' => $bookmark
]);
RatingFactory::createOne([
'user' => $user,
'bookmark' => $bookmark,
]);

$I->sendPatch('/api/ratings/' . $bookmark->getId(), [
'value' => 3
]);
$I->sendPatch('/api/ratings/'.$bookmark->getId(), [
'value' => 3,
]);

$I->seeResponseCodeIs(200);
}
$I->seeResponseCodeIs(200);
}
}
Loading

0 comments on commit 2fe2d0a

Please sign in to comment.