Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Commit

Permalink
Improve fixtures 2
Browse files Browse the repository at this point in the history
  • Loading branch information
wbloszyk committed Jul 16, 2020
1 parent f2cccd7 commit 32c457c
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 503 deletions.
2 changes: 1 addition & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ parameters:
sonata_media.cdn.host: '/uploads/media/'
sonata_user.google_authenticator.server: demo.sonata-project.org
sonata_page.varnish.command: 'if [ ! -r "/etc/varnish/secret" ]; then echo "VALID ERROR :/"; else varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 {{ COMMAND }} "{{ EXPRESSION }}"; fi;'
sonata.fixtures.product.fake: 0
sonata.fixtures.product.fake: 10
sonata.fixtures.customer.fake: 10
sonata.fixtures.page.create_subsite: false

Expand Down
211 changes: 57 additions & 154 deletions src/Sonata/Bundle/DemoBundle/DataFixtures/ORM/LoadCategoryData.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Sonata\ClassificationBundle\Model\CategoryManagerInterface;
use Sonata\ClassificationBundle\Entity\CategoryManager;
use Sonata\ClassificationBundle\Model\Category;
use Sonata\ClassificationBundle\Model\Context;

/**
* Category fixtures loader.
Expand All @@ -26,228 +28,129 @@
class LoadCategoryData extends AbstractFixture implements OrderedFixtureInterface
{
/**
* @var CategoryManagerInterface
* @var CategoryManager
*/
protected $categoryManager;

public function __construct(CategoryManagerInterface $categoryManager)
public function __construct(CategoryManager $categoryManager)
{
$this->categoryManager = $categoryManager;
}

public function getOrder()
public function getOrder(): int
{
return 2;
}

public function load(ObjectManager $manager)
public function load(ObjectManager $manager): void
{
$defaultContext = $this->getReference('context_default');

$defaultCategory = $this->categoryManager->create();
$defaultCategory->setName('Default Category');
$defaultCategory->setSlug('products');
$defaultCategory->setEnabled(true);
$defaultCategory->setContext($defaultContext);

// Default category
$defaultCategory = $this->createRootCategory('Default Category', 'default', true, $this->getReference('context_default'));
$this->setReference('root_default_category', $defaultCategory);

$this->categoryManager->save($defaultCategory);

$productContext = $this->getReference('context_product_catalog');

$rootProduct = $this->categoryManager->create();
$rootProduct->setName('Root Products');
$rootProduct->setSlug('products');
$rootProduct->setEnabled(true);
$rootProduct->setContext($productContext);
$this->setReference('products_category', $rootProduct);

// Root product category
$rootProduct = $this->createRootCategory('Root Products', 'products', true, $this->getReference('context_product_catalog'));
$this->setReference('root_products_category', $rootProduct);

$this->categoryManager->save($rootProduct);

// Travels category
$travels = $this->categoryManager->create();
$travels->setName('Travels');
$travels->setSlug('travels');
$travels->setParent($rootProduct);
$travels->setDescription('Discover our travels');
$travels->setEnabled(true);
$this->categoryManager->save($travels);
$travels = $this->createCategory('Travels', 'travels', 'Discover our travels', true, $rootProduct);
$this->setReference('travels_category', $travels);

// Asia category
$asia = $this->categoryManager->create();
$asia->setParent($travels);
$asia->setName('Asia');
$asia->setSlug('asia');
$asia->setDescription('Want to travel in Asia? Check out our travels.');
$asia->setEnabled(true);
$this->categoryManager->save($asia);
$asia = $this->createCategory('Asia', 'asia', 'Want to travel in Asia? Check out our travels.', true, $travels);
$this->setReference('travels_asia_category', $asia);

// Japan category
$japan = $this->categoryManager->create();
$japan->setParent($asia);
$japan->setName('Japan');
$japan->setSlug('japan');
$japan->setDescription('Want to travel in Japan? Check out our travels.');
$japan->setEnabled(true);
$this->categoryManager->save($japan);
$japan = $this->createCategory('Japan', 'japan', 'Want to travel in Japan? Check out our travels.', true, $asia);
$this->setReference('travels_japan_category', $japan);

// North America category
$northAmerica = $this->categoryManager->create();
$northAmerica->setParent($travels);
$northAmerica->setName('North America');
$northAmerica->setSlug('north-america');
$northAmerica->setDescription('Want to travel in North America? Check out our travels.');
$northAmerica->setEnabled(true);
$this->categoryManager->save($northAmerica);
$northAmerica = $this->createCategory('North America', 'north-america', 'Want to travel in North America? Check out our travels.', true, $travels);
$this->setReference('travels_north_america_category', $northAmerica);

// Canada category
$canada = $this->categoryManager->create();
$canada->setParent($northAmerica);
$canada->setName('Canada');
$canada->setSlug('canada');
$canada->setDescription('Want to travel in Canada? Check out our travels.');
$canada->setEnabled(true);
$this->categoryManager->save($canada);
$canada = $this->createCategory('Canada', 'canada', 'Want to travel in Canada? Check out our travels.', true, $northAmerica);
$this->setReference('travels_canada_category', $canada);

// Quebec category
$quebec = $this->categoryManager->create();
$quebec->setParent($canada);
$quebec->setName('Quebec');
$quebec->setSlug('quebec');
$quebec->setDescription('Want to travel in Quebec? Check out our travels.');
$quebec->setEnabled(true);
$this->categoryManager->save($quebec);
$quebec = $this->createCategory('Quebec', 'quebec', 'Want to travel in Quebec? Check out our travels.', true, $canada);
$this->setReference('travels_quebec_category', $quebec);

// Europe category
$europe = $this->categoryManager->create();
$europe->setParent($travels);
$europe->setName('Europe');
$europe->setSlug('europe');
$europe->setDescription('Want to travel in Europe? Check out our travels.');
$europe->setEnabled(true);
$this->categoryManager->save($europe);
$europe = $this->createCategory('Europe', 'europe', 'Want to travel in Europe? Check out our travels.', true, $travels);
$this->setReference('travels_europe_category', $europe);

// Switzerland category
$switzerland = $this->categoryManager->create();
$switzerland->setParent($europe);
$switzerland->setName('Switzerland');
$switzerland->setSlug('switzerland');
$switzerland->setDescription('Want to travel in Switzerland? Check out our travels.');
$switzerland->setEnabled(true);
$this->categoryManager->save($switzerland);
$switzerland = $this->createCategory('Switzerland', 'switzerland', 'Want to travel in Switzerland? Check out our travels.', true, $europe);
$this->setReference('travels_switzerland_category', $switzerland);

// France category
$france = $this->categoryManager->create();
$france->setParent($europe);
$france->setName('France');
$france->setSlug('france');
$france->setDescription('Want to travel in France? Check out our travels.');
$france->setEnabled(true);
$this->categoryManager->save($france);
$france = $this->createCategory('France', 'france', 'Want to travel in France? Check out our travels.', true, $europe);
$this->setReference('travels_france_category', $france);

// Paris category
$paris = $this->categoryManager->create();
$paris->setParent($france);
$paris->setName('Paris');
$paris->setSlug('paris');
$paris->setDescription('Want to travel in Paris? Check out our travels.');
$paris->setEnabled(true);
$this->categoryManager->save($paris);
$paris = $this->createCategory('Paris', 'paris', 'Want to travel in Paris? Check out our travels.', true, $europe);
$this->setReference('travels_paris_category', $paris);

// Great britain category
$greatBritain = $this->categoryManager->create();
$greatBritain->setParent($travels);
$greatBritain->setName('Great Britain');
$greatBritain->setSlug('great-britain');
$greatBritain->setDescription('Want to travel in Great Britain? Check out our travels.');
$greatBritain->setEnabled(true);
$this->categoryManager->save($greatBritain);
$greatBritain = $this->createCategory('Great Britain', 'great-britain', 'Want to travel in Great Britain? Check out our travels.', true, $travels);
$this->setReference('travels_great_britain_category', $greatBritain);

// London category
$london = $this->categoryManager->create();
$london->setParent($travels);
$london->setName('London');
$london->setSlug('london');
$london->setDescription('Want to travel in London? Check out our travels.');
$london->setEnabled(true);
$this->categoryManager->save($london);
$london = $this->createCategory('London', 'london', 'Want to travel in London? Check out our travels.', true, $travels);
$this->setReference('travels_london_category', $london);

// Goodies category
$goodies = $this->categoryManager->create();
$goodies->setName('Goodies');
$goodies->setSlug('goodies');
$goodies->setDescription('Some goodies related to Sonata and Symfony world.');
$goodies->setEnabled(true);
$goodies->setParent($rootProduct);
$this->categoryManager->save($goodies);
$goodies = $this->createCategory('Goodies', 'goodies', 'Some goodies related to Sonata and Symfony world.', true, $rootProduct);
$this->setReference('goodies_category', $goodies);

// Goodies sub-categories
$plushes = $this->categoryManager->create();
$plushes->setParent($goodies);
$plushes->setName('Plushes');
$plushes->setSlug('plushes');
$plushes->setDescription('Some plushes.');
$plushes->setEnabled(true);
$this->categoryManager->save($plushes);
$plushes = $this->createCategory('Plushes', 'plushes', 'Some plushes.', true, $goodies);
$this->setReference('plushes_goodies_category', $plushes);

// Mugs sub-categories
$mugs = $this->categoryManager->create();
$mugs->setParent($goodies);
$mugs->setName('Mugs');
$mugs->setSlug('mugs');
$mugs->setDescription('Some mugs.');
$mugs->setEnabled(true);
$this->categoryManager->save($mugs);
$mugs = $this->createCategory('Mugs', 'mugs', 'Some mugs.', true, $goodies);
$this->setReference('sonata_mugs_category', $mugs);

// Clothing sub-categories
$clothes = $this->categoryManager->create();
$clothes->setParent($goodies);
$clothes->setName('Clothes');
$clothes->setSlug('clothes');
$clothes->setDescription('Clothes for geeks.');
$clothes->setEnabled(true);
$this->categoryManager->save($clothes);
$clothes = $this->createCategory('Clothes', 'clothes', 'Clothes for geeks.', true, $goodies);
$this->setReference('sonata_clothes_category', $clothes);

// Dummy category
$dummy = $this->categoryManager->create();
$dummy->setName('Dummy');
$dummy->setSlug('dummy');
$dummy->setDescription('Dummy category with many dummy products');
$dummy->setEnabled(true);
$dummy->setParent($rootProduct);
$this->categoryManager->save($dummy);

$dummy = $this->createCategory('Dummy', 'dummy', 'Dummy category with many dummy products.', true, $rootProduct);
$this->setReference('dummy_category', $dummy);

// Disabled category for testing purpose
$shoes = $this->categoryManager->create();
$shoes->setParent($goodies);
$shoes->setName('Shoes');
$shoes->setSlug('shoes');
$shoes->setDescription('Get the last coolest Sonata shoes (seriously)');
$shoes->setEnabled(true);
$this->categoryManager->save($shoes);
$shoes = $this->createCategory('Shoes', 'shoes', 'Get the last coolest Sonata shoes (seriously)', true, $goodies);
$this->setReference('sonata_shoes_category', $shoes);

$manager->flush();
$this->categoryManager->getObjectManager()->flush();
}

protected function createRootCategory(string $name, string $slug, bool $enabled, Context $context): Category
{
$rootCategory = $this->categoryManager->create();
$rootCategory->setName($name);
$rootCategory->setSlug($slug);
$rootCategory->setEnabled($enabled);
$rootCategory->setContext($context);

$this->categoryManager->save($rootCategory, false);

return $rootCategory;
}

protected function createCategory(string $name, string $slug, string $description, bool $enabled, Category $parent): Category
{
$category = $this->categoryManager->create();
$category->setParent($parent);
$category->setName($name);
$category->setSlug($slug);
$category->setDescription($description);
$category->setEnabled($enabled);

$this->categoryManager->save($category, false);

return $category;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Sonata\ClassificationBundle\Model\CollectionManagerInterface;
use Sonata\ClassificationBundle\Entity\CollectionManager;
use Sonata\ClassificationBundle\Model\Collection;

/**
* Class LoadCollectionData.
Expand All @@ -26,57 +27,49 @@
class LoadCollectionData extends AbstractFixture implements OrderedFixtureInterface
{
/**
* @var CollectionManagerInterface
* @var CollectionManager
*/
protected $collectionManager;

public function __construct(CollectionManagerInterface $collectionManager)
public function __construct(CollectionManager $collectionManager)
{
$this->collectionManager = $collectionManager;
}

public function getOrder()
public function getOrder(): int
{
return 4;
}

public function load(ObjectManager $manager)
public function load(ObjectManager $manager): void
{
$productContext = $this->getReference('context_product_catalog');

// PHP Fan collection
$php = $this->collectionManager->create();
$php->setName('PHP Fan');
$php->setSlug('php-fan');
$php->setDescription('Everything a PHP Fan needs.');
$php->setEnabled(true);
$php->setContext($productContext);
$this->collectionManager->save($php);

// PHP Fan
$php = $this->createCollection('PHP Fan', 'php-fan', 'Everything a PHP Fan needs.', true, $productContext);
$this->setReference('php_collection', $php);

// Travels collection
$travel = $this->collectionManager->create();
$travel->setName('Travels');
$travel->setSlug('travels');
$travel->setDescription('Every travels you want');
$travel->setEnabled(true);
$travel->setContext($productContext);
$this->collectionManager->save($travel);

$travel = $this->createCollection('Travels', 'travels', 'Every travels you want.', true, $productContext);
$this->setReference('travel_collection', $travel);

// Dummy collection
$dummy = $this->collectionManager->create();
$dummy->setName('Dummys');
$dummy->setSlug('Dummys');
$dummy->setDescription('Every dummys you want');
$dummy->setEnabled(true);
$dummy->setContext($productContext);
$this->collectionManager->save($dummy);

$dummy = $this->createCollection('Dummys', 'dummys', 'Every dummys you want.', true, $productContext);
$this->setReference('dummy_collection', $dummy);

$manager->flush();
$this->collectionManager->getObjectManager()->flush();
}

protected function createCollection(string $name, string $slug, string $description, bool $enabled, $context): Collection
{
$collection = $this->collectionManager->create();
$collection->setName($name);
$collection->setSlug($slug);
$collection->setDescription($description);
$collection->setEnabled($enabled);
$collection->setContext($context);
$this->collectionManager->save($collection, false);

return $collection;
}
}
Loading

0 comments on commit 32c457c

Please sign in to comment.