提交 af64ef6e 编写于 作者: T teebbstudios

setUp()

上级 e4fb097a
......@@ -2,6 +2,7 @@
namespace App\Tests\IntegrationTest;
use App\Entity\Post;
use App\Factory\PostFactory;
use App\Repository\PostRepository;
use Doctrine\ORM\EntityManagerInterface;
......@@ -9,13 +10,24 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class EntityManagerTest extends KernelTestCase
{
private $entitymanager;
protected function setUp(): void
{
self::bootKernel();
$this->entitymanager = static::getContainer()->get('doctrine.orm.entity_manager');
$this->truncateEntities([
Post::class
]);
}
public function testEntityManager(): void
{
$kernel = self::bootKernel();
// $kernel = self::bootKernel();
$this->assertSame('test', $kernel->getEnvironment());
$entitymanager = static::getContainer()->get('doctrine.orm.entity_manager');
$this->assertInstanceOf(EntityManagerInterface::class, $entitymanager);
$this->assertSame('test', self::$kernel->getEnvironment());
// $entitymanager = static::getContainer()->get('doctrine.orm.entity_manager');
$this->assertInstanceOf(EntityManagerInterface::class, $this->entitymanager);
//$myCustomService = self::$container->get(CustomService::class);
$factory = static::getContainer()->get(PostFactory::class);
......@@ -25,17 +37,12 @@ class EntityManagerTest extends KernelTestCase
$post2 = $factory->create('Post title 02', 'Post Body 02');
$post3 = $factory->create('Post title 03', 'Post Body 03');
$post4 = $factory->create('Post title 04', 'Post Body 04');
$entitymanager->persist($post1);
$entitymanager->persist($post2);
$entitymanager->persist($post3);
$entitymanager->persist($post4);
$this->entitymanager->persist($post1);
$this->entitymanager->persist($post2);
$this->entitymanager->persist($post3);
$this->entitymanager->persist($post4);
$entitymanager->flush();
}
public function testEntityManagerQuery():void
{
$kernel = self::bootKernel();
$this->entitymanager->flush();
$postRepo = static::getContainer()->get(PostRepository::class);
......@@ -44,4 +51,34 @@ class EntityManagerTest extends KernelTestCase
$posts = $postRepo->findAll();
$this->assertCount(4, $posts);
}
// public function testEntityManagerQuery():void
// {
//// $kernel = self::bootKernel();
//
// $postRepo = static::getContainer()->get(PostRepository::class);
//
// $this->assertInstanceOf(PostRepository::class, $postRepo);
//
// $posts = $postRepo->findAll();
// $this->assertCount(4, $posts);
// }
private function truncateEntities(array $entities)
{
$connection = $this->entitymanager->getConnection();
$databasePlatform = $connection->getDatabasePlatform();
if ($databasePlatform->supportsForeignKeyConstraints()) {
$connection->executeQuery('SET FOREIGN_KEY_CHECKS=0');
}
foreach ($entities as $entity) {
$query = $databasePlatform->getTruncateTableSQL(
$this->entitymanager->getClassMetadata($entity)->getTableName()
);
$connection->executeQuery($query);
}
if ($databasePlatform->supportsForeignKeyConstraints()) {
$connection->executeQuery('SET FOREIGN_KEY_CHECKS=1');
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册