Skip to content

Commit

Permalink
Merge pull request #3702 from tuka217/immutable-code-archetype
Browse files Browse the repository at this point in the history
[Archetype] Introduce code for archetype
  • Loading branch information
Paweł Jędrzejewski committed Dec 10, 2015
2 parents 6bdbdb0 + 89a5e7a commit f09093f
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 20 deletions.
21 changes: 21 additions & 0 deletions features/backend/product_archetypes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ Feature: Product archetypes
| Beverage calories | Calories |
| Coffee caffeine | Caffeine |
And there is archetype "T-Shirt" with following configuration:
| code | Arch1 |
| options | T-Shirt color, T-Shirt size |
| attributes | T-Shirt collection, T-Shirt fabric |
And there is archetype "Beverage" with following configuration:
| code | Arch2 |
| options | Beverage size, Beverage milk |
| attributes | Beverage calories |
And I am logged in as administrator
Expand Down Expand Up @@ -119,3 +121,22 @@ Feature: Product archetypes
Then I should be on the product archetype index page
And I should see "Archetype has been successfully deleted"
And I should not see archetype with name "T-Shirt" in the list

Scenario: Cannot update archetype code
When I am editing product archetype "T-Shirt"
Then the code field should be disabled

Scenario: Try add archetype with existing code
Given I am on the product archetype creation page
When I fill in "Name" with "Coffee"
And I fill in "Code" with "Arch1"
And I press "Create"
Then I should still be on the product archetype creation page
And I should see "Archetype with given code already exists."

Scenario: Try create archetype without code
Given I am on the product archetype creation page
When I fill in "Name" with "Coffee"
And I press "Create"
Then I should still be on the product archetype creation page
And I should see "Please enter archetype code."
5 changes: 2 additions & 3 deletions src/Sylius/Bundle/ArchetypeBundle/Form/Type/ArchetypeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Sylius\Bundle\ArchetypeBundle\Form\Type;

use Sylius\Bundle\ArchetypeBundle\Form\EventListener\ParentArchetypeListener;
use Sylius\Bundle\ResourceBundle\Form\EventSubscriber\AddCodeFormSubscriber;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
Expand Down Expand Up @@ -50,9 +51,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->addEventSubscriber(new ParentArchetypeListener($this->subject))
->add('code', 'text', array(
'label' => 'sylius.form.archetype.code'
))
->addEventSubscriber(new AddCodeFormSubscriber())
->add('translations', 'a2lix_translationsForms', array(
'form_type' => sprintf('sylius_%s_archetype_translation', $this->subject),
'label' => 'sylius.form.archetype.name'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">

<class name="Sylius\Component\Archetype\Model\Archetype">
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">code</option>
<option name="message">sylius.archetype.code.unique</option>
<option name="groups">sylius</option>
</constraint>
<property name="code">
<constraint name="NotBlank">
<option name="message">sylius.archetype.code.not_blank</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\ArchetypeBundle\Form\EventListener\ParentArchetypeListener;
use Sylius\Bundle\ResourceBundle\Form\EventSubscriber\AddCodeFormSubscriber;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormEvents;
Expand Down Expand Up @@ -42,7 +43,7 @@ function it_is_a_form_type()
function it_builds_form_with_proper_fields(FormBuilder $builder)
{
$builder
->add('code', 'text', Argument::any())
->addEventSubscriber(Argument::type(AddCodeFormSubscriber::class))
->willReturn($builder)
;

Expand Down
5 changes: 2 additions & 3 deletions src/Sylius/Bundle/ProductBundle/Behat/ProductContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,11 @@ public function thereIsArchetypeWithFollowingConfiguration($name, TableNode $tab
{
$manager = $this->getEntityManager();
$factory = $this->getFactory('product_archetype');
$data = $table->getRowsHash();

$archetype = $factory->createNew();
$archetype->setName($name);
$archetype->setCode($name);

$data = $table->getRowsHash();
$archetype->setCode($data['code']);

foreach (explode(',', $data['options']) as $optionName) {
$archetype->addOption($this->findOneByName('product_option', trim($optionName)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ sylius:
not_blank: Please enter archetype name.
min_length: Archetype name must be at least 1 character long.|Archetype name must be at least {{ limit }} characters long.
max_length: Archetype name must not be longer than 1 character.|Archetype name must not be longer than {{ limit }} characters.
code:
unique: Archetype with given code already exists.
not_blank: Please enter archetype code.
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,6 @@ sylius:
create: Create archetype
create_header: New archetype
index_header: Product archetypes
code: code
name: name
no_results: There are no archetypes defined.
options: options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<table class="table">
<thead>
<tr>
<th>{{ sylius_resource_sort('code', 'sylius.archetype.code'|trans) }}</th>
<th>{{ sylius_resource_sort('code', 'sylius.ui.code'|trans) }}</th>
<th>{{ 'sylius.archetype.name'|trans }}</th>
<th>{{ 'sylius.archetype.parent'|trans }}</th>
<th>{{ 'sylius.archetype.options'|trans }}</th>
Expand Down
13 changes: 2 additions & 11 deletions src/Sylius/Component/Archetype/Model/ArchetypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Common\Collections\Collection;
use Sylius\Component\Attribute\Model\AttributeInterface as BaseAttributeInterface;
use Sylius\Component\Resource\Model\CodeAwareInterface;
use Sylius\Component\Resource\Model\TimestampableInterface;
use Sylius\Component\Variation\Model\OptionInterface as BaseOptionInterface;

Expand All @@ -24,7 +25,7 @@
* @author Paweł Jędrzejewski <[email protected]>
* @author Adam Elsodaney <[email protected]>
*/
interface ArchetypeInterface extends TimestampableInterface, ArchetypeTranslationInterface
interface ArchetypeInterface extends CodeAwareInterface, TimestampableInterface, ArchetypeTranslationInterface
{
/**
* Returns all prototype attributes.
Expand Down Expand Up @@ -110,14 +111,4 @@ public function setParent(ArchetypeInterface $parent = null);
* @return null|ArchetypeInterface
*/
public function getParent();

/**
* @return string
*/
public function getCode();

/**
* @param string $code
*/
public function setCode($code);
}

0 comments on commit f09093f

Please sign in to comment.