mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-22 09:53:35 +02:00
Centralized setElement logic for *Attachment entities.
This commit is contained in:
parent
e66fcc85fc
commit
a0c3410db6
14 changed files with 140 additions and 113 deletions
|
@ -44,6 +44,7 @@ declare(strict_types=1);
|
||||||
namespace App\Entity\Attachments;
|
namespace App\Entity\Attachments;
|
||||||
|
|
||||||
use App\Entity\Base\NamedDBElement;
|
use App\Entity\Base\NamedDBElement;
|
||||||
|
use App\Entity\Parts\Category;
|
||||||
use App\Validator\Constraints\Selectable;
|
use App\Validator\Constraints\Selectable;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
|
||||||
|
@ -74,7 +75,7 @@ abstract class Attachment extends NamedDBElement
|
||||||
* It will be used to determine if a attachment is a picture and therefore will be shown to user as preview.
|
* It will be used to determine if a attachment is a picture and therefore will be shown to user as preview.
|
||||||
*/
|
*/
|
||||||
public const PICTURE_EXTS = ['apng', 'bmp', 'gif', 'ico', 'cur', 'jpg', 'jpeg', 'jfif', 'pjpeg', 'pjp', 'png',
|
public const PICTURE_EXTS = ['apng', 'bmp', 'gif', 'ico', 'cur', 'jpg', 'jpeg', 'jfif', 'pjpeg', 'pjp', 'png',
|
||||||
'svg', 'webp'];
|
'svg', 'webp'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A list of extensions that will be treated as a 3D Model that can be shown to user directly in Part-DB.
|
* A list of extensions that will be treated as a 3D Model that can be shown to user directly in Part-DB.
|
||||||
|
@ -120,6 +121,19 @@ abstract class Attachment extends NamedDBElement
|
||||||
*/
|
*/
|
||||||
protected $attachment_type;
|
protected $attachment_type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string The class of the element that can be passed to this attachment. Must be overriden in subclasses.
|
||||||
|
*/
|
||||||
|
public const ALLOWED_ELEMENT_CLASS = '';
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
//parent::__construct();
|
||||||
|
if (static::ALLOWED_ELEMENT_CLASS === '') {
|
||||||
|
throw new \LogicException('An *Attachment class must override the ALLOWED_ELEMENT_CLASS const!');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/***********************************************************
|
/***********************************************************
|
||||||
* Various function
|
* Various function
|
||||||
***********************************************************/
|
***********************************************************/
|
||||||
|
@ -367,7 +381,19 @@ abstract class Attachment extends NamedDBElement
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract public function setElement(AttachmentContainingDBElement $element) : Attachment;
|
public function setElement(AttachmentContainingDBElement $element) : Attachment
|
||||||
|
{
|
||||||
|
if (!is_a($element,static::ALLOWED_ELEMENT_CLASS)) {
|
||||||
|
throw new \InvalidArgumentException(sprintf(
|
||||||
|
'The element associated with a %s must be a %s!',
|
||||||
|
get_class($this),
|
||||||
|
static::ALLOWED_ELEMENT_CLASS
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->element = $element;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $path
|
* @param string $path
|
||||||
|
|
|
@ -40,13 +40,5 @@ class AttachmentTypeAttachment extends Attachment
|
||||||
*/
|
*/
|
||||||
protected $element;
|
protected $element;
|
||||||
|
|
||||||
public function setElement(AttachmentContainingDBElement $element): Attachment
|
public const ALLOWED_ELEMENT_CLASS = AttachmentType::class;
|
||||||
{
|
|
||||||
if (!$element instanceof AttachmentType) {
|
|
||||||
throw new \InvalidArgumentException('The element associated with a AttachmentTypeAttachment must be an AttachmentType!');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->element = $element;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -42,13 +42,5 @@ class CategoryAttachment extends Attachment
|
||||||
*/
|
*/
|
||||||
protected $element;
|
protected $element;
|
||||||
|
|
||||||
public function setElement(AttachmentContainingDBElement $element): Attachment
|
public const ALLOWED_ELEMENT_CLASS = Category::class;
|
||||||
{
|
|
||||||
if (!$element instanceof Category) {
|
|
||||||
throw new \InvalidArgumentException('The element associated with a CategoryAttachment must be a Category!');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->element = $element;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -46,13 +46,5 @@ class CurrencyAttachment extends Attachment
|
||||||
*/
|
*/
|
||||||
protected $element;
|
protected $element;
|
||||||
|
|
||||||
public function setElement(AttachmentContainingDBElement $element): Attachment
|
public const ALLOWED_ELEMENT_CLASS = Currency::class;
|
||||||
{
|
|
||||||
if (!$element instanceof Currency) {
|
|
||||||
throw new \InvalidArgumentException('The element associated with a CurrencyAttachment must be a Currency!');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->element = $element;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -33,7 +33,6 @@ use Doctrine\ORM\Mapping as ORM;
|
||||||
*/
|
*/
|
||||||
class DeviceAttachment extends Attachment
|
class DeviceAttachment extends Attachment
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Device The element this attachment is associated with.
|
* @var Device The element this attachment is associated with.
|
||||||
* @ORM\ManyToOne(targetEntity="App\Entity\Devices\Device", inversedBy="attachments")
|
* @ORM\ManyToOne(targetEntity="App\Entity\Devices\Device", inversedBy="attachments")
|
||||||
|
@ -41,13 +40,5 @@ class DeviceAttachment extends Attachment
|
||||||
*/
|
*/
|
||||||
protected $element;
|
protected $element;
|
||||||
|
|
||||||
public function setElement(AttachmentContainingDBElement $element): Attachment
|
public const ALLOWED_ELEMENT_CLASS = Device::class;
|
||||||
{
|
|
||||||
if (!$element instanceof Device) {
|
|
||||||
throw new \InvalidArgumentException('The element associated with a PartAttachment must be a Device!');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->element = $element;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -42,13 +42,5 @@ class FootprintAttachment extends Attachment
|
||||||
*/
|
*/
|
||||||
protected $element;
|
protected $element;
|
||||||
|
|
||||||
public function setElement(AttachmentContainingDBElement $element): Attachment
|
public const ALLOWED_ELEMENT_CLASS = Footprint::class;
|
||||||
{
|
|
||||||
if (!$element instanceof Footprint) {
|
|
||||||
throw new \InvalidArgumentException('The element associated with a FootprintAttachment must be a Footprint!');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->element = $element;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -48,13 +48,5 @@ class GroupAttachment extends Attachment
|
||||||
*/
|
*/
|
||||||
protected $element;
|
protected $element;
|
||||||
|
|
||||||
public function setElement(AttachmentContainingDBElement $element): Attachment
|
public const ALLOWED_ELEMENT_CLASS = Group::class;
|
||||||
{
|
|
||||||
if (!$element instanceof Group) {
|
|
||||||
throw new \InvalidArgumentException('The element associated with a UserAttachment must be a User!');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->element = $element;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -42,13 +42,5 @@ class ManufacturerAttachment extends Attachment
|
||||||
*/
|
*/
|
||||||
protected $element;
|
protected $element;
|
||||||
|
|
||||||
public function setElement(AttachmentContainingDBElement $element): Attachment
|
public const ALLOWED_ELEMENT_CLASS = Manufacturer::class;
|
||||||
{
|
|
||||||
if (!$element instanceof Manufacturer) {
|
|
||||||
throw new \InvalidArgumentException('The element associated with a ManufacturerAttachment must be a Manufacturer!');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->element = $element;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -43,13 +43,5 @@ class MeasurementUnitAttachment extends Attachment
|
||||||
*/
|
*/
|
||||||
protected $element;
|
protected $element;
|
||||||
|
|
||||||
public function setElement(AttachmentContainingDBElement $element): Attachment
|
public const ALLOWED_ELEMENT_CLASS = MeasurementUnit::class;
|
||||||
{
|
|
||||||
if (!$element instanceof MeasurementUnit) {
|
|
||||||
throw new \InvalidArgumentException('The element associated with a MeasurementUnitAttachmentAttachment must be a MeasurementUnit!');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->element = $element;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -40,13 +40,5 @@ class PartAttachment extends Attachment
|
||||||
*/
|
*/
|
||||||
protected $element;
|
protected $element;
|
||||||
|
|
||||||
public function setElement(AttachmentContainingDBElement $element): Attachment
|
public const ALLOWED_ELEMENT_CLASS = Part::class;
|
||||||
{
|
|
||||||
if (!$element instanceof Part) {
|
|
||||||
throw new \InvalidArgumentException('The element associated with a PartAttachment must be a Part!');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->element = $element;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -28,6 +28,7 @@ use App\Entity\Parts\MeasurementUnit;
|
||||||
use App\Entity\Parts\Part;
|
use App\Entity\Parts\Part;
|
||||||
use App\Entity\Parts\Storelocation;
|
use App\Entity\Parts\Storelocation;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
use Symfony\Component\HttpKernel\HttpCache\Store;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A attachment attached to a measurement unit element.
|
* A attachment attached to a measurement unit element.
|
||||||
|
@ -44,13 +45,5 @@ class StorelocationAttachment extends Attachment
|
||||||
*/
|
*/
|
||||||
protected $element;
|
protected $element;
|
||||||
|
|
||||||
public function setElement(AttachmentContainingDBElement $element): Attachment
|
public const ALLOWED_ELEMENT_CLASS = Storelocation::class;
|
||||||
{
|
|
||||||
if (!$element instanceof Storelocation) {
|
|
||||||
throw new \InvalidArgumentException('The element associated with a StorelocationAttachment must be a Storelocation!');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->element = $element;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -45,13 +45,5 @@ class SupplierAttachment extends Attachment
|
||||||
*/
|
*/
|
||||||
protected $element;
|
protected $element;
|
||||||
|
|
||||||
public function setElement(AttachmentContainingDBElement $element): Attachment
|
public const ALLOWED_ELEMENT_CLASS = Supplier::class;
|
||||||
{
|
|
||||||
if (!$element instanceof Supplier) {
|
|
||||||
throw new \InvalidArgumentException('The element associated with a SupplierAttachment must be a Supplier!');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->element = $element;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -47,13 +47,5 @@ class UserAttachment extends Attachment
|
||||||
*/
|
*/
|
||||||
protected $element;
|
protected $element;
|
||||||
|
|
||||||
public function setElement(AttachmentContainingDBElement $element): Attachment
|
public const ALLOWED_ELEMENT_CLASS = User::class;
|
||||||
{
|
|
||||||
if (!$element instanceof User) {
|
|
||||||
throw new \InvalidArgumentException('The element associated with a UserAttachment must be a User!');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->element = $element;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -20,16 +20,113 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace App\Tests\Entity;
|
namespace App\Tests\Entity\Attachments;
|
||||||
|
|
||||||
|
|
||||||
use App\Entity\Attachments\Attachment;
|
use App\Entity\Attachments\Attachment;
|
||||||
|
use App\Entity\Attachments\AttachmentType;
|
||||||
|
use App\Entity\Attachments\AttachmentTypeAttachment;
|
||||||
|
use App\Entity\Attachments\CategoryAttachment;
|
||||||
|
use App\Entity\Attachments\CurrencyAttachment;
|
||||||
|
use App\Entity\Attachments\DeviceAttachment;
|
||||||
|
use App\Entity\Attachments\FootprintAttachment;
|
||||||
|
use App\Entity\Attachments\GroupAttachment;
|
||||||
|
use App\Entity\Attachments\ManufacturerAttachment;
|
||||||
|
use App\Entity\Attachments\MeasurementUnitAttachment;
|
||||||
use App\Entity\Attachments\PartAttachment;
|
use App\Entity\Attachments\PartAttachment;
|
||||||
|
use App\Entity\Attachments\StorelocationAttachment;
|
||||||
|
use App\Entity\Attachments\SupplierAttachment;
|
||||||
|
use App\Entity\Attachments\UserAttachment;
|
||||||
|
use App\Entity\Devices\Device;
|
||||||
|
use App\Entity\Parts\Category;
|
||||||
|
use App\Entity\Parts\Footprint;
|
||||||
|
use App\Entity\Parts\Manufacturer;
|
||||||
|
use App\Entity\Parts\MeasurementUnit;
|
||||||
|
use App\Entity\Parts\Part;
|
||||||
|
use App\Entity\Parts\Storelocation;
|
||||||
|
use App\Entity\Parts\Supplier;
|
||||||
|
use App\Entity\PriceInformations\Currency;
|
||||||
|
use App\Entity\UserSystem\Group;
|
||||||
|
use App\Entity\UserSystem\User;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
|
use Symfony\Component\HttpKernel\HttpCache\Store;
|
||||||
|
|
||||||
class AttachmentTest extends TestCase
|
class AttachmentTest extends TestCase
|
||||||
{
|
{
|
||||||
|
public function testEmptyState() : void
|
||||||
|
{
|
||||||
|
$attachment = new PartAttachment();
|
||||||
|
|
||||||
|
$this->assertNull($attachment->getAttachmentType());
|
||||||
|
$this->assertFalse($attachment->isPicture());
|
||||||
|
$this->assertFalse($attachment->isExternal());
|
||||||
|
$this->assertFalse($attachment->isSecure());
|
||||||
|
$this->assertFalse($attachment->isBuiltIn());
|
||||||
|
$this->assertFalse($attachment->is3DModel());
|
||||||
|
$this->assertFalse($attachment->getShowInTable());
|
||||||
|
$this->assertEmpty($attachment->getPath());
|
||||||
|
$this->assertEmpty($attachment->getName());
|
||||||
|
$this->assertEmpty($attachment->getURL());
|
||||||
|
$this->assertEmpty($attachment->getExtension());
|
||||||
|
$this->assertNull($attachment->getElement());
|
||||||
|
$this->assertEmpty($attachment->getFilename());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function subClassesDataProvider() : array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
[AttachmentTypeAttachment::class, AttachmentType::class],
|
||||||
|
[CategoryAttachment::class, Category::class],
|
||||||
|
[CurrencyAttachment::class, Currency::class],
|
||||||
|
[DeviceAttachment::class, Device::class],
|
||||||
|
[FootprintAttachment::class, Footprint::class],
|
||||||
|
[GroupAttachment::class, Group::class],
|
||||||
|
[ManufacturerAttachment::class, Manufacturer::class],
|
||||||
|
[MeasurementUnitAttachment::class, MeasurementUnit::class],
|
||||||
|
[PartAttachment::class, Part::class],
|
||||||
|
[StorelocationAttachment::class, Storelocation::class],
|
||||||
|
[SupplierAttachment::class, Supplier::class],
|
||||||
|
[UserAttachment::class, User::class]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider subClassesDataProvider
|
||||||
|
* @param string $attachment_class
|
||||||
|
* @param string $allowed_class
|
||||||
|
*/
|
||||||
|
public function testSetElement(string $attachment_class, string $allowed_class) : void
|
||||||
|
{
|
||||||
|
/** @var Attachment $attachment */
|
||||||
|
$attachment = new $attachment_class();
|
||||||
|
$element = new $allowed_class();
|
||||||
|
|
||||||
|
//This must not throw an exception
|
||||||
|
$attachment->setElement($element);
|
||||||
|
$this->assertEquals($element, $attachment->getElement());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that all attachment subclasses like PartAttachment or similar returns an exception, when an not allowed
|
||||||
|
* element is passed.
|
||||||
|
* @dataProvider subClassesDataProvider
|
||||||
|
* @depends testSetElement
|
||||||
|
*/
|
||||||
|
public function testSetElementExceptionOnSubClasses(string $attachment_class, string $allowed_class) : void
|
||||||
|
{
|
||||||
|
$this->expectException(\InvalidArgumentException::class);
|
||||||
|
|
||||||
|
/** @var Attachment $attachment */
|
||||||
|
$attachment = new $attachment_class();
|
||||||
|
if ($allowed_class !== Device::class) {
|
||||||
|
$element = new Device();
|
||||||
|
} else {
|
||||||
|
$element = new Category();
|
||||||
|
}
|
||||||
|
$attachment->setElement($element);
|
||||||
|
}
|
||||||
|
|
||||||
public function externalDataProvider()
|
public function externalDataProvider()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
Loading…
Add table
Add a link
Reference in a new issue