Skip to content

Commit

Permalink
add support for Symfony 5 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
grossmannmartin authored Jul 12, 2022
2 parents 5d85681 + 0f11c07 commit af92429
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 80 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/installation-and-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ jobs:
installation-and-tests:
name: Install application and run tests
runs-on: 'ubuntu-20.04'
strategy:
matrix:
composer-preferred-dependencies: [ '--prefer-lowest', '' ]
fail-fast: false
steps:
- name: Setup
uses: shivammathur/setup-php@v2
Expand All @@ -16,6 +20,8 @@ jobs:
with:
ref: ${{ github.ref }}
- name: Install composer dependencies
run: composer update --prefer-source
run: composer update --optimize-autoloader --no-interaction ${{ matrix.composer-preferred-dependencies }}
- name: Run tests
run: vendor/bin/phpunit
run: composer tests
- name: Run PhpStan
run: composer phpstan
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@
],
"require": {
"php": "^8.1",
"symfony/form": "^3.0|^4.0"
"symfony/form": "^4.0|^5.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.0",
"phpunit/phpunit": "^9.5",
"symfony/phpunit-bridge": "^3.0|^4.0"
"symfony/phpunit-bridge": "^4.0|^5.0",
"phpstan/phpstan": "^1.8"
},
"autoload": {
"psr-4": { "Ivory\\OrderedForm\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Ivory\\Tests\\OrderedForm\\": "tests/" }
},
"scripts": {
"phpstan": "phpstan analyse --level=5 src tests",
"tests": "SYMFONY_DEPRECATIONS_HELPER=weak phpunit"
}
}
3 changes: 2 additions & 1 deletion src/Extension/AbstractOrderedExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Ivory\OrderedForm\Extension;

use Ivory\OrderedForm\Builder\OrderedFormBuilder;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand All @@ -21,7 +22,7 @@
abstract class AbstractOrderedExtension extends AbstractTypeExtension
{
/**
* {@inheritdoc}
* @param OrderedFormBuilder $builder
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
Expand Down
5 changes: 4 additions & 1 deletion src/Orderer/FormOrderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Ivory\OrderedForm\Orderer;

use Ivory\OrderedForm\Exception\OrderedConfigurationException;
use Ivory\OrderedForm\OrderedFormConfigInterface;
use Symfony\Component\Form\FormInterface;

/**
Expand Down Expand Up @@ -52,7 +53,9 @@ public function order(FormInterface $form)
$this->reset();

foreach ($form as $child) {
$position = $child->getConfig()->getPosition();
/** @var OrderedFormConfigInterface $formConfig */
$formConfig = $child->getConfig();
$position = $formConfig->getPosition();

if (empty($position)) {
$this->processEmptyPosition($child);
Expand Down
29 changes: 12 additions & 17 deletions tests/Builder/AbstractOrderedBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
*/
abstract class AbstractOrderedBuilderTest extends TestCase
{
/**
* @var OrderedFormConfigBuilderInterface
*/
private $builder;
private OrderedFormConfigInterface&OrderedFormConfigBuilderInterface $builder;

/**
* {@inheritdoc}
Expand All @@ -35,70 +32,68 @@ protected function setUp(): void
$this->builder = $this->createOrderedBuilder();
}

/**
* @return OrderedFormConfigBuilderInterface
*/
abstract protected function createOrderedBuilder();
abstract protected function createOrderedBuilder(): OrderedFormConfigInterface&OrderedFormConfigBuilderInterface;

public function testDefaultState()
public function testDefaultState(): void
{
$this->assertInstanceOf(OrderedFormConfigInterface::class, $this->builder);
$this->assertInstanceOf(OrderedFormConfigBuilderInterface::class, $this->builder);

$this->assertNull($this->builder->getPosition());
}

public function testLockedPosition()
public function testLockedPosition(): void
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('The config builder cannot be modified anymore.');

/** @var OrderedFormConfigBuilderInterface $config */
$config = $this->builder->getFormConfig();
$config->setPosition('first');
}

public function testFirstPosition()
public function testFirstPosition(): void
{
$this->builder->setPosition('first');

$this->assertSame('first', $this->builder->getPosition());
}

public function testLastPosition()
public function testLastPosition(): void
{
$this->builder->setPosition('last');

$this->assertSame('last', $this->builder->getPosition());
}

public function testBeforePosition()
public function testBeforePosition(): void
{
$this->builder->setPosition(['before' => 'foo']);

$this->assertSame(['before' => 'foo'], $this->builder->getPosition());
}

public function testAfterPosition()
public function testAfterPosition(): void
{
$this->builder->setPosition(['after' => 'foo']);

$this->assertSame(['after' => 'foo'], $this->builder->getPosition());
}

public function testFluentInterface()
public function testFluentInterface(): void
{
$this->assertSame($this->builder, $this->builder->setPosition('first'));
}

public function testInvalidStringPosition()
public function testInvalidStringPosition(): void
{
$this->expectException(OrderedConfigurationException::class);
$this->expectExceptionMessage('The "foo" form uses position as string which can only be "first" or "last" (current: "foo").');

$this->builder->setPosition('foo');
}

public function testInvalidArrayPosition()
public function testInvalidArrayPosition(): void
{
$this->expectException(OrderedConfigurationException::class);
$this->expectExceptionMessage('The "foo" form uses position as array or you must define the "before" or "after" option (current: "bar").');
Expand Down
7 changes: 3 additions & 4 deletions tests/Builder/OrderedButtonBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
namespace Ivory\Tests\OrderedForm\Builder;

use Ivory\OrderedForm\Builder\OrderedButtonBuilder;
use Ivory\OrderedForm\Builder\OrderedFormConfigBuilderInterface;
use Ivory\OrderedForm\OrderedFormConfigInterface;

/**
* @author GeLo <[email protected]>
*/
class OrderedButtonBuilderTest extends AbstractOrderedBuilderTest
{
/**
* {@inheritdoc}
*/
protected function createOrderedBuilder()
protected function createOrderedBuilder(): OrderedFormConfigInterface&OrderedFormConfigBuilderInterface
{
return new OrderedButtonBuilder('foo', []);
}
Expand Down
14 changes: 7 additions & 7 deletions tests/Builder/OrderedFormBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Ivory\Tests\OrderedForm\Builder;

use Ivory\OrderedForm\Builder\OrderedFormBuilder;
use Ivory\OrderedForm\Builder\OrderedFormConfigBuilderInterface;
use Ivory\OrderedForm\OrderedFormConfigInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\FormFactoryInterface;

Expand All @@ -20,19 +22,17 @@
*/
class OrderedFormBuilderTest extends AbstractOrderedBuilderTest
{
/**
* {@inheritdoc}
*/
protected function createOrderedBuilder()
protected function createOrderedBuilder(): OrderedFormConfigInterface&OrderedFormConfigBuilderInterface
{
/** @var EventDispatcherInterface $eventDispacherMock */
$eventDispacherMock = $this->createMock(EventDispatcherInterface::class);
/** @var EventDispatcherInterface $eventDispatcherMock */
$eventDispatcherMock = $this->createMock(EventDispatcherInterface::class);
/** @var FormFactoryInterface $formFactoryMock */
$formFactoryMock = $this->createMock(FormFactoryInterface::class);

return new OrderedFormBuilder(
'foo',
null,
$eventDispacherMock,
$eventDispatcherMock,
$formFactoryMock
);
}
Expand Down
7 changes: 3 additions & 4 deletions tests/Builder/OrderedSubmitButtonBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@

namespace Ivory\Tests\OrderedForm\Builder;

use Ivory\OrderedForm\Builder\OrderedFormConfigBuilderInterface;
use Ivory\OrderedForm\Builder\OrderedSubmitButtonBuilder;
use Ivory\OrderedForm\OrderedFormConfigInterface;

/**
* @author GeLo <[email protected]>
*/
class OrderedSubmitButtonBuilderTest extends AbstractOrderedBuilderTest
{
/**
* {@inheritdoc}
*/
protected function createOrderedBuilder()
protected function createOrderedBuilder(): OrderedFormConfigInterface&OrderedFormConfigBuilderInterface
{
return new OrderedSubmitButtonBuilder('foo', []);
}
Expand Down
46 changes: 24 additions & 22 deletions tests/Extension/OrderedExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Ivory\OrderedForm\Builder\OrderedFormBuilder;
use Ivory\OrderedForm\Extension\OrderedExtension;
use Ivory\OrderedForm\OrderedFormConfigInterface;
use Ivory\OrderedForm\OrderedResolvedFormTypeFactory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\AbstractType;
Expand All @@ -25,63 +26,64 @@
*/
class OrderedExtensionTest extends TestCase
{
/**
* @var OrderedFormBuilder
*/
private $builder;
private OrderedFormBuilder $builder;

/**
* {@inheritdoc}
*/
protected function setUp(): void
{
$this->builder = Forms::createFormFactoryBuilder()
$builder = Forms::createFormFactoryBuilder()
->setResolvedTypeFactory(new OrderedResolvedFormTypeFactory())
->addExtension(new OrderedExtension())
->getFormFactory()
->createBuilder();

assert($builder instanceof OrderedFormBuilder);

$this->builder = $builder;
}

/**
* @param string $type
*
* @dataProvider formTypeProvider
*/
public function testEmptyPosition($type)
public function testEmptyPosition(string $type): void
{
$form = $this->builder->create('foo', $type)->getForm();

$this->assertNull($form->getConfig()->getPosition());
/** @var OrderedFormConfigInterface $formConfig */
$formConfig = $form->getConfig();

$this->assertNull($formConfig->getPosition());
}

/**
* @param string $type
*
* @dataProvider formTypeProvider
*/
public function testStringPosition($type)
public function testStringPosition(string $type): void
{
$form = $this->builder->create('foo', $type, ['position' => 'first'])->getForm();

$this->assertSame('first', $form->getConfig()->getPosition());
/** @var OrderedFormConfigInterface $formConfig */
$formConfig = $form->getConfig();

$this->assertSame('first', $formConfig->getPosition());
}

/**
* @param string $type
*
* @dataProvider formTypeProvider
*/
public function testArrayPosition($type)
public function testArrayPosition(string $type): void
{
$form = $this->builder->create('foo', $type, ['position' => ['before' => 'bar']])->getForm();

$this->assertSame(['before' => 'bar'], $form->getConfig()->getPosition());
/** @var OrderedFormConfigInterface $formConfig */
$formConfig = $form->getConfig();

$this->assertSame(['before' => 'bar'], $formConfig->getPosition());
}

/**
* @return array
* @return array<int, array<int, string>>
*/
public function formTypeProvider()
public function formTypeProvider(): array
{
$preferFqcn = method_exists(AbstractType::class, 'getBlockPrefix');

Expand Down
20 changes: 0 additions & 20 deletions tests/OrderedResolvedFormTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Ivory\OrderedForm\Orderer\FormOrderer;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
Expand All @@ -28,25 +27,6 @@
*/
class OrderedResolvedFormTypeTest extends TestCase
{
/**
* @var EventDispatcherInterface
*/
private $dispatcher;

/**
* @var FormFactoryInterface
*/
private $factory;

/**
* {@inheritdoc}
*/
protected function setUp(): void
{
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->factory = $this->createMock(FormFactoryInterface::class);
}

private function getOrderedResolvedFormTypeByFormType(FormTypeInterface $formType): OrderedResolvedFormType
{
return new OrderedResolvedFormType(
Expand Down

0 comments on commit af92429

Please sign in to comment.