Skip to content

Commit

Permalink
Merge pull request #133 from adambalint-srg/fix-factory-namespace
Browse files Browse the repository at this point in the history
Fixed namespace generation in `FactoryCreator` utility
  • Loading branch information
Ocramius authored Jun 13, 2022
2 parents b23bbd8 + c17d801 commit 1f22314
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Tool/FactoryCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
use function array_shift;
use function count;
use function implode;
use function preg_replace;
use function sort;
use function sprintf;
use function str_repeat;
use function str_replace;
use function strrpos;
use function substr;

Expand Down Expand Up @@ -65,7 +65,7 @@ public function createFactory($className)

return sprintf(
self::FACTORY_TEMPLATE,
str_replace('\\' . $class, '', $className),
preg_replace('/\\\\' . $class . '$/', '', $className),
$this->createImportStatements($className),
$class,
$class,
Expand Down
19 changes: 19 additions & 0 deletions test/Tool/FactoryCreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

use Laminas\ServiceManager\Tool\FactoryCreator;
use LaminasTest\ServiceManager\TestAsset\ComplexDependencyObject;
use LaminasTest\ServiceManager\TestAsset\Foo;
use LaminasTest\ServiceManager\TestAsset\InvokableObject;
use LaminasTest\ServiceManager\TestAsset\SimpleDependencyObject;
use PHPUnit\Framework\TestCase;

use function class_alias;
use function file_get_contents;
use function preg_match;

class FactoryCreatorTest extends TestCase
{
Expand Down Expand Up @@ -47,4 +50,20 @@ public function testCreateFactoryCreatesForComplexDependencies(): void

$this->assertEquals($factory, $this->factoryCreator->createFactory($className));
}

public function testNamespaceGeneration(): void
{
$testClassNames = [
'Foo\\Bar\\Service' => 'Foo\\Bar',
'Foo\\Service\\Bar\\Service' => 'Foo\\Service\\Bar',
];
foreach ($testClassNames as $testFqcn => $expectedNamespace) {
class_alias(Foo::class, $testFqcn);
$generatedFactory = $this->factoryCreator->createFactory($testFqcn);
preg_match('/^namespace\s([^;]+)/m', $generatedFactory, $namespaceMatch);

$this->assertNotEmpty($namespaceMatch);
$this->assertEquals($expectedNamespace, $namespaceMatch[1]);
}
}
}

0 comments on commit 1f22314

Please sign in to comment.