Skip to content

Commit

Permalink
test: add provider for test article and categories
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-brtrd committed Aug 1, 2023
1 parent eefab78 commit 24fc701
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 38 deletions.
36 changes: 21 additions & 15 deletions src/Entity/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@

namespace App\Entity;

use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use App\Api\Controller\Articles\ArticleCreateController;
use App\Repository\ArticleRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use ApiPlatform\Metadata\Delete;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\ArticleRepository;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Context;
use ApiPlatform\Metadata\GetCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Annotation\Context;
use Symfony\Component\Validator\Constraints as Assert;
use App\Api\Controller\Articles\ArticleCreateController;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;

#[ApiResource(
operations: [
Expand Down Expand Up @@ -155,6 +155,12 @@ class Article
*/
#[ORM\Column(length: 150, unique: true)]
#[Groups(['comment:list', 'article:list', 'article:post', 'article:put'])]
#[Assert\Length(
min: 3,
max: 150,
minMessage: 'Le titre de l\'article ne peut être inférieur à {{ limit }} caractères.',
maxMessage: 'Le titre de l\'article ne peut être supérieur à {{ limit }} caractères.'
)]
private ?string $titre = null;

/**
Expand Down
21 changes: 14 additions & 7 deletions src/Entity/Categorie.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Delete;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use App\Repository\CategorieRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

#[ApiResource(
operations: [
Expand Down Expand Up @@ -104,6 +105,12 @@ class Categorie
*/
#[ORM\Column(length: 100)]
#[Groups(['article:list', 'tags:list', 'tags:post'])]
#[Assert\Length(
min: 3,
max: 100,
minMessage: 'user.title.length.min',
maxMessage: 'user.title.length.max'
)]
private ?string $titre = null;

/**
Expand Down
46 changes: 38 additions & 8 deletions tests/Entity/ArticleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace App\Tests\Entity;

use App\Entity\Article;
use App\Repository\ArticleRepository;
use App\Repository\CategorieRepository;
use App\Repository\UserRepository;
use App\Tests\Utils\AssertTestTrait;
use Liip\TestFixturesBundle\Services\DatabaseToolCollection;
use App\Repository\ArticleRepository;
use App\Repository\CategorieRepository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Liip\TestFixturesBundle\Services\DatabaseToolCollection;

class ArticleTest extends KernelTestCase
{
Expand All @@ -27,9 +27,9 @@ public function testRepositoryCount()
{
$articles = $this->databaseTool->loadAliceFixture(
[
\dirname(__DIR__).'/Fixtures/UserTestFixtures.yaml',
\dirname(__DIR__).'/Fixtures/ArticleTestFixtures.yaml',
\dirname(__DIR__).'/Fixtures/TagTestFixtures.yaml',
\dirname(__DIR__) . '/Fixtures/UserTestFixtures.yaml',
\dirname(__DIR__) . '/Fixtures/ArticleTestFixtures.yaml',
\dirname(__DIR__) . '/Fixtures/TagTestFixtures.yaml',
]
);

Expand Down Expand Up @@ -64,14 +64,44 @@ public function testNonUniqueTitreEntity()
$this->assertHasErrors($article, 1);
}

public function testMinLentgthContentEntity()
/**
* @dataProvider provideLengthTitre
* @return void
*/
public function testLengthTitle(string $title): void
{
$article = $this->getEntity()
->setTitre($title);

$this->assertHasErrors($article, 1);
}

private function provideLengthTitre(): array
{
return [
'min' => ['title' => str_repeat('a', 2)],
'max' => ['title' => str_repeat('a', 151)],
];
}

/**
* @dataProvider provideLengthContent
*/
public function testLentgthContentEntity(string $content): void
{
$article = $this->getEntity()
->setContent('Desc');
->setContent($content);

$this->assertHasErrors($article, 1);
}

private function provideLengthContent(): array
{
return [
'min' => ['content' => str_repeat('a', 2)]
];
}

protected function tearDown(): void
{
parent::tearDown();
Expand Down
31 changes: 27 additions & 4 deletions tests/Entity/CategorieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace App\Tests\Entity;

use App\Entity\Categorie;
use App\Tests\Utils\AssertTestTrait;
use App\Repository\ArticleRepository;
use App\Repository\CategorieRepository;
use App\Tests\Utils\AssertTestTrait;
use Liip\TestFixturesBundle\Services\DatabaseToolCollection;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Liip\TestFixturesBundle\Services\DatabaseToolCollection;

class CategorieTest extends KernelTestCase
{
Expand All @@ -26,8 +26,8 @@ public function testRepositoryCount()
{
$tags = $this->databaseTool->loadAliceFixture(
[
\dirname(__DIR__).'/Fixtures/ArticleTestFixtures.yaml',
\dirname(__DIR__).'/Fixtures/TagTestFixtures.yaml',
\dirname(__DIR__) . '/Fixtures/ArticleTestFixtures.yaml',
\dirname(__DIR__) . '/Fixtures/TagTestFixtures.yaml',
]
);

Expand Down Expand Up @@ -59,6 +59,29 @@ public function testNonUniqueTitreEntity()
$this->assertHasErrors($article, 1);
}

/**
* @dataProvider provideTitleLength
* @return void
*/
public function testTitleLength(string $title): void
{
$article = $this->getEntity()
->setTitre($title);

$this->assertHasErrors($article, 1);
}

/**
* @return array
*/
private function provideTitleLength(): array
{
return [
'min' => ['title' => str_repeat('a', 2)],
'max' => ['title' => str_repeat('a', 101)],
];
}

protected function tearDown(): void
{
parent::tearDown();
Expand Down
8 changes: 4 additions & 4 deletions tests/Panther/ArticlePantherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace App\Tests\Panther;

use Facebook\WebDriver\WebDriverBy;
use Liip\TestFixturesBundle\Services\DatabaseToolCollection;
use Symfony\Component\Panther\PantherTestCase;
use Liip\TestFixturesBundle\Services\DatabaseToolCollection;

class ArticlePantherTest extends PantherTestCase
{
Expand All @@ -18,9 +18,9 @@ protected function setUp(): void

$this->databaseTool = self::getContainer()->get(DatabaseToolCollection::class)->get();
$this->databaseTool->loadAliceFixture([
\dirname(__DIR__).'/Fixtures/UserTestFixtures.yaml',
\dirname(__DIR__).'/Fixtures/ArticleTestFixtures.yaml',
\dirname(__DIR__).'/Fixtures/TagTestFixtures.yaml',
\dirname(__DIR__) . '/Fixtures/UserTestFixtures.yaml',
\dirname(__DIR__) . '/Fixtures/ArticleTestFixtures.yaml',
\dirname(__DIR__) . '/Fixtures/TagTestFixtures.yaml',
]);
}

Expand Down
6 changes: 6 additions & 0 deletions translations/Français/validators.fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ form:
not_blank: Entrez un mot de passe
min_length: Votre mot de passe doit faire plus de {{ limit }} caractères
no_match: Les mot de passe ne correspondent pas.

tag:
title:
length:
min: Le titre doit contenir au moins {{ limit }} caractères
max: Le titre doit contenir au maximum {{ limit }} caractères

0 comments on commit 24fc701

Please sign in to comment.