Skip to content
This repository has been archived by the owner on Mar 27, 2022. It is now read-only.

Add global basename support #17

Merged
merged 9 commits into from
Feb 14, 2017
1 change: 1 addition & 0 deletions example/full-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
'paths' => [],
'extension' => null,
'drivers' => [],
'global_basename' => null,
]
],
'cache' => [
Expand Down
22 changes: 10 additions & 12 deletions src/DriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,20 @@ protected function createWithConfig(ContainerInterface $container, $configKey)
),
$config['paths']
);
} else {
$driver = new $config['class']($config['paths']);
}

if (null !== $config['extension'] && $driver instanceof FileDriver) {
$locator = $driver->getLocator();
if (null !== $config['extension']
&& (FileDriver::class === $config['class'] || is_subclass_of($config['class'], FileDriver::class))
) {
$driver = new $config['class']($config['paths'], $config['extension']);
}

if (get_class($locator) !== DefaultFileLocator::class) {
throw new Exception\DomainException(sprintf(
'File locator must be a concrete instance of %s, got %s',
DefaultFileLocator::class,
get_class($locator)
));
}
if (! isset($driver)) {
$driver = new $config['class']($config['paths']);
}

$driver->setLocator(new DefaultFileLocator($locator->getPaths(), $config['extension']));
if (isset($config['global_basename']) && $driver instanceof FileDriver) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it is_subclass_of check is already done above, can't this if be moved inside there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried that, you would end up repeating the constructor invocation two times anyway, because a FileDriver may or may not have an extension parameter.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway: use array_key_exists() instead of isset().

$driver->setGlobalBasename($config['global_basename']);
}

if ($driver instanceof MappingDriverChain) {
Expand Down
86 changes: 86 additions & 0 deletions test/DriverFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* @license See the file LICENSE for copying permission
*/

namespace ContainerInteropDoctrineTest;

use Doctrine\ORM\Mapping\Driver;
use OutOfBoundsException;
use ContainerInteropDoctrine\DriverFactory;
use Interop\Container\ContainerInterface;
use PHPUnit_Framework_TestCase as TestCase;

class DriverFactoryTest extends TestCase
{
public function testMissingClassKeyWillReturnOutOfBoundException()
{
$container = $this->prophesize(ContainerInterface::class);
$factory = new DriverFactory();

$this->setExpectedException(OutOfBoundsException::class, 'Missing "class" config key');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setExpectedException is deprecated, should use expectException and expectExceptionMessage

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh never mind, I see phpunit is a very old version 😱 - ignore this then

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to update the PHPUnit version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DASPRiD I'll do that in different PR if that's ok for you


$factory($container->reveal());
}

public function testItSupportsGlobalBasenameOptionOnFileDrivers()
{
$globalBasename = 'foobar';

$container = $this->prophesize(ContainerInterface::class);
$container->has('config')->willReturn(true);
$container->get('config')->willReturn([
'doctrine' => [
'driver' => [
'orm_default' => [
'class' => TestAsset\StubFileDriver::class,
'global_basename' => $globalBasename
],
],
],
]);

$factory = new DriverFactory();

$driver = $factory($container->reveal());
$this->assertSame($globalBasename, $driver->getGlobalBasename());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertSame is a static method, so should be called so (i.e. self::assertSame) IMO

}

/**
* @param string $driverClass
*
* @dataProvider simplifiedDriverClassProvider
*/
public function testItSupportsSettingExtensionInDriversUsingSymfonyFileLocator($driverClass)
{
$extension = '.foo.bar';

$container = $this->prophesize(ContainerInterface::class);
$container->has('config')->willReturn(true);
$container->get('config')->willReturn([
'doctrine' => [
'driver' => [
'orm_default' => [
'class' => $driverClass,
'extension' => $extension,
],
],
],
]);

$factory = new DriverFactory();

/** @var Driver\SimplifiedXmlDriver $driver */
$driver = $factory($container->reveal());
$this->assertInstanceOf($driverClass, $driver);
$this->assertSame($extension, $driver->getLocator()->getFileExtension());
}

public function simplifiedDriverClassProvider()
{
return [
[ Driver\SimplifiedXmlDriver::class ],
[ Driver\SimplifiedYamlDriver::class ],
];
}
}
21 changes: 21 additions & 0 deletions test/TestAsset/StubFileDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* @license See the file LICENSE for copying permission
*/

namespace ContainerInteropDoctrineTest\TestAsset;

use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\Mapping\Driver\FileDriver;

class StubFileDriver extends FileDriver
{
protected function loadMappingFile($file)
{
return [];
}

public function loadMetadataForClass($className, ClassMetadata $metadata)
{
}
}