Skip to content

Commit

Permalink
Merge pull request #138 from laminas/3.11.x-merge-up-into-3.12.x_xqXS…
Browse files Browse the repository at this point in the history
…dXDX

Merge release 3.11.3 into 3.12.x
  • Loading branch information
Ocramius authored Jun 13, 2022
2 parents db011e3 + 9ef032c commit 05ac4b1
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 05ac4b1

Please sign in to comment.