Use an enum for target_type in log entries

This commit is contained in:
Jan Böhmer 2023-06-18 18:31:39 +02:00
parent 2da7463edf
commit 9adfcc7aec
12 changed files with 359 additions and 210 deletions

View file

@ -60,53 +60,6 @@ use PHPUnit\Framework\TestCase;
class AbstractLogEntryTest extends TestCase
{
public function targetTypeDataProvider(): array
{
return [
[1, User::class],
[2, Attachment::class],
[3, AttachmentType::class],
[4, Category::class],
[5, Project::class],
[6, ProjectBOMEntry::class],
[7, Footprint::class],
[8, Group::class],
[9, Manufacturer::class],
[10, Part::class],
[11, Storelocation::class],
[12, Supplier::class],
[-1, 'blablub', true],
];
}
/**
* @dataProvider targetTypeDataProvider
*/
public function testTargetTypeIdToClass(int $int, string $expected_class, bool $expect_exception = false): void
{
if ($expect_exception) {
$this->expectException(\InvalidArgumentException::class);
}
$this->assertSame($expected_class, AbstractLogEntry::targetTypeIdToClass($int));
}
/**
* @dataProvider targetTypeDataProvider
*/
public function testTypeClassToID(int $expected_id, string $class, bool $expect_exception = false): void
{
if ($expect_exception) {
$this->expectException(\InvalidArgumentException::class);
}
$this->assertSame($expected_id, AbstractLogEntry::targetTypeClassToID($class));
}
public function testTypeClassToIDSubclasses(): void
{
//Test if class mapping works for subclasses
$this->assertSame(2, AbstractLogEntry::targetTypeClassToID(PartAttachment::class));
}
public function testSetGetTarget(): void
{
$part = $this->createMock(Part::class);

View file

@ -0,0 +1,62 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 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\Entity\LogSystem;
use App\Entity\Attachments\Attachment;
use App\Entity\Attachments\PartAttachment;
use App\Entity\LogSystem\LogTargetType;
use App\Entity\Parameters\PartParameter;
use App\Entity\Parts\Category;
use App\Entity\UserSystem\User;
use PHPUnit\Framework\TestCase;
class LogTargetTypeTest extends TestCase
{
public function testToClass(): void
{
$this->assertNull(LogTargetType::NONE->toClass());
$this->assertSame(User::class, LogTargetType::USER->toClass());
$this->assertSame(Category::class, LogTargetType::CATEGORY->toClass());
$this->assertSame(Attachment::class, LogTargetType::ATTACHMENT->toClass());
}
public function testFromElementClass(): void
{
//Test creation from string class
$this->assertSame(LogTargetType::CATEGORY, LogTargetType::fromElementClass(Category::class));
$this->assertSame(LogTargetType::USER, LogTargetType::fromElementClass(User::class));
//Test creation from object
$this->assertSame(LogTargetType::CATEGORY, LogTargetType::fromElementClass(new Category()));
$this->assertSame(LogTargetType::USER, LogTargetType::fromElementClass(new User()));
//Test creation from subclass
$this->assertSame(LogTargetType::ATTACHMENT, LogTargetType::fromElementClass(new PartAttachment()));
$this->assertSame(LogTargetType::PARAMETER, LogTargetType::fromElementClass(new PartParameter()));
}
public function testFromElementClassInvalid(): void
{
$this->expectException(\InvalidArgumentException::class);
LogTargetType::fromElementClass(new \stdClass());
}
}