Skip to content

Commit

Permalink
Merge 73711c8 into 34546e2
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Jul 31, 2019
2 parents 34546e2 + 73711c8 commit 251cccd
Show file tree
Hide file tree
Showing 21 changed files with 83 additions and 105 deletions.
9 changes: 6 additions & 3 deletions phpstan.neon
@@ -1,6 +1,3 @@
includes:
- vendor-bin/phpstan/vendor/jangregor/phpstan-prophecy/src/extension.neon

parameters:
autoload_files:
- vendor-bin/test/vendor/autoload.php
Expand All @@ -11,5 +8,11 @@ parameters:

# Symfony DI
- '#Cannot call method end\(\) on Symfony\\Component\\Config\\Definition\\Builder\\NodeParentInterface\|null.#'
- "/Call to function method_exists.. with 'Symfony.+' and 'getRootNode' will always evaluate to false./"

# PHPUnit
-
message: '#Property .*::\$.* has no typehint specified.#'
path: tests/

- '#Core23\\Doctrine\\Tests\\Fixtures\\DemoEntityManager::__construct\(\) does not call parent constructor from Sonata\\Doctrine\\Model\\BaseManager.#'
2 changes: 1 addition & 1 deletion src/Bridge/Symfony/DependencyInjection/Configuration.php
Expand Up @@ -22,7 +22,7 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder('core23_doctrine');

// Keep compatibility with symfony/config < 4.2
if (!method_exists($treeBuilder, 'getRootNode')) {
if (!method_exists(TreeBuilder::class, 'getRootNode')) {
$rootNode = $treeBuilder->root('core23_doctrine');
} else {
$rootNode = $treeBuilder->getRootNode();
Expand Down
6 changes: 0 additions & 6 deletions src/EventListener/ORM/ConfirmableListener.php
Expand Up @@ -15,9 +15,7 @@
use Core23\Doctrine\Util\ClassUtils;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use LogicException;

final class ConfirmableListener extends AbstractListener
{
Expand All @@ -35,10 +33,6 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
{
$meta = $eventArgs->getClassMetadata();

if (!$meta instanceof ClassMetadata) {
throw new LogicException('Class metadata was no ORM');
}

$reflClass = $meta->getReflectionClass();

if (null === $reflClass || !ClassUtils::containsTrait($reflClass, ConfirmableTrait::class)) {
Expand Down
6 changes: 0 additions & 6 deletions src/EventListener/ORM/DeletableListener.php
Expand Up @@ -15,9 +15,7 @@
use Core23\Doctrine\Util\ClassUtils;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use LogicException;

final class DeletableListener extends AbstractListener
{
Expand All @@ -35,10 +33,6 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
{
$meta = $eventArgs->getClassMetadata();

if (!$meta instanceof ClassMetadata) {
throw new LogicException('Class metadata was no ORM');
}

$reflClass = $meta->getReflectionClass();

if (null === $reflClass || !ClassUtils::containsTrait($reflClass, DeleteableTrait::class)) {
Expand Down
6 changes: 0 additions & 6 deletions src/EventListener/ORM/LifecycleDateListener.php
Expand Up @@ -18,9 +18,7 @@
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use LogicException;

final class LifecycleDateListener extends AbstractListener
{
Expand Down Expand Up @@ -65,10 +63,6 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
{
$meta = $eventArgs->getClassMetadata();

if (!$meta instanceof ClassMetadata) {
throw new LogicException('Class metadata was no ORM');
}

$reflClass = $meta->getReflectionClass();

if (null === $reflClass || !ClassUtils::containsTrait($reflClass, LifecycleDateTimeTrait::class)) {
Expand Down
15 changes: 6 additions & 9 deletions src/EventListener/ORM/SortableListener.php
Expand Up @@ -20,12 +20,10 @@
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\UnitOfWork;
use LogicException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;

Expand Down Expand Up @@ -98,10 +96,6 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
{
$meta = $eventArgs->getClassMetadata();

if (!$meta instanceof ClassMetadata) {
throw new LogicException('Class metadata was no ORM');
}

$reflClass = $meta->getReflectionClass();

if (null === $reflClass || !ClassUtils::containsTrait($reflClass, SortableTrait::class)) {
Expand All @@ -124,7 +118,7 @@ private function uniquePosition(LifecycleEventArgs $args, ?int $oldPosition = nu
if (null === $entity->getPosition()) {
$position = $this->getNextPosition($args->getEntityManager(), $entity);
$entity->setPosition($position);
} elseif ($oldPosition && $oldPosition !== $entity->getPosition()) {
} elseif (null !== $oldPosition && $oldPosition !== $entity->getPosition()) {
$this->movePosition($args->getEntityManager(), $entity);
}
}
Expand Down Expand Up @@ -169,10 +163,13 @@ private function getNextPosition(EntityManager $em, PositionAwareInterface $enti
try {
$result = $qb->getQuery()->getOneOrNullResult();

return ($result instanceof PositionAwareInterface ? $result->getPosition() : 0) + 1;
if ($result instanceof PositionAwareInterface && null !== $result->getPosition()) {
return $result->getPosition() + 1;
}
} catch (NonUniqueResultException $ignored) {
return 0;
}

return 0;
}

/**
Expand Down
6 changes: 0 additions & 6 deletions src/EventListener/ORM/UniqueActiveListener.php
Expand Up @@ -16,11 +16,9 @@
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\UnitOfWork;
use LogicException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;

Expand Down Expand Up @@ -69,10 +67,6 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
{
$meta = $eventArgs->getClassMetadata();

if (!$meta instanceof ClassMetadata) {
throw new LogicException('Class metadata was no ORM');
}

$reflClass = $meta->getReflectionClass();

if (null === $reflClass || !$reflClass->implementsInterface(UniqueActiveInterface::class)) {
Expand Down
7 changes: 5 additions & 2 deletions src/Test/ORM/EntityManagerMockFactory.php
Expand Up @@ -28,7 +28,7 @@ final class EntityManagerMockFactory
*
* @return EntityManager|MockObject
*/
public static function create(TestCase $test, Closure $qbCallback, $fields): MockObject
public static function create(TestCase $test, Closure $qbCallback, array $fields): MockObject
{
$qb = $test->getMockBuilder(QueryBuilder::class)->disableOriginalConstructor()->getMock();

Expand Down Expand Up @@ -65,7 +65,10 @@ private static function prepareQueryBuilder(TestCase $test, MockObject $qb): voi
$qb->method('leftJoin')->willReturn($qb);
}

private static function prepareMetadata(TestCase $test, $fields): MockObject
/**
* @param string[] $fields
*/
private static function prepareMetadata(TestCase $test, array $fields): MockObject
{
$metadata = $test->getMockBuilder(ClassMetadataInfo::class)->disableOriginalConstructor()->getMock();
$metadata->method('getFieldNames')->willReturn($fields);
Expand Down
8 changes: 0 additions & 8 deletions tests/Bridge/Symfony/Bundle/Core23DoctrineBundleTest.php
Expand Up @@ -12,17 +12,9 @@
use Core23\Doctrine\Bridge\Symfony\Bundle\Core23DoctrineBundle;
use Core23\Doctrine\Bridge\Symfony\DependencyInjection\Core23DoctrineExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;

final class Core23DoctrineBundleTest extends TestCase
{
public function testItIsInstantiable(): void
{
$bundle = new Core23DoctrineBundle();

static::assertInstanceOf(BundleInterface::class, $bundle);
}

public function testGetPath(): void
{
$bundle = new Core23DoctrineBundle();
Expand Down
Expand Up @@ -12,6 +12,12 @@
namespace Core23\Doctrine\Tests\Bridge\Symfony\DependencyInjection;

use Core23\Doctrine\Bridge\Symfony\DependencyInjection\Core23DoctrineExtension;
use Core23\Doctrine\EventListener\ORM\ConfirmableListener;
use Core23\Doctrine\EventListener\ORM\DeletableListener;
use Core23\Doctrine\EventListener\ORM\LifecycleDateListener;
use Core23\Doctrine\EventListener\ORM\SortableListener;
use Core23\Doctrine\EventListener\ORM\TablePrefixEventListener;
use Core23\Doctrine\EventListener\ORM\UniqueActiveListener;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;

final class Core23DoctrineExtensionTest extends AbstractExtensionTestCase
Expand All @@ -20,7 +26,12 @@ public function testLoadDefault(): void
{
$this->load();

static::assertTrue(true);
$this->assertContainerBuilderHasService(ConfirmableListener::class);
$this->assertContainerBuilderHasService(DeletableListener::class);
$this->assertContainerBuilderHasService(LifecycleDateListener::class);
$this->assertContainerBuilderHasService(SortableListener::class);
$this->assertContainerBuilderHasService(UniqueActiveListener::class);
$this->assertContainerBuilderHasService(TablePrefixEventListener::class);
}

protected function getContainerExtensions()
Expand Down
8 changes: 0 additions & 8 deletions tests/EventListener/ORM/ConfirmableListenerTest.php
Expand Up @@ -12,7 +12,6 @@
use Core23\Doctrine\EventListener\ORM\ConfirmableListener;
use Core23\Doctrine\Tests\Fixtures\ClassWithAllProperties;
use Core23\Doctrine\Tests\Fixtures\EmptyClass;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
Expand All @@ -23,13 +22,6 @@

final class ConfirmableListenerTest extends TestCase
{
public function testItIsInstantiable(): void
{
$listener = new ConfirmableListener();

static::assertInstanceOf(EventSubscriber::class, $listener);
}

public function testGetSubscribedEvents(): void
{
$listener = new ConfirmableListener();
Expand Down
8 changes: 0 additions & 8 deletions tests/EventListener/ORM/DeletableListenerTest.php
Expand Up @@ -12,7 +12,6 @@
use Core23\Doctrine\EventListener\ORM\DeletableListener;
use Core23\Doctrine\Tests\Fixtures\ClassWithAllProperties;
use Core23\Doctrine\Tests\Fixtures\EmptyClass;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
Expand All @@ -23,13 +22,6 @@

final class DeletableListenerTest extends TestCase
{
public function testItIsInstantiable(): void
{
$listener = new DeletableListener();

static::assertInstanceOf(EventSubscriber::class, $listener);
}

public function testGetSubscribedEvents(): void
{
$listener = new DeletableListener();
Expand Down
23 changes: 13 additions & 10 deletions tests/EventListener/ORM/LifecycleDateListenerTest.php
Expand Up @@ -14,7 +14,7 @@
use Core23\Doctrine\Tests\Fixtures\ClassWithAllProperties;
use Core23\Doctrine\Tests\Fixtures\EmptyClass;
use DateTime;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
Expand All @@ -27,13 +27,6 @@

final class LifecycleDateListenerTest extends TestCase
{
public function testItIsInstantiable(): void
{
$listener = new LifecycleDateListener();

static::assertInstanceOf(EventSubscriber::class, $listener);
}

public function testGetSubscribedEvents(): void
{
$listener = new LifecycleDateListener();
Expand Down Expand Up @@ -68,15 +61,20 @@ public function testPrePersistForInvalidClass(): void
{
$object = $this->prophesize(stdClass::class);

$entityManager = $this->prophesize(EntityManagerInterface::class);

$eventArgs = $this->prophesize(LifecycleEventArgs::class);
$eventArgs->getObject()
->willReturn($object)
;
$eventArgs->getEntityManager()
->willReturn($entityManager)
;

$listener = new LifecycleDateListener();
$listener->prePersist($eventArgs->reveal());

static::assertTrue(true);
$entityManager->createQueryBuilder()->shouldNotHaveBeenCalled();
}

public function testPreUpdate(): void
Expand All @@ -99,15 +97,20 @@ public function testPreUpdateForInvalidClass(): void
{
$object = $this->prophesize(stdClass::class);

$entityManager = $this->prophesize(EntityManagerInterface::class);

$eventArgs = $this->prophesize(LifecycleEventArgs::class);
$eventArgs->getObject()
->willReturn($object)
;
$eventArgs->getEntityManager()
->willReturn($entityManager)
;

$listener = new LifecycleDateListener();
$listener->preUpdate($eventArgs->reveal());

static::assertTrue(true);
$entityManager->createQueryBuilder()->shouldNotHaveBeenCalled();
}

public function testLoadClassMetadataWithNoValidData(): void
Expand Down

0 comments on commit 251cccd

Please sign in to comment.