Skip to content

Commit

Permalink
Add RegisterTimezoneParameterPass
Browse files Browse the repository at this point in the history
  • Loading branch information
coldic3 committed Apr 8, 2024
1 parent 451f720 commit ec1edb6
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\GridBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class RegisterTimezoneParameterPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if ($container->hasParameter('timezone')) {
return;
}

$container->setParameter('timezone', null);
}
}
2 changes: 1 addition & 1 deletion src/Bundle/Resources/config/services/field_types.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<service id="Sylius\Component\Grid\FieldTypes\DatetimeFieldType">
<argument type="service" id="sylius.grid.data_extractor" />
<argument on-invalid="null">%timezone%</argument>
<argument>%timezone%</argument>
<tag name="sylius.grid_field" type="datetime" />
</service>
<service id="sylius.grid_field.datetime" alias="Sylius\Component\Grid\FieldTypes\DatetimeFieldType" />
Expand Down
2 changes: 2 additions & 0 deletions src/Bundle/SyliusGridBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sylius\Bundle\GridBundle\DependencyInjection\Compiler\RegisterFieldTypesPass;
use Sylius\Bundle\GridBundle\DependencyInjection\Compiler\RegisterFiltersPass;
use Sylius\Bundle\GridBundle\DependencyInjection\Compiler\RegisterStubCommandsPass;
use Sylius\Bundle\GridBundle\DependencyInjection\Compiler\RegisterTimezoneParameterPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -34,6 +35,7 @@ public function build(ContainerBuilder $container): void
$container->addCompilerPass(new RegisterFiltersPass());
$container->addCompilerPass(new RegisterFieldTypesPass());
$container->addCompilerPass(new RegisterStubCommandsPass());
$container->addCompilerPass(new RegisterTimezoneParameterPass());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace DependencyInjection\Compiler;

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use Sylius\Bundle\GridBundle\DependencyInjection\Compiler\RegisterTimezoneParameterPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class RegisterTimezoneParameterPassTest extends AbstractCompilerPassTestCase
{
/** @test */
public function it_registers_null_timezone_parameter_if_it_does_not_exist(): void
{
$this->compile();

$this->assertContainerBuilderHasParameter('timezone', null);
}

/** @test */
public function it_does_nothing_if_timezone_parameter_already_exists(): void
{
$this->container->setParameter('timezone', 'UTC');

$this->compile();

$this->assertContainerBuilderHasParameter('timezone', 'UTC');
}

protected function registerCompilerPass(ContainerBuilder $container): void
{
$container->addCompilerPass(new RegisterTimezoneParameterPass());
}
}
2 changes: 1 addition & 1 deletion src/Component/spec/FieldTypes/DatetimeFieldTypeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function it_uses_data_extractor_to_obtain_data_parse_it_with_given_configuration
function it_sets_timezone_if_specified(
DataExtractorInterface $dataExtractor,
\DateTime $dateTime,
Field $field
Field $field,
): void {
$dataExtractor->get($field, ['foo' => 'bar'])->willReturn($dateTime);

Expand Down

0 comments on commit ec1edb6

Please sign in to comment.