Skip to content

Commit

Permalink
Merge pull request #30 from Prometee/add-product-aware-on-brand
Browse files Browse the repository at this point in the history
Add fixture with unit test
  • Loading branch information
loevgaard authored Jan 2, 2019
2 parents 09ebf37 + c68f36a commit a2507a4
Show file tree
Hide file tree
Showing 17 changed files with 449 additions and 75 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,36 @@ $ php bin/console doctrine:schema:update --force

or use [Doctrine Migrations](https://symfony.com/doc/master/bundles/DoctrineMigrationsBundle/index.html).

## Fixtures

1. Add a new yaml file to the folder `config/packages` and name it as you wish, e.g. `my_own_fixtures.yaml`.

2. Fill this yaml with your own brand fixtures and don't forget to declare the definition of
your product(s) before this brand definition or use existing product(s) code.
```
# config/packages/my_own_fixtures.yaml
sylius_fixtures:
suites:
my_own_brand_fixtures:
fixtures:
loevgaard_sylius_brand_plugin_brand:
options:
custom:
flux:
name: 'My brand'
slug: 'my-brand'
products:
- product_code_1
- product_code_2
- product_code_3
```
3. Load your fixtures
```bash
php bin/console sylius:fixture:load my_own_brand_fixtures
```
## Installation and usage for plugin development
[Find more information here](install-dev.md)
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"friends-of-behat/symfony-extension": "^1.2.1",
"friends-of-behat/variadic-extension": "^1.1",
"lakion/mink-debug-extension": "^1.2.3",
"matthiasnoback/symfony-config-test": "^3.0",
"phpspec/phpspec": "^5.0",
"phpstan/phpstan-doctrine": "^0.10",
"phpstan/phpstan-shim": "^0.10",
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ parameters:

ignoreErrors:
- '/Parameter #1 $configuration of method Symfony\Component\DependencyInjection\Extension\Extension::processConfiguration() expects Symfony\Component\Config\Definition\ConfigurationInterface, Symfony\Component\Config\Definition\ConfigurationInterface|null given./'
- '/Cannot call method scalarNode\(\) on Symfony\\Component\\Config\\Definition\\Builder\\NodeParentInterface\|null/'
22 changes: 22 additions & 0 deletions src/Assigner/ProductsAssigner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Loevgaard\SyliusBrandPlugin\Assigner;

use Loevgaard\SyliusBrandPlugin\Entity\ProductsAwareInterface;

final class ProductsAssigner implements ProductsAssignerInterface
{
/**
* {@inheritdoc}
*/
public function assign(ProductsAwareInterface $productsAware, array $products): void
{
foreach ($products as $product) {
if (null !== $product) {
$productsAware->addProduct($product);
}
}
}
}
17 changes: 17 additions & 0 deletions src/Assigner/ProductsAssignerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Loevgaard\SyliusBrandPlugin\Assigner;

use Loevgaard\SyliusBrandPlugin\Entity\BrandAwareInterface;
use Loevgaard\SyliusBrandPlugin\Entity\ProductsAwareInterface;

interface ProductsAssignerInterface
{
/**
* @param ProductsAwareInterface $productsAware
* @param BrandAwareInterface[] $products
*/
public function assign(ProductsAwareInterface $productsAware, array $products): void;
}
76 changes: 6 additions & 70 deletions src/Entity/Brand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

namespace Loevgaard\SyliusBrandPlugin\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Core\Model\ImagesAwareInterface;

class Brand implements BrandInterface, ImagesAwareInterface
class Brand implements BrandInterface
{
use ProductsAwareTrait,
ImagesAwareTrait;

/**
* @var int
*/
Expand All @@ -26,14 +24,10 @@ class Brand implements BrandInterface, ImagesAwareInterface
*/
protected $name;

/**
* @var Collection|ImageInterface[]
*/
protected $images;

public function __construct()
{
$this->images = new ArrayCollection();
$this->initializeImagesCollection();
$this->initializeProductsCollection();
}

/**
Expand Down Expand Up @@ -83,62 +77,4 @@ public function setName(string $name): void
{
$this->name = $name;
}

/********************************
* ImagesAwareInterface methods *
*******************************/

/**
* {@inheritdoc}
*/
public function getImages(): Collection
{
return $this->images;
}

/**
* {@inheritdoc}
*/
public function getImagesByType(string $type): Collection
{
return $this->images->filter(function (ImageInterface $image) use ($type) {
return $type === $image->getType();
});
}

/**
* {@inheritdoc}
*/
public function hasImages(): bool
{
return !$this->images->isEmpty();
}

/**
* {@inheritdoc}
*/
public function hasImage(ImageInterface $image): bool
{
return $this->images->contains($image);
}

/**
* {@inheritdoc}
*/
public function addImage(ImageInterface $image): void
{
$image->setOwner($this);
$this->images->add($image);
}

/**
* {@inheritdoc}
*/
public function removeImage(ImageInterface $image): void
{
if ($this->hasImage($image)) {
$image->setOwner(null);
$this->images->removeElement($image);
}
}
}
2 changes: 1 addition & 1 deletion src/Entity/BrandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Sylius\Component\Resource\Model\ResourceInterface;

interface BrandInterface extends ResourceInterface
interface BrandInterface extends ResourceInterface, ProductsAwareInterface, ImagesAwareInterface
{
/**
* Returns the name of the brand
Expand Down
12 changes: 12 additions & 0 deletions src/Entity/ImagesAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Loevgaard\SyliusBrandPlugin\Entity;

use Sylius\Component\Core\Model\ImagesAwareInterface as BaseImagesAwareInterface;

interface ImagesAwareInterface extends BaseImagesAwareInterface
{
public function initializeImagesCollection(): void;
}
77 changes: 77 additions & 0 deletions src/Entity/ImagesAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Loevgaard\SyliusBrandPlugin\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Model\ImageInterface;

trait ImagesAwareTrait
{

/** @var Collection|ImageInterface[] */
protected $images;

public function initializeImagesCollection(): void
{
$this->images = new ArrayCollection();
}

/**
* {@inheritdoc}
*/
public function getImages(): Collection
{
return $this->images;
}

/**
* {@inheritdoc}
*/
public function getImagesByType(string $type): Collection
{
return $this->images->filter(function (ImageInterface $image) use ($type) {
return $type === $image->getType();
});
}

/**
* {@inheritdoc}
*/
public function hasImages(): bool
{
return !$this->images->isEmpty();
}

/**
* {@inheritdoc}
*/
public function hasImage(ImageInterface $image): bool
{
return $this->images->contains($image);
}

/**
* {@inheritdoc}
*/
public function addImage(ImageInterface $image): void
{
if (false === $this->hasImage($image)) {
$image->setOwner($this);
$this->images->add($image);
}
}

/**
* {@inheritdoc}
*/
public function removeImage(ImageInterface $image): void
{
if ($this->hasImage($image)) {
$image->setOwner(null);
$this->images->removeElement($image);
}
}
}
6 changes: 3 additions & 3 deletions src/Entity/ProductTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ trait ProductTrait
{
/**
* @var BrandInterface|null
* @ORM\ManyToOne(targetEntity="Loevgaard\SyliusBrandPlugin\Entity\Brand")
* @ORM\ManyToOne(targetEntity="Loevgaard\SyliusBrandPlugin\Entity\Brand", inversedBy="products")
* @ORM\JoinColumn(name="brand_id", referencedColumnName="id")
*/
protected $brand;

/**
* @return BrandInterface|null
* {@inheritdoc}
*/
public function getBrand(): ?BrandInterface
{
return $this->brand;
}

/**
* @param BrandInterface|null $brand
* {@inheritdoc}
*/
public function setBrand(?BrandInterface $brand): void
{
Expand Down
34 changes: 34 additions & 0 deletions src/Entity/ProductsAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Loevgaard\SyliusBrandPlugin\Entity;

use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Model\ProductInterface;

interface ProductsAwareInterface
{
public function initializeProductsCollection(): void;

/**
* @return Collection|BrandAwareInterface[]|ProductInterface[]
*/
public function getProducts(): Collection;

/**
* @param BrandAwareInterface $product
* @return bool
*/
public function hasProduct(BrandAwareInterface $product): bool;

/**
* @param BrandAwareInterface $product
*/
public function addProduct(BrandAwareInterface $product): void;

/**
* @param BrandAwareInterface $product
*/
public function removeProduct(BrandAwareInterface $product): void;
}
Loading

0 comments on commit a2507a4

Please sign in to comment.