Skip to content

Commit

Permalink
fix(manager): remove final modifier for the method get repository
Browse files Browse the repository at this point in the history
  • Loading branch information
vanphucvo authored and OskarStark committed Oct 8, 2019
1 parent f93e32f commit b8fad0f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Entity/BaseEntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getEntityManager()
return $this->getObjectManager();
}

final protected function getRepository(): EntityRepository
protected function getRepository(): EntityRepository
{
return $this->getEntityManager()->getRepository($this->class);
}
Expand Down
74 changes: 38 additions & 36 deletions tests/Entity/BaseEntityManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,74 +16,76 @@
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sonata\Doctrine\Entity\BaseEntityManager;

class EntityManager extends BaseEntityManager
{
public function getRepositoryFromBaseClass(): EntityRepository
{
return $this->getRepository();
}
}

final class BaseEntityManagerTest extends TestCase
{
public function getManager()
/**
* @var ManagerRegistry|MockObject
*/
private $registry;

/**
* @var ObjectManager|MockObject
*/
private $objectManager;

/**
* @var BaseEntityManager
*/
private $manager;

public function setUp(): void
{
$registry = $this->createMock(ManagerRegistry::class);

return new EntityManager('classname', $registry);
$this->registry = $this->createMock(ManagerRegistry::class);
$this->objectManager = $this->createMock(ObjectManager::class);
$this->manager = $this->getMockForAbstractClass(BaseEntityManager::class, ['classname', $this->registry]);
}

public function test()
public function testGetClassName(): void
{
$this->assertSame('classname', $this->getManager()->getClass());
$this->assertSame('classname', $this->manager->getClass());
}

public function testException()
public function testException(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('The property exception does not exists');

$this->getManager()->exception;
$this->manager->exception;
}

public function testExceptionOnNonMappedEntity()
public function testExceptionOnNonMappedEntity(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Unable to find the mapping information for the class classname. Please check the `auto_mapping` option (http://symfony.com/doc/current/reference/configuration/doctrine.html#configuration-overview) or add the bundle to the `mappings` section in the doctrine configuration');

$registry = $this->createMock(ManagerRegistry::class);
$registry->expects($this->once())->method('getManagerForClass')->willReturn(null);
$this->registry->expects($this->once())->method('getManagerForClass')->willReturn(null);

$manager = new EntityManager('classname', $registry);
$manager->getObjectManager();
$this->manager->getObjectManager();
}

public function testGetEntityManager()
public function testGetEntityManager(): void
{
$objectManager = $this->createMock(ObjectManager::class);

$registry = $this->createMock(ManagerRegistry::class);
$registry->expects($this->once())->method('getManagerForClass')->willReturn($objectManager);

$manager = new EntityManager('classname', $registry);
$this->registry->expects($this->once())->method('getManagerForClass')->willReturn($this->objectManager);

$manager->em;
$this->manager->em;
}

public function testGetRepository(): void
{
$entityRepository = $this->createMock(EntityRepository::class);
$objectManager = $this->createMock(ObjectManager::class);
$objectManager->expects($this->once())->method('getRepository')->with('classname')->willReturn($entityRepository);

$registry = $this->createMock(ManagerRegistry::class);
$registry->expects($this->once())->method('getManagerForClass')->willReturn($objectManager);
$this->objectManager->expects($this->once())->method('getRepository')->with('classname')->willReturn($entityRepository);

$this->registry->expects($this->once())->method('getManagerForClass')->willReturn($this->objectManager);

$r = new \ReflectionObject($this->manager);
$m = $r->getMethod('getRepository');
$m->setAccessible(true);

$manager = new EntityManager('classname', $registry);
$repository = $manager->getRepositoryFromBaseClass();
$this->assertInstanceOf(EntityRepository::class, $repository);
$this->assertInstanceOf(EntityRepository::class, $m->invoke($this->manager));
}
}

0 comments on commit b8fad0f

Please sign in to comment.