Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs] Recommend trait usage in Plugin Development Guide #11111

Merged
merged 1 commit into from
Mar 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Docs] Recommend trait usage in Plugin Development Guide
  • Loading branch information
Zales0123 committed Feb 10, 2020
commit ea6511e986d9d7fa070444b6deb8270f8b228f13
91 changes: 14 additions & 77 deletions docs/plugin-development-guide/implementation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,27 @@ take a look at :doc:`customizing models</customization/model>`, :doc:`form</cust
Model
*****

The only field we need to add is an additional ``$availableOnDemand`` boolean. We should start with the unit tests (written with
PHPSpec, PHPUnit, or any other unit testing tool):
The only field we need to add is an additional ``$availableOnDemand`` boolean. To allow plugin's user to use multiple plugins extending
the same entity, it's recommended to provide a trait with new properties and methods, together with ORM mapping written in annotations
(if necessary). Providing an interface containing new methods is advisable.

.. code-block:: php

<?php

// spec/Entity/ProductVariantSpec.php
// src/Model/ProductVariantTrait.php

declare(strict_types=1);

namespace spec\IronMan\SyliusProductOnDemandPlugin\Entity;
namespace IronMan\SyliusProductOnDemandPlugin\Model;

use IronMan\SyliusProductOnDemandPlugin\Entity\ProductVariantInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\ProductVariant;

final class ProductVariantSpec extends ObjectBehavior
trait ProductVariantTrait
{
function it_is_sylius_product_variant(): void
{
$this->shouldHaveType(ProductVariant::class);
}

function it_implements_product_variant_interface(): void
{
$this->shouldImplement(ProductVariantInterface::class);
}

function it_can_be_available_on_demand(): void
{
$this->isAvailableOnDemand()->shouldReturn(false);

$this->setAvailableOnDemand(true);
$this->isAvailableOnDemand()->shouldReturn(true);
}
}

.. code-block:: php

<?php

// src/Entity/ProductVariant.php

declare(strict_types=1);

namespace IronMan\SyliusProductOnDemandPlugin\Entity;

use Sylius\Component\Core\Model\ProductVariant as BaseProductVariant;

class ProductVariant extends BaseProductVariant implements ProductVariantInterface
{
/** @var bool */
/**
* @var bool
*
* @ORM\Column(type="boolean", name="available_on_demand")
*/
private $availableOnDemand = false;

public function setAvailableOnDemand(bool $availableOnDemand): void
Expand All @@ -86,54 +54,23 @@ PHPSpec, PHPUnit, or any other unit testing tool):

<?php

// src/Entity/ProductVariantInterface.php
// src/Model/ProductVariantInterface.php

declare(strict_types=1);

namespace IronMan\SyliusProductOnDemandPlugin\Entity;

use Sylius\Component\Core\Model\ProductVariantInterface as BaseProductVariantInterface;

interface ProductVariantInterface extends BaseProductVariantInterface
interface ProductVariantInterface
{
public function setAvailableOnDemand(bool $availableOnDemand): void;

public function isAvailableOnDemand(): bool;
}

Of course you need to remember about entity mapping customization as well:

.. code-block:: yaml

# src/Resources/config/doctrine/ProductVariant.orm.yml

IronMan\SyliusProductOnDemandPlugin\Entity\ProductVariant:
type: entity
table: sylius_product_variant
fields:
availableOnDemand:
type: boolean

Then our new entity should be configured as a resource model:

.. code-block:: yaml

# src/Resources/config/config.yml

sylius_product:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about recommending this configuration in the test folder, so the entity will be checked if wired up properly (and preferably will be a good introduction for testing)

resources:
product_variant:
classes:
model: IronMan\SyliusProductOnDemandPlugin\Entity\ProductVariant

This configuration should be placed in ``src/Resources/config/config.yml``. It also has to be imported
(``- { resource: "@IronManSyliusProductOnDemandPlugin/Resources/config/config.yml" }``) in ``tests/Application/config/services.yaml``
to make it work in Behat tests. And at the end importing this file should be one of the steps described in plugin installation.

.. warning::

Remember that if you modify or add some mapping, you should either provide a migration for the plugin user (that could be
copied to their migration folder) or mention the requirement of migration generation in the installation instructions!
copied to their migration folder) or mention the requirement of migration generation in the installation instructions.

Form
****
Expand Down