Skip to content

Commit

Permalink
Add form attribute on SyliusCrudRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Apr 7, 2022
1 parent 20998a1 commit d08ee33
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Bundle/Routing/RouteAttributesFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public function createRouteForClass(RouteCollection $routeCollection, string $cl
$syliusOptions['serialization_version'] = $arguments['serializationVersion'];
}

if (isset($arguments['form'])) {
$syliusOptions['form'] = $arguments['form'];
}

$route = new Route(
$arguments['path'],
[
Expand Down
24 changes: 24 additions & 0 deletions src/Bundle/Tests/Routing/RoutesAttributesLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,28 @@ public function it_generates_routes_from_resource_with_schemes(): void
$this->assertEquals('/book/{id}', $route->getPath());
$this->assertEquals(['https'], $route->getSchemes());
}

/**
* @test
*/
public function it_generates_routes_from_resource_with_form(): void
{
self::bootKernel(['environment' => 'test_with_attributes']);

$container = static::$container;

$attributesLoader = $container->get('sylius.routing.loader.routes_attributes');

$routesCollection = $attributesLoader->__invoke();

$route = $routesCollection->get('register_user_with_form');
$this->assertNotNull($route);
$this->assertEquals('/users/register', $route->getPath());
$this->assertEquals([
'_controller' => 'app.controller.user:createAction',
'_sylius' => [
'form' => 'App\Form\Type\RegisterType',
],
], $route->getDefaults());
}
}
17 changes: 17 additions & 0 deletions src/Bundle/spec/Routing/RouteAttributesFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace spec\Sylius\Bundle\ResourceBundle\Routing;

use App\Entity\Route\RegisterUserWithForm;
use App\Entity\Route\ShowBook;
use App\Entity\Route\ShowBookWithCriteria;
use App\Entity\Route\ShowBookWithHost;
Expand Down Expand Up @@ -237,4 +238,20 @@ function it_generates_routes_from_resource_with_schemes(): void
Assert::eq($route->getPath(), '/book/{id}');
Assert::eq($route->getSchemes(), ['https']);
}

function it_generates_routes_from_resource_with_form(): void
{
$routeCollection = new RouteCollection();

$this->createRouteForClass($routeCollection, RegisterUserWithForm::class);

$route = $routeCollection->get('register_user_with_form');
Assert::eq($route->getPath(), '/users/register');
Assert::eq($route->getDefaults(), [
'_controller' => 'app.controller.user:createAction',
'_sylius' => [
'form' => 'App\Form\Type\RegisterType',
],
]);
}
}
2 changes: 1 addition & 1 deletion src/Bundle/spec/Routing/RoutesAttributesLoaderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function it_is_a_route_loader(): void

function it_generates_routes_from_paths(RouteAttributesFactoryInterface $routeAttributesFactory): void
{
$routeAttributesFactory->createRouteForClass(Argument::type(RouteCollection::class), Argument::type('string'))->shouldBeCalledTimes(13);
$routeAttributesFactory->createRouteForClass(Argument::type(RouteCollection::class), Argument::type('string'))->shouldBeCalledTimes(14);

$this->__invoke();
}
Expand Down
16 changes: 16 additions & 0 deletions src/Bundle/test/config/doctrine/User.orm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
App\Entity\User:
type: mappedSuperclass
table: app_user
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
username:
type: string
length: 255
password:
type: string
length: 255
29 changes: 29 additions & 0 deletions src/Bundle/test/src/Entity/Route/RegisterUserWithForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Entity\Route;

use App\Entity\User;
use App\Form\Type\RegisterType;
use Sylius\Component\Resource\Annotation\SyliusRoute;

#[SyliusRoute(
name: 'register_user_with_form',
path: '/users/register',
methods: ['GET', 'POST'],
controller: 'app.controller.user:createAction',
form: RegisterType::class
)]
final class RegisterUserWithForm extends User
{
}
50 changes: 50 additions & 0 deletions src/Bundle/test/src/Entity/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Entity;

use Sylius\Component\Resource\Model\ResourceInterface;

class User implements ResourceInterface
{
private ?int $id = null;

private ?string $username = null;

private ?string $password = null;

public function getId(): ?int
{
return $this->id;
}

public function getUsername(): ?string
{
return $this->username;
}

public function setUsername(?string $username): void
{
$this->username = $username;
}

public function getPassword(): ?string
{
return $this->password;
}

public function setPassword(?string $password): void
{
$this->password = $password;
}
}
20 changes: 20 additions & 0 deletions src/Bundle/test/src/Form/Type/RegisterType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;

final class RegisterType extends AbstractType
{
}
6 changes: 5 additions & 1 deletion src/Component/Annotation/SyliusRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ final class SyliusRoute

public ?array $vars = null;

public ?string $form = null;

public function __construct(
?string $name = null,
?string $path = null,
Expand All @@ -61,7 +63,8 @@ public function __construct(
?string $host = null,
?array $schemes = null,
?int $priority = null,
?array $vars = null
?array $vars = null,
?string $form = null
) {
$this->name = $name;
$this->path = $path;
Expand All @@ -78,5 +81,6 @@ public function __construct(
$this->schemes = $schemes;
$this->vars = $vars;
$this->priority = $priority;
$this->form = $form;
}
}

0 comments on commit d08ee33

Please sign in to comment.