Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DNM] Example test entity #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions DoctrineMigrations/Version20180225122246.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types = 1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20180225122246 extends AbstractMigration
{
public function up(Schema $schema): void {
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');

$this->addSql('CREATE TABLE test_entity (id UUID NOT NULL, data TEXT NOT NULL, PRIMARY KEY(id))');
}

public function down(Schema $schema): void {
$this->throwIrreversibleMigrationException('Down migrations are hell!');
}
}
46 changes: 46 additions & 0 deletions src/Entity/TestEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types = 1);

namespace App\Entity;


use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

/**
* @ORM\Entity
*/
class TestEntity
{
/**
* @var UuidInterface
*
* @ORM\Id
* @ORM\Column(type="guid", unique=true)
*/
protected $id;

/**
* @var string
*
* @ORM\Column(type="text")
*/
protected $data;

public function __construct() {
$this->id = Uuid::uuid4();
$this->data = '';
}

public function getId(): UuidInterface {
return $this->id;
}

public function getData(): string {
return $this->data;
}

public function setData(string $data): void {
$this->data = $data;
}
}
8 changes: 8 additions & 0 deletions src/Repository/TestEntityRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace App\Repository;


class TestEntityRepository
{
}
64 changes: 64 additions & 0 deletions tests/Entity/TestEntityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php declare(strict_types = 1);

namespace App\Tests\Entity;


use App\Entity\TestEntity;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\ObjectRepository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class TestEntityTest extends KernelTestCase
{
/**
* @var ObjectManager
*/
private $entityManager;

/**
* @var ObjectRepository
*/
protected $repository;

/**
* {@inheritDoc}
*/
protected function setUp() {
$kernel = self::bootKernel();

$doctrine = $kernel->getContainer()->get('doctrine');

assert($doctrine instanceof ManagerRegistry);

$this->entityManager = $doctrine->getManager();

$this->repository = $doctrine->getRepository(TestEntity::class);
}

public function testGetData(): void {
$data = 'test';

$entity = new TestEntity();

$id = $entity->getId();

$this->assertEmpty($entity->getData());

$entity->setData($data);

$this->assertEquals($data, $entity->getData());

$this->entityManager->persist($entity);
$this->entityManager->flush();
unset($entity);
$this->entityManager->clear();


$entity = $this->repository->find($id);
assert($entity instanceof TestEntity);

$this->assertEquals($data, $entity->getData());

}
}