Test if a Doctrine entity is already persisted in the database
Published on 2018-09-11 • Modified on 2018-09-23
This code is called from an action function of a Symfony 4.1 standard controller (extending Symfony\Bundle\FrameworkBundle\Controller\Controller
). If you want to test if an entity is modified, check out this snippet.
<?php
declare(strict_types=1);
namespace App\Controller\Snippet;
use App\Entity\Article;
use App\Entity\ArticleType;
use App\Repository\ArticleTypeRepository;
use Doctrine\Bundle\DoctrineBundle\Registry;
/**
* I am using a PHP trait to isolate each snippet in a file.
* This code should be called from a Symfony controller extending AbstractController (as of Symfony 4.2)
* or Symfony\Bundle\FrameworkBundle\Controller\Controller (Symfony <= 4.1).
* Services are injected in the main controller constructor..
*/
trait Snippet2Trait
{
public function snippet2(): void
{
// Get doctrine manager (From a Symfony >4.2 controller)
$doctrine = $this->getDoctrine();
if (!$doctrine instanceof Registry) {
throw new \RuntimeException('Houston, We Have a Problem. 💥');
}
$manager = $doctrine->getManager();
$article = new Article(); // create a fresh Doctrine object
$isPersisted = $manager->contains($article);
echo $isPersisted ? 'true' : 'false'; // returns false
// Set minimum database constraints so the entity can be persisted
$repo = $manager->getRepository(ArticleType::class);
if (!$repo instanceof ArticleTypeRepository) {
throw new \RuntimeException('Doctrine repository not found.');
}
$article->setType($repo->getArticleType());
$article->setActive(false);
// $manager->persist($article); // persist in database
// (I will not persist because I don't want to pollute my database
// but you've got the spirit!)
$manager->flush();
$isPersisted = $manager->contains($article);
echo PHP_EOL;
echo $isPersisted ? 'true' : 'false'; // would returns true without the line commented.
// That's it! 😁
}
}
Run this snippet ≪ this.showUnitTest ? this.trans.hide_unit_test : this.trans.show_unit_test ≫ More on Stackoverflow Random snippet
<?php
declare(strict_types=1);
namespace App\Tests\Controller\Snippets;
use App\Entity\Article;
use App\Entity\ArticleType;
use App\Repository\ArticleTypeRepository;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @covers Snippet2Trait
*/
final class Snippet2Test extends KernelTestCase
{
private Registry $doctrine;
protected function setUp(): void
{
self::bootKernel();
$this->doctrine = self::$container->get('doctrine');
}
/**
* @covers Snippet2Trait::snippet2
*/
public function testSnippet2(): void
{
$projectDir = self::$container->getParameter('kernel.project_dir');
self::assertFileExists($projectDir.'/src/Controller/Snippet/Snippet2Trait.php');
$manager = $this->doctrine->getManager();
$article = new Article();
self::assertFalse($manager->contains($article));
$repo = $manager->getRepository(ArticleType::class);
if (!$repo instanceof ArticleTypeRepository) {
throw new \RuntimeException('Doctrine repository not found.');
}
$article->setType($repo->getArticleType());
$article->setActive(false);
$manager->persist($article);
$manager->flush();
self::assertTrue($manager->contains($article));
$manager->remove($article);
$manager->flush();
self::assertFalse($manager->contains($article));
}
}