Skip to content

Commit

Permalink
Merge pull request #7 from ipublikuj/drop-kdyby
Browse files Browse the repository at this point in the history
Feature: Dropping kdyby/doctrine
  • Loading branch information
akadlec authored Nov 10, 2019
2 parents 9af66f8 + 142061b commit c779591
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 55 deletions.
21 changes: 21 additions & 0 deletions .travis.composer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

$version = getenv('NETTE');

if (!$version || $version == 'default') {
exit;
}

echo "Nette version " . $version . PHP_EOL;

$file = __DIR__ . '/composer.json';

$content = file_get_contents($file);

$composer = json_decode($content, TRUE);

$composer['require']['nette/di'] = $version;

$content = json_encode($composer);

file_put_contents($file, $content);
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ before_install:
- composer self-update

before_script:
- php .travis.composer.php
- composer install --no-interaction --prefer-source --dev

script:
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
"require" : {
"php" : ">=7.2.0",

"doctrine/orm" : "~2.5",

"nette/di" : "~2.4 || ~3.0",
"nette/utils" : "~2.5 || ~3.0",

Expand All @@ -63,7 +65,7 @@

"pds/skeleton" : "~1.0",

"kdyby/doctrine" : "^3.3"
"nettrine/orm" : "~0.3"
},

"autoload" : {
Expand Down
2 changes: 1 addition & 1 deletion src/IPub/DoctrinePhone/DI/DoctrinePhoneExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function beforeCompile()

$builder = $this->getContainerBuilder();

$builder->getDefinition($builder->getByType('Doctrine\ORM\EntityManagerInterface') ?: 'doctrine.default.entityManager')
$builder->getDefinition($builder->getByType('Doctrine\ORM\EntityManagerInterface', TRUE))
->addSetup('?->getEventManager()->addEventSubscriber(?)', ['@self', $builder->getDefinition($this->prefix('subscriber'))]);
}

Expand Down
15 changes: 4 additions & 11 deletions src/IPub/DoctrinePhone/Events/PhoneObjectSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ final class PhoneObjectSubscriber implements Common\EventSubscriber
*/
private $annotationReader;

/**
* @var Common\Persistence\ManagerRegistry
*/
private $managerRegistry;

/**
* @var Phone\Phone
*/
Expand Down Expand Up @@ -81,11 +76,9 @@ public function getSubscribedEvents()
*/
public function __construct(
Common\Annotations\Reader $annotationReader,
Common\Persistence\ManagerRegistry $managerRegistry,
Phone\Phone $phoneHelper
)
{
$this->managerRegistry = $managerRegistry;
$this->annotationReader = $annotationReader;

$this->phoneHelper = $phoneHelper;
Expand Down Expand Up @@ -162,7 +155,7 @@ private function postLoadAndPreFlush(
) : void {
$cache = $objectManager->getMetadataFactory()->getCacheDriver();

if (!$fieldsMap = $this->getEntityPhoneFields($entity, $cache)) {
if (!$fieldsMap = $this->getEntityPhoneFields($entity, $cache, $objectManager)) {
return;
}

Expand All @@ -189,9 +182,9 @@ private function postLoadAndPreFlush(
private function getEntityPhoneFields(
$entity,
Common\Cache\CacheProvider $cache,
Common\Persistence\Mapping\ClassMetadata $class = NULL
Common\Persistence\ObjectManager $objectManager
) : array {
$class = $class ?: $this->managerRegistry->getManager()->getClassMetadata(get_class($entity));
$class = $objectManager->getClassMetadata(get_class($entity));

if (isset($this->phoneFieldsCache[$class->getName()])) {
return $this->phoneFieldsCache[$class->getName()];
Expand All @@ -211,7 +204,7 @@ private function getEntityPhoneFields(
foreach ($phoneFields as $phoneField => $mapping) {
if (!isset($fieldsMap[$mapping['phoneFieldClass']])) {
$fieldsMap[$mapping['phoneFieldClass']] = [
'class' => $this->managerRegistry->getManager()->getClassMetadata($mapping['phoneFieldClass']),
'class' => $objectManager->getClassMetadata($mapping['phoneFieldClass']),
'fields' => [$phoneField],
];

Expand Down
6 changes: 4 additions & 2 deletions tests/IPubTests/DoctrinePhone/ExtensionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace IPubTests\DoctrinePhone;

use Nette;

use Doctrine\DBAL;

use Tester;
use Tester\Assert;

Expand All @@ -45,8 +47,8 @@ class ExtensionTest extends Tester\TestCase
{
$dic = $this->createContainer();

/** @var \Kdyby\Doctrine\Connection $connection */
$connection = $dic->getByType('Kdyby\Doctrine\Connection');
/** @var \Doctrine\DBAL\Connection $connection */
$connection = $dic->getByType(DBAL\Connection::class);
$connection->connect(); // initializes the types

Assert::true(Type::getType('phone') instanceof Types\Phone);
Expand Down
32 changes: 17 additions & 15 deletions tests/IPubTests/DoctrinePhone/HydrationListenerTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use Nette;

use Doctrine\ORM;

use Nettrine;

use Tester;
use Tester\Assert;

Expand Down Expand Up @@ -50,7 +52,7 @@ class HydrationListenerTest extends Tester\TestCase
private $container;

/**
* @var ORM\EntityManager
* @var Nettrine\ORM\EntityManagerDecorator
*/
private $em;

Expand All @@ -67,8 +69,8 @@ class HydrationListenerTest extends Tester\TestCase
parent::setUp();

$this->container = $this->createContainer();
$this->em = $this->container->getByType('Kdyby\Doctrine\EntityManager');
$this->listener = $this->container->getByType('IPub\DoctrinePhone\Events\PhoneObjectSubscriber');
$this->em = $this->container->getByType(Nettrine\ORM\EntityManagerDecorator::class);
$this->listener = $this->container->getByType(Events\PhoneObjectSubscriber::class);
}

/**
Expand All @@ -77,8 +79,8 @@ class HydrationListenerTest extends Tester\TestCase
public function dataEntityClasses() : array
{
return [
[AddressEntity::getClassName()],
[SpecificAddressEntity::getClassName()],
[AddressEntity::class],
[SpecificAddressEntity::class],
];
}

Expand All @@ -87,14 +89,6 @@ class HydrationListenerTest extends Tester\TestCase
*/
public function testFunctional($className)
{
$class = $this->em->getClassMetadata($className);

// assert that listener was binded to entity
Assert::same([
ORM\Events::postLoad => [['class' => 'IPub\\DoctrinePhone\\Events\\PhoneObjectSubscriber', 'method' => 'postLoad']],
ORM\Events::preFlush => [['class' => 'IPub\\DoctrinePhone\\Events\\PhoneObjectSubscriber', 'method' => ORM\Events::preFlush]],
], $class->entityListeners);

$this->generateDbSchema();

// Test phone hydration
Expand All @@ -106,6 +100,14 @@ class HydrationListenerTest extends Tester\TestCase
$address = $this->em->find($className, 1);

Assert::equal(Phone\Entities\Phone::fromNumber('+420234567890'), $address->getPhone());

$class = $this->em->getClassMetadata($className);

// assert that listener was binded to entity
Assert::same([
ORM\Events::postLoad => [['class' => 'IPub\\DoctrinePhone\\Events\\PhoneObjectSubscriber', 'method' => ORM\Events::postLoad]],
ORM\Events::preFlush => [['class' => 'IPub\\DoctrinePhone\\Events\\PhoneObjectSubscriber', 'method' => ORM\Events::preFlush]],
], $class->entityListeners);
}

/**
Expand Down Expand Up @@ -138,14 +140,14 @@ class HydrationListenerTest extends Tester\TestCase
$this->em->clear();

/** @var AddressEntity $order */
$address = $this->em->find(AddressEntity::getClassName(), 1);
$address = $this->em->find(AddressEntity::class, 1);

Assert::equal(Phone\Entities\Phone::fromNumber('+420234567890'), $address->getPhone());

// Following loading should not fail
$address2 = $this->em->createQueryBuilder()
->select('a')
->from(AddressEntity::getClassName(), 'a')
->from(AddressEntity::class, 'a')
->where('a.id = :id')->setParameter('id', 1)
->getQuery()->getSingleResult();

Expand Down
6 changes: 3 additions & 3 deletions tests/IPubTests/DoctrinePhone/files/address.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
doctrine:
metadata:
IPubTests\DoctrinePhone: %appDir%IPubTests/DoctrinePhone/models
orm.annotations:
paths:
- %appDir%IPubTests/DoctrinePhone/models
32 changes: 13 additions & 19 deletions tests/IPubTests/DoctrinePhone/files/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,20 @@ php:
date.timezone: Europe/Prague

extensions:
annotations: Kdyby\Annotations\DI\AnnotationsExtension
doctrine: Kdyby\Doctrine\DI\OrmExtension
console: Kdyby\Console\DI\ConsoleExtension
events: Kdyby\Events\DI\EventsExtension
dbal: Nettrine\DBAL\DI\DbalExtension
orm: Nettrine\ORM\DI\OrmExtension
orm.annotations: Nettrine\ORM\DI\OrmAnnotationsExtension
orm.cache: Nettrine\ORM\DI\OrmCacheExtension
phone: IPub\Phone\DI\PhoneExtension

services:
cacheStorage:
class: Nette\Caching\Storages\MemoryStorage
dbal:
connection:
host: 127.0.0.1
driver: pdo_sqlite
memory: true

annotations:
cache: array
orm.annotations:
defaultCache: array

console:
url: http://example.com

doctrine:
metadataCache: array
queryCache: array
resultCache: array
hydrationCache: array
driver: pdo_sqlite
memory: true
orm.cache:
defaultDriver: array
4 changes: 1 addition & 3 deletions tests/IPubTests/DoctrinePhone/models/address.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

use Doctrine\ORM\Mapping as ORM;

use Kdyby\Doctrine\Entities\BaseEntity;

use IPub\Phone;

/**
Expand All @@ -33,7 +31,7 @@
* "specific" = "SpecificAddressEntity",
* })
*/
class AddressEntity extends BaseEntity
class AddressEntity
{
/**
* @ORM\Id()
Expand Down

0 comments on commit c779591

Please sign in to comment.