partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit 6a0ce6da839052204ea42f644537b6eb27118c1f
parent 9101d393d0d6caccfccd5218a6f79d62c51afaf4
Author: Felicitus <felicitus@felicitus.org>
Date:   Sat, 31 Oct 2015 18:28:42 +0100

Added unit tests for the part

Diffstat:
Asrc/PartKeepr/CoreBundle/Exceptions/TranslatableException.php | 12++++++++++++
Asrc/PartKeepr/DistributorBundle/Tests/DataFixtures/DistributorDataLoader.php | 20++++++++++++++++++++
Asrc/PartKeepr/ManufacturerBundle/Tests/DataFixtures/ManufacturerDataLoader.php | 21+++++++++++++++++++++
Msrc/PartKeepr/PartBundle/Entity/Part.php | 43++++++++++++-------------------------------
Asrc/PartKeepr/PartBundle/Exceptions/CategoryNotAssignedException.php | 15+++++++++++++++
Asrc/PartKeepr/PartBundle/Exceptions/StorageLocationNotAssignedException.php | 15+++++++++++++++
Asrc/PartKeepr/PartBundle/Tests/PartTest.php | 107+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/PartKeepr/ProjectBundle/Entity/Project.php | 2+-
Msrc/PartKeepr/StorageLocationBundle/Entity/StorageLocation.php | 12++++++------
Dsrc/backend/PartKeepr/Part/Exceptions/CategoryNotAssignedException.php | 21---------------------
Dsrc/backend/PartKeepr/Part/Exceptions/StorageLocationNotAssignedException.php | 20--------------------
11 files changed, 209 insertions(+), 79 deletions(-)

diff --git a/src/PartKeepr/CoreBundle/Exceptions/TranslatableException.php b/src/PartKeepr/CoreBundle/Exceptions/TranslatableException.php @@ -0,0 +1,12 @@ +<?php +namespace PartKeepr\CoreBundle\Exceptions; + + +abstract class TranslatableException extends \Exception +{ + /** + * Returns the translatable message + * @return mixed + */ + abstract public function getMessageKey(); +} diff --git a/src/PartKeepr/DistributorBundle/Tests/DataFixtures/DistributorDataLoader.php b/src/PartKeepr/DistributorBundle/Tests/DataFixtures/DistributorDataLoader.php @@ -0,0 +1,20 @@ +<?php +namespace PartKeepr\DistributorBundle\Tests\DataFixtures; + +use Doctrine\Common\DataFixtures\AbstractFixture; +use Doctrine\Common\Persistence\ObjectManager; +use PartKeepr\DistributorBundle\Entity\Distributor; + +class DistributorDataLoader extends AbstractFixture +{ + public function load(ObjectManager $manager) + { + $distributor = new Distributor(); + $distributor->setName("TEST"); + + $manager->persist($distributor); + $manager->flush(); + + $this->addReference("distributor.first", $distributor); + } +} diff --git a/src/PartKeepr/ManufacturerBundle/Tests/DataFixtures/ManufacturerDataLoader.php b/src/PartKeepr/ManufacturerBundle/Tests/DataFixtures/ManufacturerDataLoader.php @@ -0,0 +1,21 @@ +<?php +namespace PartKeepr\ManufacturerBundle\Tests\DataFixtures; + +use Doctrine\Common\DataFixtures\AbstractFixture; +use Doctrine\Common\Persistence\ObjectManager; +use PartKeepr\ManufacturerBundle\Entity\Manufacturer; + + +class ManufacturerDataLoader extends AbstractFixture +{ + public function load(ObjectManager $manager) + { + $manufacturer = new Manufacturer(); + $manufacturer->setName("TEST"); + + $manager->persist($manufacturer); + $manager->flush(); + + $this->addReference("manufacturer.first", $manufacturer); + } +} diff --git a/src/PartKeepr/PartBundle/Entity/Part.php b/src/PartKeepr/PartBundle/Entity/Part.php @@ -5,8 +5,8 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; use PartKeepr\DoctrineReflectionBundle\Annotation\TargetService; use PartKeepr\FootprintBundle\Entity\Footprint; -use PartKeepr\Part\Exceptions\CategoryNotAssignedException; -use PartKeepr\Part\Exceptions\StorageLocationNotAssignedException; +use PartKeepr\PartBundle\Exceptions\CategoryNotAssignedException; +use PartKeepr\PartBundle\Exceptions\StorageLocationNotAssignedException; use PartKeepr\PartKeepr; use PartKeepr\ProjectBundle\Entity\ProjectPart; use PartKeepr\StockBundle\Entity\StockEntry; @@ -82,7 +82,7 @@ class Part extends BaseEntity /** * Holds the manufacturers which can manufacture this part - * @ORM\OneToMany(targetEntity="PartKeepr\PartBundle\Entity\PartManufacturer",mappedBy="part",cascade={"persist", "remove"}) + * @ORM\OneToMany(targetEntity="PartKeepr\PartBundle\Entity\PartManufacturer",mappedBy="part",cascade={"persist", "remove"}, orphanRemoval=true) * @Groups({"default"}) * * @var ArrayCollection @@ -91,7 +91,7 @@ class Part extends BaseEntity /** * Holds the distributors from where we can buy the part - * @ORM\OneToMany(targetEntity="PartKeepr\PartBundle\Entity\PartDistributor",mappedBy="part",cascade={"persist", "remove"}) + * @ORM\OneToMany(targetEntity="PartKeepr\PartBundle\Entity\PartDistributor",mappedBy="part",cascade={"persist", "remove"}, orphanRemoval=true) * @Groups({"default"}) * * @var ArrayCollection @@ -99,18 +99,9 @@ class Part extends BaseEntity private $distributors; /** - * Holds the part images - * @ORM\OneToMany(targetEntity="PartKeepr\PartBundle\Entity\PartImage",mappedBy="part",cascade={"persist", "remove"}) - * @Groups({"default"}) - * - * @var PartImage - */ - private $images; - - /** * Holds the part attachments * @ORM\OneToMany(targetEntity="PartKeepr\PartBundle\Entity\PartAttachment", - * mappedBy="part",cascade={"persist", "remove"}) + * mappedBy="part",cascade={"persist", "remove"}, orphanRemoval=true) * @Groups({"default"}) * @UploadedFileCollection() * @@ -164,7 +155,7 @@ class Part extends BaseEntity /** * The parameters for this part * @ORM\OneToMany(targetEntity="PartKeepr\PartBundle\Entity\PartParameter", - * mappedBy="part",cascade={"persist", "remove"}) + * mappedBy="part",cascade={"persist", "remove"}, orphanRemoval=true) * * @var ArrayCollection */ @@ -238,7 +229,6 @@ class Part extends BaseEntity $this->distributors = new ArrayCollection(); $this->manufacturers = new ArrayCollection(); $this->parameters = new ArrayCollection(); - $this->images = new ArrayCollection(); $this->attachments = new ArrayCollection(); $this->stockLevels = new ArrayCollection(); $this->setCreateDate(new \DateTime()); @@ -466,16 +456,6 @@ class Part extends BaseEntity } /** - * Returns the part images array - * - * @return ArrayCollection the part images - */ - public function getImages() - { - return $this->images; - } - - /** * Returns the part attachments array * * @return ArrayCollection the part attachments @@ -563,7 +543,7 @@ class Part extends BaseEntity private function checkCategoryConsistency() { if ($this->getCategory() === null) { - throw new CategoryNotAssignedException($this); + throw new CategoryNotAssignedException(); } } @@ -590,7 +570,7 @@ class Part extends BaseEntity /** * Checks if the part storage location is set. * - * @throws StorageLocationNotAssignedException + * @throws \PartKeepr\PartBundle\Exceptions\StorageLocationNotAssignedException */ private function checkStorageLocationConsistency() { @@ -741,9 +721,9 @@ class Part extends BaseEntity } /** - * Adds a Part Attachment + * Removes a Part Attachment * - * @param PartAttachment $partAttachment An attachment to add + * @param PartAttachment $partAttachment An attachment to remove */ public function removeAttachment($partAttachment) { @@ -805,7 +785,8 @@ class Part extends BaseEntity return $this->projectParts; } - public function recomputeStockLevels () { + public function recomputeStockLevels() + { $sum = 0; $price = 0; diff --git a/src/PartKeepr/PartBundle/Exceptions/CategoryNotAssignedException.php b/src/PartKeepr/PartBundle/Exceptions/CategoryNotAssignedException.php @@ -0,0 +1,15 @@ +<?php +namespace PartKeepr\PartBundle\Exceptions; + +use PartKeepr\CoreBundle\Exceptions\TranslatableException; + +/** + * This exception is thrown when a part hasn't got a category assigned + */ +class CategoryNotAssignedException extends TranslatableException +{ + public function getMessageKey() + { + return "The part has no category assigned"; + } +} diff --git a/src/PartKeepr/PartBundle/Exceptions/StorageLocationNotAssignedException.php b/src/PartKeepr/PartBundle/Exceptions/StorageLocationNotAssignedException.php @@ -0,0 +1,15 @@ +<?php +namespace PartKeepr\PartBundle\Exceptions; + +use PartKeepr\CoreBundle\Exceptions\TranslatableException; + +/** + * This exception is thrown when a part hasn't got a storage location assigned + */ +class StorageLocationNotAssignedException extends TranslatableException +{ + public function getMessageKey() + { + return "The part has no storage location assigned"; + } +} diff --git a/src/PartKeepr/PartBundle/Tests/PartTest.php b/src/PartKeepr/PartBundle/Tests/PartTest.php @@ -0,0 +1,107 @@ +<?php +namespace PartKeepr\PartBundle\Tests; + +use Doctrine\Common\DataFixtures\ProxyReferenceRepository; +use Liip\FunctionalTestBundle\Test\WebTestCase; +use PartKeepr\PartBundle\Entity\Part; +use PartKeepr\PartBundle\Entity\PartAttachment; +use PartKeepr\PartBundle\Entity\PartDistributor; +use PartKeepr\PartBundle\Entity\PartManufacturer; + +class PartTest extends WebTestCase +{ + /** + * @var ProxyReferenceRepository + */ + protected $fixtures; + + public function setUp() + { + $this->fixtures = $this->loadFixtures( + array( + 'PartKeepr\StorageLocationBundle\DataFixtures\CategoryDataLoader', + 'PartKeepr\StorageLocationBundle\DataFixtures\StorageLocationLoader', + 'PartKeepr\PartBundle\DataFixtures\CategoryDataLoader', + 'PartKeepr\PartBundle\DataFixtures\PartDataLoader', + 'PartKeepr\ManufacturerBundle\Tests\DataFixtures\ManufacturerDataLoader', + 'PartKeepr\DistributorBundle\Tests\DataFixtures\DistributorDataLoader', + ) + )->getReferenceRepository(); + } + + /** + * @expectedException PartKeepr\PartBundle\Exceptions\CategoryNotAssignedException + */ + public function testCategoryRequired() + { + $part = new Part(); + $part->setName("TEST"); + $part->setStorageLocation($this->fixtures->getReference("storagelocation.first")); + + $this->getContainer()->get("doctrine.orm.default_entity_manager")->persist($part); + $this->getContainer()->get("doctrine.orm.default_entity_manager")->flush($part); + } + + /** + * @expectedException PartKeepr\PartBundle\Exceptions\StorageLocationNotAssignedException + */ + public function testStorageLocationRequired() + { + $part = new Part(); + $part->setName("TEST"); + $part->setCategory($this->getContainer()->get("partkeepr.part.category_service")->getRootNode()); + + $this->getContainer()->get("doctrine.orm.default_entity_manager")->persist($part); + $this->getContainer()->get("doctrine.orm.default_entity_manager")->flush($part); + } + + public function testBasics() + { + $part = new Part(); + $part->setName("TEST"); + $part->setCategory($this->getContainer()->get("partkeepr.part.category_service")->getRootNode()); + $part->setStorageLocation($this->fixtures->getReference("storagelocation.first")); + + $this->getContainer()->get("doctrine.orm.default_entity_manager")->persist($part); + $this->getContainer()->get("doctrine.orm.default_entity_manager")->flush($part); + } + + public function testAssociationRemoval() + { + $part = new Part(); + $part->setName("TEST"); + $part->setCategory($this->getContainer()->get("partkeepr.part.category_service")->getRootNode()); + $part->setStorageLocation($this->fixtures->getReference("storagelocation.first")); + + $partManufacturer = new PartManufacturer(); + $partManufacturer->setManufacturer($this->fixtures->getReference("manufacturer.first")); + $part->addManufacturer($partManufacturer); + + $partDistributor = new PartDistributor(); + $partDistributor->setDistributor($this->fixtures->getReference("distributor.first")); + $part->addDistributor($partDistributor); + + $partAttachment = new PartAttachment(); + + $fileService = $this->getContainer()->get("partkeepr_uploadedfile_service"); + $fileService->replaceFromData($partAttachment, "BLA", "test.txt"); + + $part->addAttachment($partAttachment); + + $this->getContainer()->get("doctrine.orm.default_entity_manager")->persist($part); + $this->getContainer()->get("doctrine.orm.default_entity_manager")->flush($part); + + $part->removeDistributor($partDistributor); + $part->removeManufacturer($partManufacturer); + $part->removeAttachment($partAttachment); + + $this->getContainer()->get("doctrine.orm.default_entity_manager")->flush($part); + + $storage = $fileService->getStorage($partAttachment); + + $this->assertNull($partDistributor->getId()); + $this->assertNull($partDistributor->getId()); + $this->assertNull($partAttachment->getId()); + $this->assertFalse($storage->has($partAttachment->getFullFilename())); + } +} diff --git a/src/PartKeepr/ProjectBundle/Entity/Project.php b/src/PartKeepr/ProjectBundle/Entity/Project.php @@ -55,7 +55,7 @@ class Project extends BaseEntity /** * Holds the project attachments * - * @ORM\OneToMany(targetEntity="PartKeepr\ProjectBundle\Entity\ProjectAttachment",mappedBy="project",cascade={"persist", "remove"}) + * @ORM\OneToMany(targetEntity="PartKeepr\ProjectBundle\Entity\ProjectAttachment",mappedBy="project",cascade={"persist", "remove"}, orphanRemoval=true) * @UploadedFileCollection() * @Groups({"default"}) * @var ArrayCollection diff --git a/src/PartKeepr/StorageLocationBundle/Entity/StorageLocation.php b/src/PartKeepr/StorageLocationBundle/Entity/StorageLocation.php @@ -24,7 +24,7 @@ class StorageLocation extends BaseEntity /** * Holds the storage location image * @ORM\OneToOne(targetEntity="PartKeepr\StorageLocationBundle\Entity\StorageLocationImage", - * mappedBy="storageLocation",cascade={"persist", "remove"}) + * mappedBy="storageLocation",cascade={"persist", "remove"}, orphanRemoval=true) * @Groups({"default"}) * @UploadedFile() * @var StorageLocationImage @@ -32,7 +32,7 @@ class StorageLocation extends BaseEntity private $image; /** - * The category of the footprint + * The category of the storage location * * @ORM\ManyToOne(targetEntity="PartKeepr\StorageLocationBundle\Entity\StorageLocationCategory", * inversedBy="storageLocations") @@ -100,9 +100,9 @@ class StorageLocation extends BaseEntity } /** - * Sets the footprint image + * Sets the storage location image * - * @param StorageLocationImage $image The footprint image + * @param StorageLocationImage $image The storage location image * * @return void */ @@ -121,9 +121,9 @@ class StorageLocation extends BaseEntity } /** - * Returns the footprint image + * Returns the storage location image * - * @return StorageLocationImage The footprint image + * @return StorageLocationImage The storage location image */ public function getImage() { diff --git a/src/backend/PartKeepr/Part/Exceptions/CategoryNotAssignedException.php b/src/backend/PartKeepr/Part/Exceptions/CategoryNotAssignedException.php @@ -1,20 +0,0 @@ -<?php -namespace PartKeepr\Part\Exceptions; - -use PartKeepr\PartKeepr, - PartKeepr\Util\SerializableException, - PartKeepr\PartBundle\Entity\Part; - -/** - * This exception is thrown when a part hasn't got a category assigned - */ -class CategoryNotAssignedException extends SerializableException { - - /** - * Constructs the exception - * @param BaseEntity $entity - */ - public function __construct (Part $part) { - parent::__construct(PartKeepr::i18n("Part %s has no category assigned", $part)); - } -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/Part/Exceptions/StorageLocationNotAssignedException.php b/src/backend/PartKeepr/Part/Exceptions/StorageLocationNotAssignedException.php @@ -1,19 +0,0 @@ -<?php -namespace PartKeepr\Part\Exceptions; - -use PartKeepr\PartKeepr, - PartKeepr\Util\SerializableException; - -/** - * This exception is thrown when a part hasn't got a storage location assigned - */ -class StorageLocationNotAssignedException extends SerializableException { - - /** - * Constructs the exception - * @param BaseEntity $entity - */ - public function __construct () { - parent::__construct(PartKeepr::i18n("No storage location assigned")); - } -}- \ No newline at end of file