Started adding basic tests for LogEntryRepository

This commit is contained in:
Jan Böhmer 2024-06-23 15:41:57 +02:00
parent 8fdf37261d
commit 204178740c
4 changed files with 213 additions and 0 deletions

View file

@ -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);

View file

@ -0,0 +1,71 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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
];
}
}

View file

@ -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();
}

View file

@ -0,0 +1,130 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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
{
}
}