Vous utilisez un navigateur obsolète, veuillez le mettre à jour.
Veuillez utiliser un navigateur internet moderne avec JavaScript activé pour naviguer sur OpenClassrooms.com
Une question ? Pas de panique, on va vous aider !
Bonjour,
Sur Symfony je suis en train de faire des tests
J'ai mon entité Brand
<?php namespace App\Entity; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; use App\Repository\BrandRepository; use Gedmo\Mapping\Annotation\Translatable; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Validator\Constraints as Assert; use Gedmo\Mapping\Annotation as Gedmo; #[ORM\Entity(repositoryClass: BrandRepository::class)] #[UniqueEntity(fields: ['name'], message: 'There is already an Brand with this name')] class Brand { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; #[ORM\Column(length: 255)] #[Assert\NotBlank()] #[Assert\NotNull()] #[Assert\Length(min: 2, max: 255)] #[Assert\NoSuspiciousCharacters] private ?string $name = null; #[ORM\Column(type: Types::TEXT, nullable: true)] #[Assert\NoSuspiciousCharacters] #[Assert\Length(min: 0, max: 4000)] #[Translatable] private ?string $description = null; /** * @var Collection<int, Product> */ #[ORM\OneToMany(targetEntity: Product::class, mappedBy: 'brand')] private Collection $products; /** * @var Collection<int, ProductRange> */ #[ORM\OneToMany(targetEntity: ProductRange::class, mappedBy: 'brand', orphanRemoval: true)] private Collection $productRanges; #[ORM\Column(nullable: true)] private ?bool $isPublic = null; #[ORM\Column(length: 255)] #[Gedmo\Slug(fields: ['name'])] private ?string $slug = null; #[ORM\Column] #[Gedmo\Timestampable(on: 'create')] private ?\DateTimeImmutable $createdAt = null; #[ORM\Column] #[Gedmo\Timestampable(on: 'update')] private ?\DateTimeImmutable $updatedAt = null; #[ORM\Column(length: 255, nullable: true)] #[Gedmo\Blameable(on: 'update')] private ?string $updatedBy = null;
J'ai mon config/test/doctrine.yaml
doctrine: dbal: driver: pdo_sqlite path: "%kernel.cache_dir%/test.db" url: null
Mon config/test/liip_fixtures.yaml
liip_test_fixtures: keep_database_and_schema: false cache_metadata: true
Voici mon test en WebTestCase. Je veux créer deux entités Brand pour vérifier l'unicité.
public function getEntity(): Brand { return (new Brand())->setName('Brand #1') ->setDescription("Description #1") ->setIsPublic(true); } public function testInvalidUsedBrand(): void { self::bootkernel(); $brand = $this->getEntity(); $brand2 = $this->getEntity(); $validator = static::getContainer()->get('validator'); $this->manager->persist($brand); $this->manager->flush(); $errors = $validator->validate($brand2); self::assertCount(1, $errors); }
Le problème c'est lorsque je lance mon test j'ai l'erreur ci dessous :
J'ai l'impression que le Gedmo Sluggable ne fonctionne pas avec sqllite ? Et je ne vois pas comment corriger cela ?
C'est le flush qui ne fonctionne pas correctement.
1) App\Test\Controller\BrandControllerTest::testInvalidUsedBrand Doctrine\DBAL\Exception\NotNullConstraintViolationException: An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: brand.slug C:\Users\ericl\OneDrive\Documents\1_Projet\Web\Project03\vendor\doctrine\dbal\src\Driver\API\SQLite\ExceptionConverter.php:48 C:\Users\ericl\OneDrive\Documents\1_Projet\Web\Project03\vendor\doctrine\dbal\src\Connection.php:1939 C:\Users\ericl\OneDrive\Documents\1_Projet\Web\Project03\vendor\doctrine\dbal\src\Connection.php:1881 C:\Users\ericl\OneDrive\Documents\1_Projet\Web\Project03\vendor\doctrine\dbal\src\Statement.php:194 C:\Users\ericl\OneDrive\Documents\1_Projet\Web\Project03\vendor\doctrine\dbal\src\Statement.php:249 C:\Users\ericl\OneDrive\Documents\1_Projet\Web\Project03\vendor\doctrine\orm\src\Persisters\Entity\BasicEntityPersister.php:251 C:\Users\ericl\OneDrive\Documents\1_Projet\Web\Project03\vendor\doctrine\orm\src\UnitOfWork.php:1051 C:\Users\ericl\OneDrive\Documents\1_Projet\Web\Project03\vendor\doctrine\orm\src\UnitOfWork.php:400 C:\Users\ericl\OneDrive\Documents\1_Projet\Web\Project03\vendor\doctrine\orm\src\EntityManager.php:264 C:\Users\ericl\OneDrive\Documents\1_Projet\Web\Project03\tests\Controller\BrandControllerTest.php:69 C:\Users\ericl\OneDrive\Documents\1_Projet\Web\Project03\vendor\phpunit\phpunit\phpunit:107
J'ai testé avec le framework Liip/Test-FixtureBundle couplé au Alice bundle comme j'ai vu sur d'autres tuto mais j'ai la même erreur
public function testToto(): void { $this->databaseTool->loadAliceFixture( [__DIR__ . '/fixtures/brand.yaml'] ); $container = static::getContainer(); $nbBrands = $container>get(BrandRepository::class)->count([]); self::assertEquals(11, $nbBrands); }
Si quelqu'un a déjà eu le problème ca m'aiderai énormément pour mes tests.
Merci
Je m'étais trompé sur la correction d'une dépréciation donc ça ne fonctionnait pas. En mettant le code correct c'est bon.
Sujet clos
-Edité par EricLtr 17 janvier 2025 à 17:03:35
Vous pouvez rédiger votre message en Markdown ou en HTML uniquement.