Skip to content

Commit

Permalink
compatibnility with phpunit8
Browse files Browse the repository at this point in the history
See #30071
  • Loading branch information
garak committed Mar 7, 2019
1 parent e9c8e19 commit a045ef0
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 47 deletions.
12 changes: 1 addition & 11 deletions src/Symfony/Component/Form/Test/FormIntegrationTestCase.php
Expand Up @@ -13,28 +13,18 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\Forms;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
abstract class FormIntegrationTestCase extends TestCase
{
use FormIntegrationTestCaseSetupTrait;
/**
* @var FormFactoryInterface
*/
protected $factory;

protected function setUp()
{
$this->factory = Forms::createFormFactoryBuilder()
->addExtensions($this->getExtensions())
->addTypeExtensions($this->getTypeExtensions())
->addTypes($this->getTypes())
->addTypeGuessers($this->getTypeGuessers())
->getFormFactory();
}

protected function getExtensions()
{
return [];
Expand Down
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Test;


use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Forms;

// Auto-adapt to PHPUnit 8 that added a `void` return-type to the setUp/tearDown methods

if (method_exists(\ReflectionMethod::class, 'hasReturnType') && (new \ReflectionMethod(TestCase::class, 'tearDown'))->hasReturnType()) {
eval('
namespace Symfony\Component\Validator\Test;
/**
* @internal
*/
trait FormIntegrationTestCaseSetupTrait
{
protected function setUp(): void
{
$this->factory = Forms::createFormFactoryBuilder()
->addExtensions($this->getExtensions())
->addTypeExtensions($this->getTypeExtensions())
->addTypes($this->getTypes())
->addTypeGuessers($this->getTypeGuessers())
->getFormFactory();
}
}
');
} else {
/**
* @internal
*/
trait FormIntegrationTestCaseSetupTrait
{
/**
* @return void
*/
protected function setUp()
{
$this->factory = Forms::createFormFactoryBuilder()
->addExtensions($this->getExtensions())
->addTypeExtensions($this->getTypeExtensions())
->addTypes($this->getTypes())
->addTypeGuessers($this->getTypeGuessers())
->getFormFactory();
}
}
}
10 changes: 2 additions & 8 deletions src/Symfony/Component/Form/Test/TypeTestCase.php
Expand Up @@ -17,6 +17,8 @@

abstract class TypeTestCase extends FormIntegrationTestCase
{
use TypeTestCaseSetupTeardownTrait;

/**
* @var FormBuilder
*/
Expand All @@ -27,14 +29,6 @@ abstract class TypeTestCase extends FormIntegrationTestCase
*/
protected $dispatcher;

protected function setUp()
{
parent::setUp();

$this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
$this->builder = new FormBuilder('', null, $this->dispatcher, $this->factory);
}

protected function tearDown()
{
if (\in_array(ValidatorExtensionTrait::class, class_uses($this))) {
Expand Down
69 changes: 69 additions & 0 deletions src/Symfony/Component/Form/Test/TypeTestCaseSetupTeardownTrait.php
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Test;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\Test\Traits\ValidatorExtensionTrait;

// Auto-adapt to PHPUnit 8 that added a `void` return-type to the setUp/tearDown methods

if (method_exists(\ReflectionMethod::class, 'hasReturnType') && (new \ReflectionMethod(TestCase::class, 'tearDown'))->hasReturnType()) {
eval('
namespace Symfony\Component\Validator\Test;
/**
* @internal
*/
trait TypeTestCaseSetupTeardownTrait
{
protected function setUp(): void
{
parent::setUp();
$this->dispatcher = $this->getMockBuilder(\'Symfony\Component\EventDispatcher\EventDispatcherInterface\')->getMock();
$this->builder = new FormBuilder(\'\', null, $this->dispatcher, $this->factory);
}
protected function tearDown(): void
{
if (\in_array(ValidatorExtensionTrait::class, class_uses($this))) {
$this->validator = null;
}
}
}
');
} else {
/**
* @internal
*/
trait TypeTestCaseSetupTeardownTrait
{
/**
* @return void
*/
protected function setUp()
{
parent::setUp();

$this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
$this->builder = new FormBuilder('', null, $this->dispatcher, $this->factory);
}

protected function tearDown()
{
if (\in_array(ValidatorExtensionTrait::class, class_uses($this))) {
$this->validator = null;
}
}
}
}
Expand Up @@ -14,7 +14,6 @@
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\Context\ExecutionContext;
Expand All @@ -29,6 +28,8 @@
*/
abstract class ConstraintValidatorTestCase extends TestCase
{
use ConstraintValidatorTestCaseSetupTrait;

/**
* @var ExecutionContextInterface
*/
Expand All @@ -48,33 +49,6 @@ abstract class ConstraintValidatorTestCase extends TestCase
protected $constraint;
protected $defaultTimezone;

protected function setUp()
{
$this->group = 'MyGroup';
$this->metadata = null;
$this->object = null;
$this->value = 'InvalidValue';
$this->root = 'root';
$this->propertyPath = 'property.path';

// Initialize the context with some constraint so that we can
// successfully build a violation.
$this->constraint = new NotNull();

$this->context = $this->createContext();
$this->validator = $this->createValidator();
$this->validator->initialize($this->context);

\Locale::setDefault('en');

$this->setDefaultTimezone('UTC');
}

protected function tearDown()
{
$this->restoreDefaultTimezone();
}

protected function setDefaultTimezone($defaultTimezone)
{
// Make sure this method can not be called twice before calling
Expand Down
@@ -0,0 +1,81 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Test;


use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\NotNull;

// Auto-adapt to PHPUnit 8 that added a `void` return-type to the setUp method

if (method_exists(\ReflectionMethod::class, 'hasReturnType') && (new \ReflectionMethod(TestCase::class, 'tearDown'))->hasReturnType()) {
eval('
namespace Symfony\Component\Validator\Test;
/**
* @internal
*/
trait ConstraintValidatorTestCaseSetupTrait
{
protected function setUp(): void
{
$this->group = \'MyGroup\';
$this->metadata = null;
$this->object = null;
$this->value = \'InvalidValue\';
$this->root = \'root\';
$this->propertyPath = \'property.path\';
$this->constraint = new NotNull();
$this->context = $this->createContext();
$this->validator = $this->createValidator();
$this->validator->initialize($this->context);
\Locale::setDefault(\'en\');
$this->setDefaultTimezone(\'UTC\');
}
}
');
} else {
/**
* @internal
*/
trait ConstraintValidatorTestCaseSetupTrait
{
/**
* @return void
*/
protected function setUp()
{
$this->group = 'MyGroup';
$this->metadata = null;
$this->object = null;
$this->value = 'InvalidValue';
$this->root = 'root';
$this->propertyPath = 'property.path';

// Initialize the context with some constraint so that we can
// successfully build a violation.
$this->constraint = new NotNull();

$this->context = $this->createContext();
$this->validator = $this->createValidator();
$this->validator->initialize($this->context);

\Locale::setDefault('en');

$this->setDefaultTimezone('UTC');
}
}
}

0 comments on commit a045ef0

Please sign in to comment.