Skip to content

Commit

Permalink
Merge pull request #58 from Nek-/update/symfony6-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Nek- authored Jan 14, 2022
2 parents b8e80f9 + 151d99c commit 9a61968
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
composer.lock
vendor
.php_cs.cache
.php-cs-fixer.cache
tests/TestApplication/var
.couscous
.phpunit.result.cache
4 changes: 2 additions & 2 deletions .php_cs.dist → .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
->exclude(['tests/TestApplication/var', 'vendor'])
;

return PhpCsFixer\Config::create()
return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'align_multiline_comment' => true,
'binary_operator_spaces' => ['default' => 'single_space'],
'blank_line_before_statement' => ['statements' => ['return', 'throw']],
'concat_space' => ['spacing' => 'one'],
'no_extra_consecutive_blank_lines' => ['tokens' => ['break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block']],
'no_extra_blank_lines' => ['tokens' => ['break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block']],
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => true,
Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
"description": "Library that helps you build APIs",
"type": "symfony-bundle",
"scripts": {
"phpcs": "PHP_CS_FIXER_IGNORE_ENV=true php-cs-fixer fix --using-cache=false",
"phpcs": "PHP_CS_FIXER_IGNORE_ENV=true php-cs-fixer fix --using-cache=no",
"test-app": "@php -S localhost:8000 -t tests/TestApplication/public",
"post-install-cmd": "make hooks.install"
},
"require": {
"php": ">=7.3",
"nekland/tools": "^2.5.1",
"symfony/event-dispatcher": "^5.3",
"symfony/event-dispatcher": "^5.3 || ^6.0",
"pagerfanta/pagerfanta": "^2.0.1 || ^3.0.0",
"symfony/yaml": "^5.3",
"symfony/serializer": "^5.3",
"symfony/yaml": "^5.3 || ^6.0",
"symfony/serializer": "^5.3 || ^6.0",
"pagerfanta/doctrine-orm-adapter": "^3.5"
},
"require-dev": {
"psr/container": "^v1.1.1",
"psr/container": "^v1.1.1 || ^2.0",
"phpunit/phpunit": "^8.5",
"friendsofphp/php-cs-fixer": "^2.17",
"friendsofphp/php-cs-fixer": "^v3.3.2",
"phpspec/prophecy": "^1.8",
"twig/twig": "^2.5",
"symfony/symfony": "^5.3",
"symfony/symfony": "^5.3 || ^6.0",
"doctrine/orm": "^v2.6.6",
"justinrainbow/json-schema": "^5.2",
"doctrine/annotations": "^1.8",
"doctrine/doctrine-bundle": "^2.1",
"behat/behat": "3.10.0",
"friends-of-behat/symfony-extension": "2.2.1",
"friends-of-behat/symfony-extension": "^2.3.0",
"fakerphp/faker": "^1.12.1"
},
"autoload": {
Expand Down
6 changes: 3 additions & 3 deletions src/DependencyInjection/MelodiiaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

class MelodiiaExtension extends Extension
{
const TAG_CRUD_FILTER = 'melodiia.crud_filter';
const TAG_CONTEXT_BUILDER = 'melodiia.context_builder';
public const TAG_CRUD_FILTER = 'melodiia.crud_filter';
public const TAG_CONTEXT_BUILDER = 'melodiia.context_builder';

public function load(array $configs, ContainerBuilder $container)
{
Expand Down Expand Up @@ -76,7 +76,7 @@ private function getServiceName(string $apiName, string $serviceName)
return 'melodiia.' . $apiName . '.' . $serviceName;
}

public function getAlias()
public function getAlias(): string
{
return 'melodiia';
}
Expand Down
5 changes: 3 additions & 2 deletions src/Form/ApiRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ public function handleRequest(FormInterface $form, $request = null)

/**
* Notice: this impacts allow_file_upload field.
* {@inheritdoc}
*
* @param mixed $data
*/
public function isFileUpload($data)
public function isFileUpload($data): bool
{
return false;
}
Expand Down
35 changes: 27 additions & 8 deletions src/Form/DomainObjectsDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@

namespace SwagIndustries\Melodiia\Form;

use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpKernel\Kernel;

/**
* Add support for objects that require constructor instantiation.
*
* @internal use DomainObjectsDataMapper instead
*/
class DomainObjectsDataMapper extends PropertyPathMapper implements DomainObjectDataMapperInterface
class DomainObjectsDataMapperBase extends DataMapper implements DomainObjectDataMapperInterface
{
public function mapFormsToData($forms, &$data)
{
$data = $this->createObject($forms, !empty($data) && is_object($data) ? get_class($data) : null);
parent::mapFormsToData($forms, $data);
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -69,3 +66,25 @@ public function createObject(iterable $form, string $dataClass = null)
return new $dataClass(...$constructorData);
}
}

if (version_compare(Kernel::VERSION, '6.0', '>=')) {
class DomainObjectsDataMapper extends DomainObjectsDataMapperBase
{
public function mapFormsToData(\Traversable $forms, mixed &$data): void
{
$data = $this->createObject($forms, !empty($data) && is_object($data) ? get_class($data) : null);
parent::mapFormsToData($forms, $data);
}
}
} else {
// BC Layer for PHP 7.4
// Because mixed type is not supported!
class DomainObjectsDataMapper extends DomainObjectsDataMapperBase
{
public function mapFormsToData(iterable $forms, &$data): void
{
$data = $this->createObject($forms, !empty($data) && is_object($data) ? get_class($data) : null);
parent::mapFormsToData($forms, $data);
}
}
}
3 changes: 2 additions & 1 deletion src/MelodiiaBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
namespace SwagIndustries\Melodiia;

use SwagIndustries\Melodiia\DependencyInjection\MelodiiaExtension;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class MelodiiaBundle extends Bundle
{
/**
* {@inheritdoc}
*/
public function getContainerExtension()
public function getContainerExtension(): ?ExtensionInterface
{
return new MelodiiaExtension();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Serialization/Json/CreatedNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class CreatedNormalizer implements NormalizerInterface
{
public function normalize($object, $format = null, array $context = [])
public function normalize($object, string $format = null, array $context = [])
{
$res = [];
$resource = $object->getResourceId();
Expand All @@ -22,7 +22,7 @@ public function normalize($object, $format = null, array $context = [])
return $res;
}

public function supportsNormalization($data, $format = null)
public function supportsNormalization($data, string $format = null)
{
return is_object($data) && $data instanceof Created;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Serialization/Json/OkContentNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function normalize($object, $format = null, array $context = [])
'currentPage' => $content->getCurrentPage(),
'maxPerPage' => $content->getMaxPerPage(),
];
$uri = $this->requestStack->getMasterRequest()->getUri();
$uri = $this->requestStack->getMainRequest()->getUri();
$previousPage = null;
$nextPage = null;

Expand All @@ -83,7 +83,7 @@ public function normalize($object, $format = null, array $context = [])
];
}
if ($content instanceof Collection) {
$uri = $this->requestStack->getMasterRequest()->getUri();
$uri = $this->requestStack->getMainRequest()->getUri();
$result['meta'] = [
'totalPages' => 1,
'totalResults' => count($content),
Expand Down
4 changes: 2 additions & 2 deletions tests/Melodiia/Crud/Controller/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function setUp(): void
$this->attributes->get(CrudControllerInterface::SECURITY_CHECK, null)->willReturn(null);
$this->attributes->getBoolean(CrudControllerInterface::FORM_CLEAR_MISSING, true)->willReturn(true);
$this->request->attributes = $this->attributes->reveal();
$this->form->handleRequest($this->request)->willReturn();
$this->form->handleRequest($this->request)->willReturn($this->form->reveal());
$this->formFactory->createNamed('', Argument::cetera())->willReturn($this->form);

$this->controller = new Create(
Expand Down Expand Up @@ -117,7 +117,7 @@ public function testICanChangeTheClearSubmitParam()
$this->mockDispatch($this->dispatcher, Argument::type(CustomResponseEvent::class), Create::EVENT_POST_CREATE)->shouldBeCalled();
$this->dataStore->save(Argument::type(FakeMelodiiaModel::class))->shouldBeCalled();
$this->attributes->getBoolean(CrudControllerInterface::FORM_CLEAR_MISSING, true)->willReturn(false);
$this->form->handleRequest($this->request)->willReturn()->shouldBeCalled();
$this->form->handleRequest($this->request)->willReturn($this->form->reveal())->shouldBeCalled();
/** @var ApiResponse $res */
$res = ($this->controller)($this->request->reveal());

Expand Down
17 changes: 10 additions & 7 deletions tests/Melodiia/Crud/Controller/GetAllTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class GetAllTest extends TestCase
/** @var PaginationRequest|ObjectProphecy */
private $paginationRequest;

/** @var FormInterface|ObjectProphecy */
private $form;

/** @var GetAll */
private $controller;

Expand All @@ -71,8 +74,11 @@ public function setUp(): void
$this->request->attributes = $this->attributes->reveal();
$this->request->query = $this->queries->reveal();

$this->form = $this->prophesize(FormInterface::class);
$this->form->handleRequest(Argument::any())->willReturn($this->form->reveal());
$this->form->isSubmitted()->willReturn(false);
$this->filtersCollection = $this->prophesize(FilterCollection::class);
$this->filtersCollection->getForm()->willReturn($this->prophesize(FormInterface::class)->reveal());
$this->filtersCollection->getForm()->willReturn($this->form->reveal());
$this->filtersFactory = $this->prophesize(FilterCollectionFactoryInterface::class);
$this->filtersFactory->createCollection(Argument::cetera())->willReturn($this->filtersCollection->reveal());

Expand Down Expand Up @@ -129,13 +135,10 @@ public function testItCheckAccessToResourceIfSpecifiedInConfiguration()
public function testItReturnsErrorFromFilters()
{
$request = $this->request->reveal();
/** @var FormInterface|ObjectProphecy $form */
$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled()->willReturn();
$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(false);
$this->form->handleRequest($request)->shouldBeCalled()->willReturn($this->form->reveal());
$this->form->isSubmitted()->willReturn(true);
$this->form->isValid()->willReturn(false);

$this->filtersCollection->getForm()->willReturn($form->reveal());
$this->dataStore->getPaginated(Argument::cetera())->shouldNotBeCalled();

$res = ($this->controller)($request);
Expand Down
2 changes: 1 addition & 1 deletion tests/Melodiia/Crud/Controller/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function setUp(): void
$this->attributes->has(CrudControllerInterface::FORM_CLEAR_MISSING)->willReturn(false);
$this->attributes->getBoolean(CrudControllerInterface::FORM_CLEAR_MISSING, false)->willReturn(false);
$this->request->attributes = $this->attributes->reveal();
$this->form->handleRequest($this->request)->willReturn();
$this->form->handleRequest($this->request)->willReturn($this->form->reveal());
$this->formFactory->createNamed('', Argument::cetera())->willReturn($this->form);

$this->dataStore->find(FakeMelodiiaModel::class, 'id')->willReturn(new \stdClass());
Expand Down
4 changes: 2 additions & 2 deletions tests/Melodiia/Crud/FilterCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testItBuildAForm()
{
/** @var FormBuilderInterface|ObjectProphecy $builder */
$builder = $this->prophesize(FormBuilderInterface::class);
$builder->add('fake', TextType::class)->shouldBeCalled();
$builder->add('fake', TextType::class)->willReturn($builder->reveal())->shouldBeCalled();
$builder->getForm()->willReturn($form = $this->prophesize(FormInterface::class)->reveal())->shouldBeCalled();
$this->formFactory->createNamedBuilder(Argument::cetera())->willReturn($builder->reveal());

Expand All @@ -67,7 +67,7 @@ public function testItBuildsAFormEvenWithNoFilters()
{
/** @var FormBuilderInterface|ObjectProphecy $builder */
$builder = $this->prophesize(FormBuilderInterface::class);
$builder->add('fake', TextType::class)->shouldNotBeCalled();
$builder->add('fake', TextType::class)->willReturn($builder->reveal())->shouldNotBeCalled();
$builder->getForm()->willReturn($form = $this->prophesize(FormInterface::class)->reveal())->shouldBeCalled();
$this->formFactory->createNamedBuilder(Argument::cetera())->willReturn($builder->reveal());

Expand Down
12 changes: 6 additions & 6 deletions tests/Melodiia/Form/ApiRequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,24 @@ public function tearDown(): void

public function testItSubmitJsonData()
{
$this->form->submit(['hello' => 'foo'], false)->shouldBeCalled();
$this->form->submit(['hello' => 'foo'], false)->willReturn($this->form->reveal())->shouldBeCalled();
$this->request->getContent()->willReturn('{"hello":"foo"}');

$this->subject->handleRequest($this->form->reveal(), $this->request->reveal());
}

public function testItAddsAFormErrorInCaseOfWrongInput()
{
$this->form->submit(null, false)->shouldBeCalled();
$this->form->addError(Argument::type(FormError::class))->shouldBeCalled();
$this->form->submit(null, false)->willReturn($this->form->reveal())->shouldBeCalled();
$this->form->addError(Argument::type(FormError::class))->willReturn($this->form->reveal())->shouldBeCalled();
$this->request->getContent()->willReturn('{"hello":"foo}');

$this->subject->handleRequest($this->form->reveal(), $this->request->reveal());
}

public function testItClearsMissingOnPatchRequest()
{
$this->form->submit(['hello' => 'foo'], true)->shouldBeCalled();
$this->form->submit(['hello' => 'foo'], true)->willReturn($this->form->reveal())->shouldBeCalled();
$this->request->getContent()->willReturn('{"hello":"foo"}');
$this->request->getMethod()->willReturn('PATCH');

Expand All @@ -76,7 +76,7 @@ public function testItClearsMissingOnPatchRequest()

public function testICanChangeClearMissingOption()
{
$this->form->submit(['hello' => 'foo'], true)->shouldBeCalled();
$this->form->submit(['hello' => 'foo'], true)->willReturn($this->form->reveal())->shouldBeCalled();
$this->formConfig->getOption(ApiType::CLEAR_MISSING_OPTION)->willReturn(true);
$this->request->getContent()->willReturn('{"hello":"foo"}');

Expand All @@ -86,7 +86,7 @@ public function testICanChangeClearMissingOption()
public function testItSupportsGetRequests()
{
$query = new ParameterBag(['hello' => 'foo']);
$this->form->submit(['hello' => 'foo'])->shouldBeCalled();
$this->form->submit(['hello' => 'foo'])->willReturn($this->form->reveal())->shouldBeCalled();
$this->request->getMethod()->willReturn('GET');
$this->request->query = $query;

Expand Down
12 changes: 7 additions & 5 deletions tests/Melodiia/Form/DomainObjectsDataMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace SwagIndustries\Melodiia\Test\Form;

use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use SwagIndustries\Melodiia\Form\DomainObjectsDataMapper;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormConfigBuilder;

Expand All @@ -21,16 +22,17 @@ public function testItIsInstanceOfDataMapper()

public function testItExtendsPropertyPathDataMapper()
{
$this->assertInstanceOf(PropertyPathMapper::class, new DomainObjectsDataMapper());
$this->assertInstanceOf(DataMapper::class, new DomainObjectsDataMapper());
}

public function testItBuildObjectFromForm()
{
$mapper = new DomainObjectsDataMapper();

$dispatcher = $this->prophesize(EventDispatcherInterface::class)->reveal();
$form1 = new Form((new FormConfigBuilder('hello', null, $dispatcher))->setData('world')->getFormConfig());
$form2 = new Form((new FormConfigBuilder('foo', null, $dispatcher))->setData('bar')->getFormConfig());
$dispatcher = $this->prophesize(EventDispatcherInterface::class);
$dispatcher->hasListeners(Argument::cetera())->willReturn(false);
$form1 = new Form((new FormConfigBuilder('hello', null, $dispatcher->reveal()))->setData('world')->getFormConfig());
$form2 = new Form((new FormConfigBuilder('foo', null, $dispatcher->reveal()))->setData('bar')->getFormConfig());

$form = new \ArrayIterator(['hello' => $form1, 'foo' => $form2]);

Expand Down
Loading

0 comments on commit 9a61968

Please sign in to comment.