From 829a572e0a385f09c75a337a7c81966a7d54bcf8 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Sat, 16 Nov 2019 21:19:08 +0100 Subject: [PATCH] Test case for the trait for entity test case (the EntityTestCaseTrait) --- .../Test/Entity/EntityTestCaseTrait.php | 10 +- .../Test/Entity/EntityTestCaseTraitTest.php | 91 +++++++++++++++++++ 2 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 tests/Traits/Test/Entity/EntityTestCaseTraitTest.php diff --git a/src/Traits/Test/Entity/EntityTestCaseTrait.php b/src/Traits/Test/Entity/EntityTestCaseTrait.php index d0fe53c..1e6ff4d 100644 --- a/src/Traits/Test/Entity/EntityTestCaseTrait.php +++ b/src/Traits/Test/Entity/EntityTestCaseTrait.php @@ -27,7 +27,7 @@ trait EntityTestCaseTrait * * @param mixed $entity The entity */ - protected function persistAndFlush($entity): void + public function persistAndFlush($entity): void { $entityManager = $this->getEntityManager(); @@ -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(); @@ -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(); @@ -68,7 +68,7 @@ protected function dropDatabaseSchema(): void * * @return EntityManagerInterface */ - protected function getEntityManager(): EntityManagerInterface + public function getEntityManager(): EntityManagerInterface { return static::$kernel ->getContainer() @@ -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() diff --git a/tests/Traits/Test/Entity/EntityTestCaseTraitTest.php b/tests/Traits/Test/Entity/EntityTestCaseTraitTest.php new file mode 100644 index 0000000..f7a99a6 --- /dev/null +++ b/tests/Traits/Test/Entity/EntityTestCaseTraitTest.php @@ -0,0 +1,91 @@ +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]); + } +}