8000 fix tests depending on other components' tests by xabbuh · Pull Request #33618 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

fix tests depending on other components' tests #33618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Doctrine/composer.json
Original fi 8000 le line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"doctrine/annotations": "~1.7",
"symfony/stopwatch": "~2.8|~3.0|~4.0",
"symfony/dependency-injection": "~3.4|~4.0",
"symfony/form": "^3.3.10|~4.0",
"symfony/form": "^3.4.32|^4.3.5",
"symfony/http-kernel": "~2.8|~3.0|~4.0",
"symfony/property-access": "~2.8|~3.0|~4.0",
"symfony/property-info": "~2.8|3.0|~4.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Tests\Controller\ContainerControllerResolverTest;
use Symfony\Component\HttpKernel\Test\Controller\ContainerControllerResolverTestCase;

class ControllerResolverTest extends ContainerControllerResolverTest
class ControllerResolverTest extends ContainerControllerResolverTestCase
{
public function testGetControllerOnContainerAware()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"symfony/debug": "~2.8|~3.0|~4.0",
"symfony/event-dispatcher": "~3.4|~4.0",
"symfony/http-foundation": "^3.4.13|~4.3",
"symfony/http-kernel": "^3.4.31|^4.3.4",
"symfony/http-kernel": "^3.4.32|^4.3.5",
"symfony/polyfill-mbstring": "~1.0",
"symfony/filesystem": "~2.8|~3.0|~4.0",
"symfony/finder": "~2.8|~3.0|~4.0",
Expand Down
2 changes: 0 additions & 2 deletions src/Symfony/Component/Form/Test/FormPerformanceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Symfony\Component\Form\Test;

use Symfony\Component\Form\Tests\VersionAwareTest;

/**
* Base class for performance tests.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests;
namespace Symfony\Component\Form\Test;

/**
* @internal
*/
trait VersionAwareTest
{
protected static $supportedFeatureSetVersion = 304;
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Test\FormIntegrationTestCase;
use Symfony\Component\Form\Test\VersionAwareTest;

abstract class AbstractLayoutTest extends FormIntegrationTestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Form\Tests\VersionAwareTest;
use Symfony\Component\Form\Test\VersionAwareTest;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,301 @@
<?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\HttpKernel\Test\Controller;

use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ContainerControllerResolver;

/**
* @internal
*/
class ContainerControllerResolverTestCase extends ControllerResolverTestCase
{
public function testGetControllerService()
{
$container = $this->createMockContainer();
$container->expects($this->once())
->method('has')
->with('foo')
->willReturn(true);
$container->expects($this->once())
->method('get')
->with('foo')
->willReturn($this)
;

$resolver = $this->createControllerResolver(null, $container);
$request = Request::create('/');
$request->attributes->set('_controller', 'foo:controllerMethod1');

$controller = $resolver->getController($request);

$this->assertInstanceOf(\get_class($this), $controller[0]);
$this->assertSame('controllerMethod1', $controller[1]);
}

public function testGetControllerInvokableService()
{
$invokableController = new InvokableController('bar');

$container = $this->createMockContainer();
$container->expects($this->once())
->method('has')
->with('foo')
->willReturn(true)
;
$container->expects($this->once())
->method('get')
->with('foo')
->willReturn($invokableController)
;

$resolver = $this->createControllerResolver(null, $container);
$request = Request::create('/');
$request->attributes->set('_controller', 'foo');

$controller = $resolver->getController($request);

$this->assertEquals($invokableController, $controller);
}

public function testGetControllerInvokableServiceWithClassNameAsName()
{
$invokableController = new InvokableController('bar');
$className = __NAMESPACE__.'\InvokableController';

$container = $this->createMockContainer();
$container->expects($this->once())
->method('has')
->with($className)
->willReturn(true)
;
$container->expects($this->once())
->method('get')
->with($className)
->willReturn($invokableController)
;

$resolver = $this->createControllerResolver(null, $container);
$request = Request::create('/');
$request->attributes->set('_controller', $className);

$controller = $resolver->getController($request);

$this->assertEquals($invokableController, $controller);
}

public function testNonInstantiableController()
{
$container = $this->createMockContainer();
$container->expects($this->once())
->method('has')
->with(NonInstantiableController::class)
->willReturn(false)
;

$resolver = $this->createControllerResolver(null, $container);
$request = Request::create('/');
$request->attributes->set('_controller', [NonInstantiableController::class, 'action']);

$controller = $resolver->getController($request);

$this->assertSame([NonInstantiableController::class, 'action'], $controller);
}

public function testNonConstructController()
{
$this->expectException('LogicException');
$this->expectExceptionMessage('Controller "Symfony\Component\HttpKernel\Test\Controller\ImpossibleConstructController" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?');
$container = $this->getMockBuilder(Container::class)->getMock();
$container->expects($this->at(0))
->method('has')
->with(ImpossibleConstructController::class)
->willReturn(true)
;

$container->expects($this->at(1))
->method('has')
->with(ImpossibleConstructController::class)
->willReturn(false)
;

$container->expects($this->atLeastOnce())
->method('getRemovedIds')
->with()
->willReturn([ImpossibleConstructController::class => true])
;

$resolver = $this->createControllerResolver(null, $container);
$request = Request::create('/');
$request->attributes->set('_controller', [ImpossibleConstructController::class, 'action']);

if (\PHP_VERSION_ID < 70100) {
ErrorHandler::register();
try {
$resolver->getController($request);
} finally {
restore_error_handler();
restore_exception_handler();
}
} else {
$resolver->getController($request);
}
}

public function testNonInstantiableControllerWithCorrespondingService()
{
$service = new \stdClass();

$container = $this->createMockContainer();
$container->expects($this->atLeastOnce())
->method('has')
->with(NonInstantiableController::class)
->willReturn(true)
;
$container->expects($this->atLeastOnce())
->method('get')
->with(NonInstantiableController::class)
->willReturn($service)
;

$resolver = $this->createControllerResolver(null, $container);
$request = Request::create('/');
$request->attributes->set('_controller', [NonInstantiableController::class, 'action']);

$controller = $resolver->getController($request);

$this->assertSame([$service, 'action'], $controller);
}

public function testExceptionWhenUsingRemovedControllerService()
{
$this->expectException('LogicException');
$this->expectExceptionMessage('Controller "app.my_controller" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?');
$container = $this->getMockBuilder(Container::class)->getMock();
$container->expects($this->at(0))
->method('has')
->with('app.my_controller')
->willReturn(false)
;

$container->expects($this->atLeastOnce())
->method('getRemovedIds')
->with()
->willReturn(['app.my_controller' => true])
;

$resolver = $this->createControllerResolver(null, $container);

$request = Request::create('/');
$request->attributes->set('_controller', 'app.my_controller');
$resolver->getController($request);
}

public function testExceptionWhenUsingControllerWithoutAnInvokeMethod()
{
$this->expectException('LogicException');
$this->expectExceptionMessage('Controller "app.my_controller" cannot be called without a method name. Did you forget an "__invoke" method?');
$container = $this->getMockBuilder(Container::class)->getMock();
$container->expects($this->once())
->method('has')
->with('app.my_controller')
->willReturn(true)
;
$container->expects($this->once())
->method('get')
->with('app.my_controller')
->willReturn(new ImpossibleConstructController('toto', 'controller'))
;

$resolver = $this->createControllerResolver(null, $container);

$request = Request::create('/');
$request->attributes->set('_controller', 'app.my_controller');
$resolver->getController($request);
}

/**
* @dataProvider getUndefinedControllers
*/
public function testGetControllerOnNonUndefinedFunction($controller, $exceptionName = null, $exceptionMessage = null)
{
// All this logic needs to be duplicated, since calling parent::testGetControllerOnNonUndefinedFunction will override the expected excetion and not use the regex
$resolver = $this->createControllerResolver();
$this->expectException($exceptionName);
$this->expectExceptionMessageRegExp($exceptionMessage);

$request = Request::create('/');
$request->attributes->set('_controller', $controller);
$resolver->getController($request);
}

public function getUndefinedControllers()
{
return [
['foo', \LogicException::class, '/Controller not found: service "foo" does not exist\./'],
['oof::bar', \InvalidArgumentException::class, '/Class "oof" does not exist\./'],
['stdClass', \LogicException::class, '/Controller not found: service "stdClass" does not exist\./'],
[
'Symfony\Component\HttpKernel\Test\Controller\ControllerResolverTest::bar',
\InvalidArgumentException::class,
'/.?[cC]ontroller(.*?) for URI "\/" is not callable\.( Expected method(.*) Available methods)?/',
],
];
}

protected function createControllerResolver(LoggerInterface $logger = null, ContainerInterface $container = null)
{
if (!$container) {
$container = $this->createMockContainer();
}

return new ContainerControllerResolver($container, $logger);
}

protected function createMockContainer()
{
return $this->getMockBuilder(ContainerInterface::class)->getMock();
}
}

class InvokableController
{
public function __construct($bar) // mandatory argument to prevent automatic instantiation
{
}

public function __invoke()
{
}
}

abstract class NonInstantiableController
{
public static function action()
{
}
}

class ImpossibleConstructController
{
public function __construct($toto, $controller)
{
}

public function action()
{
}
}
Loading
0