View source
<?php
namespace Drupal\Tests\Component\Bridge;
use Drupal\Component\Bridge\ZfExtensionManagerSfContainer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Laminas\Feed\Reader\Extension\Atom\Entry;
use Laminas\Feed\Reader\StandaloneExtensionManager;
class ZfExtensionManagerSfContainerTest extends TestCase {
public function testGet() {
$service = new \stdClass();
$service->value = 'myvalue';
$container = new ContainerBuilder();
$container
->set('foo', $service);
$bridge = new ZfExtensionManagerSfContainer();
$bridge
->setContainer($container);
$this
->assertEquals($service, $bridge
->get('foo'));
$bridge
->setStandalone(StandaloneExtensionManager::class);
$this
->assertInstanceOf(Entry::class, $bridge
->get('Atom\\Entry'));
$container
->set('atomentry', $service);
$this
->assertEquals($service, $bridge
->get('Atom\\Entry'));
}
public function testHas() {
$service = new \stdClass();
$service->value = 'myvalue';
$container = new ContainerBuilder();
$container
->set('foo', $service);
$bridge = new ZfExtensionManagerSfContainer();
$bridge
->setContainer($container);
$this
->assertTrue($bridge
->has('foo'));
$this
->assertFalse($bridge
->has('bar'));
$this
->assertFalse($bridge
->has('Atom\\Entry'));
$bridge
->setStandalone(StandaloneExtensionManager::class);
$this
->assertTrue($bridge
->has('Atom\\Entry'));
}
public function testSetStandaloneException() {
$this
->expectException(\RuntimeException::class);
$this
->expectExceptionMessage('Drupal\\Tests\\Component\\Bridge\\ZfExtensionManagerSfContainerTest must implement Laminas\\Feed\\Reader\\ExtensionManagerInterface or Laminas\\Feed\\Writer\\ExtensionManagerInterface');
$bridge = new ZfExtensionManagerSfContainer();
$bridge
->setStandalone(static::class);
}
public function testGetContainerException() {
$this
->expectException(ServiceNotFoundException::class);
$this
->expectExceptionMessage('You have requested a non-existent service "test.foo".');
$container = new ContainerBuilder();
$bridge = new ZfExtensionManagerSfContainer('test.');
$bridge
->setContainer($container);
$bridge
->setStandalone(StandaloneExtensionManager::class);
$bridge
->get('foo');
}
public function testPrefix() {
$service = new \stdClass();
$service->value = 'myvalue';
$container = new ContainerBuilder();
$container
->set('foo.bar', $service);
$bridge = new ZfExtensionManagerSfContainer('foo.');
$bridge
->setContainer($container);
$this
->assertTrue($bridge
->has('bar'));
$this
->assertFalse($bridge
->has('baz'));
$this
->assertEquals($service, $bridge
->get('bar'));
}
public function testCanonicalizeName($name, $canonical_name) {
$service = new \stdClass();
$service->value = 'myvalue';
$container = new ContainerBuilder();
$container
->set($canonical_name, $service);
$bridge = new ZfExtensionManagerSfContainer();
$bridge
->setContainer($container);
$this
->assertTrue($bridge
->has($name));
$this
->assertEquals($service, $bridge
->get($name));
}
public function canonicalizeNameProvider() {
return [
[
'foobar',
'foobar',
],
[
'foo-bar',
'foobar',
],
[
'foo_bar',
'foobar',
],
[
'foo bar',
'foobar',
],
[
'foo\\bar',
'foobar',
],
[
'foo/bar',
'foobar',
],
[
'Foo/bAr',
'foobar',
],
[
'foo/-_\\ bar',
'foobar',
],
];
}
}