Skip to content

Latest commit

 

History

History
318 lines (228 loc) · 9.96 KB

File metadata and controls

318 lines (228 loc) · 9.96 KB

Sylius Supplier Plugin

Latest Stable Version Latest Unstable Version Total Downloads License Build

The Sylius supplier plugin adds support for a supplier model which can be used to extend products.

Installation

  1. Install with Composer

    composer require babdev/supplier-plugin:^1.0-dev
  2. Add the plugin to config/bundles.php after the SyliusResourceBundle, but before the SyliusGridBundle

    Sylius\Bundle\ResourceBundle\SyliusResourceBundle::class => ['all' => true],
    // other plugins and bundles
    BabDev\SyliusSupplierPlugin\BabDevSyliusSupplierPlugin::class => ['all' => true],
    Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true],
  3. Import config

    (in config/packages/_sylius.yaml or config/packages/babdev_sylius_supplier.yaml))

    imports:
        - { resource: "@BabDevSyliusSupplierPlugin/Resources/config/app/config.yml" }
  4. Import the routing

    (in config/routes.yaml or config/routes/babdev_sylius_supplier.yaml)

    babdev_sylius_supplier_admin:
        resource: "@BabDevSyliusSupplierPlugin/Resources/config/app/admin_routing.yml"
        prefix: /%sylius_admin.path_name%
  5. Add the corresponding traits

    • Product

      use BabDev\SyliusSupplierPlugin\Model\ProductTrait;
      use BabDev\SyliusSupplierPlugin\Model\SupplierAwareInterface;
      use Sylius\Component\Core\Model\Product as BaseProduct;
      
      class Product extends BaseProduct implements SupplierAwareInterface
      {
          use ProductTrait;
      }
    • ProductVariant

      use BabDev\SyliusSupplierPlugin\Model\ProductVariantTrait;
      use Sylius\Component\Core\Model\ProductVariant as BaseProductVariant;
      
      class ProductVariant extends BaseProductVariant
      {
          use ProductVariantTrait;
      }
  6. Add the Doctrine mapping in the preferred format.

    • Product

      • Annotations

        namespace App\Entity\Product;
        
        use BabDev\SyliusSupplierPlugin\Model\ProductTrait;
        use BabDev\SyliusSupplierPlugin\Model\SupplierAwareInterface;
        use BabDev\SyliusSupplierPlugin\Model\SupplierInterface;
        use Doctrine\ORM\Mapping as ORM;
        use Sylius\Component\Core\Model\Product as BaseProduct;
        
        /**
         * @ORM\Entity
         * @ORM\Table(name="sylius_product")
         */
        class Product extends BaseProduct implements SupplierAwareInterface
        {
            use ProductTrait;
        
            /**
             * @ORM\ManyToOne(targetEntity="BabDev\SyliusSupplierPlugin\Model\SupplierInterface")
             * @ORM\JoinColumn(name="supplier_id")
             *
             * @var SupplierInterface
             */
            protected $supplier;
      • XML

        <many-to-one field="supplier" target-entity="BabDev\SyliusSupplierPlugin\Model\SupplierInterface">
            <join-column name="supplier_id" />
        </many-to-one>
      • YAML

        manyToOne:
            supplier:
                targetEntity: BabDev\SyliusSupplierPlugin\Model\SupplierInterface
                joinColumn:
                    name: supplier_id
    • ProductVariant

      • Annotations

        namespace App\Entity\Product;
        
        use BabDev\SyliusSupplierPlugin\Model\ProductVariantTrait;
        use BabDev\SyliusSupplierPlugin\Model\SupplierPricingInterface;
        use Doctrine\ORM\Mapping as ORM;
        use Sylius\Component\Core\Model\ProductVariant as BaseProductVariant;
        
        /**
         * @ORM\Entity
         * @ORM\Table(name="sylius_product_variant")
         */
        class ProductVariant extends BaseProductVariant
        {
            use ProductVariantTrait;
        
            /**
             * @ORM\ManyToOne(targetEntity="BabDev\SyliusSupplierPlugin\Model\SupplierPricingInterface", inversedBy="productVariant", cascade="ALL")
             * @ORM\JoinColumn(name="supplier_pricing_id", onDelete="SET NULL")
             *
             * @var SupplierPricingInterface
             */
            protected $supplierPricing;
      • XML

        <many-to-one field="supplierPricing" target-entity="BabDev\SyliusSupplierPlugin\Model\SupplierPricingInterface" inversed-by="productVariant">
            <join-column name="supplier_pricing_id" on-delete="SET NULL" />
            <cascade>
                <cascade-all/>
            </cascade>
        </many-to-one>
      • YAML

        manyToOne:
            supplierPricing:
                targetEntity: BabDev\SyliusSupplierPlugin\Model\SupplierPricingInterface
                inversedBy: productVariant
                joinColumn:
                    name: supplier_pricing_id
                    onDelete: SET NULL
  7. Update database schema via Doctrine migrations

    • Generate the migration

      bin/console doctrine:migrations:diff
    • Check the generated migration, then execute it

      bin/console doctrine:migrations:migrate
  8. Add the field to the admin product form, for example in templates/bundles/SyliusAdminBundle/Product/Tab/_details.html.twig

    {{ form_row(form.supplier) }}

Development and testing

Setup

  1. Clone the repository (or a repository fork)

    git clone https://github.com/BabDev/supplier-plugin
    cd supplier-plugin
  2. Install the dependencies

    composer install
  3. Initialize the test app with the following commands:

    (cd tests/Application && yarn install)
    (cd tests/Application && yarn build)
    (cd tests/Application && bin/console APP_ENV=test assets:install public)
    (cd tests/Application && bin/console APP_ENV=test doctrine:database:create)
    (cd tests/Application && bin/console APP_ENV=test doctrine:schema:create)
  4. (Optional) Setup MySQL database

    By default, an SQLite database will be used, but you can setup a MySQL database, and configure its credentials in tests/Application/.env.test.local.

    Here's an example for running MySQL database in a Docker container:

    docker run -p 3307:3306 --env MYSQL_ROOT_PASSWORD=root --env MYSQL_DATABASE=sylius_test --env MYSQL_USER=sylius --env MYSQL_PASSWORD=sylius --name sylius-supplier-test -v sylius-supplier-test-data:/var/lib/mysql mysql:5.7 

    and the tests/Application/.env.test.local

    DATABASE_URL=mysql://sylius:sylius@127.0.0.1:3307/sylius_test?serverVersion=5.7
    

Running plugin tests

  • PHPUnit

    vendor/bin/phpunit
  • PHPSpec

    vendor/bin/phpspec run
  • Behat (non-JS scenarios)

    vendor/bin/behat --strict --tags="~@javascript"
  • Behat (JS scenarios)

    1. Install Symfony CLI command.

    2. Start Headless Chrome:

    google-chrome-stable --enable-automation --disable-background-networking --no-default-browser-check --no-first-run --disable-popup-blocking --disable-default-apps --allow-insecure-localhost --disable-translate --disable-extensions --no-sandbox --enable-features=Metal --headless --remote-debugging-port=9222 --window-size=2880,1800 --proxy-server='direct://' --proxy-bypass-list='*' http://127.0.0.1
    1. Install SSL certificates (only once needed) and run test application's webserver on 127.0.0.1:8080:
    symfony server:ca:install
    APP_ENV=test symfony server:start --port=8080 --dir=tests/Application/public --daemon
    1. Run Behat:
    vendor/bin/behat --strict --tags="@javascript"
  • Static Analysis

    • Psalm

      vendor/bin/psalm
    • PHPStan

      vendor/bin/phpstan analyse -c phpstan.neon -l max src/  
  • Coding Standard

    vendor/bin/ecs check src

Opening Sylius with your plugin

  • Using test environment:

    (cd tests/Application && APP_ENV=test bin/console sylius:fixtures:load)
    (cd tests/Application && APP_ENV=test symfony serve)
  • Using dev environment:

    (cd tests/Application && APP_ENV=dev bin/console sylius:fixtures:load)
    (cd tests/Application && APP_ENV=dev symfony serve)

Security

If you believe you have discovered a security issue with this plugin, please email michael.babker@gmail.com with information about the issue. Do NOT use the public issue tracker for security issues.

License

Sylius and this plugin is licensed under the MIT License. See the LICENSE file for full details.