Skip to content

Commit 4d83fb7

Browse files
committed
Example test entity
1 parent 1a168b6 commit 4d83fb7

4 files changed

Lines changed: 141 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace DoctrineMigrations;
4+
5+
use Doctrine\DBAL\Migrations\AbstractMigration;
6+
use Doctrine\DBAL\Schema\Schema;
7+
8+
/**
9+
* Auto-generated Migration: Please modify to your needs!
10+
*/
11+
class Version20180225122246 extends AbstractMigration
12+
{
13+
public function up(Schema $schema): void {
14+
// this up() migration is auto-generated, please modify it to your needs
15+
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
16+
17+
$this->addSql('CREATE TABLE test_entity (id UUID NOT NULL, data TEXT NOT NULL, PRIMARY KEY(id))');
18+
}
19+
20+
public function down(Schema $schema): void {
21+
$this->throwIrreversibleMigrationException('Down migrations are hell!');
22+
}
23+
}

src/Entity/TestEntity.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace App\Entity;
4+
5+
6+
use Doctrine\ORM\Mapping as ORM;
7+
use Ramsey\Uuid\Uuid;
8+
use Ramsey\Uuid\UuidInterface;
9+
10+
/**
11+
* @ORM\Entity
12+
*/
13+
class TestEntity
14+
{
15+
/**
16+
* @var UuidInterface
17+
*
18+
* @ORM\Id
19+
* @ORM\Column(type="guid", unique=true)
20+
*/
21+
protected $id;
22+
23+
/**
24+
* @var string
25+
*
26+
* @ORM\Column(type="text")
27+
*/
28+
protected $data;
29+
30+
public function __construct() {
31+
$this->id = Uuid::uuid4();
32+
$this->data = '';
33+
}
34+
35+
public function getId(): UuidInterface {
36+
return $this->id;
37+
}
38+
39+
public function getData(): string {
40+
return $this->data;
41+
}
42+
43+
public function setData(string $data): void {
44+
$this->data = $data;
45+
}
46+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace App\Repository;
4+
5+
6+
class TestEntityRepository
7+
{
8+
}

tests/Entity/TestEntityTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace App\Tests\Entity;
4+
5+
6+
use App\Entity\TestEntity;
7+
use Doctrine\Common\Persistence\ManagerRegistry;
8+
use Doctrine\Common\Persistence\ObjectManager;
9+
use Doctrine\Common\Persistence\ObjectRepository;
10+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
11+
12+
class TestEntityTest extends KernelTestCase
13+
{
14+
/**
15+
* @var ObjectManager
16+
*/
17+
private $entityManager;
18+
19+
/**
20+
* @var ObjectRepository
21+
*/
22+
protected $repository;
23+
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
protected function setUp() {
28+
$kernel = self::bootKernel();
29+
30+
$doctrine = $kernel->getContainer()->get('doctrine');
31+
32+
assert($doctrine instanceof ManagerRegistry);
33+
34+
$this->entityManager = $doctrine->getManager();
35+
36+
$this->repository = $doctrine->getRepository(TestEntity::class);
37+
}
38+
39+
public function testGetData(): void {
40+
$data = 'test';
41+
42+
$entity = new TestEntity();
43+
44+
$id = $entity->getId();
45+
46+
$this->assertEmpty($entity->getData());
47+
48+
$entity->setData($data);
49+
50+
$this->assertEquals($data, $entity->getData());
51+
52+
$this->entityManager->persist($entity);
53+
$this->entityManager->flush();
54+
unset($entity);
55+
$this->entityManager->clear();
56+
57+
58+
$entity = $this->repository->find($id);
59+
assert($entity instanceof TestEntity);
60+
61+
$this->assertEquals($data, $entity->getData());
62+
63+
}
64+
}

0 commit comments

Comments
 (0)