Beispiel #1
0
 public function testDuplicateImages()
 {
     $image = new Image();
     $image->setFilename($this->faker->numberBetween(0, 10000));
     $image->setMainImage(false);
     $this->dm->persist($image);
     $this->dm->flush();
     $this->assertNotNull($image->getId());
     $product = new Product();
     $product->setLocale('en');
     $product->setName($this->faker->sentence(1));
     $product->setDescriptionShort($this->faker->sentence(10));
     $product->setDescription($this->faker->sentence(10));
     $product->setStatus(true);
     $product->setCommentStatus(true);
     $product->setMetaUrl('url_' . time());
     $product->addImage($image);
     $product->addImage($image);
     $this->dm->persist($product);
     $this->dm->flush();
     $this->assertNotNull($product->getId());
     $this->assertEquals(count($product->getImages()), 1);
     $this->dm->remove($image);
     $this->dm->flush();
     $this->dm->remove($product);
     $this->dm->flush();
 }
Beispiel #2
0
 private function setProductImage(ObjectManager $manager, $product)
 {
     $images = new ArrayCollection();
     $uploadPath = $this->container->getParameter('application.media.product.path');
     $uploadDir = $this->container->getParameter('application.media.product.upload_dir');
     $productDir = $uploadDir . DIRECTORY_SEPARATOR . $product->getId();
     // Create product directory if not exists
     $this->createDirIfNotExists($uploadDir);
     $finder = new Finder();
     $productImages = $finder->files()->in($this->getRandomProductDirectory());
     // Copy and attach image to product
     foreach ($productImages as $productImage) {
         /** @var SplFileInfo $image */
         if (file_exists($productDir) === false) {
             mkdir($productDir);
         }
         $newPath = $productDir . DIRECTORY_SEPARATOR . $productImage->getFilename();
         if (file_exists($newPath)) {
             unlink($newPath);
         }
         copy($productImage->getPathname(), $newPath);
         $fileName = $uploadPath . '/' . $product->getId() . '/' . $productImage->getFilename();
         $image = new Image();
         $image->setFilename($fileName);
         $image->setMainImage(false);
         $manager->persist($image);
         $manager->flush();
         $images->add($image);
     }
     // Set last image as main
     $image->setMainImage(true);
     $manager->persist($image);
     $manager->flush();
     return $images;
 }