Skip to content

Commit

Permalink
Merge branch '1.2'
Browse files Browse the repository at this point in the history
* 1.2:
  Fix docs build
  Make tests great again
  Remove vendorPath command-line argument from root gulpfile
  Corrected the example in docs and added custom factory for testing
  Add ability to use a custom service as factory
  • Loading branch information
pamil committed Jun 1, 2018
2 parents 8bcfe43 + 03e24d5 commit 482f778
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,28 @@ Additionally, if you want to provide your custom method with arguments from the
With this configuration, ``$factory->createNewWithAuthor($request->get('author'))`` will be called to create new resource within the ``createAction``.

Using Custom Factory Service
----------------------------

If you would like to use your own service to create the resource, then try the following configuration:

.. code-block:: yaml
# app/config/routing.yml
app_book_create:
path: /{authorId}/books/new
methods: [GET, POST]
defaults:
_controller: app.controller.book:createAction
_sylius:
factory:
method: ["expr:service('app.factory.custom_book_factory')", "createNewByAuthorId"]
arguments: $authorId
With this configuration, service with id "app.factory.custom_book_factory" will be called to create new resource within the ``createNewByAuthorId`` method and the author id from the url as argument.

Custom Redirect After Success
-----------------------------

Expand Down
11 changes: 0 additions & 11 deletions gulpfile.babel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import chug from 'gulp-chug';
import gulp from 'gulp';
import upath from 'path';
import yargs from 'yargs';

const { argv } = yargs
Expand All @@ -11,12 +10,6 @@ const { argv } = yargs
requiresArg: true,
required: false,
},
vendorPath: {
description: '<path> path to vendor directory',
type: 'string',
requiresArg: true,
required: false,
},
nodeModulesPath: {
description: '<path> path to node_modules directory',
type: 'string',
Expand All @@ -28,10 +21,6 @@ const { argv } = yargs
const config = [
'--rootPath',
argv.rootPath || '../../../../web/assets',
...(argv.vendorPath ? [
'--vendorPath',
upath.joinSafe(argv.vendorPath, 'sylius/sylius/src/Sylius/Bundle'),
] : []),
'--nodeModulesPath',
argv.nodeModulesPath || '../../../../node_modules',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public function create(RequestConfiguration $requestConfiguration, FactoryInterf
return $factory->createNew();
}

if (is_array($method) && 2 === count($method)) {
$factory = $method[0];
$method = $method[1];
}

$arguments = array_values($requestConfiguration->getFactoryArguments());

return $factory->$method(...$arguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,19 @@ function it_calls_proper_factory_methods_based_on_configuration(

$this->create($requestConfiguration, $factory)->shouldReturn($resource);
}

function it_calls_proper_service_based_on_configuration(
RequestConfiguration $requestConfiguration,
FactoryInterface $factory,
FactoryInterface $customFactory,
ResourceInterface $resource
): void {
$requestConfiguration->getFactoryMethod()->willReturn([$customFactory, 'createNew']);
$requestConfiguration->getFactoryArguments()->willReturn(['foo', 'bar']);

$customFactory->createNew('foo', 'bar')->willReturn($resource);
$factory->createNew()->shouldNotBeCalled();

$this->create($requestConfiguration, $factory)->shouldReturn($resource);
}
}
7 changes: 7 additions & 0 deletions src/Sylius/Bundle/ResourceBundle/test/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,10 @@ services:
arguments:
- ["pl_PL", "en_US", "de_DE"]
- "en_US"

test.custom_book_factory:
class: AppBundle\Factory\CustomBookFactory
public: true
arguments:
- "%app.model.book.class%"
- "@test.translation_locale_provider"
9 changes: 9 additions & 0 deletions src/Sylius/Bundle/ResourceBundle/test/app/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ app_book_filterable_index:
app_versioned_book:
resource: "versioned_routing.yml"
prefix: /v{version}

app_create_book_with_custom_factory:
path: /books/create-custom
methods: [POST]
defaults:
_controller: app.controller.book:createAction
_sylius:
factory:
method: ["expr:service('test.custom_book_factory')", "createCustom"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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 AppBundle\Factory;

use AppBundle\Entity\Book;
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;

final class CustomBookFactory
{
/** @var string */
private $className;

/** @var TranslationLocaleProviderInterface */
private $localeProvider;

public function __construct(string $className, TranslationLocaleProviderInterface $localeProvider)
{
$this->className = $className;
$this->localeProvider = $localeProvider;
}

public function createCustom(): Book
{
/** @var Book $book */
$book = new $this->className;

$book->setCurrentLocale($this->localeProvider->getDefaultLocaleCode());
$book->setFallbackLocale($this->localeProvider->getDefaultLocaleCode());

return $book;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,26 @@ public function it_applies_filtering_for_existing_field()

$this->assertResponseCode($response, Response::HTTP_OK);
}

/**
* @test
*/
public function it_allows_creating_a_book_via_custom_factory()
{
$data =
<<<EOT
{
"translations": {
"en_US": {
"title": "Star Wars: Dark Disciple"
}
},
"author": "Christie Golden"
}
EOT;

$this->client->request('POST', '/books/create-custom', [], [], ['CONTENT_TYPE' => 'application/json'], $data);
$response = $this->client->getResponse();
$this->assertResponse($response, 'books/create_response', Response::HTTP_CREATED);
}
}

0 comments on commit 482f778

Please sign in to comment.