fix tests
This commit is contained in:
parent
04c104c2a7
commit
2f79ccdc74
@ -16,9 +16,6 @@ use PHPUnit\Framework\TestCase;
|
||||
// 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\Bundle\FrameworkBundle\Test;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
@ -42,7 +39,6 @@ if (method_exists(\ReflectionMethod::class, 'hasReturnType') && (new \Reflection
|
||||
$this->doTearDown();
|
||||
}
|
||||
}
|
||||
');
|
||||
} else {
|
||||
/**
|
||||
* @internal
|
||||
|
@ -1,203 +0,0 @@
|
||||
<?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\ClassLoader\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
|
||||
use Symfony\Component\ClassLoader\ApcClassLoader;
|
||||
use Symfony\Component\ClassLoader\ClassLoader;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
class ApcClassLoaderTest extends TestCase
|
||||
{
|
||||
use ForwardCompatTestTrait;
|
||||
|
||||
private function doSetUp()
|
||||
{
|
||||
if (!(filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) && filter_var(ini_get('apc.enable_cli'), FILTER_VALIDATE_BOOLEAN))) {
|
||||
$this->markTestSkipped('The apc extension is not enabled.');
|
||||
} else {
|
||||
apcu_clear_cache();
|
||||
}
|
||||
}
|
||||
|
||||
private function doTearDown()
|
||||
{
|
||||
if (filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) && filter_var(ini_get('apc.enable_cli'), FILTER_VALIDATE_BOOLEAN)) {
|
||||
apcu_clear_cache();
|
||||
}
|
||||
}
|
||||
|
||||
public function testConstructor()
|
||||
{
|
||||
$loader = new ClassLoader();
|
||||
$loader->addPrefix('Apc\Namespaced', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
|
||||
|
||||
$loader = new ApcClassLoader('test.prefix.', $loader);
|
||||
|
||||
$this->assertEquals($loader->findFile('\Apc\Namespaced\FooBar'), apcu_fetch('test.prefix.\Apc\Namespaced\FooBar'), '__construct() takes a prefix as its first argument');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getLoadClassTests
|
||||
*/
|
||||
public function testLoadClass($className, $testClassName, $message)
|
||||
{
|
||||
$loader = new ClassLoader();
|
||||
$loader->addPrefix('Apc\Namespaced', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
|
||||
$loader->addPrefix('Apc_Pearlike_', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
|
||||
|
||||
$loader = new ApcClassLoader('test.prefix.', $loader);
|
||||
$loader->loadClass($testClassName);
|
||||
$this->assertTrue(class_exists($className), $message);
|
||||
}
|
||||
|
||||
public function getLoadClassTests()
|
||||
{
|
||||
return [
|
||||
['\\Apc\\Namespaced\\Foo', 'Apc\\Namespaced\\Foo', '->loadClass() loads Apc\Namespaced\Foo class'],
|
||||
['Apc_Pearlike_Foo', 'Apc_Pearlike_Foo', '->loadClass() loads Apc_Pearlike_Foo class'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getLoadClassFromFallbackTests
|
||||
*/
|
||||
public function testLoadClassFromFallback($className, $testClassName, $message)
|
||||
{
|
||||
$loader = new ClassLoader();
|
||||
$loader->addPrefix('Apc\Namespaced', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
|
||||
$loader->addPrefix('Apc_Pearlike_', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
|
||||
$loader->addPrefix('', [__DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/fallback']);
|
||||
|
||||
$loader = new ApcClassLoader('test.prefix.fallback', $loader);
|
||||
$loader->loadClass($testClassName);
|
||||
|
||||
$this->assertTrue(class_exists($className), $message);
|
||||
}
|
||||
|
||||
public function getLoadClassFromFallbackTests()
|
||||
{
|
||||
return [
|
||||
['\\Apc\\Namespaced\\Baz', 'Apc\\Namespaced\\Baz', '->loadClass() loads Apc\Namespaced\Baz class'],
|
||||
['Apc_Pearlike_Baz', 'Apc_Pearlike_Baz', '->loadClass() loads Apc_Pearlike_Baz class'],
|
||||
['\\Apc\\Namespaced\\FooBar', 'Apc\\Namespaced\\FooBar', '->loadClass() loads Apc\Namespaced\Baz class from fallback dir'],
|
||||
['Apc_Pearlike_FooBar', 'Apc_Pearlike_FooBar', '->loadClass() loads Apc_Pearlike_Baz class from fallback dir'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getLoadClassNamespaceCollisionTests
|
||||
*/
|
||||
public function testLoadClassNamespaceCollision($namespaces, $className, $message)
|
||||
{
|
||||
$loader = new ClassLoader();
|
||||
$loader->addPrefixes($namespaces);
|
||||
|
||||
$loader = new ApcClassLoader('test.prefix.collision.', $loader);
|
||||
$loader->loadClass($className);
|
||||
|
||||
$this->assertTrue(class_exists($className), $message);
|
||||
}
|
||||
|
||||
public function getLoadClassNamespaceCollisionTests()
|
||||
{
|
||||
return [
|
||||
[
|
||||
[
|
||||
'Apc\\NamespaceCollision\\A' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha',
|
||||
'Apc\\NamespaceCollision\\A\\B' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/beta',
|
||||
],
|
||||
'Apc\NamespaceCollision\A\Foo',
|
||||
'->loadClass() loads NamespaceCollision\A\Foo from alpha.',
|
||||
],
|
||||
[
|
||||
[
|
||||
'Apc\\NamespaceCollision\\A\\B' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/beta',
|
||||
'Apc\\NamespaceCollision\\A' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha',
|
||||
],
|
||||
'Apc\NamespaceCollision\A\Bar',
|
||||
'->loadClass() loads NamespaceCollision\A\Bar from alpha.',
|
||||
],
|
||||
[
|
||||
[
|
||||
'Apc\\NamespaceCollision\\A' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha',
|
||||
'Apc\\NamespaceCollision\\A\\B' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/beta',
|
||||
],
|
||||
'Apc\NamespaceCollision\A\B\Foo',
|
||||
'->loadClass() loads NamespaceCollision\A\B\Foo from beta.',
|
||||
],
|
||||
[
|
||||
[
|
||||
'Apc\\NamespaceCollision\\A\\B' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/beta',
|
||||
'Apc\\NamespaceCollision\\A' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha',
|
||||
],
|
||||
'Apc\NamespaceCollision\A\B\Bar',
|
||||
'->loadClass() loads NamespaceCollision\A\B\Bar from beta.',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getLoadClassPrefixCollisionTests
|
||||
*/
|
||||
public function testLoadClassPrefixCollision($prefixes, $className, $message)
|
||||
{
|
||||
$loader = new ClassLoader();
|
||||
$loader->addPrefixes($prefixes);
|
||||
|
||||
$loader = new ApcClassLoader('test.prefix.collision.', $loader);
|
||||
$loader->loadClass($className);
|
||||
|
||||
$this->assertTrue(class_exists($className), $message);
|
||||
}
|
||||
|
||||
public function getLoadClassPrefixCollisionTests()
|
||||
{
|
||||
return [
|
||||
[
|
||||
[
|
||||
'ApcPrefixCollision_A_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha/Apc',
|
||||
'ApcPrefixCollision_A_B_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/beta/Apc',
|
||||
],
|
||||
'ApcPrefixCollision_A_Foo',
|
||||
'->loadClass() loads ApcPrefixCollision_A_Foo from alpha.',
|
||||
],
|
||||
[
|
||||
[
|
||||
'ApcPrefixCollision_A_B_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/beta/Apc',
|
||||
'ApcPrefixCollision_A_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha/Apc',
|
||||
],
|
||||
'ApcPrefixCollision_A_Bar',
|
||||
'->loadClass() loads ApcPrefixCollision_A_Bar from alpha.',
|
||||
],
|
||||
[
|
||||
[
|
||||
'ApcPrefixCollision_A_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha/Apc',
|
||||
'ApcPrefixCollision_A_B_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/beta/Apc',
|
||||
],
|
||||
'ApcPrefixCollision_A_B_Foo',
|
||||
'->loadClass() loads ApcPrefixCollision_A_B_Foo from beta.',
|
||||
],
|
||||
[
|
||||
[
|
||||
'ApcPrefixCollision_A_B_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/beta/Apc',
|
||||
'ApcPrefixCollision_A_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha/Apc',
|
||||
],
|
||||
'ApcPrefixCollision_A_B_Bar',
|
||||
'->loadClass() loads ApcPrefixCollision_A_B_Bar from beta.',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
@ -1,127 +0,0 @@
|
||||
<?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\DependencyInjection\Tests\Config;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
|
||||
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
|
||||
use Symfony\Component\DependencyInjection\Config\AutowireServiceResource;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
class AutowireServiceResourceTest extends TestCase
|
||||
{
|
||||
use ForwardCompatTestTrait;
|
||||
|
||||
/**
|
||||
* @var AutowireServiceResource
|
||||
*/
|
||||
private $resource;
|
||||
private $file;
|
||||
private $class;
|
||||
private $time;
|
||||
|
||||
private function doSetUp()
|
||||
{
|
||||
$this->file = realpath(sys_get_temp_dir()).'/tmp.php';
|
||||
$this->time = time();
|
||||
touch($this->file, $this->time);
|
||||
|
||||
$this->class = __NAMESPACE__.'\Foo';
|
||||
$this->resource = new AutowireServiceResource(
|
||||
$this->class,
|
||||
$this->file,
|
||||
[]
|
||||
);
|
||||
}
|
||||
|
||||
public function testToString()
|
||||
{
|
||||
$this->assertSame('service.autowire.'.$this->class, (string) $this->resource);
|
||||
}
|
||||
|
||||
public function testSerializeUnserialize()
|
||||
{
|
||||
$unserialized = unserialize(serialize($this->resource));
|
||||
|
||||
$this->assertEquals($this->resource, $unserialized);
|
||||
}
|
||||
|
||||
public function testIsFresh()
|
||||
{
|
||||
$this->assertTrue($this->resource->isFresh($this->time), '->isFresh() returns true if the resource has not changed in same second');
|
||||
$this->assertTrue($this->resource->isFresh($this->time + 10), '->isFresh() returns true if the resource has not changed');
|
||||
$this->assertFalse($this->resource->isFresh($this->time - 86400), '->isFresh() returns false if the resource has been updated');
|
||||
}
|
||||
|
||||
public function testIsFreshForDeletedResources()
|
||||
{
|
||||
unlink($this->file);
|
||||
|
||||
$this->assertFalse($this->resource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the resource does not exist');
|
||||
}
|
||||
|
||||
public function testIsNotFreshChangedResource()
|
||||
{
|
||||
$oldResource = new AutowireServiceResource(
|
||||
$this->class,
|
||||
$this->file,
|
||||
['will_be_different']
|
||||
);
|
||||
|
||||
// test with a stale file *and* a resource that *will* be different than the actual
|
||||
$this->assertFalse($oldResource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the constructor arguments have changed');
|
||||
}
|
||||
|
||||
public function testIsFreshSameConstructorArgs()
|
||||
{
|
||||
$oldResource = AutowirePass::createResourceForClass(
|
||||
new \ReflectionClass(__NAMESPACE__.'\Foo')
|
||||
);
|
||||
|
||||
// test with a stale file *but* the resource will not be changed
|
||||
$this->assertTrue($oldResource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the constructor arguments have changed');
|
||||
}
|
||||
|
||||
public function testNotFreshIfClassNotFound()
|
||||
{
|
||||
$resource = new AutowireServiceResource(
|
||||
'Some\Non\Existent\Class',
|
||||
$this->file,
|
||||
[]
|
||||
);
|
||||
|
||||
$this->assertFalse($resource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the class no longer exists');
|
||||
}
|
||||
|
||||
private function doTearDown()
|
||||
{
|
||||
if (!file_exists($this->file)) {
|
||||
return;
|
||||
}
|
||||
|
||||
unlink($this->file);
|
||||
}
|
||||
|
||||
private function getStaleFileTime()
|
||||
{
|
||||
return $this->time - 10;
|
||||
}
|
||||
}
|
||||
|
||||
class Foo
|
||||
{
|
||||
public function __construct($foo)
|
||||
{
|
||||
}
|
||||
}
|
@ -1,445 +0,0 @@
|
||||
<?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\EventDispatcher\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
abstract class AbstractEventDispatcherTest extends TestCase
|
||||
{
|
||||
use ForwardCompatTestTrait;
|
||||
|
||||
/* Some pseudo events */
|
||||
const preFoo = 'pre.foo';
|
||||
const postFoo = 'post.foo';
|
||||
const preBar = 'pre.bar';
|
||||
const postBar = 'post.bar';
|
||||
|
||||
/**
|
||||
* @var EventDispatcher
|
||||
*/
|
||||
private $dispatcher;
|
||||
|
||||
private $listener;
|
||||
|
||||
private function doSetUp()
|
||||
{
|
||||
$this->dispatcher = $this->createEventDispatcher();
|
||||
$this->listener = new TestEventListener();
|
||||
}
|
||||
|
||||
private function doTearDown()
|
||||
{
|
||||
$this->dispatcher = null;
|
||||
$this->listener = null;
|
||||
}
|
||||
|
||||
abstract protected function createEventDispatcher();
|
||||
|
||||
public function testInitialState()
|
||||
{
|
||||
$this->assertEquals([], $this->dispatcher->getListeners());
|
||||
$this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
|
||||
$this->assertFalse($this->dispatcher->hasListeners(self::postFoo));
|
||||
}
|
||||
|
||||
public function testAddListener()
|
||||
{
|
||||
$this->dispatcher->addListener('pre.foo', [$this->listener, 'preFoo']);
|
||||
$this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo']);
|
||||
$this->assertTrue($this->dispatcher->hasListeners());
|
||||
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
|
||||
$this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
|
||||
$this->assertCount(1, $this->dispatcher->getListeners(self::preFoo));
|
||||
$this->assertCount(1, $this->dispatcher->getListeners(self::postFoo));
|
||||
$this->assertCount(2, $this->dispatcher->getListeners());
|
||||
}
|
||||
|
||||
public function testGetListenersSortsByPriority()
|
||||
{
|
||||
$listener1 = new TestEventListener();
|
||||
$listener2 = new TestEventListener();
|
||||
$listener3 = new TestEventListener();
|
||||
$listener1->name = '1';
|
||||
$listener2->name = '2';
|
||||
$listener3->name = '3';
|
||||
|
||||
$this->dispatcher->addListener('pre.foo', [$listener1, 'preFoo'], -10);
|
||||
$this->dispatcher->addListener('pre.foo', [$listener2, 'preFoo'], 10);
|
||||
$this->dispatcher->addListener('pre.foo', [$listener3, 'preFoo']);
|
||||
|
||||
$expected = [
|
||||
[$listener2, 'preFoo'],
|
||||
[$listener3, 'preFoo'],
|
||||
[$listener1, 'preFoo'],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, $this->dispatcher->getListeners('pre.foo'));
|
||||
}
|
||||
|
||||
public function testGetAllListenersSortsByPriority()
|
||||
{
|
||||
$listener1 = new TestEventListener();
|
||||
$listener2 = new TestEventListener();
|
||||
$listener3 = new TestEventListener();
|
||||
$listener4 = new TestEventListener();
|
||||
$listener5 = new TestEventListener();
|
||||
$listener6 = new TestEventListener();
|
||||
|
||||
$this->dispatcher->addListener('pre.foo', $listener1, -10);
|
||||
$this->dispatcher->addListener('pre.foo', $listener2);
|
||||
$this->dispatcher->addListener('pre.foo', $listener3, 10);
|
||||
$this->dispatcher->addListener('post.foo', $listener4, -10);
|
||||
$this->dispatcher->addListener('post.foo', $listener5);
|
||||
$this->dispatcher->addListener('post.foo', $listener6, 10);
|
||||
|
||||
$expected = [
|
||||
'pre.foo' => [$listener3, $listener2, $listener1],
|
||||
'post.foo' => [$listener6, $listener5, $listener4],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, $this->dispatcher->getListeners());
|
||||
}
|
||||
|
||||
public function testGetListenerPriority()
|
||||
{
|
||||
$listener1 = new TestEventListener();
|
||||
$listener2 = new TestEventListener();
|
||||
|
||||
$this->dispatcher->addListener('pre.foo', $listener1, -10);
|
||||
$this->dispatcher->addListener('pre.foo', $listener2);
|
||||
|
||||
$this->assertSame(-10, $this->dispatcher->getListenerPriority('pre.foo', $listener1));
|
||||
$this->assertSame(0, $this->dispatcher->getListenerPriority('pre.foo', $listener2));
|
||||
$this->assertNull($this->dispatcher->getListenerPriority('pre.bar', $listener2));
|
||||
$this->assertNull($this->dispatcher->getListenerPriority('pre.foo', function () {}));
|
||||
}
|
||||
|
||||
public function testDispatch()
|
||||
{
|
||||
$this->dispatcher->addListener('pre.foo', [$this->listener, 'preFoo']);
|
||||
$this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo']);
|
||||
$this->dispatcher->dispatch(self::preFoo);
|
||||
$this->assertTrue($this->listener->preFooInvoked);
|
||||
$this->assertFalse($this->listener->postFooInvoked);
|
||||
$this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch('noevent'));
|
||||
$this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch(self::preFoo));
|
||||
$event = new Event();
|
||||
$return = $this->dispatcher->dispatch(self::preFoo, $event);
|
||||
$this->assertSame($event, $return);
|
||||
}
|
||||
|
||||
public function testDispatchForClosure()
|
||||
{
|
||||
$invoked = 0;
|
||||
$listener = function () use (&$invoked) {
|
||||
++$invoked;
|
||||
};
|
||||
$this->dispatcher->addListener('pre.foo', $listener);
|
||||
$this->dispatcher->addListener('post.foo', $listener);
|
||||
$this->dispatcher->dispatch(self::preFoo);
|
||||
$this->assertEquals(1, $invoked);
|
||||
}
|
||||
|
||||
public function testStopEventPropagation()
|
||||
{
|
||||
$otherListener = new TestEventListener();
|
||||
|
||||
// postFoo() stops the propagation, so only one listener should
|
||||
// be executed
|
||||
// Manually set priority to enforce $this->listener to be called first
|
||||
$this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo'], 10);
|
||||
$this->dispatcher->addListener('post.foo', [$otherListener, 'postFoo']);
|
||||
$this->dispatcher->dispatch(self::postFoo);
|
||||
$this->assertTrue($this->listener->postFooInvoked);
|
||||
$this->assertFalse($otherListener->postFooInvoked);
|
||||
}
|
||||
|
||||
public function testDispatchByPriority()
|
||||
{
|
||||
$invoked = [];
|
||||
$listener1 = function () use (&$invoked) {
|
||||
$invoked[] = '1';
|
||||
};
|
||||
$listener2 = function () use (&$invoked) {
|
||||
$invoked[] = '2';
|
||||
};
|
||||
$listener3 = function () use (&$invoked) {
|
||||
$invoked[] = '3';
|
||||
};
|
||||
$this->dispatcher->addListener('pre.foo', $listener1, -10);
|
||||
$this->dispatcher->addListener('pre.foo', $listener2);
|
||||
$this->dispatcher->addListener('pre.foo', $listener3, 10);
|
||||
$this->dispatcher->dispatch(self::preFoo);
|
||||
$this->assertEquals(['3', '2', '1'], $invoked);
|
||||
}
|
||||
|
||||
public function testRemoveListener()
|
||||
{
|
||||
$this->dispatcher->addListener('pre.bar', $this->listener);
|
||||
$this->assertTrue($this->dispatcher->hasListeners(self::preBar));
|
||||
$this->dispatcher->removeListener('pre.bar', $this->listener);
|
||||
$this->assertFalse($this->dispatcher->hasListeners(self::preBar));
|
||||
$this->dispatcher->removeListener('notExists', $this->listener);
|
||||
}
|
||||
|
||||
public function testAddSubscriber()
|
||||
{
|
||||
$eventSubscriber = new TestEventSubscriber();
|
||||
$this->dispatcher->addSubscriber($eventSubscriber);
|
||||
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
|
||||
$this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
|
||||
}
|
||||
|
||||
public function testAddSubscriberWithPriorities()
|
||||
{
|
||||
$eventSubscriber = new TestEventSubscriber();
|
||||
$this->dispatcher->addSubscriber($eventSubscriber);
|
||||
|
||||
$eventSubscriber = new TestEventSubscriberWithPriorities();
|
||||
$this->dispatcher->addSubscriber($eventSubscriber);
|
||||
|
||||
$listeners = $this->dispatcher->getListeners('pre.foo');
|
||||
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
|
||||
$this->assertCount(2, $listeners);
|
||||
$this->assertInstanceOf('Symfony\Component\EventDispatcher\Tests\TestEventSubscriberWithPriorities', $listeners[0][0]);
|
||||
}
|
||||
|
||||
public function testAddSubscriberWithMultipleListeners()
|
||||
{
|
||||
$eventSubscriber = new TestEventSubscriberWithMultipleListeners();
|
||||
$this->dispatcher->addSubscriber($eventSubscriber);
|
||||
|
||||
$listeners = $this->dispatcher->getListeners('pre.foo');
|
||||
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
|
||||
$this->assertCount(2, $listeners);
|
||||
$this->assertEquals('preFoo2', $listeners[0][1]);
|
||||
}
|
||||
|
||||
public function testRemoveSubscriber()
|
||||
{
|
||||
$eventSubscriber = new TestEventSubscriber();
|
||||
$this->dispatcher->addSubscriber($eventSubscriber);
|
||||
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
|
||||
$this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
|
||||
$this->dispatcher->removeSubscriber($eventSubscriber);
|
||||
$this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
|
||||
$this->assertFalse($this->dispatcher->hasListeners(self::postFoo));
|
||||
}
|
||||
|
||||
public function testRemoveSubscriberWithPriorities()
|
||||
{
|
||||
$eventSubscriber = new TestEventSubscriberWithPriorities();
|
||||
$this->dispatcher->addSubscriber($eventSubscriber);
|
||||
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
|
||||
$this->dispatcher->removeSubscriber($eventSubscriber);
|
||||
$this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
|
||||
}
|
||||
|
||||
public function testRemoveSubscriberWithMultipleListeners()
|
||||
{
|
||||
$eventSubscriber = new TestEventSubscriberWithMultipleListeners();
|
||||
$this->dispatcher->addSubscriber($eventSubscriber);
|
||||
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
|
||||
$this->assertCount(2, $this->dispatcher->getListeners(self::preFoo));
|
||||
$this->dispatcher->removeSubscriber($eventSubscriber);
|
||||
$this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
|
||||
}
|
||||
|
||||
public function testEventReceivesTheDispatcherInstanceAsArgument()
|
||||
{
|
||||
$listener = new TestWithDispatcher();
|
||||
$this->dispatcher->addListener('test', [$listener, 'foo']);
|
||||
$this->assertNull($listener->name);
|
||||
$this->assertNull($listener->dispatcher);
|
||||
$this->dispatcher->dispatch('test');
|
||||
$this->assertEquals('test', $listener->name);
|
||||
$this->assertSame($this->dispatcher, $listener->dispatcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://bugs.php.net/bug.php?id=62976
|
||||
*
|
||||
* This bug affects:
|
||||
* - The PHP 5.3 branch for versions < 5.3.18
|
||||
* - The PHP 5.4 branch for versions < 5.4.8
|
||||
* - The PHP 5.5 branch is not affected
|
||||
*/
|
||||
public function testWorkaroundForPhpBug62976()
|
||||
{
|
||||
$dispatcher = $this->createEventDispatcher();
|
||||
$dispatcher->addListener('bug.62976', new CallableClass());
|
||||
$dispatcher->removeListener('bug.62976', function () {});
|
||||
$this->assertTrue($dispatcher->hasListeners('bug.62976'));
|
||||
}
|
||||
|
||||
public function testHasListenersWhenAddedCallbackListenerIsRemoved()
|
||||
{
|
||||
$listener = function () {};
|
||||
$this->dispatcher->addListener('foo', $listener);
|
||||
$this->dispatcher->removeListener('foo', $listener);
|
||||
$this->assertFalse($this->dispatcher->hasListeners());
|
||||
}
|
||||
|
||||
public function testGetListenersWhenAddedCallbackListenerIsRemoved()
|
||||
{
|
||||
$listener = function () {};
|
||||
$this->dispatcher->addListener('foo', $listener);
|
||||
$this->dispatcher->removeListener('foo', $listener);
|
||||
$this->assertSame([], $this->dispatcher->getListeners());
|
||||
}
|
||||
|
||||
public function testHasListenersWithoutEventsReturnsFalseAfterHasListenersWithEventHasBeenCalled()
|
||||
{
|
||||
$this->assertFalse($this->dispatcher->hasListeners('foo'));
|
||||
$this->assertFalse($this->dispatcher->hasListeners());
|
||||
}
|
||||
|
||||
public function testHasListenersIsLazy()
|
||||
{
|
||||
$called = 0;
|
||||
$listener = [function () use (&$called) { ++$called; }, 'onFoo'];
|
||||
$this->dispatcher->addListener('foo', $listener);
|
||||
$this->assertTrue($this->dispatcher->hasListeners());
|
||||
$this->assertTrue($this->dispatcher->hasListeners('foo'));
|
||||
$this->assertSame(0, $called);
|
||||
}
|
||||
|
||||
public function testDispatchLazyListener()
|
||||
{
|
||||
$called = 0;
|
||||
$factory = function () use (&$called) {
|
||||
++$called;
|
||||
|
||||
return new TestWithDispatcher();
|
||||
};
|
||||
$this->dispatcher->addListener('foo', [$factory, 'foo']);
|
||||
$this->assertSame(0, $called);
|
||||
$this->dispatcher->dispatch('foo', new Event());
|
||||
$this->dispatcher->dispatch('foo', new Event());
|
||||
$this->assertSame(1, $called);
|
||||
}
|
||||
|
||||
public function testRemoveFindsLazyListeners()
|
||||
{
|
||||
$test = new TestWithDispatcher();
|
||||
$factory = function () use ($test) { return $test; };
|
||||
|
||||
$this->dispatcher->addListener('foo', [$factory, 'foo']);
|
||||
$this->assertTrue($this->dispatcher->hasListeners('foo'));
|
||||
$this->dispatcher->removeListener('foo', [$test, 'foo']);
|
||||
$this->assertFalse($this->dispatcher->hasListeners('foo'));
|
||||
|
||||
$this->dispatcher->addListener('foo', [$test, 'foo']);
|
||||
$this->assertTrue($this->dispatcher->hasListeners('foo'));
|
||||
$this->dispatcher->removeListener('foo', [$factory, 'foo']);
|
||||
$this->assertFalse($this->dispatcher->hasListeners('foo'));
|
||||
}
|
||||
|
||||
public function testPriorityFindsLazyListeners()
|
||||
{
|
||||
$test = new TestWithDispatcher();
|
||||
$factory = function () use ($test) { return $test; };
|
||||
|
||||
$this->dispatcher->addListener('foo', [$factory, 'foo'], 3);
|
||||
$this->assertSame(3, $this->dispatcher->getListenerPriority('foo', [$test, 'foo']));
|
||||
$this->dispatcher->removeListener('foo', [$factory, 'foo']);
|
||||
|
||||
$this->dispatcher->addListener('foo', [$test, 'foo'], 5);
|
||||
$this->assertSame(5, $this->dispatcher->getListenerPriority('foo', [$factory, 'foo']));
|
||||
}
|
||||
|
||||
public function testGetLazyListeners()
|
||||
{
|
||||
$test = new TestWithDispatcher();
|
||||
$factory = function () use ($test) { return $test; };
|
||||
|
||||
$this->dispatcher->addListener('foo', [$factory, 'foo'], 3);
|
||||
$this->assertSame([[$test, 'foo']], $this->dispatcher->getListeners('foo'));
|
||||
|
||||
$this->dispatcher->removeListener('foo', [$test, 'foo']);
|
||||
$this->dispatcher->addListener('bar', [$factory, 'foo'], 3);
|
||||
$this->assertSame(['bar' => [[$test, 'foo']]], $this->dispatcher->getListeners());
|
||||
}
|
||||
}
|
||||
|
||||
class CallableClass
|
||||
{
|
||||
public function __invoke()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class TestEventListener
|
||||
{
|
||||
public $preFooInvoked = false;
|
||||
public $postFooInvoked = false;
|
||||
|
||||
/* Listener methods */
|
||||
|
||||
public function preFoo(Event $e)
|
||||
{
|
||||
$this->preFooInvoked = true;
|
||||
}
|
||||
|
||||
public function postFoo(Event $e)
|
||||
{
|
||||
$this->postFooInvoked = true;
|
||||
|
||||
$e->stopPropagation();
|
||||
}
|
||||
}
|
||||
|
||||
class TestWithDispatcher
|
||||
{
|
||||
public $name;
|
||||
public $dispatcher;
|
||||
|
||||
public function foo(Event $e, $name, $dispatcher)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->dispatcher = $dispatcher;
|
||||
}
|
||||
}
|
||||
|
||||
class TestEventSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return ['pre.foo' => 'preFoo', 'post.foo' => 'postFoo'];
|
||||
}
|
||||
}
|
||||
|
||||
class TestEventSubscriberWithPriorities implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
'pre.foo' => ['preFoo', 10],
|
||||
'post.foo' => ['postFoo'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class TestEventSubscriberWithMultipleListeners implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return ['pre.foo' => [
|
||||
['preFoo1'],
|
||||
['preFoo2', 10],
|
||||
]];
|
||||
}
|
||||
}
|
@ -1,138 +0,0 @@
|
||||
<?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\HttpFoundation\Tests\Session\Storage\Handler;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler;
|
||||
|
||||
/**
|
||||
* @requires extension memcache
|
||||
* @group time-sensitive
|
||||
* @group legacy
|
||||
*/
|
||||
class MemcacheSessionHandlerTest extends TestCase
|
||||
{
|
||||
use ForwardCompatTestTrait;
|
||||
|
||||
const PREFIX = 'prefix_';
|
||||
const TTL = 1000;
|
||||
|
||||
/**
|
||||
* @var MemcacheSessionHandler
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
protected $memcache;
|
||||
|
||||
private function doSetUp()
|
||||
{
|
||||
if (\defined('HHVM_VERSION')) {
|
||||
$this->markTestSkipped('PHPUnit_MockObject cannot mock the Memcache class on HHVM. See https://github.com/sebastianbergmann/phpunit-mock-objects/pull/289');
|
||||
}
|
||||
|
||||
parent::setUp();
|
||||
$this->memcache = $this->getMockBuilder('Memcache')->getMock();
|
||||
$this->storage = new MemcacheSessionHandler(
|
||||
$this->memcache,
|
||||
['prefix' => self::PREFIX, 'expiretime' => self::TTL]
|
||||
);
|
||||
}
|
||||
|
||||
private function doTearDown()
|
||||
{
|
||||
$this->memcache = null;
|
||||
$this->storage = null;
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testOpenSession()
|
||||
{
|
||||
$this->assertTrue($this->storage->open('', ''));
|
||||
}
|
||||
|
||||
public function testCloseSession()
|
||||
{
|
||||
$this->assertTrue($this->storage->close());
|
||||
}
|
||||
|
||||
public function testReadSession()
|
||||
{
|
||||
$this->memcache
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with(self::PREFIX.'id')
|
||||
;
|
||||
|
||||
$this->assertEquals('', $this->storage->read('id'));
|
||||
}
|
||||
|
||||
public function testWriteSession()
|
||||
{
|
||||
$this->memcache
|
||||
->expects($this->once())
|
||||
->method('set')
|
||||
->with(self::PREFIX.'id', 'data', 0, $this->equalTo(time() + self::TTL, 2))
|
||||
->willReturn(true)
|
||||
;
|
||||
|
||||
$this->assertTrue($this->storage->write('id', 'data'));
|
||||
}
|
||||
|
||||
public function testDestroySession()
|
||||
{
|
||||
$this->memcache
|
||||
->expects($this->once())
|
||||
->method('delete')
|
||||
->with(self::PREFIX.'id')
|
||||
->willReturn(true)
|
||||
;
|
||||
|
||||
$this->assertTrue($this->storage->destroy('id'));
|
||||
}
|
||||
|
||||
public function testGcSession()
|
||||
{
|
||||
$this->assertTrue($this->storage->gc(123));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getOptionFixtures
|
||||
*/
|
||||
public function testSupportedOptions($options, $supported)
|
||||
{
|
||||
try {
|
||||
new MemcacheSessionHandler($this->memcache, $options);
|
||||
$this->assertTrue($supported);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->assertFalse($supported);
|
||||
}
|
||||
}
|
||||
|
||||
public function getOptionFixtures()
|
||||
{
|
||||
return [
|
||||
[['prefix' => 'session'], true],
|
||||
[['expiretime' => 100], true],
|
||||
[['prefix' => 'session', 'expiretime' => 200], true],
|
||||
[['expiretime' => 100, 'foo' => 'bar'], false],
|
||||
];
|
||||
}
|
||||
|
||||
public function testGetConnection()
|
||||
{
|
||||
$method = new \ReflectionMethod($this->storage, 'getMemcache');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$this->assertInstanceOf('\Memcache', $method->invoke($this->storage));
|
||||
}
|
||||
}
|
@ -1,113 +0,0 @@
|
||||
<?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\Tests\Config;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
|
||||
use Symfony\Component\HttpKernel\Config\EnvParametersResource;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
class EnvParametersResourceTest extends TestCase
|
||||
{
|
||||
use ForwardCompatTestTrait;
|
||||
|
||||
protected $prefix = '__DUMMY_';
|
||||
protected $initialEnv;
|
||||
protected $resource;
|
||||
|
||||
private function doSetUp()
|
||||
{
|
||||
$this->initialEnv = [
|
||||
$this->prefix.'1' => 'foo',
|
||||
$this->prefix.'2' => 'bar',
|
||||
];
|
||||
|
||||
foreach ($this->initialEnv as $key => $value) {
|
||||
$_SERVER[$key] = $value;
|
||||
}
|
||||
|
||||
$this->resource = new EnvParametersResource($this->prefix);
|
||||
}
|
||||
|
||||
private function doTearDown()
|
||||
{
|
||||
foreach ($_SERVER as $key => $value) {
|
||||
if (0 === strpos($key, $this->prefix)) {
|
||||
unset($_SERVER[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function testGetResource()
|
||||
{
|
||||
$this->assertSame(
|
||||
['prefix' => $this->prefix, 'variables' => $this->initialEnv],
|
||||
$this->resource->getResource(),
|
||||
'->getResource() returns the resource'
|
||||
);
|
||||
}
|
||||
|
||||
public function testToString()
|
||||
{
|
||||
$this->assertSame(
|
||||
serialize(['prefix' => $this->prefix, 'variables' => $this->initialEnv]),
|
||||
(string) $this->resource
|
||||
);
|
||||
}
|
||||
|
||||
public function testIsFreshNotChanged()
|
||||
{
|
||||
$this->assertTrue(
|
||||
$this->resource->isFresh(time()),
|
||||
'->isFresh() returns true if the variables have not changed'
|
||||
);
|
||||
}
|
||||
|
||||
public function testIsFreshValueChanged()
|
||||
{
|
||||
reset($this->initialEnv);
|
||||
$_SERVER[key($this->initialEnv)] = 'baz';
|
||||
|
||||
$this->assertFalse(
|
||||
$this->resource->isFresh(time()),
|
||||
'->isFresh() returns false if a variable has been changed'
|
||||
);
|
||||
}
|
||||
|
||||
public function testIsFreshValueRemoved()
|
||||
{
|
||||
reset($this->initialEnv);
|
||||
unset($_SERVER[key($this->initialEnv)]);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->resource->isFresh(time()),
|
||||
'->isFresh() returns false if a variable has been removed'
|
||||
);
|
||||
}
|
||||
|
||||
public function testIsFreshValueAdded()
|
||||
{
|
||||
$_SERVER[$this->prefix.'3'] = 'foo';
|
||||
|
||||
$this->assertFalse(
|
||||
$this->resource->isFresh(time()),
|
||||
'->isFresh() returns false if a variable has been added'
|
||||
);
|
||||
}
|
||||
|
||||
public function testSerializeUnserialize()
|
||||
{
|
||||
$this->assertEquals($this->resource, unserialize(serialize($this->resource)));
|
||||
}
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
<?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\Tests\DataCollector\Util;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
|
||||
use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporter;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
class ValueExporterTest extends TestCase
|
||||
{
|
||||
use ForwardCompatTestTrait;
|
||||
|
||||
/**
|
||||
* @var ValueExporter
|
||||
*/
|
||||
private $valueExporter;
|
||||
|
||||
private function doSetUp()
|
||||
{
|
||||
$this->valueExporter = new ValueExporter();
|
||||
}
|
||||
|
||||
public function testDateTime()
|
||||
{
|
||||
$dateTime = new \DateTime('2014-06-10 07:35:40', new \DateTimeZone('UTC'));
|
||||
$this->assertSame('Object(DateTime) - 2014-06-10T07:35:40+00:00', $this->valueExporter->exportValue($dateTime));
|
||||
}
|
||||
|
||||
public function testDateTimeImmutable()
|
||||
{
|
||||
$dateTime = new \DateTimeImmutable('2014-06-10 07:35:40', new \DateTimeZone('UTC'));
|
||||
$this->assertSame('Object(DateTimeImmutable) - 2014-06-10T07:35:40+00:00', $this->valueExporter->exportValue($dateTime));
|
||||
}
|
||||
|
||||
public function testIncompleteClass()
|
||||
{
|
||||
$foo = new \__PHP_Incomplete_Class();
|
||||
$array = new \ArrayObject($foo);
|
||||
$array['__PHP_Incomplete_Class_Name'] = 'AppBundle/Foo';
|
||||
$this->assertSame('__PHP_Incomplete_Class(AppBundle/Foo)', $this->valueExporter->exportValue($foo));
|
||||
}
|
||||
}
|
@ -1,232 +0,0 @@
|
||||
<?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\Ldap\Tests;
|
||||
|
||||
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
|
||||
use Symfony\Component\Ldap\Adapter\CollectionInterface;
|
||||
use Symfony\Component\Ldap\Adapter\QueryInterface;
|
||||
use Symfony\Component\Ldap\Entry;
|
||||
use Symfony\Component\Ldap\LdapClient;
|
||||
use Symfony\Component\Ldap\LdapInterface;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
class LdapClientTest extends LdapTestCase
|
||||
{
|
||||
use ForwardCompatTestTrait;
|
||||
|
||||
/** @var LdapClient */
|
||||
private $client;
|
||||
/** @var \PHPUnit_Framework_MockObject_MockObject */
|
||||
private $ldap;
|
||||
|
||||
private function doSetUp()
|
||||
{
|
||||
$this->ldap = $this->getMockBuilder(LdapInterface::class)->getMock();
|
||||
|
||||
$this->client = new LdapClient(null, 389, 3, false, false, false, $this->ldap);
|
||||
}
|
||||
|
||||
public function testLdapBind()
|
||||
{
|
||||
$this->ldap
|
||||
->expects($this->once())
|
||||
->method('bind')
|
||||
->with('foo', 'bar')
|
||||
;
|
||||
$this->client->bind('foo', 'bar');
|
||||
}
|
||||
|
||||
public function testLdapEscape()
|
||||
{
|
||||
$this->ldap
|
||||
->expects($this->once())
|
||||
->method('escape')
|
||||
->with('foo', 'bar', 'baz')
|
||||
;
|
||||
$this->client->escape('foo', 'bar', 'baz');
|
||||
}
|
||||
|
||||
public function testLdapQuery()
|
||||
{
|
||||
$this->ldap
|
||||
->expects($this->once())
|
||||
->method('query')
|
||||
->with('foo', 'bar', ['baz'])
|
||||
;
|
||||
$this->client->query('foo', 'bar', ['baz']);
|
||||
}
|
||||
|
||||
public function testLdapFind()
|
||||
{
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)->getMock();
|
||||
$collection
|
||||
->expects($this->once())
|
||||
->method('getIterator')
|
||||
->willReturn(new \ArrayIterator([
|
||||
new Entry('cn=qux,dc=foo,dc=com', [
|
||||
'cn' => ['qux'],
|
||||
'dc' => ['com', 'foo'],
|
||||
'givenName' => ['Qux'],
|
||||
]),
|
||||
new Entry('cn=baz,dc=foo,dc=com', [
|
||||
'cn' => ['baz'],
|
||||
'dc' => ['com', 'foo'],
|
||||
'givenName' => ['Baz'],
|
||||
]),
|
||||
]))
|
||||
;
|
||||
$query = $this->getMockBuilder(QueryInterface::class)->getMock();
|
||||
$query
|
||||
->expects($this->once())
|
||||
->method('execute')
|
||||
->willReturn($collection)
|
||||
;
|
||||
$this->ldap
|
||||
->expects($this->once())
|
||||
->method('query')
|
||||
->with('dc=foo,dc=com', 'bar', ['filter' => 'baz'])
|
||||
->willReturn($query)
|
||||
;
|
||||
|
||||
$expected = [
|
||||
'count' => 2,
|
||||
0 => [
|
||||
'count' => 3,
|
||||
0 => 'cn',
|
||||
'cn' => [
|
||||
'count' => 1,
|
||||
0 => 'qux',
|
||||
],
|
||||
1 => 'dc',
|
||||
'dc' => [
|
||||
'count' => 2,
|
||||
0 => 'com',
|
||||
1 => 'foo',
|
||||
],
|
||||
2 => 'givenname',
|
||||
'givenname' => [
|
||||
'count' => 1,
|
||||
0 => 'Qux',
|
||||
],
|
||||
'dn' => 'cn=qux,dc=foo,dc=com',
|
||||
],
|
||||
1 => [
|
||||
'count' => 3,
|
||||
0 => 'cn',
|
||||
'cn' => [
|
||||
'count' => 1,
|
||||
0 => 'baz',
|
||||
],
|
||||
1 => 'dc',
|
||||
'dc' => [
|
||||
'count' => 2,
|
||||
0 => 'com',
|
||||
1 => 'foo',
|
||||
],
|
||||
2 => 'givenname',
|
||||
'givenname' => [
|
||||
'count' => 1,
|
||||
0 => 'Baz',
|
||||
],
|
||||
'dn' => 'cn=baz,dc=foo,dc=com',
|
||||
],
|
||||
];
|
||||
$this->assertEquals($expected, $this->client->find('dc=foo,dc=com', 'bar', 'baz'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideConfig
|
||||
*/
|
||||
public function testLdapClientConfig($args, $expected)
|
||||
{
|
||||
$reflObj = new \ReflectionObject($this->client);
|
||||
$reflMethod = $reflObj->getMethod('normalizeConfig');
|
||||
$reflMethod->setAccessible(true);
|
||||
array_unshift($args, $this->client);
|
||||
$this->assertEquals($expected, \call_user_func_array([$reflMethod, 'invoke'], $args));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group functional
|
||||
* @requires extension ldap
|
||||
*/
|
||||
public function testLdapClientFunctional()
|
||||
{
|
||||
$config = $this->getLdapConfig();
|
||||
$ldap = new LdapClient($config['host'], $config['port']);
|
||||
$ldap->bind('cn=admin,dc=symfony,dc=com', 'symfony');
|
||||
$result = $ldap->find('dc=symfony,dc=com', '(&(objectclass=person)(ou=Maintainers))');
|
||||
|
||||
$con = @ldap_connect($config['host'], $config['port']);
|
||||
@ldap_bind($con, 'cn=admin,dc=symfony,dc=com', 'symfony');
|
||||
$search = @ldap_search($con, 'dc=symfony,dc=com', '(&(objectclass=person)(ou=Maintainers))', ['*']);
|
||||
$expected = @ldap_get_entries($con, $search);
|
||||
|
||||
$this->assertSame($expected, $result);
|
||||
}
|
||||
|
||||
public function provideConfig()
|
||||
{
|
||||
return [
|
||||
[
|
||||
['localhost', 389, 3, true, false, false],
|
||||
[
|
||||
'host' => 'localhost',
|
||||
'port' => 389,
|
||||
'encryption' => 'ssl',
|
||||
'options' => [
|
||||
'protocol_version' => 3,
|
||||
'referrals' => false,
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
['localhost', 389, 3, false, true, false],
|
||||
[
|
||||
'host' => 'localhost',
|
||||
'port' => 389,
|
||||
'encryption' => 'tls',
|
||||
'options' => [
|
||||
'protocol_version' => 3,
|
||||
'referrals' => false,
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
['localhost', 389, 3, false, false, false],
|
||||
[
|
||||
'host' => 'localhost',
|
||||
'port' => 389,
|
||||
'encryption' => 'none',
|
||||
'options' => [
|
||||
'protocol_version' => 3,
|
||||
'referrals' => false,
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
['localhost', 389, 3, false, false, false],
|
||||
[
|
||||
'host' => 'localhost',
|
||||
'port' => 389,
|
||||
'encryption' => 'none',
|
||||
'options' => [
|
||||
'protocol_version' => 3,
|
||||
'referrals' => false,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
@ -1,191 +0,0 @@
|
||||
<?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\Security\Http\Tests\Firewall;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
|
||||
use Symfony\Component\Security\Http\Firewall\DigestData;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
class DigestDataTest extends TestCase
|
||||
{
|
||||
use ForwardCompatTestTrait;
|
||||
|
||||
public function testGetResponse()
|
||||
{
|
||||
$digestAuth = new DigestData(
|
||||
'username="user", realm="Welcome, robot!", '.
|
||||
'nonce="MTM0NzMyMTgyMy42NzkzOmRlZjM4NmIzOGNjMjE0OWJiNDU0MDAxNzJmYmM1MmZl", '.
|
||||
'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '.
|
||||
'response="b52938fc9e6d7c01be7702ece9031b42"'
|
||||
);
|
||||
|
||||
$this->assertEquals('b52938fc9e6d7c01be7702ece9031b42', $digestAuth->getResponse());
|
||||
}
|
||||
|
||||
public function testGetUsername()
|
||||
{
|
||||
$digestAuth = new DigestData(
|
||||
'username="user", realm="Welcome, robot!", '.
|
||||
'nonce="MTM0NzMyMTgyMy42NzkzOmRlZjM4NmIzOGNjMjE0OWJiNDU0MDAxNzJmYmM1MmZl", '.
|
||||
'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '.
|
||||
'response="b52938fc9e6d7c01be7702ece9031b42"'
|
||||
);
|
||||
|
||||
$this->assertEquals('user', $digestAuth->getUsername());
|
||||
}
|
||||
|
||||
public function testGetUsernameWithQuote()
|
||||
{
|
||||
$digestAuth = new DigestData(
|
||||
'username="\"user\"", realm="Welcome, robot!", '.
|
||||
'nonce="MTM0NzMyMTgyMy42NzkzOmRlZjM4NmIzOGNjMjE0OWJiNDU0MDAxNzJmYmM1MmZl", '.
|
||||
'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '.
|
||||
'response="b52938fc9e6d7c01be7702ece9031b42"'
|
||||
);
|
||||
|
||||
$this->assertEquals('"user"', $digestAuth->getUsername());
|
||||
}
|
||||
|
||||
public function testGetUsernameWithQuoteAndEscape()
|
||||
{
|
||||
$digestAuth = new DigestData(
|
||||
'username="\"u\\\\\"ser\"", realm="Welcome, robot!", '.
|
||||
'nonce="MTM0NzMyMTgyMy42NzkzOmRlZjM4NmIzOGNjMjE0OWJiNDU0MDAxNzJmYmM1MmZl", '.
|
||||
'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '.
|
||||
'response="b52938fc9e6d7c01be7702ece9031b42"'
|
||||
);
|
||||
|
||||
$this->assertEquals('"u\\"ser"', $digestAuth->getUsername());
|
||||
}
|
||||
|
||||
public function testGetUsernameWithSingleQuote()
|
||||
{
|
||||
$digestAuth = new DigestData(
|
||||
'username="\"u\'ser\"", realm="Welcome, robot!", '.
|
||||
'nonce="MTM0NzMyMTgyMy42NzkzOmRlZjM4NmIzOGNjMjE0OWJiNDU0MDAxNzJmYmM1MmZl", '.
|
||||
'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '.
|
||||
'response="b52938fc9e6d7c01be7702ece9031b42"'
|
||||
);
|
||||
|
||||
$this->assertEquals('"u\'ser"', $digestAuth->getUsername());
|
||||
}
|
||||
|
||||
public function testGetUsernameWithSingleQuoteAndEscape()
|
||||
{
|
||||
$digestAuth = new DigestData(
|
||||
'username="\"u\\\'ser\"", realm="Welcome, robot!", '.
|
||||
'nonce="MTM0NzMyMTgyMy42NzkzOmRlZjM4NmIzOGNjMjE0OWJiNDU0MDAxNzJmYmM1MmZl", '.
|
||||
'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '.
|
||||
'response="b52938fc9e6d7c01be7702ece9031b42"'
|
||||
);
|
||||
|
||||
$this->assertEquals('"u\\\'ser"', $digestAuth->getUsername());
|
||||
}
|
||||
|
||||
public function testGetUsernameWithEscape()
|
||||
{
|
||||
$digestAuth = new DigestData(
|
||||
'username="\"u\\ser\"", realm="Welcome, robot!", '.
|
||||
'nonce="MTM0NzMyMTgyMy42NzkzOmRlZjM4NmIzOGNjMjE0OWJiNDU0MDAxNzJmYmM1MmZl", '.
|
||||
'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '.
|
||||
'response="b52938fc9e6d7c01be7702ece9031b42"'
|
||||
);
|
||||
|
||||
$this->assertEquals('"u\\ser"', $digestAuth->getUsername());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group time-sensitive
|
||||
*/
|
||||
public function testValidateAndDecode()
|
||||
{
|
||||
$time = microtime(true);
|
||||
$key = 'ThisIsAKey';
|
||||
$nonce = base64_encode($time.':'.md5($time.':'.$key));
|
||||
|
||||
$digestAuth = new DigestData(
|
||||
'username="user", realm="Welcome, robot!", nonce="'.$nonce.'", '.
|
||||
'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '.
|
||||
'response="b52938fc9e6d7c01be7702ece9031b42"'
|
||||
);
|
||||
|
||||
$digestAuth->validateAndDecode($key, 'Welcome, robot!');
|
||||
|
||||
sleep(1);
|
||||
|
||||
$this->assertTrue($digestAuth->isNonceExpired());
|
||||
}
|
||||
|
||||
public function testCalculateServerDigest()
|
||||
{
|
||||
$this->calculateServerDigest('user', 'Welcome, robot!', 'pass,word=password', 'ThisIsAKey', '00000001', 'MDIwODkz', 'auth', 'GET', '/path/info?p1=5&p2=5');
|
||||
}
|
||||
|
||||
public function testCalculateServerDigestWithQuote()
|
||||
{
|
||||
$this->calculateServerDigest('\"user\"', 'Welcome, \"robot\"!', 'pass,word=password', 'ThisIsAKey', '00000001', 'MDIwODkz', 'auth', 'GET', '/path/info?p1=5&p2=5');
|
||||
}
|
||||
|
||||
public function testCalculateServerDigestWithQuoteAndEscape()
|
||||
{
|
||||
$this->calculateServerDigest('\"u\\\\\"ser\"', 'Welcome, \"robot\"!', 'pass,word=password', 'ThisIsAKey', '00000001', 'MDIwODkz', 'auth', 'GET', '/path/info?p1=5&p2=5');
|
||||
}
|
||||
|
||||
public function testCalculateServerDigestEscape()
|
||||
{
|
||||
$this->calculateServerDigest('\"u\\ser\"', 'Welcome, \"robot\"!', 'pass,word=password', 'ThisIsAKey', '00000001', 'MDIwODkz', 'auth', 'GET', '/path/info?p1=5&p2=5');
|
||||
$this->calculateServerDigest('\"u\\ser\\\\\"', 'Welcome, \"robot\"!', 'pass,word=password', 'ThisIsAKey', '00000001', 'MDIwODkz', 'auth', 'GET', '/path/info?p1=5&p2=5');
|
||||
}
|
||||
|
||||
public function testIsNonceExpired()
|
||||
{
|
||||
$time = microtime(true) + 10;
|
||||
$key = 'ThisIsAKey';
|
||||
$nonce = base64_encode($time.':'.md5($time.':'.$key));
|
||||
|
||||
$digestAuth = new DigestData(
|
||||
'username="user", realm="Welcome, robot!", nonce="'.$nonce.'", '.
|
||||
'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '.
|
||||
'response="b52938fc9e6d7c01be7702ece9031b42"'
|
||||
);
|
||||
|
||||
$digestAuth->validateAndDecode($key, 'Welcome, robot!');
|
||||
|
||||
$this->assertFalse($digestAuth->isNonceExpired());
|
||||
}
|
||||
|
||||
private function doSetUp()
|
||||
{
|
||||
class_exists('Symfony\Component\Security\Http\Firewall\DigestAuthenticationListener', true);
|
||||
}
|
||||
|
||||
private function calculateServerDigest($username, $realm, $password, $key, $nc, $cnonce, $qop, $method, $uri)
|
||||
{
|
||||
$time = microtime(true);
|
||||
$nonce = base64_encode($time.':'.md5($time.':'.$key));
|
||||
|
||||
$response = md5(
|
||||
md5($username.':'.$realm.':'.$password).':'.$nonce.':'.$nc.':'.$cnonce.':'.$qop.':'.md5($method.':'.$uri)
|
||||
);
|
||||
|
||||
$digest = sprintf('username="%s", realm="%s", nonce="%s", uri="%s", cnonce="%s", nc=%s, qop="%s", response="%s"',
|
||||
$username, $realm, $nonce, $uri, $cnonce, $nc, $qop, $response
|
||||
);
|
||||
|
||||
$digestAuth = new DigestData($digest);
|
||||
|
||||
$this->assertEquals($digestAuth->getResponse(), $digestAuth->calculateServerDigest($password, $method));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user