Skip to content

Commit

Permalink
avoid type error regarding setTranslator with Symfony < 4.2 while s…
Browse files Browse the repository at this point in the history
…ymfony/translation-contracts is installed

fixes #347
  • Loading branch information
craue committed Nov 26, 2019
1 parent a4572ab commit 537e0b2
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 2 deletions.
2 changes: 1 addition & 1 deletion EventListener/FlowExpiredEventListener.php
Expand Up @@ -34,7 +34,7 @@ protected function getFlowExpiredFormError() {
}

// TODO revert to one clean class definition as soon as Symfony >= 4.2 is required
if (interface_exists(TranslatorInterface::class)) {
if (!interface_exists(LegacyTranslatorInterface::class)) {
/**
* Adds a validation error to the current step's form if an expired flow is detected.
*
Expand Down
2 changes: 1 addition & 1 deletion EventListener/PreviousStepInvalidEventListener.php
Expand Up @@ -35,7 +35,7 @@ protected function getPreviousStepInvalidFormError($stepNumber) {
}

// TODO revert to one clean class definition as soon as Symfony >= 4.2 is required
if (interface_exists(TranslatorInterface::class)) {
if (!interface_exists(LegacyTranslatorInterface::class)) {
/**
* Adds a validation error to the current step's form if revalidating previous steps failed.
*
Expand Down
46 changes: 46 additions & 0 deletions Tests/EventListener/EventListenerNeedsTranslatorTest.php
@@ -0,0 +1,46 @@
<?php

namespace Craue\FormFlowBundle\Tests\EventListener;

use Craue\FormFlowBundle\Tests\UnitTestCase;
use Symfony\Component\Translation\DataCollectorTranslator;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @group unit
*
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2019 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
abstract class EventListenerNeedsTranslatorTest extends UnitTestCase {

abstract protected function getListener();

/**
* TranslatorInterface (from contracts) was introduced with Symfony 4.2, but could be installed separately by symfony/translation-contracts along with Symfony < 4.2.
*
* @dataProvider dataSetTranslator
* @doesNotPerformAssertions
*/
public function testSetTranslator($translator) {
$this->getListener()->setTranslator($translator);
}

public function dataSetTranslator() {
$translators = [
[$this->createMock(DataCollectorTranslator::class)],
];

if (interface_exists(LegacyTranslatorInterface::class)) {
// TODO remove as soon as Symfony >= 4.2 is required
$translators[] = [$this->createMock(LegacyTranslatorInterface::class)];
} else {
$translators[] = [$this->createMock(TranslatorInterface::class)];
}

return $translators;
}

}
20 changes: 20 additions & 0 deletions Tests/EventListener/FlowExpiredEventListenerTest.php
@@ -0,0 +1,20 @@
<?php

namespace Craue\FormFlowBundle\Tests\EventListener;

use Craue\FormFlowBundle\EventListener\FlowExpiredEventListener;

/**
* @group unit
*
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2019 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class FlowExpiredEventListenerTest extends EventListenerNeedsTranslatorTest {

protected function getListener() {
return new FlowExpiredEventListener();
}

}
20 changes: 20 additions & 0 deletions Tests/EventListener/PreviousStepInvalidEventListenerTest.php
@@ -0,0 +1,20 @@
<?php

namespace Craue\FormFlowBundle\Tests\EventListener;

use Craue\FormFlowBundle\EventListener\PreviousStepInvalidEventListener;

/**
* @group unit
*
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2019 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class PreviousStepInvalidEventListenerTest extends EventListenerNeedsTranslatorTest {

protected function getListener() {
return new PreviousStepInvalidEventListener();
}

}
6 changes: 6 additions & 0 deletions phpstan-config.neon
Expand Up @@ -27,6 +27,12 @@ parameters:
message: '#^Class Symfony\\Component\\HttpFoundation\\File\\UploadedFile constructor invoked with 6 parameters, 2-5 required\.$#'
path: %currentWorkingDirectory%/Storage/SerializableFile.php
# TODO remove as soon as Symfony >= 4.2 is required
-
message: '#^Class Symfony\\Component\\Translation\\TranslatorInterface not found\.$#'
paths:
- %currentWorkingDirectory%/EventListener/FlowExpiredEventListener.php
- %currentWorkingDirectory%/EventListener/PreviousStepInvalidEventListener.php
# TODO remove as soon as Symfony >= 4.2 is required
-
message: '#^Property Craue\\FormFlowBundle\\EventListener\\Base(FlowExpired|PreviousStepInvalid)EventListener::\$translator has unknown class Symfony\\Component\\Translation\\TranslatorInterface as its type\.$#'
paths:
Expand Down

0 comments on commit 537e0b2

Please sign in to comment.