Skip to content

Commit

Permalink
Test case for the trait for entity test case (the EntityTestCaseTrait)
Browse files Browse the repository at this point in the history
  • Loading branch information
meritoo committed Nov 16, 2019
1 parent b54fc83 commit 829a572
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Traits/Test/Entity/EntityTestCaseTrait.php
Expand Up @@ -27,7 +27,7 @@ trait EntityTestCaseTrait
*
* @param mixed $entity The entity
*/
protected function persistAndFlush($entity): void
public function persistAndFlush($entity): void
{
$entityManager = $this->getEntityManager();

Expand All @@ -39,7 +39,7 @@ protected function persistAndFlush($entity): void
* Updates database schema.
* Creates all tables based on gathered/existing metadata.
*/
protected function updateDatabaseSchema(): void
public function updateDatabaseSchema(): void
{
$allMetadata = $this->getAllEntitiesMeta();

Expand All @@ -53,7 +53,7 @@ protected function updateDatabaseSchema(): void
* Drops database schema.
* Removes all tables based on gathered/existing metadata.
*/
protected function dropDatabaseSchema(): void
public function dropDatabaseSchema(): void
{
$allMetadata = $this->getAllEntitiesMeta();

Expand All @@ -68,7 +68,7 @@ protected function dropDatabaseSchema(): void
*
* @return EntityManagerInterface
*/
protected function getEntityManager(): EntityManagerInterface
public function getEntityManager(): EntityManagerInterface
{
return static::$kernel
->getContainer()
Expand All @@ -83,7 +83,7 @@ protected function getEntityManager(): EntityManagerInterface
* @param string $entityClass Fully qualified class name of entity
* @return ObjectRepository
*/
protected function getRepository(string $entityClass): ObjectRepository
public function getRepository(string $entityClass): ObjectRepository
{
return $this
->getEntityManager()
Expand Down
91 changes: 91 additions & 0 deletions tests/Traits/Test/Entity/EntityTestCaseTraitTest.php
@@ -0,0 +1,91 @@
<?php

/**
* (c) Meritoo.pl, http://www.meritoo.pl
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Meritoo\Test\CommonBundle\Traits\Test\Entity;

use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ORM\EntityManagerInterface;
use Meritoo\CommonBundle\Traits\Test\Entity\EntityTestCaseTrait;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @internal
* @covers \Meritoo\CommonBundle\Traits\Test\Entity\EntityTestCaseTrait
*/
class EntityTestCaseTraitTest extends TestCase
{
public function testPersistAndFlush(): void
{
$trait = $this->getMockedTrait('persistAndFlush');

$trait
->method('persistAndFlush')
->willReturn(null)
;

static::assertNull($trait->persistAndFlush(new \stdClass()));
}

public function testUpdateDatabaseSchema(): void
{
$trait = $this->getMockedTrait('updateDatabaseSchema');

$trait
->method('updateDatabaseSchema')
->willReturn(null)
;

static::assertNull($trait->updateDatabaseSchema());
}

public function testDropDatabaseSchema(): void
{
$trait = $this->getMockedTrait('dropDatabaseSchema');

$trait
->method('dropDatabaseSchema')
->willReturn(null)
;

static::assertNull($trait->dropDatabaseSchema());
}

public function testGetEntityManager(): void
{
$trait = $this->getMockedTrait('getEntityManager');
$entityManager = $this->createMock(EntityManagerInterface::class);

$trait
->method('getEntityManager')
->willReturn($entityManager)
;

static::assertInstanceOf(EntityManagerInterface::class, $trait->getEntityManager());
}

public function testGetRepository(): void
{
$trait = $this->getMockedTrait('getRepository');
$repository = $this->createMock(ObjectRepository::class);

$trait
->method('getRepository')
->willReturn($repository)
;

static::assertInstanceOf(ObjectRepository::class, $trait->getRepository(\stdClass::class));
}

private function getMockedTrait(string $method): MockObject
{
return $this->getMockForTrait(EntityTestCaseTrait::class, [], '', true, true, true, [$method]);
}
}

0 comments on commit 829a572

Please sign in to comment.