Example #1
0
 public function testCreateClone()
 {
     $kernel = new Thelia('test', true);
     $kernel->boot();
     $originalProduct = ProductQuery::create()->addAscendingOrderByColumn('RAND()')->findOne();
     $newRef = uniqid('testClone-');
     $event = new ProductCloneEvent($newRef, 'fr_FR', $originalProduct);
     $event->setDispatcher($kernel->getContainer()->get('event_dispatcher'));
     $originalProductDefaultI18n = ProductI18nQuery::create()->findPk([$originalProduct->getId(), $event->getLang()]);
     $originalProductDefaultPrice = ProductPriceQuery::create()->findOneByProductSaleElementsId($originalProduct->getDefaultSaleElements()->getId());
     // Call function to test
     $action = new Product();
     $action->createClone($event, $originalProductDefaultI18n, $originalProductDefaultPrice);
     $cloneProduct = $event->getClonedProduct();
     // Check product information
     $this->assertInstanceOf('Thelia\\Model\\Product', $cloneProduct, 'Instance of clone product must be Thelia\\Model\\Product');
     $this->assertFalse($cloneProduct->isNew(), 'IsNew must be false');
     $this->assertEquals('fr_FR', $cloneProduct->getLocale(), 'Locale must be equal');
     $this->assertEquals($originalProductDefaultI18n->getTitle(), $cloneProduct->getTitle(), 'Title must be equal');
     $this->assertEquals($newRef, $cloneProduct->getRef(), 'Ref must be equal');
     $this->assertEquals(0, $cloneProduct->getVisible(), 'Visible must be false');
     $this->assertEquals($originalProduct->getDefaultCategoryId(), $cloneProduct->getDefaultCategoryId(), 'Default categories must be equal');
     $this->assertGreaterThan(0, $cloneProduct->getPosition(), 'Position must be greater than 0');
     $clonedProductSaleElements = $cloneProduct->getProductSaleElementss();
     $this->assertCount(1, $clonedProductSaleElements, 'There is not only one default PSE (maybe more, maybe none)');
     $clonedDefaultPSE = $clonedProductSaleElements->getFirst();
     $this->assertTrue($clonedDefaultPSE->getIsDefault(), 'IsDefault must be true for the default PSE');
     $this->assertEquals($cloneProduct->getRef(), $clonedDefaultPSE->getRef(), 'Clone product ref and clone PSE ref must be equal');
     $clonedProductPrice = $clonedDefaultPSE->getProductPrices()->getFirst();
     $this->assertEquals($originalProductDefaultPrice->getPrice(), $clonedProductPrice->getPrice(), 'Default price must be equal');
     $this->assertEquals($originalProductDefaultPrice->getCurrencyId(), $clonedProductPrice->getCurrencyId(), 'Currency IDs must be equal');
     return $event;
 }