This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,209 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\CoreBundle\Test;
use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Bridge\Twig\Form\TwigRendererEngineInterface;
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTranslator;
use Symfony\Component\Form\FormExtensionInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Test\TypeTestCase;
/**
* Base class for tests checking rendering of form widgets.
*
* @author Christian Gripp <mail@core23.de>
*/
abstract class AbstractWidgetTestCase extends TypeTestCase
{
/**
* @var FormExtensionInterface
*/
private $extension;
/**
* @var TwigRenderer
*/
private $renderer;
/**
* {@inheritdoc}
*/
protected function setUp()
{
// NEXT_MAJOR: remove this block when dropping symfony < 2.7 support
if (!class_exists('Symfony\Bridge\Twig\Extension\AssetExtension')) {
$this->markTestSkipped();
}
parent::setUp();
// NEXT_MAJOR: Remove BC hack when dropping symfony 2.4 support
$csrfProviderClasses = array_filter(array(
// symfony <=2.4
'Symfony\Component\Security\Csrf\CsrfTokenManagerInterface',
// symfony >=2.4
'Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface',
), 'interface_exists');
// TODO: remove the condition when dropping symfony/twig-bundle < 3.2
if (method_exists('Symfony\Bridge\Twig\AppVariable', 'getToken')) {
$this->extension = new FormExtension();
$environment = $this->getEnvironment();
$this->renderer = new TwigRenderer(
$this->getRenderingEngine($environment),
$this->createMock(current($csrfProviderClasses))
);
$runtimeLoader = $this
->getMockBuilder('Twig_RuntimeLoaderInterface')
->getMock();
$runtimeLoader->expects($this->any())
->method('load')
->with($this->equalTo('Symfony\Bridge\Twig\Form\TwigRenderer'))
->will($this->returnValue($this->renderer));
$environment->addRuntimeLoader($runtimeLoader);
} else {
$this->renderer = new TwigRenderer(
$this->getRenderingEngine(),
$this->createMock(current($csrfProviderClasses))
);
$this->extension = new FormExtension($this->renderer);
$environment = $this->getEnvironment();
}
$this->extension->initRuntime($environment);
}
/**
* @return \Twig_Environment
*/
protected function getEnvironment()
{
$loader = new StubFilesystemLoader($this->getTemplatePaths());
$environment = new \Twig_Environment($loader, array(
'strict_variables' => true,
));
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addExtension($this->extension);
return $environment;
}
/**
* Returns a list of template paths.
*
* @return string[]
*/
protected function getTemplatePaths()
{
// this is an workaround for different composer requirements and different TwigBridge installation directories
$twigPaths = array_filter(array(
// symfony/twig-bridge (running from this bundle)
__DIR__.'/../vendor/symfony/twig-bridge/Resources/views/Form',
// symfony/twig-bridge (running from other bundles)
__DIR__.'/../../../symfony/twig-bridge/Resources/views/Form',
// NEXT_MAJOR: Remove BC hacks when dropping symfony 2.3 support
// symfony/twig-bridge 2.3 (running from this bundle)
__DIR__.'/../vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Resources/views/Form',
// symfony/twig-bridge 2.3 (running from other bundles)
__DIR__.'/../../../symfony/twig-bridge/Symfony/Bridge/Twig/Resources/views/Form',
// symfony/symfony (running from this bundle)
__DIR__.'/../vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form',
// symfony/symfony (running from other bundles)
__DIR__.'/../../../symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form',
), 'is_dir');
$twigPaths[] = __DIR__.'/../Resources/views/Form';
return $twigPaths;
}
/**
* NEXT_MAJOR: uncomment and use the $environment argument.
*
* @return TwigRendererEngineInterface
*/
protected function getRenderingEngine(/* \Twig_Environment $environment = null */)
{
$environment = current(func_get_args());
if (is_null($environment) && method_exists('Symfony\Bridge\Twig\AppVariable', 'getToken')) {
@trigger_error(
'Not passing a \Twig_Environment instance to '.__METHOD__.
' is deprecated since 3.3 and will not be possible in 4.0',
E_USER_DEPRECATED
);
}
return new TwigRendererEngine(array('form_div_layout.html.twig'), $environment);
}
/**
* Renders widget from FormView, in SonataAdmin context, with optional view variables $vars. Returns plain HTML.
*
* @param FormView $view
* @param array $vars
*
* @return string
*/
final protected function renderWidget(FormView $view, array $vars = array())
{
return (string) $this->renderer->searchAndRenderBlock($view, 'widget', $vars);
}
/**
* Helper method to strip newline and space characters from html string to make comparing easier.
*
* @param string $html
*
* @return string
*/
final protected function cleanHtmlWhitespace($html)
{
return preg_replace_callback('/\s*>([^<]+)</', function ($value) {
return '>'.trim($value[1]).'<';
}, $html);
}
/**
* @param string $html
*
* @return string
*/
final protected function cleanHtmlAttributeWhitespace($html)
{
return preg_replace_callback('~<([A-Z0-9]+) \K(.*?)>~i', function ($m) {
return preg_replace('~\s*~', '', $m[0]);
}, $html);
}
/**
* NEXT_MAJOR: Remove this method when dropping support for < PHPUnit 5.4.
*
* @param string $class
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
protected function createMock($class)
{
if (is_callable('parent::createMock')) {
return parent::createMock($class);
}
return $this->getMock($class);
}
}

View File

@@ -0,0 +1,61 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\CoreBundle\Test;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Version;
class EntityManagerMockFactory
{
/**
* @param \PHPUnit_Framework_TestCase $test
* @param \Closure $qbCallback
* @param $fields
*
* @return EntityManagerInterface
*/
public static function create(\PHPUnit_Framework_TestCase $test, \Closure $qbCallback, $fields)
{
$query = $test->getMockBuilder('Doctrine\ORM\AbstractQuery')
->disableOriginalConstructor()->getMock();
$query->expects($test->any())->method('execute')->will($test->returnValue(true));
if (Version::compare('2.5.0') < 1) {
$entityManager = $test->getMockBuilder('Doctrine\ORM\EntityManagerInterface')->getMock();
$qb = $test->getMockBuilder('Doctrine\ORM\QueryBuilder')->setConstructorArgs(array($entityManager))->getMock();
} else {
$qb = $test->getMockBuilder('Doctrine\ORM\QueryBuilder')->disableOriginalConstructor()->getMock();
}
$qb->expects($test->any())->method('select')->will($test->returnValue($qb));
$qb->expects($test->any())->method('getQuery')->will($test->returnValue($query));
$qb->expects($test->any())->method('where')->will($test->returnValue($qb));
$qb->expects($test->any())->method('orderBy')->will($test->returnValue($qb));
$qb->expects($test->any())->method('andWhere')->will($test->returnValue($qb));
$qb->expects($test->any())->method('leftJoin')->will($test->returnValue($qb));
$qbCallback($qb);
$repository = $test->getMockBuilder('Doctrine\ORM\EntityRepository')->disableOriginalConstructor()->getMock();
$repository->expects($test->any())->method('createQueryBuilder')->will($test->returnValue($qb));
$metadata = $test->getMockBuilder('Doctrine\Common\Persistence\Mapping\ClassMetadata')->getMock();
$metadata->expects($test->any())->method('getFieldNames')->will($test->returnValue($fields));
$metadata->expects($test->any())->method('getName')->will($test->returnValue('className'));
$em = $test->getMockBuilder('Doctrine\ORM\EntityManager')->disableOriginalConstructor()->getMock();
$em->expects($test->any())->method('getRepository')->will($test->returnValue($repository));
$em->expects($test->any())->method('getClassMetadata')->will($test->returnValue($metadata));
return $em;
}
}

View File

@@ -0,0 +1,75 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\CoreBundle\Test;
use Symfony\Component\Translation\Exception\InvalidResourceException;
use Symfony\Component\Translation\Loader\XliffFileLoader;
abstract class XliffValidatorTestCase extends \PHPUnit_Framework_TestCase
{
/**
* @var XliffFileLoader
*/
protected $loader;
/**
* @var string[]
*/
protected $errors = array();
public function setUp()
{
$this->loader = new XliffFileLoader();
}
/**
* @dataProvider getXliffPaths
*/
public function testXliff($path)
{
$this->validatePath($path);
if (count($this->errors) > 0) {
$this->fail(sprintf('Unable to parse xliff files: %s', implode(', ', $this->errors)));
}
}
/**
* @return array List all path to validate xliff
*/
abstract public function getXliffPaths();
/**
* @param string $file The path to the xliff file
*/
protected function validateXliff($file)
{
try {
$this->loader->load($file, 'en');
$this->assertTrue(true, sprintf('Successful loading file: %s', $file));
} catch (InvalidResourceException $e) {
$this->errors[] = sprintf('%s => %s', $file, $e->getMessage());
}
}
/**
* @param string $path The path to lookup for Xliff file
*/
protected function validatePath($path)
{
$files = glob(sprintf('%s/*.xliff', $path));
foreach ($files as $file) {
$this->validateXliff($file);
}
}
}