diff --git a/src/Entity/Attachments/Attachment.php b/src/Entity/Attachments/Attachment.php index 37a6f4fc..caf9559e 100644 --- a/src/Entity/Attachments/Attachment.php +++ b/src/Entity/Attachments/Attachment.php @@ -44,6 +44,7 @@ declare(strict_types=1); namespace App\Entity\Attachments; use App\Entity\Base\NamedDBElement; +use App\Entity\Parts\Category; use App\Validator\Constraints\Selectable; 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. */ 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. @@ -120,6 +121,19 @@ abstract class Attachment extends NamedDBElement */ 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 ***********************************************************/ @@ -367,7 +381,19 @@ abstract class Attachment extends NamedDBElement 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 diff --git a/src/Entity/Attachments/AttachmentTypeAttachment.php b/src/Entity/Attachments/AttachmentTypeAttachment.php index 892756f1..b8c0577a 100644 --- a/src/Entity/Attachments/AttachmentTypeAttachment.php +++ b/src/Entity/Attachments/AttachmentTypeAttachment.php @@ -40,13 +40,5 @@ class AttachmentTypeAttachment extends Attachment */ protected $element; - public function setElement(AttachmentContainingDBElement $element): Attachment - { - if (!$element instanceof AttachmentType) { - throw new \InvalidArgumentException('The element associated with a AttachmentTypeAttachment must be an AttachmentType!'); - } - - $this->element = $element; - return $this; - } + public const ALLOWED_ELEMENT_CLASS = AttachmentType::class; } \ No newline at end of file diff --git a/src/Entity/Attachments/CategoryAttachment.php b/src/Entity/Attachments/CategoryAttachment.php index 0ff0cbfc..bd7ed4cc 100644 --- a/src/Entity/Attachments/CategoryAttachment.php +++ b/src/Entity/Attachments/CategoryAttachment.php @@ -42,13 +42,5 @@ class CategoryAttachment extends Attachment */ protected $element; - public function setElement(AttachmentContainingDBElement $element): Attachment - { - if (!$element instanceof Category) { - throw new \InvalidArgumentException('The element associated with a CategoryAttachment must be a Category!'); - } - - $this->element = $element; - return $this; - } + public const ALLOWED_ELEMENT_CLASS = Category::class; } \ No newline at end of file diff --git a/src/Entity/Attachments/CurrencyAttachment.php b/src/Entity/Attachments/CurrencyAttachment.php index dfb70b0d..af90fe88 100644 --- a/src/Entity/Attachments/CurrencyAttachment.php +++ b/src/Entity/Attachments/CurrencyAttachment.php @@ -46,13 +46,5 @@ class CurrencyAttachment extends Attachment */ protected $element; - public function setElement(AttachmentContainingDBElement $element): Attachment - { - if (!$element instanceof Currency) { - throw new \InvalidArgumentException('The element associated with a CurrencyAttachment must be a Currency!'); - } - - $this->element = $element; - return $this; - } + public const ALLOWED_ELEMENT_CLASS = Currency::class; } \ No newline at end of file diff --git a/src/Entity/Attachments/DeviceAttachment.php b/src/Entity/Attachments/DeviceAttachment.php index 9e28523a..670e6a17 100644 --- a/src/Entity/Attachments/DeviceAttachment.php +++ b/src/Entity/Attachments/DeviceAttachment.php @@ -33,7 +33,6 @@ use Doctrine\ORM\Mapping as ORM; */ class DeviceAttachment extends Attachment { - /** * @var Device The element this attachment is associated with. * @ORM\ManyToOne(targetEntity="App\Entity\Devices\Device", inversedBy="attachments") @@ -41,13 +40,5 @@ class DeviceAttachment extends Attachment */ protected $element; - public function setElement(AttachmentContainingDBElement $element): Attachment - { - if (!$element instanceof Device) { - throw new \InvalidArgumentException('The element associated with a PartAttachment must be a Device!'); - } - - $this->element = $element; - return $this; - } + public const ALLOWED_ELEMENT_CLASS = Device::class; } \ No newline at end of file diff --git a/src/Entity/Attachments/FootprintAttachment.php b/src/Entity/Attachments/FootprintAttachment.php index 21aecc3f..9251b7b2 100644 --- a/src/Entity/Attachments/FootprintAttachment.php +++ b/src/Entity/Attachments/FootprintAttachment.php @@ -42,13 +42,5 @@ class FootprintAttachment extends Attachment */ protected $element; - public function setElement(AttachmentContainingDBElement $element): Attachment - { - if (!$element instanceof Footprint) { - throw new \InvalidArgumentException('The element associated with a FootprintAttachment must be a Footprint!'); - } - - $this->element = $element; - return $this; - } + public const ALLOWED_ELEMENT_CLASS = Footprint::class; } \ No newline at end of file diff --git a/src/Entity/Attachments/GroupAttachment.php b/src/Entity/Attachments/GroupAttachment.php index 0951ce9b..d1300172 100644 --- a/src/Entity/Attachments/GroupAttachment.php +++ b/src/Entity/Attachments/GroupAttachment.php @@ -48,13 +48,5 @@ class GroupAttachment extends Attachment */ protected $element; - public function setElement(AttachmentContainingDBElement $element): Attachment - { - if (!$element instanceof Group) { - throw new \InvalidArgumentException('The element associated with a UserAttachment must be a User!'); - } - - $this->element = $element; - return $this; - } -} \ No newline at end of file + public const ALLOWED_ELEMENT_CLASS = Group::class; +} diff --git a/src/Entity/Attachments/ManufacturerAttachment.php b/src/Entity/Attachments/ManufacturerAttachment.php index d6829047..a9827fa1 100644 --- a/src/Entity/Attachments/ManufacturerAttachment.php +++ b/src/Entity/Attachments/ManufacturerAttachment.php @@ -42,13 +42,5 @@ class ManufacturerAttachment extends Attachment */ protected $element; - public function setElement(AttachmentContainingDBElement $element): Attachment - { - if (!$element instanceof Manufacturer) { - throw new \InvalidArgumentException('The element associated with a ManufacturerAttachment must be a Manufacturer!'); - } - - $this->element = $element; - return $this; - } + public const ALLOWED_ELEMENT_CLASS = Manufacturer::class; } \ No newline at end of file diff --git a/src/Entity/Attachments/MeasurementUnitAttachment.php b/src/Entity/Attachments/MeasurementUnitAttachment.php index a47bd0a1..1f873ac6 100644 --- a/src/Entity/Attachments/MeasurementUnitAttachment.php +++ b/src/Entity/Attachments/MeasurementUnitAttachment.php @@ -43,13 +43,5 @@ class MeasurementUnitAttachment extends Attachment */ protected $element; - public function setElement(AttachmentContainingDBElement $element): Attachment - { - if (!$element instanceof MeasurementUnit) { - throw new \InvalidArgumentException('The element associated with a MeasurementUnitAttachmentAttachment must be a MeasurementUnit!'); - } - - $this->element = $element; - return $this; - } + public const ALLOWED_ELEMENT_CLASS = MeasurementUnit::class; } \ No newline at end of file diff --git a/src/Entity/Attachments/PartAttachment.php b/src/Entity/Attachments/PartAttachment.php index e1c5f5c9..986dddbc 100644 --- a/src/Entity/Attachments/PartAttachment.php +++ b/src/Entity/Attachments/PartAttachment.php @@ -40,13 +40,5 @@ class PartAttachment extends Attachment */ protected $element; - public function setElement(AttachmentContainingDBElement $element): Attachment - { - if (!$element instanceof Part) { - throw new \InvalidArgumentException('The element associated with a PartAttachment must be a Part!'); - } - - $this->element = $element; - return $this; - } + public const ALLOWED_ELEMENT_CLASS = Part::class; } \ No newline at end of file diff --git a/src/Entity/Attachments/StorelocationAttachment.php b/src/Entity/Attachments/StorelocationAttachment.php index 6819d803..ee27f5a7 100644 --- a/src/Entity/Attachments/StorelocationAttachment.php +++ b/src/Entity/Attachments/StorelocationAttachment.php @@ -28,6 +28,7 @@ use App\Entity\Parts\MeasurementUnit; use App\Entity\Parts\Part; use App\Entity\Parts\Storelocation; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\HttpKernel\HttpCache\Store; /** * A attachment attached to a measurement unit element. @@ -44,13 +45,5 @@ class StorelocationAttachment extends Attachment */ protected $element; - public function setElement(AttachmentContainingDBElement $element): Attachment - { - if (!$element instanceof Storelocation) { - throw new \InvalidArgumentException('The element associated with a StorelocationAttachment must be a Storelocation!'); - } - - $this->element = $element; - return $this; - } + public const ALLOWED_ELEMENT_CLASS = Storelocation::class; } \ No newline at end of file diff --git a/src/Entity/Attachments/SupplierAttachment.php b/src/Entity/Attachments/SupplierAttachment.php index 616dcff5..5be36d8b 100644 --- a/src/Entity/Attachments/SupplierAttachment.php +++ b/src/Entity/Attachments/SupplierAttachment.php @@ -45,13 +45,5 @@ class SupplierAttachment extends Attachment */ protected $element; - public function setElement(AttachmentContainingDBElement $element): Attachment - { - if (!$element instanceof Supplier) { - throw new \InvalidArgumentException('The element associated with a SupplierAttachment must be a Supplier!'); - } - - $this->element = $element; - return $this; - } + public const ALLOWED_ELEMENT_CLASS = Supplier::class; } \ No newline at end of file diff --git a/src/Entity/Attachments/UserAttachment.php b/src/Entity/Attachments/UserAttachment.php index d2dfba64..76bbe08b 100644 --- a/src/Entity/Attachments/UserAttachment.php +++ b/src/Entity/Attachments/UserAttachment.php @@ -47,13 +47,5 @@ class UserAttachment extends Attachment */ protected $element; - public function setElement(AttachmentContainingDBElement $element): Attachment - { - if (!$element instanceof User) { - throw new \InvalidArgumentException('The element associated with a UserAttachment must be a User!'); - } - - $this->element = $element; - return $this; - } + public const ALLOWED_ELEMENT_CLASS = User::class; } \ No newline at end of file diff --git a/tests/Entity/AttachmentTest.php b/tests/Entity/Attachments/AttachmentTest.php similarity index 62% rename from tests/Entity/AttachmentTest.php rename to tests/Entity/Attachments/AttachmentTest.php index 6335c19d..1a908758 100644 --- a/tests/Entity/AttachmentTest.php +++ b/tests/Entity/Attachments/AttachmentTest.php @@ -20,16 +20,113 @@ * */ -namespace App\Tests\Entity; +namespace App\Tests\Entity\Attachments; 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\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 ReflectionClass; +use Symfony\Component\HttpKernel\HttpCache\Store; 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() { return [