Skip to content

Commit

Permalink
fixup unittesting
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-Jan Bijl committed Nov 16, 2023
1 parent d1b5c80 commit f5e4389
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Prezent\Tests\Doctrine\Translatable\Mapping;
namespace Prezent\Tests\Doctrine\Translatable\EventListener;

use Prezent\Tests\Tool\ORMTestCase;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Prezent\Tests\Doctrine\Translatable\Mapping;
namespace Prezent\Tests\Doctrine\Translatable\EventListener;

use Doctrine\Persistence\Mapping\RuntimeReflectionService;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
Expand Down
192 changes: 98 additions & 94 deletions tests/Tool/ORMTestCase.php
Original file line number Diff line number Diff line change
@@ -1,94 +1,98 @@
<?php

namespace Prezent\Tests\Tool;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\PsrCachedReader;
use Doctrine\Common\EventManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver as ORMAnnotationDriver;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\Setup;
use Metadata\MetadataFactory;
use PHPUnit\Framework\TestCase;
use Prezent\Doctrine\Translatable\EventListener\TranslatableListener;
use Prezent\Doctrine\Translatable\Mapping\Driver\AnnotationDriver;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

abstract class ORMTestCase extends TestCase
{
private $em;
private $evm;
private $listener;

public function getEntityManager()
{
if (!$this->em) {
$this->em = $this->createEntityManager();
}

return $this->em;
}

public function createEntityManager()
{
$conn = array(
'driver' => 'pdo_sqlite',
'memory' => true,
);

AnnotationRegistry::registerFile(realpath(__DIR__ . '/../../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'));

$reader = new AnnotationReader();
$reader = new PsrCachedReader($reader, new ArrayAdapter());

$config = Setup::createConfiguration(true);
$config->setMetadataDriverImpl(new ORMAnnotationDriver($reader, array(__DIR__ . '/../Fixture')));

$em = EntityManager::create($conn, $config, $this->getEventManager());

$schemaTool = new SchemaTool($em);
$schemaTool->createSchema(array_map(function ($class) use ($em) {
return $em->getClassMetadata($class);
}, $this->getFixtureClasses()));

return $em;
}

public function getEventManager()
{
if (!$this->evm) {
$this->evm = $this->createEventManager();
}

return $this->evm;
}

public function createEventManager()
{
$evm = new EventManager();
$evm->addEventSubscriber($this->getTranslatableListener());

return $evm;
}

public function getTranslatableListener()
{
if (!$this->listener) {
$this->listener = $this->createTranslatableListener();
}

return $this->listener;
}

public function createTranslatableListener()
{
return new TranslatableListener(new MetadataFactory(new AnnotationDriver(new AnnotationReader())));
}

public function getFixtureClasses()
{
return array();
}
}
<?php

namespace Prezent\Tests\Tool;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\PsrCachedReader;
use Doctrine\Common\EventManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver as ORMAnnotationDriver;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\Setup;
use Metadata\MetadataFactory;
use PHPUnit\Framework\TestCase;
use Prezent\Doctrine\Translatable\EventListener\TranslatableListener;
use Prezent\Doctrine\Translatable\Mapping\Driver\AnnotationDriver;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

abstract class ORMTestCase extends TestCase
{
private $em;

private $evm;

private $listener;

public function getEntityManager()
{
if (!$this->em) {
$this->em = $this->createEntityManager();
}

return $this->em;
}

public function createEntityManager()
{
$conn = array(
'driver' => 'pdo_sqlite',
'memory' => true,
);

AnnotationRegistry::registerFile(
realpath(__DIR__ . '/../../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php')
);

$reader = new AnnotationReader();
$reader = new PsrCachedReader($reader, new ArrayAdapter());

$config = Setup::createConfiguration(true);
$config->setMetadataDriverImpl(new ORMAnnotationDriver($reader, array(__DIR__ . '/../Fixture')));

$em = EntityManager::create($conn, $config, $this->getEventManager());

$schemaTool = new SchemaTool($em);
$schemaTool->createSchema(array_map(function ($class) use ($em) {
return $em->getClassMetadata($class);
}, $this->getFixtureClasses()));

return $em;
}

public function getEventManager()
{
if (!$this->evm) {
$this->evm = $this->createEventManager();
}

return $this->evm;
}

public function createEventManager()
{
$evm = new EventManager();
$evm->addEventSubscriber($this->getTranslatableListener());

return $evm;
}

public function getTranslatableListener()
{
if (!$this->listener) {
$this->listener = $this->createTranslatableListener();
}

return $this->listener;
}

public function createTranslatableListener()
{
return new TranslatableListener(new MetadataFactory(new AnnotationDriver(new AnnotationReader())));
}

public function getFixtureClasses()
{
return array();
}
}

0 comments on commit f5e4389

Please sign in to comment.