From 204178740c2ea17f2082b9c3eb834cb20db7bcbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 23 Jun 2024 15:41:57 +0200 Subject: [PATCH] Started adding basic tests for LogEntryRepository --- src/DataFixtures/DataStructureFixtures.php | 7 ++ src/DataFixtures/LogEntryFixtures.php | 71 +++++++++++ src/DataFixtures/PartFixtures.php | 5 + tests/Repository/LogEntryRepositoryTest.php | 130 ++++++++++++++++++++ 4 files changed, 213 insertions(+) create mode 100644 src/DataFixtures/LogEntryFixtures.php create mode 100644 tests/Repository/LogEntryRepositoryTest.php diff --git a/src/DataFixtures/DataStructureFixtures.php b/src/DataFixtures/DataStructureFixtures.php index 72c57077..fc713d4d 100644 --- a/src/DataFixtures/DataStructureFixtures.php +++ b/src/DataFixtures/DataStructureFixtures.php @@ -74,30 +74,37 @@ class DataStructureFixtures extends Fixture implements DependentFixtureInterface /** @var AbstractStructuralDBElement $node1 */ $node1 = new $class(); $node1->setName('Node 1'); + $this->addReference($class . '_1', $node1); /** @var AbstractStructuralDBElement $node2 */ $node2 = new $class(); $node2->setName('Node 2'); + $this->addReference($class . '_2', $node2); /** @var AbstractStructuralDBElement $node3 */ $node3 = new $class(); $node3->setName('Node 3'); + $this->addReference($class . '_3', $node3); $node1_1 = new $class(); $node1_1->setName('Node 1.1'); $node1_1->setParent($node1); + $this->addReference($class . '_4', $node1_1); $node1_2 = new $class(); $node1_2->setName('Node 1.2'); $node1_2->setParent($node1); + $this->addReference($class . '_5', $node1_2); $node2_1 = new $class(); $node2_1->setName('Node 2.1'); $node2_1->setParent($node2); + $this->addReference($class . '_6', $node2_1); $node1_1_1 = new $class(); $node1_1_1->setName('Node 1.1.1'); $node1_1_1->setParent($node1_1); + $this->addReference($class . '_7', $node1_1_1); $manager->persist($node1); $manager->persist($node2); diff --git a/src/DataFixtures/LogEntryFixtures.php b/src/DataFixtures/LogEntryFixtures.php new file mode 100644 index 00000000..e61a5b77 --- /dev/null +++ b/src/DataFixtures/LogEntryFixtures.php @@ -0,0 +1,71 @@ +. + */ + +declare(strict_types=1); + + +namespace App\DataFixtures; + +use App\Entity\LogSystem\ElementCreatedLogEntry; +use App\Entity\LogSystem\ElementEditedLogEntry; +use App\Entity\Parts\Category; +use App\Entity\UserSystem\User; +use Doctrine\Bundle\FixturesBundle\Fixture; +use Doctrine\Common\DataFixtures\DependentFixtureInterface; +use Doctrine\Persistence\ObjectManager; + +class LogEntryFixtures extends Fixture implements DependentFixtureInterface +{ + + public function load(ObjectManager $manager) + { + $this->createCategoryEntries($manager); + } + + public function createCategoryEntries(ObjectManager $manager): void + { + $category = $this->getReference(Category::class . '_1', Category::class); + + $logEntry = new ElementCreatedLogEntry($category); + $logEntry->setUser($this->getReference(UserFixtures::ADMIN, User::class)); + $logEntry->setComment('Test'); + $manager->persist($logEntry); + + $logEntry = new ElementEditedLogEntry($category); + $logEntry->setUser($this->getReference(UserFixtures::ADMIN, User::class)); + $logEntry->setComment('Test'); + + $logEntry->setOldData(['name' => 'Test']); + $logEntry->setNewData(['name' => 'Node 1.1']); + + $manager->persist($logEntry); + $manager->flush(); + + + } + + public function getDependencies(): array + { + return [ + UserFixtures::class, + DataStructureFixtures::class + ]; + } +} \ No newline at end of file diff --git a/src/DataFixtures/PartFixtures.php b/src/DataFixtures/PartFixtures.php index b7193ec0..a9290cbf 100644 --- a/src/DataFixtures/PartFixtures.php +++ b/src/DataFixtures/PartFixtures.php @@ -73,6 +73,7 @@ class PartFixtures extends Fixture implements DependentFixtureInterface $part = new Part(); $part->setName('Part 1'); $part->setCategory($manager->find(Category::class, 1)); + $this->addReference(Part::class . '_1', $part); $manager->persist($part); /** More complex part */ @@ -86,6 +87,7 @@ class PartFixtures extends Fixture implements DependentFixtureInterface $part->setIpn('IPN123'); $part->setNeedsReview(true); $part->setManufacturingStatus(ManufacturingStatus::ACTIVE); + $this->addReference(Part::class . '_2', $part); $manager->persist($part); /** Part with orderdetails, storelocations and Attachments */ @@ -98,6 +100,7 @@ class PartFixtures extends Fixture implements DependentFixtureInterface $partLot1->setStorageLocation($manager->find(StorageLocation::class, 1)); $part->addPartLot($partLot1); + $partLot2 = new PartLot(); $partLot2->setExpirationDate(new \DateTimeImmutable()); $partLot2->setComment('Test'); @@ -133,6 +136,8 @@ class PartFixtures extends Fixture implements DependentFixtureInterface $attachment->setAttachmentType($manager->find(AttachmentType::class, 1)); $part->addAttachment($attachment); + $this->addReference(Part::class . '_3', $part); + $manager->persist($part); $manager->flush(); } diff --git a/tests/Repository/LogEntryRepositoryTest.php b/tests/Repository/LogEntryRepositoryTest.php new file mode 100644 index 00000000..c03c2023 --- /dev/null +++ b/tests/Repository/LogEntryRepositoryTest.php @@ -0,0 +1,130 @@ +. + */ + +namespace App\Tests\Repository; + +use App\Entity\LogSystem\AbstractLogEntry; +use App\Entity\Parts\Category; +use App\Entity\Parts\Part; +use App\Entity\UserSystem\User; +use App\Repository\LogEntryRepository; +use App\Tests\Entity\LogSystem\AbstractLogEntryTest; +use Doctrine\ORM\EntityManagerInterface; +use PHPUnit\Framework\TestCase; +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; + +class LogEntryRepositoryTest extends KernelTestCase +{ + + private EntityManagerInterface $entityManager; + private LogEntryRepository $repo; + + protected function setUp(): void + { + $kernel = self::bootKernel(); + + $this->entityManager = $kernel->getContainer() + ->get('doctrine')->getManager(); + + $this->repo = $this->entityManager->getRepository(AbstractLogEntry::class); + } + + public function testFindBy(): void + { + //The findBy method should be able the target criteria and split it into the needed criterias + + $part = $this->entityManager->find(Part::class, 3); + $elements = $this->repo->findBy(['target' => $part]); + + //It should only contain one log entry, where the part was created. + $this->assertCount(1, $elements); + } + + public function testGetTargetElement(): void + { + $part = $this->entityManager->find(Part::class, 3); + $logEntry = $this->repo->findBy(['target' => $part])[0]; + + $element = $this->repo->getTargetElement($logEntry); + //The target element, must be the part we searched for + $this->assertSame($part, $element); + } + + public function testGetLastEditingUser(): void + { + //We have a edit log entry for the category with ID 1 + $category = $this->entityManager->find(Category::class, 1); + $adminUser = $this->entityManager->getRepository(User::class)->findOneBy(['name' => 'admin']); + + $user = $this->repo->getLastEditingUser($category); + + //The last editing user should be the admin user + $this->assertSame($adminUser, $user); + + //For the category 2, the user must be null + $category = $this->entityManager->find(Category::class, 2); + $user = $this->repo->getLastEditingUser($category); + $this->assertNull($user); + } + + public function testGetCreatingUser(): void + { + //We have a edit log entry for the category with ID 1 + $category = $this->entityManager->find(Category::class, 1); + $adminUser = $this->entityManager->getRepository(User::class)->findOneBy(['name' => 'admin']); + + $user = $this->repo->getCreatingUser($category); + + //The last editing user should be the admin user + $this->assertSame($adminUser, $user); + + //For the category 2, the user must be null + $category = $this->entityManager->find(Category::class, 2); + $user = $this->repo->getCreatingUser($category); + $this->assertNull($user); + } + + public function testGetLogsOrderedByTimestamp(): void + { + $logs = $this->repo->getLogsOrderedByTimestamp('DESC', 2, 0); + + //We have 2 log entries + $this->assertCount(2, $logs); + + //The first one must be newer than the second one + $this->assertGreaterThanOrEqual($logs[0]->getTimestamp(), $logs[1]->getTimestamp()); + } + + public function testGetElementExistedAtTimestamp(): void + { + } + + public function testGetTimetravelDataForElement(): void + { + } + + public function testGetElementHistory(): void + { + } + + public function testGetUndeleteDataForElement(): void + { + } +}