Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests #25

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
14 changes: 8 additions & 6 deletions tests/Enum/EnumPostLoadEntityListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\ClassMetadata as PersistenceClassMetadata;
use PHPUnit\Framework\Assert;

class EnumPostLoadEntityListenerTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -24,16 +25,17 @@ public function testLoadWrongClassMetadata(): void

$entityManager = $this->createMock(EntityManager::class);
$entityManager
->expects($this->once())
->expects(self::once())
->method('getClassMetadata')
->will($this->returnValue($classMetadata));
->will(self::returnValue($classMetadata));

$loadEvent = new LifecycleEventArgs(new FooEntity(), $entityManager);

try {
$postLoadListener->postLoad($loadEvent);
Assert::fail('Exception expected');
} catch (\Consistence\Doctrine\Enum\UnsupportedClassMetadataException $e) {
$this->assertSame(get_class($classMetadata), $e->getGivenClassMetadataClass());
Assert::assertSame(get_class($classMetadata), $e->getGivenClassMetadataClass());
}
}

Expand All @@ -48,13 +50,13 @@ public function testWarmupCache(): void

$entityManager = $this->createMock(EntityManager::class);
$entityManager
->expects($this->once())
->expects(self::once())
->method('getClassMetadata')
->will($this->returnValue($classMetadata));
->will(self::returnValue($classMetadata));

$postLoadListener->warmUpCache($entityManager, FooEntity::class);

$this->assertTrue($cache->contains(FooEntity::class));
Assert::assertTrue($cache->contains(FooEntity::class));
}

}
129 changes: 65 additions & 64 deletions tests/Enum/LoadEnumToEntityIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,77 +7,64 @@
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Tools\Setup;
use Generator;
use PHPUnit\Framework\Assert;

class LoadEnumToEntityIntegrationTest extends \PHPUnit\Framework\TestCase
{

public function testLoadEnumToEntity(): void
{
$foo = new FooEntity();
$this->callPostLoadEventOnEntity($foo);

$this->assertSame(FooEnum::get(FooEnum::ONE), $foo->getEnum());
}

public function testLoadNullEnumToEntity(): void
{
$foo = new FooEntity();
$this->callPostLoadEventOnEntity($foo);

$this->assertNull($foo->getNullableEnum());
}

public function testLoadEnumToUnserializedEntity(): void
{
$fooBeforeSerialization = new FooEntity();
$fooBeforeSerialization->setEnum(FooEnum::get(FooEnum::ONE));

$foo = unserialize(serialize($fooBeforeSerialization));
$this->callPostLoadEventOnEntity($foo);

$this->assertSame(FooEnum::get(FooEnum::ONE), $foo->getEnum());
$this->assertNull($foo->getNullableEnum());
}

public function testMultipleLoadEvents(): void
/**
* @return mixed[][]|\Generator
*/
public function loadEnumToEntityDataProvider(): Generator
{
$foo = new FooEntity();
$this->callPostLoadEventOnEntity($foo);
$this->callPostLoadEventOnEntity($foo);

$this->assertSame(FooEnum::get(FooEnum::ONE), $foo->getEnum());
}
yield 'entity' => [
'foo' => new FooEntity(),
];

public function testLoadEnumClassWithoutNamespace(): void
{
$foo = new FooEntity();
$this->callPostLoadEventOnEntity($foo);
yield 'unserialized entity' => (function (): array {
$fooBeforeSerialization = new FooEntity();
$fooBeforeSerialization->setEnum(FooEnum::get(FooEnum::ONE));

$this->assertSame(FooEnum::get(FooEnum::ONE), $foo->getWithoutNamespace());
return [
'foo' => unserialize(serialize($fooBeforeSerialization)),
];
})();
}

public function testLoadEnumInEmbeddable(): void
/**
* @dataProvider loadEnumToEntityDataProvider
*
* @param \Consistence\Doctrine\Enum\FooEntity $foo
*/
public function testLoadEnumToEntity(FooEntity $foo): void
{
$foo = new FooEntity();
$this->callPostLoadEventOnEntity($foo);

$this->assertSame(FooEnum::get(FooEnum::ONE), $foo->getEmbedded()->getEnum());
Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getEnum());
Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getWithoutNamespace());
Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getEmbedded()->getEnum());
Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getEmbedded()->getEmbedded()->getEnum());
Assert::assertNull($foo->getNullableEnum());
Assert::assertNull($foo->getNotLoadedEmbedded());
}

public function testLoadEnumInEmbeddableWeNeedToGoDeeper(): void
/**
* @dataProvider loadEnumToEntityDataProvider
*
* @param \Consistence\Doctrine\Enum\FooEntity $foo
*/
public function testMultipleLoadEvents(FooEntity $foo): void
{
$foo = new FooEntity();
$this->callPostLoadEventOnEntity($foo);

$this->assertSame(FooEnum::get(FooEnum::ONE), $foo->getEmbedded()->getEmbedded()->getEnum());
}

public function testLoadEnumInNotLoadedEmbeddable(): void
{
$foo = new FooEntity();
$this->callPostLoadEventOnEntity($foo);

$this->assertNull($foo->getNotLoadedEmbedded());
Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getEnum());
Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getWithoutNamespace());
Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getEmbedded()->getEnum());
Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getEmbedded()->getEmbedded()->getEnum());
Assert::assertNull($foo->getNullableEnum());
Assert::assertNull($foo->getNotLoadedEmbedded());
}

public function testLoadEnumMissingEnumClass(): void
Expand All @@ -88,21 +75,35 @@ public function testLoadEnumMissingEnumClass(): void
$this->callPostLoadEventOnEntity(new BarEntity());
}

public function testLoadEnumNonExistingEnumClass(): void
public function loadEnumNotEnumClassDataProvider(): Generator
{
try {
$this->callPostLoadEventOnEntity(new BazEntity());
} catch (\Consistence\Doctrine\Enum\NotEnumException $e) {
$this->assertSame('Consistence\Doctrine\Enum\NonExistingClass', $e->getEnumClass());
}
yield 'non-existing class' => [
'entity' => new BazEntity(),
'expectedNotEnumClass' => 'Consistence\Doctrine\Enum\NonExistingClass',
];

yield 'not enum class' => [
'entity' => new BaxEntity(),
'expectedNotEnumClass' => FooEntity::class,
];
}

public function testLoadEnumNotEnumClass(): void
/**
* @dataProvider loadEnumNotEnumClassDataProvider
*
* @param object $entity
* @param string $expectedNotEnumClass
*/
public function testLoadEnumNotEnumClass(
object $entity,
string $expectedNotEnumClass
): void
{
try {
$this->callPostLoadEventOnEntity(new BaxEntity());
$this->callPostLoadEventOnEntity($entity);
Assert::fail('Exception expected');
} catch (\Consistence\Doctrine\Enum\NotEnumException $e) {
$this->assertSame(FooEntity::class, $e->getEnumClass());
Assert::assertSame($expectedNotEnumClass, $e->getEnumClass());
}
}

Expand All @@ -116,8 +117,8 @@ public function testLoadMultipleInstancesOfOneEntity(): void
$postLoadListener->postLoad(new LifecycleEventArgs($foo, $entityManager));
$postLoadListener->postLoad(new LifecycleEventArgs($iAmFooToo, $entityManager));

$this->assertSame(FooEnum::get(FooEnum::ONE), $foo->getEnum());
$this->assertSame(FooEnum::get(FooEnum::ONE), $iAmFooToo->getEnum());
Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getEnum());
Assert::assertSame(FooEnum::get(FooEnum::ONE), $iAmFooToo->getEnum());
}

private function callPostLoadEventOnEntity(object $entity): void
Expand Down
Loading