commit 385fcb9f94dc91ace56c1287ce6630052e4010ac
parent 1b4714b335a56b9c5019e4bf3d0e8b00e2e721fe
Author: Timo A. Hummel <felicitus@felicitus.org>
Date: Sun, 17 Apr 2016 17:09:42 +0200
Merge pull request #641 from partkeepr/PartKeepr-615
Fixes for #615 and #618
Diffstat:
4 files changed, 59 insertions(+), 9 deletions(-)
diff --git a/src/PartKeepr/PartBundle/Entity/Part.php b/src/PartKeepr/PartBundle/Entity/Part.php
@@ -9,6 +9,7 @@ use PartKeepr\FootprintBundle\Entity\Footprint;
use PartKeepr\PartBundle\Exceptions\CategoryNotAssignedException;
use PartKeepr\PartBundle\Exceptions\MinStockLevelOutOfRangeException;
use PartKeepr\PartBundle\Exceptions\StorageLocationNotAssignedException;
+use PartKeepr\ProjectBundle\Entity\Project;
use PartKeepr\ProjectBundle\Entity\ProjectPart;
use PartKeepr\StockBundle\Entity\StockEntry;
use PartKeepr\StorageLocationBundle\Entity\StorageLocation;
@@ -813,7 +814,9 @@ class Part extends BaseEntity
{
$projectNames = [];
foreach ($this->projectParts as $projectPart) {
- $projectNames[] = $projectPart->getProject()->getName();
+ if ($projectPart->getProject() instanceof Project) {
+ $projectNames[] = $projectPart->getProject()->getName();
+ }
}
return array_unique($projectNames);
diff --git a/src/PartKeepr/ProjectBundle/DataFixtures/ProjectFixtureLoader.php b/src/PartKeepr/ProjectBundle/DataFixtures/ProjectFixtureLoader.php
@@ -4,6 +4,7 @@ namespace PartKeepr\ProjectBundle\DataFixtures;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;
use PartKeepr\ProjectBundle\Entity\Project;
+use PartKeepr\ProjectBundle\Entity\ProjectAttachment;
use PartKeepr\ProjectBundle\Entity\ProjectPart;
class ProjectFixtureLoader extends AbstractFixture
diff --git a/src/PartKeepr/ProjectBundle/Entity/Project.php b/src/PartKeepr/ProjectBundle/Entity/Project.php
@@ -37,7 +37,7 @@ class Project extends BaseEntity
/**
* Holds the parts needed for this project
*
- * @ORM\OneToMany(targetEntity="PartKeepr\ProjectBundle\Entity\ProjectPart",mappedBy="project",cascade={"persist", "remove"})
+ * @ORM\OneToMany(targetEntity="PartKeepr\ProjectBundle\Entity\ProjectPart",mappedBy="project",cascade={"persist", "remove"}, orphanRemoval=true)
* @Groups({"default"})
* @var ArrayCollection
*/
@@ -135,7 +135,7 @@ class Project extends BaseEntity
*/
public function getParts()
{
- return $this->parts;
+ return $this->parts->getValues();
}
/**
@@ -168,7 +168,7 @@ class Project extends BaseEntity
*/
public function getAttachments()
{
- return $this->attachments;
+ return $this->attachments->getValues();
}
/**
@@ -191,7 +191,9 @@ class Project extends BaseEntity
*/
public function removeAttachment($projectAttachment)
{
- $projectAttachment->setProject(null);
+ if ($projectAttachment instanceof ProjectAttachment) {
+ $projectAttachment->setProject(null);
+ }
$this->attachments->removeElement($projectAttachment);
}
}
diff --git a/src/PartKeepr/ProjectBundle/Tests/ProjectTest.php b/src/PartKeepr/ProjectBundle/Tests/ProjectTest.php
@@ -4,6 +4,8 @@ namespace PartKeepr\ProjectBundle\Tests;
use Doctrine\Common\DataFixtures\ProxyReferenceRepository;
use PartKeepr\CoreBundle\Tests\WebTestCase;
use PartKeepr\PartBundle\Entity\Part;
+use PartKeepr\ProjectBundle\Entity\Project;
+use PartKeepr\ProjectBundle\Entity\ProjectAttachment;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class ProjectTest extends WebTestCase
@@ -155,12 +157,54 @@ class ProjectTest extends WebTestCase
$response = json_decode($client->getResponse()->getContent());
- $this->markTestIncomplete(
- "This test can't be completed yet. See https://github.com/dunglas/DunglasApiBundle/issues/285"
- );
-
$this->assertInternalType("array", $response->parts);
$this->assertArrayNotHasKey(1, $response->parts,
"When removing an entry from the ArrayCollection, the array must be resorted!");
+ $this->assertEquals(1, count($response->parts));
+ }
+
+ public function testProjectAttachmentRemoval()
+ {
+ $client = static::makeClient(true);
+
+ /**
+ * @var $project Project
+ */
+ $project = $this->fixtures->getReference("project");
+
+ $projectAttachment = new ProjectAttachment();
+
+ $fileService = $this->getContainer()->get("partkeepr_uploadedfile_service");
+ $fileService->replaceFromData($projectAttachment, "BLA", "test.txt");
+
+ $project->addAttachment($projectAttachment);
+ $this->getContainer()->get("doctrine.orm.default_entity_manager")->flush($project);
+
+ $project->removeAttachment($projectAttachment);
+
+ $serializedProject = $this->getContainer()->get("serializer")->normalize(
+ $project,
+ 'jsonld'
+ );
+
+ $iriConverter = $this->getContainer()->get("api.iri_converter");
+ $iri = $iriConverter->getIriFromItem($project);
+
+ $client->request(
+ 'PUT',
+ $iri,
+ array(),
+ array(),
+ array(),
+ json_encode($serializedProject)
+ );
+
+ $response = json_decode($client->getResponse()->getContent());
+
+ $this->assertInternalType("array", $response->attachments);
+ $this->assertArrayNotHasKey(1, $response->attachments,
+ "When removing an entry from the ArrayCollection, the array must be resorted!");
+
+ $this->assertEquals(0, count($response->attachments));
}
}