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
4 changes: 4 additions & 0 deletions src/DriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ protected function createWithConfig(ContainerInterface $container, $configKey)
$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) {
foreach ($config['drivers'] as $namespace => $driverName) {
if (null === $driverName) {
Expand Down
47 changes: 47 additions & 0 deletions test/DriverFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* @license See the file LICENSE for copying permission
*/

namespace ContainerInteropDoctrineTest;

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

}
}
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)
{
}
}