Implement Toggleable interface on Product

This commit is contained in:
TheMadeleine 2015-09-24 10:59:10 +02:00 committed by Magdalena Banasiak
parent cf3e608c0e
commit 2fe64adae4
9 changed files with 156 additions and 2 deletions

View file

@ -0,0 +1,34 @@
<?php
namespace Sylius\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20160104133517 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE sylius_product ADD enabled TINYINT(1) NOT NULL');
}
/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE sylius_product DROP enabled');
}
}

View file

@ -234,3 +234,17 @@ Feature: Products
Then I should be on the product index page
And I should see "Product has been successfully deleted."
And I should not see product with name "Sticker" in that list
Scenario: Enabling product
Given There is disabled product named "Mug"
And I am on the page of product "Mug"
When I press "Enable"
Then I should be on the product index page
And I should see "Product has been successfully enabled."
Scenario: Disabling product
Given There is enabled product named "Mug"
And I am on the page of product "Mug"
When I press "Disable"
Then I should be on the product index page
And I should see "Product has been successfully disabled."

View file

@ -357,4 +357,25 @@ class ProductContext extends DefaultContext
{
$this->assertSession()->addressEquals($this->generatePageUrl('product attribute creation', array('type' => $type)));
}
/**
* @Given /^There is (enabled|disabled) product named "([^""]*)"$/
*/
public function thereIsProduct($enabled, $name)
{
$product = $this->getRepository('product')->findOneBy(array('name' => $name));
if (null === $product) {
$product = $this->getRepository('product')->createNew();
$product->setName($name);
$product->setPrice(0);
$product->setDescription('Lorem ipsum');
}
$product->setEnabled('enabled' === $enabled);
$manager = $this->getEntityManager();
$manager->persist($product);
$manager->flush();
}
}

View file

@ -67,6 +67,7 @@
<gedmo:timestampable on="update"/>
</field>
<field name="deletedAt" column="deleted_at" type="datetime" nullable="true" />
<field name="enabled" column="enabled" type="boolean" />
<gedmo:soft-deleteable field-name="deletedAt" />
<gedmo:loggable />

View file

@ -85,3 +85,19 @@ sylius_backend_product_show:
repository:
method: findForDetailsPage
arguments: [$id]
sylius_backend_product_enable:
path: /{id}/enable
methods: [PATCH]
defaults:
_controller: sylius.controller.product:enableAction
_sylius:
redirect: sylius_backend_product_index
sylius_backend_product_disable:
path: /{id}/disable
methods: [PATCH]
defaults:
_controller: sylius.controller.product:disableAction
_sylius:
redirect: sylius_backend_product_index

View file

@ -17,6 +17,11 @@
<div class="page-header">
<div class="actions-menu">
{{ buttons.manage(path('sylius_backend_product_index'), 'sylius.product.manage'|trans) }}
{% if product.enabled %}
{{ buttons.disable(path('sylius_backend_product_disable', {'id': product.id})) }}
{% else %}
{{ buttons.enable(path('sylius_backend_product_enable', {'id': product.id})) }}
{% endif %}
{% if not product.deleted %}
{{ buttons.edit(path('sylius_backend_product_update', {'id': product.id})) }}
{{ buttons.delete(path('sylius_backend_product_delete', {'id': product.id}), null, false, true) }}
@ -151,6 +156,10 @@
<td>{{ 'sylius.product.stock'|trans }}</td>
<td><span class="label label-{{ product.masterVariant.inStock ? 'success' : 'important' }}">{{ product.masterVariant.onHand }}</span></td>
</tr>
<tr>
<td></td>
<td><span class="label label-{{ product.enabled ? 'success' : 'important'}}">{{ product.enabled ? 'Enabled' : 'Disabled' }}</span></td>
</tr>
<tr>
<td>{{ 'sylius.product.price'|trans }}</td>
<td>{{ product.masterVariant.price|sylius_price }}</td>

View file

@ -75,6 +75,11 @@ class Product extends AbstractTranslatable implements ProductInterface
*/
protected $deletedAt;
/**
* @var bool
*/
protected $enabled = true;
public function __construct()
{
parent::__construct();
@ -94,7 +99,7 @@ class Product extends AbstractTranslatable implements ProductInterface
}
/**
* @return null|ArchetypeInterface
* {@inheritdoc}
*/
public function getArchetype()
{
@ -102,7 +107,7 @@ class Product extends AbstractTranslatable implements ProductInterface
}
/**
* @param null|ArchetypeInterface $archetype
* @param null|BaseArchetypeInterface $archetype
*/
public function setArchetype(BaseArchetypeInterface $archetype = null)
{
@ -515,4 +520,36 @@ class Product extends AbstractTranslatable implements ProductInterface
}
}
}
/**
* {@inheritdoc}
*/
public function isEnabled()
{
return $this->enabled;
}
/**
* {@inheritdoc}
*/
public function setEnabled($enabled)
{
$this->enabled = (bool) $enabled;
}
/**
* {@inheritdoc}
*/
public function enable()
{
$this->enabled = true;
}
/**
* {@inheritdoc}
*/
public function disable()
{
$this->enabled = false;
}
}

View file

@ -15,6 +15,7 @@ use Sylius\Component\Archetype\Model\ArchetypeSubjectInterface;
use Sylius\Component\Resource\Model\SlugAwareInterface;
use Sylius\Component\Resource\Model\SoftDeletableInterface;
use Sylius\Component\Resource\Model\TimestampableInterface;
use Sylius\Component\Resource\Model\ToggleableInterface;
/**
* @author Paweł Jędrzejewski <pawel@sylius.org>
@ -25,6 +26,7 @@ interface ProductInterface extends
SlugAwareInterface,
SoftDeletableInterface,
TimestampableInterface,
ToggleableInterface,
ProductTranslationInterface
{
/**

View file

@ -13,6 +13,7 @@ namespace spec\Sylius\Component\Product\Model;
use Doctrine\Common\Collections\Collection;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Resource\Model\ToggleableInterface;
use Sylius\Component\Product\Model\ArchetypeInterface;
use Sylius\Component\Product\Model\AttributeValueInterface;
use Sylius\Component\Product\Model\OptionInterface;
@ -41,6 +42,11 @@ class ProductSpec extends ObjectBehavior
$this->shouldImplement(ProductInterface::class);
}
function it_implements_toggleable_interface()
{
$this->shouldImplement(ToggleableInterface::class);
}
function it_has_no_id_by_default()
{
$this->getId()->shouldReturn(null);
@ -288,4 +294,18 @@ class ProductSpec extends ObjectBehavior
$this->setDeletedAt($deletedAt);
$this->shouldNotBeDeleted();
}
function it_is_enabled_by_default()
{
$this->shouldBeEnabled();
}
function it_is_toggleable()
{
$this->disable();
$this->shouldNotBeEnabled();
$this->enable();
$this->shouldBeEnabled();
}
}