Skip to content

Commit

Permalink
Opsrc-355/Add ability to add agreements to each form in Sylius (#10)
Browse files Browse the repository at this point in the history
* Agreement extended body can be null

* Fix GH actions

* Fix GH actions

* Add parent autocomplete function

* Fix

* Namespace fix

* Add OS header

* Agreement services cleaning

* Add agreement adding/editing template

* Add sylius 1.11 support

* GHA fix

* Fix 404 error after not accepting the required agreement

* Add AutocompleteChoiceType test

* Add Form/Type/Admin phpunit tests

* Add Form/Type/Shop phpunit tests

* Add agreement subscriber phpunit test

* Function name fix & typo fix & change coding standard

* Bug fix

* Phpstan fix

* Add behat tests for user registration with required agreement

* Fix composer dependencies

* Move translation to the proper place

* Change composer plugin name

* Fix translation path

* Readme update

* Fix infinity page reloading when set parent same as agreement (validator)

* Fix phpstan

* ecs fix

* Fixed typo and changed snake case function names

* Change behat declarations from yml files to xml

* behat fix

* Fix behat

* Add compilerpass configuration to extend forms automatically

* Resolvers repairing to allow extending other forms

* Create custom event to allow extending other forms

* Class, services and forms cleaning to allow extending other forms

* Fix phpstan, ecs and phpunit tests

* Code extraction to subscribers and handler

* Add OS headers/fix ecs

* Add PHPSpec

* Add trait to order entity

* Add event subscriber spec tests

* Add handler spec tests

* Fix typo

* Fix typo

* Add resolvers phpspec tests

* Fix resolvers

* Add resolver spec test

* Resolvers cleaning

* Remove phpunit

* Remove phpunit

* Fix PHPStan

* Change rand to type name

* Remove CompositeAgreementResolver

* Remove CompositeAgreementResolverSpec

* Use constraints instead of strings

* Move getter train into custom event instead of subscriber

* Use entity instead of mapped-superclass

* Fix spec tests after custom event changes

* Changed CompositeAgreementApprovalResolver to AgreementHistoryChecker

* Remove unneccesary dependencies

* ECS autofix

* Add checker spec missing scenario

* Move setAgreementHistoryProperties function to new class and spec it

* Move determineState function to new class and spec it

* Remove instanceof check (history resolver now can only return history object)

* Change else to check that agreement history is null

* Move functions to new classes and repair spec tests

* Change return type

* Remove unnecessary instanceof

* ecs fix

* Readme update

* Throw exception when agreements array is empty

* Add missing spec tests scenarios

* Fix typo

* Change array checking function from empty() to count()
  • Loading branch information
liszkapawel authored May 19, 2022
1 parent ab6e12b commit 0398688
Show file tree
Hide file tree
Showing 67 changed files with 1,351 additions and 2,256 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ jobs:
- name: Run ECS
run: vendor/bin/ecs check src

- name: Run PHPUnit
run: vendor/bin/phpunit --colors=always
- name: Run PHPSpec
run: vendor/bin/phpspec run --ansi -f progress --no-interaction

- name: Run Behat
run: vendor/bin/behat --colors --strict -vvv --no-interaction || vendor/bin/behat --colors --strict -vvv --no-interaction --rerun
Expand Down
32 changes: 26 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<p>
<img align="left" src="https://sylius.com/assets/badge-approved-by-sylius.png" width="85">
</p>
</p>

At BitBag we do believe in open source. However, we are able to do it just because of our awesome clients, who are kind enough to share some parts of our work with the community. Therefore, if you feel like there is a possibility for us to work together, feel free to reach out. You will find out more about our professional services, technologies, and contact details at [https://bitbag.io/](https://bitbag.io/contact-us/?utm_source=github&utm_medium=referral&utm_campaign=plugins_cms).

Expand All @@ -19,7 +19,8 @@ Like what we do? Want to join us? Check out our job listings on our [career page
* [Overview](#overview)
* [Support](#we-are-here-to-help)
* [Installation](#installation)
* [Testing](#testing)
* [Usage](#usage)
* [Testing](#testing)
* [About us](#about-us)
* [Community](#community)
* [Demo](#demo)
Expand Down Expand Up @@ -63,14 +64,34 @@ bitbag_sylius_agreement_plugin:
resource: "@BitBagSyliusAgreementPlugin/Resources/config/routing.yml"
```
4.Add trait to your Customer class:
## Usage
***
1.Find a form/forms where you want to add agreements and set it in your config.yaml file, for example:
```yaml
bit_bag_sylius_agreement:
contexts:
registration_form:
- Sylius\Bundle\CoreBundle\Form\Type\Customer\CustomerRegistrationType
checkout_form:
- Sylius\Bundle\CoreBundle\Form\Type\Checkout\AddressType
```
2.Find an entity which is used in your form and add trait to that class:
```php
use AgreementsRequiredTrait;
```

5.[Overwrite](https://symfony.com/doc/3.4/templating/overriding.html) following templates:
3.In the admin panel, create a new agreement and select context to it according to the configuration in the config.yaml file.

4.[Overwrite](https://symfony.com/doc/3.4/templating/overriding.html) templates used by yours extended form by adding:

- [@SyliusShopBundle/Register/_form.html.twig](/tests/Application/templates/bundles/SyliusShopBundle/Register/_form.html.twig)
````twig
{% for agreement in form.agreements %}
{{ form_row(agreement.approved) }}
{% endfor %}
````

Examples are in the package you have downloaded under the path [tests/Application/templates/bundles/*](/tests/Application/templates/bundles/)

Expand All @@ -89,7 +110,6 @@ $ bin/console sylius:fixtures:load --env=test
$ APP_ENV=test symfony server:start --dir=public/
$ cd ../..
$ vendor/bin/behat
$ vendor/bin/phpunit
```

# About us
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"friends-of-behat/symfony-extension": "^2.1",
"friends-of-behat/variadic-extension": "^1.3",
"friendsofsymfony/oauth-server-bundle": "^1.6 || >2.0.0-alpha.0 ^2.0@dev",
"phpunit/phpunit": "^9.5",
"symfony/dotenv": "^4.4 || ^5.2",
"symfony/config": "^2.7 || ^3.0 || ^4.0 || ^5.0",
"symfony/debug-bundle": "^4.4 || ^5.2",
Expand All @@ -38,7 +37,8 @@
"polishsymfonycommunity/symfony-mocker-container": "^1.0",
"symfony/security-bundle": "^5.2 | ^4.4",
"symfony/dependency-injection": "^5.2 | ^4.4",
"symfony/event-dispatcher": "^5.2 | ^4.4"
"symfony/event-dispatcher": "^5.2 | ^4.4",
"phpspec/phpspec": "^7.0"
},
"scripts": {
"check:code":
Expand Down
4 changes: 4 additions & 0 deletions phpspec.yml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
suites:
main:
namespace: BitBag\SyliusAgreementPlugin
psr4_prefix: BitBag\SyliusAgreementPlugin
35 changes: 0 additions & 35 deletions phpunit.xml.dist

This file was deleted.

56 changes: 56 additions & 0 deletions spec/Checker/AgreementHistoryCheckerSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace spec\BitBag\SyliusAgreementPlugin\Checker;

use BitBag\SyliusAgreementPlugin\Checker\AgreementHistoryChecker;
use BitBag\SyliusAgreementPlugin\Entity\Agreement\AgreementHistoryInterface;
use BitBag\SyliusAgreementPlugin\Entity\Agreement\AgreementInterface;
use BitBag\SyliusAgreementPlugin\Resolver\AgreementHistoryResolverInterface;
use PhpSpec\ObjectBehavior;

final class AgreementHistoryCheckerSpec extends ObjectBehavior
{
function let(
AgreementHistoryResolverInterface $agreementHistoryResolver
): void {
$this->beConstructedWith(
$agreementHistoryResolver
);
}

function it_is_initializable(): void
{
$this->shouldHaveType(AgreementHistoryChecker::class);
}

function it_resolves_agreement_correctly(
AgreementInterface $agreement,
AgreementHistoryInterface $agreementHistory,
AgreementHistoryResolverInterface $agreementHistoryResolver
): void {
$agreementHistoryResolver->resolveHistory($agreement)->willReturn($agreementHistory);
$agreementHistory->getState()->willReturn('accepted');

$this->isAgreementAccepted($agreement)->shouldReturn(true);
}

function it_not_resolves_agreement_when_state_is_other(
AgreementInterface $agreement,
AgreementHistoryInterface $agreementHistory,
AgreementHistoryResolverInterface $agreementHistoryResolver
): void {
$agreementHistoryResolver->resolveHistory($agreement)->willReturn($agreementHistory);
$agreementHistory->getState()->willReturn('rejected');

$this->isAgreementAccepted($agreement)->shouldReturn(false);
}

}
79 changes: 79 additions & 0 deletions spec/DataModifier/AgreementHistoryModifierSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace spec\BitBag\SyliusAgreementPlugin\DataModifier;

use BitBag\SyliusAgreementPlugin\DataModifier\AgreementHistoryModifier;
use BitBag\SyliusAgreementPlugin\Entity\Agreement\AgreementHistoryInterface;
use BitBag\SyliusAgreementPlugin\Entity\Agreement\AgreementInterface;
use BitBag\SyliusAgreementPlugin\Resolver\AgreementHistoryResolverInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShopUserInterface;

final class AgreementHistoryModifierSpec extends ObjectBehavior
{
function let(
AgreementHistoryResolverInterface $agreementHistoryResolver
): void {
$this->beConstructedWith(
$agreementHistoryResolver
);
}

function it_is_initializable(): void
{
$this->shouldHaveType(AgreementHistoryModifier::class);
}

function it_returns_agreement_history_from_repository(
OrderInterface $order,
ShopUserInterface $shopUser,
AgreementInterface $agreement,
AgreementHistoryResolverInterface $agreementHistoryResolver,
AgreementHistoryInterface $agreementHistory
): void {
$agreementHistoryResolver->resolveHistory($agreement)->willReturn($agreementHistory);
$agreementHistory->getId()->willReturn('1');

$agreementHistory->setContext('registration_form')->shouldNotBeCalled();

$this->setAgreementHistoryProperties(
'registration_form',
$order,
$shopUser,
$agreement
)->shouldReturn($agreementHistory);
}

function it_sets_data_when_history_id_is_null(
OrderInterface $order,
ShopUserInterface $shopUser,
AgreementInterface $agreement,
AgreementHistoryResolverInterface $agreementHistoryResolver,
AgreementHistoryInterface $agreementHistory
): void {
$agreementHistoryResolver->resolveHistory($agreement)->willReturn($agreementHistory);
$agreementHistory->getId()->willReturn(null);

$agreementHistory->setContext('registration_form')->shouldBeCalled();
$agreementHistory->setShopUser($shopUser)->shouldBeCalled();
$agreementHistory->setOrder($order)->shouldBeCalled();
$agreementHistory->setAgreement($agreement)->shouldBeCalled();

$this->setAgreementHistoryProperties(
'registration_form',
$order,
$shopUser,
$agreement
)->shouldReturn($agreementHistory);
}

}
Loading

0 comments on commit 0398688

Please sign in to comment.