public function testDuplicateImages() { $user = $this->em->getRepository('Aisel\\UserBundle\\Entity\\User')->findOneBy(['email' => '*****@*****.**']); $this->setExpectedException('Doctrine\\DBAL\\Exception\\UniqueConstraintViolationException'); $image = new Media(); $image->setType(Media::MEDIA_IMAGE); $image->setFilename($this->faker->numberBetween(0, 10000)); $image->setMainImage(false); $this->em->persist($image); $this->em->flush(); $this->assertNotNull($image->getId()); $product = new Product(); $product->setUser($user); $product->setLocale('en'); $product->setName($this->faker->sentence(1)); $product->setContentShort($this->faker->sentence(10)); $product->setContent($this->faker->sentence(10)); $product->setStatus(true); $product->setSku($this->faker->numberBetween()); $product->setCommentStatus(true); $product->setMetaUrl('url_' . time()); $product->addMedia($image); $product->addMedia($image); $this->em->persist($product); $this->em->flush(); }
/** * createMediaFromFile * * @param string $pathOld * @param string $type * @param boolean $move * * @return Media $media */ public function createMediaFromFile($pathOld, $type, $move = true) { $media = new Media(); $media->setType($type); $media->setMainImage(false); $this->dm->persist($media); $this->dm->flush(); $newName = $media->getId() . '.' . pathinfo($pathOld, PATHINFO_EXTENSION); $fileDir = realpath($this->uploadPath) . '/' . date("Y") . '/' . date("m") . '/' . date("d"); $pathNew = $fileDir . '/' . $newName; $pathWeb = $this->mediaPath . '/' . date("Y") . '/' . date("m") . '/' . date("d") . '/' . $newName; $fs = new Filesystem(); $fs->mkdir($fileDir); if ($move) { $fs->rename($pathOld, $pathNew); } else { $fs->copy($pathOld, $pathNew); } //Update document $media->setFilename($pathWeb); $this->dm->persist($media); $this->dm->flush(); return $media; }